add postgres exporter

Signed-off-by: Tuan-Dat Tran <tuan-dat.tran@tudattr.dev>
rewrite
Tuan-Dat Tran 2024-10-08 11:17:03 +02:00
parent 05c35a546a
commit 0c1a8a95f2
12 changed files with 117 additions and 13 deletions

3
db.yml
View File

@ -14,3 +14,6 @@
- role: node_exporter
tags:
- node_exporter
- role: postgres_exporter
tags:
- postgres_exporter

View File

@ -10,9 +10,9 @@
- name: Set version
set_fact:
version: "{{ _github_release.json.tag_name
tag: "{{ _github_release.json.tag_name
| regex_replace('^v?([0-9\\.]+)$', '\\1') }}"
- name: Set download_url
set_fact:
download_url: "https://github.com/prometheus/node_exporter/releases/download/v{{ version }}/node_exporter-{{ version }}.linux-{{ go_arch }}.tar.gz"
download_url: "https://github.com/prometheus/node_exporter/releases/download/v{{ tag }}/node_exporter-{{ tag }}.linux-{{ go_arch }}.tar.gz"

View File

@ -8,7 +8,7 @@
- name: Move node_exporter into path
copy:
src: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}/node_exporter"
src: "/tmp/node_exporter-{{ tag }}.linux-{{ go_arch }}/node_exporter"
dest: "{{ bin_path }}"
mode: 755
remote_src: true

View File

@ -1,7 +1,7 @@
---
- name: Update apt cache
apt:
update_cache: yes
update_cache: true
become: true
- name: Install ansible dependencies

View File

@ -16,18 +16,18 @@
encoding: UTF8
lc_collate: "en_US.UTF-8"
lc_ctype: "en_US.UTF-8"
become: yes
become: true
become_user: postgres
vars:
ansible_remote_temp: "/tmp/"
- name: "Grant all privileges on database {{ db.name }} to {{ db.user }};"
- name: "Grant privileges to {{ db.user }}"
community.postgresql.postgresql_privs:
db: "{{ db.name }}"
privs: ALL
type: database
roles: "{{ db.user }}"
become: yes
become: true
become_user: postgres
vars:
ansible_remote_temp: "/tmp/"
@ -39,13 +39,13 @@
type: schema
obj: "public"
roles: "{{ db.user }}"
become: yes
become: true
become_user: postgres
vars:
ansible_remote_temp: "/tmp/"
- name: "Allow md5 connection for the {{ db.user }} user"
postgresql_pg_hba:
- name: "Allow md5 connection for the user {{ db.user }}"
community.postgresql.postgresql_pg_hba:
dest: "/etc/postgresql/15/main/pg_hba.conf"
contype: host
databases: all
@ -53,16 +53,17 @@
address: "{{ k3s.net }}"
users: "{{ db.user }}"
create: false
become: yes
become: true
notify:
- Restart postgres
- name: "Set public listen address"
become: true
lineinfile:
ansible.builtin.lineinfile:
dest: "/etc/postgresql/15/main/conf.d/listen.conf"
regexp: "^#?listen_addresses="
line: "listen_addresses='{{ db.listen_address | default('localhost') }}'"
state: present
create: yes
mode: "644"
create: true
notify: "Restart postgres"

View File

@ -0,0 +1,6 @@
---
- name: "Restart {{ bin_name }}"
ansible.builtin.service:
name: "{{ bin_name }}"
state: restarted
become: true

View File

@ -0,0 +1,18 @@
---
- name: Determine latest GitHub release (local)
delegate_to: localhost
ansible.builtin.uri:
url: "https://api.github.com/repos/{{ repository }}/releases/{{ version }}"
body_format: json
register: _github_release
until: _github_release.status == 200
retries: 3
- name: Set version
ansible.builtin.set_fact:
tag: "{{ _github_release.json.tag_name
| regex_replace('^v?([0-9\\.]+)$', '\\1') }}"
- name: Set download_url
ansible.builtin.set_fact:
download_url: "https://github.com/{{ repository }}/releases/download/v{{ tag }}/{{ bin_name }}-{{ tag }}.linux-{{ go_arch }}.tar.gz"

View File

@ -0,0 +1,29 @@
---
- name: Download/Extract "{{ download_url }}"
ansible.builtin.unarchive:
src: "{{ download_url }}"
dest: /tmp/
remote_src: true
mode: "755"
- name: "Move binary into path: {{ bin_path }}"
ansible.builtin.copy:
src: "/tmp/{{ bin_name }}-{{ tag }}.linux-{{ go_arch }}/{{ bin_name }}"
dest: "{{ bin_path }}"
mode: "755"
remote_src: true
become: true
- name: "Create user: {{ bin_name }}"
ansible.builtin.user:
name: "{{ bin_name }}"
shell: /sbin/nologin
state: present
become: true
- name: Copy the node_exporter systemd unit file.
ansible.builtin.template:
src: "{{ bin_name }}.service.j2"
dest: "/etc/systemd/system/{{ bin_name }}.service"
mode: "644"
become: true

View File

@ -0,0 +1,7 @@
---
- name: Get Version
ansible.builtin.include_tasks: get_version.yml
- name: Install exporter
ansible.builtin.include_tasks: install.yml
- name: Create service
ansible.builtin.include_tasks: systemd.yml

View File

@ -0,0 +1,10 @@
---
- name: "Ensure service is running and enabled: {{ bin_name }}"
ansible.builtin.service:
name: "{{ bin_name }}"
state: started
daemon_reload: true
enabled: true
notify:
- Restart "{{ bin_name }}"
become: true

View File

@ -0,0 +1,14 @@
[Unit]
Description=PostgresExporter
[Service]
TimeoutStartSec=0
User={{ bin_name }}
ExecStart={{ bin_path }} --web.listen-address={{ host.ip }}:{{ bind_port }} {{ options }}
Environment="DATA_SOURCE_URI=localhost:5432/postgres?sslmode=disable"
Environment="DATA_SOURCE_USER={{ db.user }}"
Environment="DATA_SOURCE_PASS={{ db.password }}"
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,16 @@
go_arch_map:
i386: "386"
x86_64: "amd64"
aarch64: "arm64"
armv7l: "armv7"
armv6l: "armv6"
go_arch: "{{ go_arch_map[ansible_architecture] | default(ansible_architecture) }}"
repository: "prometheus-community/postgres_exporter"
bind_port: 9187
version: "latest"
serve: "localhost"
options: ""
bin_name: postgres_exporter
bin_path: "/usr/local/bin/{{ bin_name }}"