Skip to content

Commit 06490d7

Browse files
authored
feat: Add postgres_exporter role (#291)
* feat: Add postgres_exporter role --------- Signed-off-by: anviar <oleg.kluchkin+github@gmail.com>
1 parent 3e4ff17 commit 06490d7

File tree

26 files changed

+1015
-0
lines changed

26 files changed

+1015
-0
lines changed

roles/postgres_exporter/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<p><img src="https://www.circonus.com/wp-content/uploads/2015/03/sol-icon-itOps.png" alt="graph logo" title="graph" align="right" height="60" /></p>
2+
3+
# Ansible Role: PostgreSQL exporter
4+
5+
6+
Deploy prometheus [postgresql exporter](https://github.com/prometheus-community/postgres_exporter) using ansible.
7+
8+
## Requirements
9+
10+
- Ansible >= 2.9 (It might work on previous versions, but we cannot guarantee it)
11+
- gnu-tar on Mac deployer host (`brew install gnu-tar`)
12+
- Passlib is required when using the basic authentication feature (`pip install passlib[bcrypt]`)
13+
14+
## Role Variables
15+
All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in [meta/argument_specs.yml](meta/argument_specs.yml).
16+
Please refer to the [collection docs](https://prometheus-community.github.io/ansible/branch/main/postgres_exporter_role.html) for description and default values of the variables.
17+
18+
## Example
19+
20+
### Playbook
21+
22+
Use it in a playbook as follows:
23+
```yaml
24+
- hosts: all
25+
collections:
26+
- prometheus.prometheus
27+
roles:
28+
- prometheus.prometheus.postgres_exporter
29+
```
30+
31+
### TLS config
32+
33+
Before running postgres_exporter role, the user needs to provision their own certificate and key.
34+
```yaml
35+
- hosts: all
36+
pre_tasks:
37+
- name: Create postgres_exporter cert dir
38+
ansible.builtin.file:
39+
path: "/etc/postgres_exporter"
40+
state: directory
41+
owner: root
42+
group: root
43+
44+
- name: Create cert and key
45+
community.crypto.x509_certificate:
46+
path: /etc/postgres_exporter/tls.cert
47+
csr_path: /etc/postgres_exporter/tls.csr
48+
privatekey_path: /etc/postgres_exporter/tls.key
49+
provider: selfsigned
50+
collections:
51+
- prometheus.prometheus
52+
roles:
53+
- prometheus.prometheus.postgres_exporter
54+
vars:
55+
postgres_exporter_tls_server_config:
56+
cert_file: /etc/postgres_exporter/tls.cert
57+
key_file: /etc/postgres_exporter/tls.key
58+
postgres_exporter_basic_auth_users:
59+
randomuser: examplepassword
60+
```
61+
62+
63+
### Demo site
64+
65+
We provide an example site that demonstrates a full monitoring solution based on prometheus and grafana. The repository with code and links to running instances is [available on github](https://github.com/prometheus/demo-site) and the site is hosted on [DigitalOcean](https://digitalocean.com).
66+
67+
## Local Testing
68+
69+
The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/ansible-community/molecule) (v3.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable for your system. Running your tests is as simple as executing `molecule test`.
70+
71+
## Continuous Integration
72+
73+
Combining molecule and circle CI allows us to test how new PRs will behave when used with multiple ansible versions and multiple operating systems. This also allows use to create test scenarios for different role configurations. As a result we have quite a large test matrix which can take more time than local testing, so please be patient.
74+
75+
## Contributing
76+
77+
See [contributor guideline](CONTRIBUTING.md).
78+
79+
## Troubleshooting
80+
81+
See [troubleshooting](TROUBLESHOOTING.md).
82+
83+
## License
84+
85+
This project is licensed under MIT License. See [LICENSE](/LICENSE) for more details.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Troubleshooting
2+
3+
## Bad requests (HTTP 400)
4+
5+
This role downloads checksums from the Github project to verify the integrity of artifacts installed on your servers. When downloading the checksums, a "bad request" error might occur.
6+
7+
This happens in environments which (knowningly or unknowling) use the [netrc mechanism](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) to auto-login into servers.
8+
9+
Unless netrc is needed by your playbook and ansible roles, please unset the var like so:
10+
11+
```
12+
$ NETRC= ansible-playbook ...
13+
```
14+
15+
Or:
16+
17+
```
18+
$ export NETRC=
19+
$ ansible-playbook ...
20+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
postgres_exporter_version: 0.15.0
3+
postgres_exporter_binary_local_dir: ""
4+
postgres_exporter_binary_url: "https://github.com/{{ _postgres_exporter_repo }}/releases/download/v{{ postgres_exporter_version }}/\
5+
postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}.tar.gz"
6+
postgres_exporter_checksums_url: "https://github.com/{{ _postgres_exporter_repo }}/releases/download/v{{ postgres_exporter_version }}/sha256sums.txt"
7+
postgres_exporter_skip_install: false
8+
9+
postgres_exporter_web_listen_address: "0.0.0.0:9187"
10+
postgres_exporter_web_telemetry_path: "/metrics"
11+
12+
postgres_exporter_config_file: "postgres_exporter.yml"
13+
14+
postgres_exporter_name: "postgresql:///postgres?host=/var/run/postgresql"
15+
postgres_exporter_uri: ""
16+
postgres_exporter_username: ""
17+
postgres_exporter_password: ""
18+
19+
postgres_exporter_tls_server_config: {}
20+
21+
postgres_exporter_http_server_config: {}
22+
23+
postgres_exporter_basic_auth_users: {}
24+
25+
postgres_exporter_enabled_collectors: []
26+
27+
postgres_exporter_disabled_collectors: []
28+
29+
# Internal variables.
30+
postgres_exporter_binary_install_dir: "/usr/local/bin"
31+
postgres_exporter_config_dir: "/etc/postgres_exporter"
32+
postgres_exporter_system_group: "postgres-exp"
33+
postgres_exporter_system_user: "{{ postgres_exporter_system_group }}"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: Restart postgres_exporter
3+
listen: "restart postgres_exporter"
4+
become: true
5+
ansible.builtin.systemd:
6+
daemon_reload: true
7+
name: postgres_exporter
8+
state: restarted
9+
when:
10+
- not ansible_check_mode
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
# yamllint disable rule:line-length
3+
argument_specs:
4+
main:
5+
short_description: "Prometheus PostgreSQL Exporter"
6+
description:
7+
- "Deploy prometheus L(postgres_exporter,https://github.com/prometheus-community/postgres_exporter) using ansible"
8+
author:
9+
- "Prometheus Community"
10+
options:
11+
postgres_exporter_version:
12+
description: "PostgreSQL exporter package version. Also accepts latest as parameter."
13+
default: "0.15.0"
14+
postgres_exporter_skip_install:
15+
description: "PostgreSQL installation tasks gets skipped when set to true."
16+
type: bool
17+
default: false
18+
postgres_exporter_binary_local_dir:
19+
description:
20+
- "Enables the use of local packages instead of those distributed on github."
21+
- "The parameter may be set to a directory where the C(postgres_exporter) binary is stored on the host where ansible is run."
22+
- "This overrides the I(postgres_exporter_version) parameter"
23+
postgres_exporter_binary_url:
24+
description: "URL of the postgres_exporter binaries .tar.gz file"
25+
default: "https://github.com/{{ _postgres_exporter_repo }}/download/v{{ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch }}.tar.gz"
26+
postgres_exporter_checksums_url:
27+
description: "URL of the postgres_exporter checksums file"
28+
default: "https://github.com/{{ _postgres_exporter_repo }}/releases/download/v{{ postgres_exporter_version }}/sha256sums.txt"
29+
postgres_exporter_web_listen_address:
30+
description: "Address on which postgres_exporter will listen"
31+
default: "0.0.0.0:9187"
32+
postgres_exporter_web_telemetry_path:
33+
description: "Path under which to expose metrics"
34+
default: "/metrics"
35+
postgres_exporter_config_dir:
36+
description: "The path where exporter configuration is stored"
37+
default: "/etc/postgres_exporter"
38+
postgres_exporter_config_file:
39+
description: "The filename of the postgres exporter config file"
40+
default: "/etc/postgres_exporter/postgres_exporter.yml"
41+
postgres_exporter_name:
42+
description: "The target PostgreSQL L(URI, https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS)"
43+
default: "postgresql:///postgres?host=/var/run/postgresql"
44+
postgres_exporter_uri:
45+
description: "The target PostgreSQL, alternative format, single target only is supported. You should set postgres_exporter_name to empty string to use it. Read more L(here,https://github.com/prometheus-community/postgres_exporter?tab=readme-ov-file#environment-variables)"
46+
postgres_exporter_username:
47+
description: "The username for PostgreSQL login, required for postgres_exporter_uri"
48+
default: "exporter"
49+
postgres_exporter_password:
50+
description: "The password for PostgreSQL password, required for postgres_exporter_uri"
51+
default: "secret"
52+
postgres_exporter_enabled_collectors:
53+
description:
54+
- "List of dicts defining additionally enabled collectors and their configuration."
55+
- "It adds collectors to L(those enabled by default,https://github.com/prometheus-community/postgres_exporter?tab=readme-ov-file#flags)."
56+
type: "list"
57+
default: []
58+
postgres_exporter_disabled_collectors:
59+
description:
60+
- "List of disabled collectors."
61+
- "By default postgres_exporter disables collectors listed L(here,https://github.com/prometheus-community/postgres_exporter?tab=readme-ov-file#flags)."
62+
type: "list"
63+
elements: "str"
64+
postgres_exporter_tls_server_config:
65+
description:
66+
- "Configuration for TLS authentication."
67+
- "Keys and values are the same as in L(prometheus docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
68+
type: "dict"
69+
postgres_exporter_http_server_config:
70+
description:
71+
- "Config for HTTP/2 support."
72+
- "Keys and values are the same as in L(prometheus docs,https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md)."
73+
type: "dict"
74+
postgres_exporter_basic_auth_users:
75+
description: "Dictionary of users and password for basic authentication. Passwords are automatically hashed with bcrypt."
76+
type: "dict"
77+
postgres_exporter_binary_install_dir:
78+
description:
79+
- "I(Advanced)"
80+
- "Directory to install postgres_exporter binary"
81+
default: "/usr/local/bin"
82+
postgres_exporter_system_group:
83+
description:
84+
- "I(Advanced)"
85+
- "System group for PostgreSQL Exporter"
86+
default: "postgres-exp"
87+
postgres_exporter_system_user:
88+
description:
89+
- "I(Advanced)"
90+
- "PostgreSQL Exporter user"
91+
default: "postgres-exp"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
galaxy_info:
3+
author: "Prometheus Community"
4+
description: "Prometheus PostgreSQL Exporter"
5+
license: "Apache"
6+
min_ansible_version: "2.9"
7+
platforms:
8+
- name: "Ubuntu"
9+
versions:
10+
- "focal"
11+
- "jammy"
12+
- name: "Debian"
13+
versions:
14+
- "bullseye"
15+
- "buster"
16+
- name: "EL"
17+
versions:
18+
- "7"
19+
- "8"
20+
- "9"
21+
- name: "Fedora"
22+
versions:
23+
- "37"
24+
- '38'
25+
galaxy_tags:
26+
- "monitoring"
27+
- "prometheus"
28+
- "exporter"
29+
- "metrics"
30+
- "system"
31+
- "postgresql"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
provisioner:
3+
inventory:
4+
group_vars:
5+
all:
6+
postgres_exporter_binary_local_dir: "/tmp/postgres_exporter-linux-amd64"
7+
postgres_exporter_web_listen_address:
8+
- '127.0.0.1:8080'
9+
- '127.0.1.1:8080'
10+
postgres_exporter_enabled_collectors:
11+
- postmaster
12+
postgres_exporter_disabled_collectors:
13+
- replication
14+
15+
postgres_exporter_tls_server_config:
16+
cert_file: /etc/postgres_exporter/tls.cert
17+
key_file: /etc/postgres_exporter/tls.key
18+
postgres_exporter_http_server_config:
19+
http2: true
20+
postgres_exporter_basic_auth_users:
21+
randomuser: examplepassword
22+
go_arch: amd64
23+
postgres_exporter_version: 0.15.0
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
- name: Run local preparation
3+
hosts: localhost
4+
gather_facts: false
5+
tasks:
6+
- name: Download postgres_exporter binary to local folder
7+
become: false
8+
ansible.builtin.get_url:
9+
url: "https://github.com/prometheus-community/postgres_exporter/releases/download/v{{\
10+
\ postgres_exporter_version }}/postgres_exporter-{{ postgres_exporter_version\
11+
\ }}.linux-{{ go_arch }}.tar.gz"
12+
dest: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch\
13+
\ }}.tar.gz"
14+
mode: 0644
15+
register: _download_binary
16+
until: _download_binary is succeeded
17+
retries: 5
18+
delay: 2
19+
check_mode: false
20+
21+
- name: Unpack postgres_exporter binary
22+
become: false
23+
ansible.builtin.unarchive:
24+
src: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch\
25+
\ }}.tar.gz"
26+
dest: "/tmp"
27+
creates: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-{{ go_arch\
28+
\ }}/postgres_exporter"
29+
check_mode: false
30+
31+
- name: Link to postgres_exporter binaries directory
32+
become: false
33+
ansible.builtin.file:
34+
src: "/tmp/postgres_exporter-{{ postgres_exporter_version }}.linux-amd64"
35+
dest: "/tmp/postgres_exporter-linux-amd64"
36+
state: link
37+
check_mode: false
38+
39+
- name: Install pyOpenSSL for certificate generation
40+
ansible.builtin.pip:
41+
name: "pyOpenSSL"
42+
43+
- name: Create private key
44+
community.crypto.openssl_privatekey:
45+
path: "/tmp/tls.key"
46+
47+
- name: Create CSR
48+
community.crypto.openssl_csr:
49+
path: "/tmp/tls.csr"
50+
privatekey_path: "/tmp/tls.key"
51+
52+
- name: Create certificate
53+
community.crypto.x509_certificate:
54+
path: "/tmp/tls.cert"
55+
csr_path: "/tmp/tls.csr"
56+
privatekey_path: "/tmp/tls.key"
57+
provider: selfsigned
58+
59+
- name: Run target preparation
60+
hosts: all
61+
any_errors_fatal: true
62+
tasks:
63+
- name: Create postgres_exporter cert dir
64+
ansible.builtin.file:
65+
path: "{{ postgres_exporter_tls_server_config.cert_file | dirname }}"
66+
state: directory
67+
owner: root
68+
group: root
69+
mode: u+rwX,g+rwX,o=rX
70+
71+
- name: Copy cert and key
72+
ansible.builtin.copy:
73+
src: "{{ item.src }}"
74+
dest: "{{ item.dest }}"
75+
mode: "{{ item.mode | default('0644') }}"
76+
loop:
77+
- src: "/tmp/tls.cert"
78+
dest: "{{ postgres_exporter_tls_server_config.cert_file }}"
79+
- src: "/tmp/tls.key"
80+
dest: "{{ postgres_exporter_tls_server_config.key_file }}"

0 commit comments

Comments
 (0)