https://github.com/prometheus-community/postgres_exporter
用于 PostgreSQL 服务器指标的 Prometheus 导出器。
PostgreSQL 可用版本:9.4, 9.5, 9.6, 10, 11, 12,13
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
注:
PostgreSQL 服务器的数据源名称 必须通过DATA_SOURCE_NAME环境变量设置。
解压进入目录执行./postgres_exporter --help可查看具体配置项:
usage: postgres_exporter []
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 with your root user.
-- GRANT postgres_exporter TO ;
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;