diff --git a/defaults/main.yml b/defaults/main.yml index e151c72..bd6cea2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -59,3 +59,9 @@ pgsql_users: [] # - name: database1 # owner: user1 pgsql_databases: [] + +# Zabbix/monitoring part +psql_zabbix_templates: + - Template CentOS PostgreSQL server +psql_zabbix_groups: + - CentOS PostgreSQL servers diff --git a/files/psql-monitoring-checks.sh b/files/psql-monitoring-checks.sh new file mode 100644 index 0000000..0ae8259 --- /dev/null +++ b/files/psql-monitoring-checks.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Managed by Ansible : don't edit ! +# Purposes : checks some services locally (like kojira or others) and send status to zabbix +# Called by: cron + +# Check connections in PostgreSQL via the postgres user. +# Sends all the data as CSV to Zabbix for pre-processing + +# avoid dir_read issues with sudo +cd /tmp + +# Send the raw data directly +zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k psql.raw_connections -o "$(sudo -u postgres psql --csv \ + -c 'select state, count(*) from pg_stat_activity where pid <> pg_backend_pid() group by 1 order by 1;')" >/dev/null 2>&1 diff --git a/tasks/main.yml b/tasks/main.yml index 1d82745..a5dea08 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -100,3 +100,7 @@ name: centos-backup tasks_from: client +- name: Include monitoring + ansible.builtin.include_tasks: monitoring.yml + tags: + - monitoring diff --git a/tasks/monitoring.yml b/tasks/monitoring.yml new file mode 100644 index 0000000..1486fa0 --- /dev/null +++ b/tasks/monitoring.yml @@ -0,0 +1,26 @@ +- name: Add host in Zabbix UI + block: + - name: Configuring agent in Zabbix server + ansible.builtin.include_role: + name: zabbix-server + tasks_from: agent_config + vars: + zabbix_templates: "{{ psql_zabbix_templates }}" + zabbix_groups: "{{ psql_zabbix_groups }}" + tags: + - monitoring + delegate_to: "{{ zabbix_api_srv }}" + when: zabbix_api_srv is defined + +- name: Distribute local PostgreSQL idle_connections check + copy: + src: files/psql-monitoring-checks.sh + dest: /usr/lib/zabbix/psql-monitoring-checks.sh + mode: 0750 + owner: root + +- name: Zabbix cron jobs + ansible.builtin.cron: + name: PostgreSQL monitoring + job: /usr/lib/zabbix/psql-monitoring-checks.sh + minute: "*/5"