Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions files/psql-monitoring-checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


#!/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 idle_connections in PostgreSQL
cd /tmp # avoid dir_read issues with sudo
idle=$(sudo -u postgres psql koji -A -t -F" " \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need/want to hardcode a db name for postgresql activity itself, as koji db doesn't exist everywhere :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, let me template that

-c "select state, count(*) from pg_stat_activity where state = 'idle' and pid <> pg_backend_pid() group by 1 order by 1;" |\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering (just another metric) selecting active, idle , others, and the three together == count() from pg_stat_activity , or just count() from pg_activity ?

select count(*) from pg_stat_activity ; 
 count 
-------
    91

 select state, count(*) from pg_stat_activity  where pid <> pg_backend_pid() group by 1 order by 1;
 state  | count 
--------+-------
 active |     4
 idle   |    93
        |     5

There can be time were there is no active connections and that's fine too (so sending zero)

cut -f2 -d" ")

zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k psql.idle_connections -o $idle >/dev/null 2>&1
4 changes: 4 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@
name: centos-backup
tasks_from: client

- name: Include monitoring
ansible.builtin.include_tasks: monitoring.yml
tags:
- monitoring
26 changes: 26 additions & 0 deletions tasks/monitoring.yml
Original file line number Diff line number Diff line change
@@ -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"