postgres-exporter部署书册

postgres-exporter部署书册

来源

https://github.com/prometheus-community/postgres_exporter

说明

用于 PostgreSQL 服务器指标的 Prometheus 导出器。

兼容性

PostgreSQL 可用版本:9.4, 9.5, 9.6, 10, 11, 12,13

部署方式

docker部署:

Start an example database:

docker run --net=host -it --rm -e POSTGRES_PASSWORD=password postgres

Connect to it:

docker run  --net=host -e DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable" quay.io/prometheuscommunity/postgres-exporter

二进制文件部署:

使用环境变量运行:

export  DATA_SOURCE_NAME="user=postgres_exporter host=192.168.12.50 password=123456a? port=9999 dbname=postgres sslmode=disable"
./postgres_exporter <flags>

注:
PostgreSQL 服务器的数据源名称 必须通过DATA_SOURCE_NAME环境变量设置。

解压进入目录执行./postgres_exporter --help可查看具体配置项:

usage: postgres_exporter [<flags>]

Flags:
  --help                        Show context-sensitive help (also try
                                --help-long and --help-man).
  --web.listen-address=":9187"  Address to listen on for web interface and
                                telemetry.
  --web.telemetry-path="/metrics"
                                Path under which to expose metrics.
  --disable-default-metrics     Do not include default metrics.
  --disable-settings-metrics    Do not include pg_settings metrics.
  --auto-discover-databases     Whether to discover the databases on a server
                                dynamically.
  --extend.query-path=""        Path to custom queries to run.
  --dumpmaps                    Do not run, simply dump the maps.
  --constantLabels=""           A list of label=value separated by comma(,).
  --exclude-databases=""        A list of databases to remove when
                                autoDiscoverDatabases is enabled
  --metric-prefix="pg"          A metric prefix can be used to have non-default
                                (not "pg") prefixes for each of the metrics
  --version                     Show application version.
  --log.level="info"            Only log messages with the given severity or
                                above. Valid levels: [debug, info, warn, error,
                                fatal]
  --log.format="logger:stderr"  Set the log target and format. Example:
                                "logger:syslog?appname=bob&local=7" or
                                "logger:stdout?json=true"

注:
以非超级用户身份运行:
  为了能够从pg_stat_activity和pg_stat_replication 作为非超级用户收集指标,您必须以超级用户的身份创建功能和视图,并分别为它们分配权限。
  在 PostgreSQL 中,视图通过创建它们的用户的权限运行,因此它们可以充当安全屏障。需要创建函数以与非超级用户共享此数据。只有创建视图才会遗漏最重要的数据位。

-- To use IF statements, hence to be able to check if the user exists before
-- attempting creation, we need to switch to procedural SQL (PL/pgSQL)
-- instead of standard SQL.
-- More: https://www.postgresql.org/docs/9.3/plpgsql-overview.html
-- To preserve compatibility with <9.0, DO blocks are not used; instead,
-- a function is created and dropped.
CREATE OR REPLACE FUNCTION __tmp_create_user() returns void as $$
BEGIN
  IF NOT EXISTS (
          SELECT                       -- SELECT list can stay empty for this
          FROM   pg_catalog.pg_user
          WHERE  usename = 'postgres_exporter') THEN
    CREATE USER postgres_exporter;
  END IF;
END;
$$ language plpgsql;

SELECT __tmp_create_user();
DROP FUNCTION __tmp_create_user();

ALTER USER postgres_exporter WITH PASSWORD 'password';
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;

-- If deploying as non-superuser (for example in AWS RDS), uncomment the GRANT
-- line below and replace <MASTER_USER> with your root user.
-- GRANT postgres_exporter TO <MASTER_USER>;
CREATE SCHEMA IF NOT EXISTS postgres_exporter;
GRANT USAGE ON SCHEMA postgres_exporter TO postgres_exporter;
GRANT CONNECT ON DATABASE postgres TO postgres_exporter;

CREATE OR REPLACE FUNCTION get_pg_stat_activity() RETURNS SETOF pg_stat_activity AS
$$ SELECT * FROM pg_catalog.pg_stat_activity; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;

CREATE OR REPLACE VIEW postgres_exporter.pg_stat_activity
AS
  SELECT * from get_pg_stat_activity();

GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter;

CREATE OR REPLACE FUNCTION get_pg_stat_replication() RETURNS SETOF pg_stat_replication AS
$$ SELECT * FROM pg_catalog.pg_stat_replication; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;

CREATE OR REPLACE VIEW postgres_exporter.pg_stat_replication
AS
  SELECT * FROM get_pg_stat_replication();

GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;

CREATE OR REPLACE FUNCTION get_pg_stat_statements() RETURNS SETOF pg_stat_statements AS
$$ SELECT * FROM public.pg_stat_statements; $$
LANGUAGE sql
VOLATILE
SECURITY DEFINER;

CREATE OR REPLACE VIEW postgres_exporter.pg_stat_statements
AS
  SELECT * FROM get_pg_stat_statements();

GRANT SELECT ON postgres_exporter.pg_stat_statements TO postgres_exporter;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值