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 - role: node_exporter
tags: tags:
- node_exporter - node_exporter
- role: postgres_exporter
tags:
- postgres_exporter

View File

@ -10,9 +10,9 @@
- name: Set version - name: Set version
set_fact: set_fact:
version: "{{ _github_release.json.tag_name tag: "{{ _github_release.json.tag_name
| regex_replace('^v?([0-9\\.]+)$', '\\1') }}" | regex_replace('^v?([0-9\\.]+)$', '\\1') }}"
- name: Set download_url - name: Set download_url
set_fact: 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 - name: Move node_exporter into path
copy: copy:
src: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}/node_exporter" src: "/tmp/node_exporter-{{ tag }}.linux-{{ go_arch }}/node_exporter"
dest: "{{ bin_path }}" dest: "{{ bin_path }}"
mode: 755 mode: 755
remote_src: true remote_src: true

View File

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

View File

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