From e6b83d1b29ccaa06b08926e076452ba5a9a07941 Mon Sep 17 00:00:00 2001
From: Jelle van der Waa <jelle@vdwaa.nl>
Date: Sun, 28 Apr 2019 21:01:15 +0200
Subject: [PATCH] Add postgresql performance metrics monitoring

---
 .../files/postgresql-user-stats.pl            | 31 +++++++++++++++++++
 roles/zabbix-agent/tasks/main.yml             |  1 +
 .../templates/zabbix-agent-sudoers.conf.j2    |  2 +-
 .../zabbix-agent/templates/zabbix_agentd.conf |  3 ++
 4 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100755 roles/zabbix-agent/files/postgresql-user-stats.pl

diff --git a/roles/zabbix-agent/files/postgresql-user-stats.pl b/roles/zabbix-agent/files/postgresql-user-stats.pl
new file mode 100755
index 000000000..f88148f32
--- /dev/null
+++ b/roles/zabbix-agent/files/postgresql-user-stats.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use DBI;
+use JSON;
+
+my $mode = $ARGV[0];
+my $dbname = $ARGV[1];
+
+if (!($mode eq "userstat" or $mode eq "discover")) {
+	print STDERR "mode is not userstat or discover\n";
+	die;
+}
+
+if ($mode eq "userstat" and not defined $dbname) {
+	print STDERR "userstat requires dbname to be defined\n";
+	die;
+}
+
+my $db = DBI->connect("DBI:Pg:");
+
+if ($mode eq "userstat") {
+	print encode_json($db->selectrow_hashref("SELECT tup_fetched,tup_returned,tup_inserted,tup_deleted,tup_updated,numbackends,deadlocks from pg_stat_database where datname = ?;", undef, $dbname));
+} elsif ($mode eq "discover") {
+	print encode_json({
+		data => $db->selectall_arrayref("SELECT datname \"{#DATNAME}\" FROM pg_database;", {Slice => {}})
+	});
+} else {
+	die "unhandled mode";
+}
diff --git a/roles/zabbix-agent/tasks/main.yml b/roles/zabbix-agent/tasks/main.yml
index 1ba100571..2e527d35f 100644
--- a/roles/zabbix-agent/tasks/main.yml
+++ b/roles/zabbix-agent/tasks/main.yml
@@ -82,6 +82,7 @@
     - zabbix-borg-timestamps.pl
     - mysql-dbsize-stats.pl
     - postgresql-dbsize-stats.pl
+    - postgresql-user-stats.pl
 
 - name: Install helper scripts
   copy: src=nginx-zabbix.pl dest=/usr/local/bin/nginx-zabbix.pl mode=0755 owner=root group=root
diff --git a/roles/zabbix-agent/templates/zabbix-agent-sudoers.conf.j2 b/roles/zabbix-agent/templates/zabbix-agent-sudoers.conf.j2
index d332cadd4..788436768 100644
--- a/roles/zabbix-agent/templates/zabbix-agent-sudoers.conf.j2
+++ b/roles/zabbix-agent/templates/zabbix-agent-sudoers.conf.j2
@@ -1,2 +1,2 @@
 zabbix-agent ALL=(root) NOPASSWD: /usr/local/bin/borg list --json --format {start}{end} --sort-by timestamp --last 1, /usr/local/bin/mysql-dbsize-stats.pl
-zabbix-agent ALL=(postgres) NOPASSWD: /usr/local/bin/postgresql-dbsize-stats.pl
+zabbix-agent ALL=(postgres) NOPASSWD: /usr/local/bin/postgresql-dbsize-stats.pl, /usr/local/bin/postgresql-user-stats.pl
diff --git a/roles/zabbix-agent/templates/zabbix_agentd.conf b/roles/zabbix-agent/templates/zabbix_agentd.conf
index 571f2db81..31540c098 100644
--- a/roles/zabbix-agent/templates/zabbix_agentd.conf
+++ b/roles/zabbix-agent/templates/zabbix_agentd.conf
@@ -20,6 +20,9 @@ UserParameter=mysql.dbsize_discover,sudo /usr/local/bin/mysql-dbsize-stats.pl "/
 UserParameter=postgresql.dbsize[*],sudo -u postgres /usr/local/bin/postgresql-dbsize-stats.pl dbsize '$1'
 UserParameter=postgresql.dbsize_discover,sudo -u postgres /usr/local/bin/postgresql-dbsize-stats.pl discover
 
+UserParameter=postgresql.userstats[*],sudo -u postgres /usr/local/bin/postgresql-dbsize-user-stats.pl userstat '$1'
+UserParameter=postgresql.userstats_discover,sudo -u postgres /usr/local/bin/postgresql-user-stats.pl discover
+
 UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet '$1' && echo 1 || echo 0
 UserParameter=systemd.unit.is-failed[*],systemctl is-failed --quiet '$1' && echo 1 || echo 0
 UserParameter=systemd.unit.is-enabled[*],systemctl is-enabled --quiet '$1' && echo 1 || echo 0
-- 
GitLab