2016-05-28 04:35:17 +00:00
|
|
|
[![Build Status](https://travis-ci.org/wrouesnel/postgres_exporter.svg?branch=master)](https://travis-ci.org/wrouesnel/postgres_exporter)
|
2017-02-24 13:19:07 +00:00
|
|
|
[![Coverage Status](https://coveralls.io/repos/github/wrouesnel/postgres_exporter/badge.svg?branch=master)](https://coveralls.io/github/wrouesnel/postgres_exporter?branch=master)
|
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/wrouesnel/postgres_exporter)](https://goreportcard.com/report/github.com/wrouesnel/postgres_exporter)
|
2019-06-15 15:29:11 +00:00
|
|
|
[![Docker Pulls](https://img.shields.io/docker/pulls/wrouesnel/postgres_exporter.svg)](https://hub.docker.com/r/wrouesnel/postgres_exporter/tags)
|
2016-05-28 04:35:17 +00:00
|
|
|
|
2017-06-09 02:37:06 +00:00
|
|
|
# PostgreSQL Server Exporter
|
2015-08-24 17:27:14 +00:00
|
|
|
|
2017-06-09 02:37:06 +00:00
|
|
|
Prometheus exporter for PostgreSQL server metrics.
|
2018-11-11 04:45:30 +00:00
|
|
|
|
2019-06-15 06:21:59 +00:00
|
|
|
CI Tested PostgreSQL versions: `9.4`, `9.5`, `9.6`, `10`, `11`
|
2015-08-24 17:27:14 +00:00
|
|
|
|
2016-03-12 10:07:31 +00:00
|
|
|
## Quick Start
|
|
|
|
This package is available for Docker:
|
|
|
|
```
|
2016-05-20 09:04:31 +00:00
|
|
|
# Start an example database
|
|
|
|
docker run --net=host -it --rm -e POSTGRES_PASSWORD=password postgres
|
|
|
|
# Connect to it
|
2018-10-02 12:15:39 +00:00
|
|
|
docker run --net=host -e DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable" wrouesnel/postgres_exporter
|
2016-03-12 10:07:31 +00:00
|
|
|
```
|
|
|
|
|
2015-08-24 17:27:14 +00:00
|
|
|
## Building and running
|
2018-02-22 14:55:49 +00:00
|
|
|
|
|
|
|
The build system is based on [Mage](https://magefile.org)
|
|
|
|
|
2016-03-12 10:07:31 +00:00
|
|
|
The default make file behavior is to build the binary:
|
|
|
|
```
|
2018-02-22 14:55:49 +00:00
|
|
|
$ go get github.com/wrouesnel/postgres_exporter
|
|
|
|
$ cd ${GOPATH-$HOME/go}/src/github.com/wrouesnel/postgres_exporter
|
2019-07-03 12:36:32 +00:00
|
|
|
$ go run mage.go binary
|
2018-02-22 14:55:49 +00:00
|
|
|
$ export DATA_SOURCE_NAME="postgresql://login:password@hostname:port/dbname"
|
|
|
|
$ ./postgres_exporter <flags>
|
2016-03-12 10:07:31 +00:00
|
|
|
```
|
2015-08-24 17:27:14 +00:00
|
|
|
|
2018-02-22 14:55:49 +00:00
|
|
|
To build the dockerfile, run `go run mage.go docker`.
|
2015-08-24 17:27:14 +00:00
|
|
|
|
2018-02-02 09:04:07 +00:00
|
|
|
This will build the docker image as `wrouesnel/postgres_exporter:latest`. This
|
|
|
|
is a minimal docker image containing *just* postgres_exporter. By default no SSL
|
|
|
|
certificates are included, if you need to use SSL you should either bind-mount
|
2016-03-12 10:07:31 +00:00
|
|
|
`/etc/ssl/certs/ca-certificates.crt` or derive a new image containing them.
|
2015-08-24 17:27:14 +00:00
|
|
|
|
2016-03-12 10:07:31 +00:00
|
|
|
### Vendoring
|
|
|
|
Package vendoring is handled with [`govendor`](https://github.com/kardianos/govendor)
|
2015-08-27 21:05:30 +00:00
|
|
|
|
2015-08-24 17:27:14 +00:00
|
|
|
### Flags
|
|
|
|
|
2018-02-02 09:04:07 +00:00
|
|
|
* `web.listen-address`
|
2018-07-16 15:20:01 +00:00
|
|
|
Address to listen on for web interface and telemetry. Default is `:9187`.
|
2016-11-17 11:14:13 +00:00
|
|
|
|
|
|
|
* `web.telemetry-path`
|
2018-07-16 15:20:01 +00:00
|
|
|
Path under which to expose metrics. Default is `/metrics`.
|
2016-11-17 11:14:13 +00:00
|
|
|
|
2018-03-20 13:41:58 +00:00
|
|
|
* `disable-default-metrics`
|
2018-09-04 10:02:20 +00:00
|
|
|
Use only metrics supplied from `queries.yaml` via `--extend.query-path`.
|
|
|
|
|
|
|
|
* `disable-settings-metrics`
|
|
|
|
Use the flag if you don't want to scrape `pg_settings`.
|
2018-03-20 13:41:58 +00:00
|
|
|
|
|
|
|
* `extend.query-path`
|
|
|
|
Path to a YAML file containing custom queries to run. Check out [`queries.yaml`](queries.yaml)
|
|
|
|
for examples of the format.
|
2018-09-04 10:02:20 +00:00
|
|
|
|
2018-03-20 13:41:58 +00:00
|
|
|
* `dumpmaps`
|
|
|
|
Do not run - print the internal representation of the metric maps. Useful when debugging a custom
|
|
|
|
queries file.
|
2018-09-04 10:02:20 +00:00
|
|
|
|
2018-03-20 13:41:58 +00:00
|
|
|
* `log.level`
|
|
|
|
Set logging level: one of `debug`, `info`, `warn`, `error`, `fatal`
|
|
|
|
|
|
|
|
* `log.format`
|
|
|
|
Set the log output target and format. e.g. `logger:syslog?appname=bob&local=7` or `logger:stdout?json=true`
|
|
|
|
Defaults to `logger:stderr`.
|
|
|
|
|
2018-10-18 15:16:17 +00:00
|
|
|
* `constantLabels`
|
|
|
|
Labels to set in all metrics. A list of `label=value` pairs, separated by commas.
|
|
|
|
|
2017-10-26 08:44:54 +00:00
|
|
|
### Environment Variables
|
|
|
|
|
|
|
|
The following environment variables configure the exporter:
|
|
|
|
|
|
|
|
* `DATA_SOURCE_NAME`
|
|
|
|
the default legacy format. Accepts URI form and key=value form arguments. The
|
|
|
|
URI may contain the username and password to connect with.
|
|
|
|
|
|
|
|
* `DATA_SOURCE_URI`
|
2018-09-04 10:02:20 +00:00
|
|
|
an alternative to `DATA_SOURCE_NAME` which exclusively accepts the raw URI
|
2017-10-26 08:44:54 +00:00
|
|
|
without a username and password component.
|
|
|
|
|
|
|
|
* `DATA_SOURCE_USER`
|
|
|
|
When using `DATA_SOURCE_URI`, this environment variable is used to specify
|
|
|
|
the username.
|
2018-09-04 10:02:20 +00:00
|
|
|
|
2017-10-26 08:44:54 +00:00
|
|
|
* `DATA_SOURCE_USER_FILE`
|
|
|
|
The same, but reads the username from a file.
|
|
|
|
|
|
|
|
* `DATA_SOURCE_PASS`
|
|
|
|
When using `DATA_SOURCE_URI`, this environment variable is used to specify
|
|
|
|
the password to connect with.
|
2018-09-04 10:02:20 +00:00
|
|
|
|
2017-10-26 08:44:54 +00:00
|
|
|
* `DATA_SOURCE_PASS_FILE`
|
|
|
|
The same as above but reads the password from a file.
|
2018-09-04 10:02:20 +00:00
|
|
|
|
2018-07-16 15:20:01 +00:00
|
|
|
* `PG_EXPORTER_WEB_LISTEN_ADDRESS`
|
|
|
|
Address to listen on for web interface and telemetry. Default is `:9187`.
|
|
|
|
|
|
|
|
* `PG_EXPORTER_WEB_TELEMETRY_PATH`
|
|
|
|
Path under which to expose metrics. Default is `/metrics`.
|
|
|
|
|
|
|
|
* `PG_EXPORTER_DISABLE_DEFAULT_METRICS`
|
|
|
|
Use only metrics supplied from `queries.yaml`. Value can be `true` or `false`. Default is `false`.
|
|
|
|
|
2018-09-04 10:02:20 +00:00
|
|
|
* `PG_EXPORTER_DISABLE_SETTINGS_METRICS`
|
|
|
|
Use the flag if you don't want to scrape `pg_settings`. Value can be `true` or `false`. Defauls is `false`.
|
|
|
|
|
2018-07-16 15:20:01 +00:00
|
|
|
* `PG_EXPORTER_EXTEND_QUERY_PATH`
|
|
|
|
Path to a YAML file containing custom queries to run. Check out [`queries.yaml`](queries.yaml)
|
|
|
|
for examples of the format.
|
2018-10-18 15:16:17 +00:00
|
|
|
|
2018-11-11 04:45:30 +00:00
|
|
|
* `PG_EXPORTER_CONSTANT_LABELS`
|
2018-10-18 15:16:17 +00:00
|
|
|
Labels to set in all metrics. A list of `label=value` pairs, separated by commas.
|
2018-09-04 10:02:20 +00:00
|
|
|
|
2018-07-16 15:20:01 +00:00
|
|
|
Settings set by environment variables starting with `PG_` will be overwritten by the corresponding CLI flag if given.
|
2017-10-26 08:44:54 +00:00
|
|
|
|
2017-02-21 10:32:05 +00:00
|
|
|
### Setting the Postgres server's data source name
|
2015-08-24 17:27:14 +00:00
|
|
|
|
2017-06-09 02:37:06 +00:00
|
|
|
The PostgreSQL server's [data source name](http://en.wikipedia.org/wiki/Data_source_name)
|
2015-08-24 17:27:14 +00:00
|
|
|
must be set via the `DATA_SOURCE_NAME` environment variable.
|
|
|
|
|
2015-09-07 05:25:10 +00:00
|
|
|
For running it locally on a default Debian/Ubuntu install, this will work (transpose to init script as appropriate):
|
|
|
|
|
|
|
|
sudo -u postgres DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable" postgres_exporter
|
|
|
|
|
2018-09-04 10:02:20 +00:00
|
|
|
Also, you can set a list of sources to scrape different instances from the one exporter setup. Just define a comma separated string.
|
|
|
|
|
|
|
|
sudo -u postgres DATA_SOURCE_NAME="port=5432,port=6432" postgres_exporter
|
|
|
|
|
2016-03-12 10:18:05 +00:00
|
|
|
See the [github.com/lib/pq](http://github.com/lib/pq) module for other ways to format the connection string.
|
2015-08-24 17:27:14 +00:00
|
|
|
|
|
|
|
### Adding new metrics
|
|
|
|
|
|
|
|
The exporter will attempt to dynamically export additional metrics if they are added in the
|
|
|
|
future, but they will be marked as "untyped". Additional metric maps can be easily created
|
2017-02-21 10:32:05 +00:00
|
|
|
from Postgres documentation by copying the tables and using the following Python snippet:
|
2015-08-24 17:27:14 +00:00
|
|
|
|
|
|
|
```python
|
|
|
|
x = """tab separated raw text of a documentation table"""
|
|
|
|
for l in StringIO(x):
|
|
|
|
column, ctype, description = l.split('\t')
|
|
|
|
print """"{0}" : {{ prometheus.CounterValue, prometheus.NewDesc("pg_stat_database_{0}", "{2}", nil, nil) }}, """.format(column.strip(), ctype, description.strip())
|
|
|
|
```
|
|
|
|
Adjust the value of the resultant prometheus value type appropriately. This helps build
|
2015-08-27 21:05:30 +00:00
|
|
|
rich self-documenting metrics for the exporter.
|
2016-01-06 19:19:41 +00:00
|
|
|
|
2016-09-07 19:15:52 +00:00
|
|
|
### Adding new metrics via a config file
|
|
|
|
|
|
|
|
The -extend.query-path command-line argument specifies a YAML file containing additional queries to run.
|
|
|
|
Some examples are provided in [queries.yaml](queries.yaml).
|
2016-01-06 19:19:41 +00:00
|
|
|
|
2018-03-20 13:41:58 +00:00
|
|
|
### Disabling default metrics
|
2018-09-04 10:02:20 +00:00
|
|
|
To work with non-officially-supported postgres versions you can try disabling (e.g. 8.2.15)
|
2018-03-20 13:41:58 +00:00
|
|
|
or a variant of postgres (e.g. Greenplum) you can disable the default metrics with the `--disable-default-metrics`
|
|
|
|
flag. This removes all built-in metrics, and uses only metrics defined by queries in the `queries.yaml` file you supply
|
|
|
|
(so you must supply one, otherwise the exporter will return nothing but internal statuses and not your database).
|
2018-02-27 18:20:44 +00:00
|
|
|
|
2019-04-16 10:03:52 +00:00
|
|
|
### Automatically discover databases
|
|
|
|
To scrape metrics from all databases on a database server, the database DSN's can be dynamically discovered via the
|
|
|
|
`--auto-discover-databases` flag. When true, `SELECT datname FROM pg_database` is run for all configured DSN's. From the
|
|
|
|
result a new set of DSN's is created for which the metrics are scraped.
|
|
|
|
|
2016-01-06 19:19:41 +00:00
|
|
|
### Running as non-superuser
|
|
|
|
|
2018-09-04 10:02:20 +00:00
|
|
|
To be able to collect metrics from `pg_stat_activity` and `pg_stat_replication`
|
2019-02-20 12:31:09 +00:00
|
|
|
as non-superuser you have to create functions and views as a superuser, and
|
|
|
|
assign permissions separately to those.
|
2018-11-11 04:45:30 +00:00
|
|
|
|
2018-09-04 10:02:20 +00:00
|
|
|
In PostgreSQL, views run with the permissions of the user that created them so
|
2019-02-20 12:31:09 +00:00
|
|
|
they can act as security barriers. Functions need to be created to share this
|
|
|
|
data with the non-superuser. Only creating the views will leave out the most
|
|
|
|
important bits of data.
|
2016-01-06 19:19:41 +00:00
|
|
|
|
|
|
|
```sql
|
|
|
|
CREATE USER postgres_exporter PASSWORD 'password';
|
|
|
|
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;
|
|
|
|
|
2018-11-11 04:45:30 +00:00
|
|
|
-- If deploying as non-superuser (for example in AWS RDS), uncomment the GRANT
|
|
|
|
-- line below and replace <MASTER_USER> with your root user.
|
2019-06-05 12:57:44 +00:00
|
|
|
-- GRANT postgres_exporter TO <MASTER_USER>;
|
2019-01-24 15:41:53 +00:00
|
|
|
CREATE SCHEMA IF NOT EXISTS postgres_exporter;
|
2019-02-20 12:31:09 +00:00
|
|
|
GRANT USAGE ON SCHEMA postgres_exporter TO postgres_exporter;
|
|
|
|
|
|
|
|
CREATE FUNCTION get_pg_stat_activity() RETURNS SETOF pg_stat_activity AS
|
|
|
|
$$ SELECT * FROM pg_catalog.pg_stat_activity; $$
|
|
|
|
LANGUAGE sql
|
|
|
|
VOLATILE
|
|
|
|
SECURITY DEFINER;
|
2016-01-06 19:19:41 +00:00
|
|
|
|
2019-01-24 15:41:53 +00:00
|
|
|
CREATE OR REPLACE VIEW postgres_exporter.pg_stat_activity
|
2017-09-12 10:08:36 +00:00
|
|
|
AS
|
2019-02-20 12:31:09 +00:00
|
|
|
SELECT * from get_pg_stat_activity();
|
2018-02-02 09:04:07 +00:00
|
|
|
|
2017-09-12 10:08:36 +00:00
|
|
|
GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter;
|
2016-01-06 19:19:41 +00:00
|
|
|
|
2019-01-24 15:41:53 +00:00
|
|
|
CREATE OR REPLACE FUNCTION get_pg_stat_replication() RETURNS SETOF pg_stat_replication AS
|
2019-02-20 12:31:09 +00:00
|
|
|
$$ SELECT * FROM pg_catalog.pg_stat_replication; $$
|
|
|
|
LANGUAGE sql
|
|
|
|
VOLATILE
|
|
|
|
SECURITY DEFINER;
|
|
|
|
|
2019-01-24 15:41:53 +00:00
|
|
|
CREATE OR REPLACE VIEW postgres_exporter.pg_stat_replication
|
2019-02-20 12:31:09 +00:00
|
|
|
AS
|
|
|
|
SELECT * FROM get_pg_stat_replication();
|
2016-01-06 19:19:41 +00:00
|
|
|
|
|
|
|
GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;
|
|
|
|
```
|
2017-02-21 10:32:05 +00:00
|
|
|
|
|
|
|
> **NOTE**
|
|
|
|
> <br />Remember to use `postgres` database name in the connection string:
|
|
|
|
> ```
|
|
|
|
> DATA_SOURCE_NAME=postgresql://postgres_exporter:password@localhost:5432/postgres?sslmode=disable
|
|
|
|
> ```
|
2017-11-29 16:15:53 +00:00
|
|
|
|
|
|
|
# Hacking
|
2018-02-22 14:55:49 +00:00
|
|
|
* To build a copy for your current architecture run `go run mage.go binary` or just `go run mage.go`
|
2017-11-29 16:15:53 +00:00
|
|
|
This will create a symlink to the just built binary in the root directory.
|
2018-02-22 14:55:49 +00:00
|
|
|
* To build release tar balls run `go run mage.go release`.
|
2018-03-20 13:41:58 +00:00
|
|
|
* Build system is a bit temperamental at the moment since the conversion to mage - I am working on getting it
|
|
|
|
to be a perfect out of the box experience, but am time-constrained on it at the moment.
|