From 9765abcb969e74402d31d93ba3b69d7a0f4da508 Mon Sep 17 00:00:00 2001 From: Dave Fennell Date: Fri, 22 Feb 2013 16:49:21 +0000 Subject: [PATCH 1/2] Slight change to default connection options for postgresql_active_backends postgresql_database_ratio so they are more likely to work with a default postgres setup. --- plugins/postgresql/postgresql_active_backends | 10 ++++++---- plugins/postgresql/postgresql_database_ratio | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/postgresql/postgresql_active_backends b/plugins/postgresql/postgresql_active_backends index 225e0d35..df137cf6 100755 --- a/plugins/postgresql/postgresql_active_backends +++ b/plugins/postgresql/postgresql_active_backends @@ -20,14 +20,16 @@ # # Log info: # 2007/11/30 - Review on comments +# 2012/12/19 - Updated to connect locally instead of localhost by default +# (PostgreSQL has different permissions for these). # -dbserver='localhost' +dbserver='' #'-h hostname' dbuser='postgres' if [ "$1" = "config" ]; then - maximum=$(psql -h ${dbserver} -U ${dbuser} -tc "SHOW max_connections;" | bc) - reserved=$(psql -h ${dbserver} -U ${dbuser} -tc "SHOW superuser_reserved_connections;" | bc) + maximum=$(psql ${dbserver} -U ${dbuser} -tc "SHOW max_connections;" | bc) + reserved=$(psql ${dbserver} -U ${dbuser} -tc "SHOW superuser_reserved_connections;" | bc) warning=$(((maximum-reserved)*70/100)) critical=$(((maximum-reserved)*90/100)) echo 'graph_args --base 1000 --lower-limit 0 --upper-limit '${maximum} @@ -46,4 +48,4 @@ if [ "$1" = "config" ]; then exit 0 fi -echo 'backends.value '$(psql -h ${dbserver} -U ${dbuser} -tc "SELECT SUM(numbackends) FROM pg_stat_database;" | bc) +echo 'backends.value '$(psql ${dbserver} -U ${dbuser} -tc "SELECT SUM(numbackends) FROM pg_stat_database;" | bc) diff --git a/plugins/postgresql/postgresql_database_ratio b/plugins/postgresql/postgresql_database_ratio index 298e5f55..ea9ef000 100755 --- a/plugins/postgresql/postgresql_database_ratio +++ b/plugins/postgresql/postgresql_database_ratio @@ -54,4 +54,4 @@ if [ "$1" = "config" ]; then done exit 0 fi -psql -h ${dbserver} -U ${dbuser} -tc "SELECT '\n'||datname||'.value '||(CASE WHEN (blks_hit > 0) THEN ROUND((blks_hit::NUMERIC / (blks_hit + blks_read)::NUMERIC) * 100, 2) ELSE 0 END)::TEXT FROM pg_stat_database WHERE datname != 'template0' ORDER BY datname;" +psql -h ${dbserver} -U ${dbuser} -tc "SELECT datname||'.value '||(CASE WHEN (blks_hit > 0) THEN ROUND((blks_hit::NUMERIC / (blks_hit + blks_read)::NUMERIC) * 100, 2) ELSE 0 END)::TEXT FROM pg_stat_database WHERE datname != 'template0' ORDER BY datname;" From 87728bb27976fead688316436562192487e0f0cd Mon Sep 17 00:00:00 2001 From: Dave Fennell Date: Fri, 22 Feb 2013 16:49:55 +0000 Subject: [PATCH 2/2] Added a new plugin postgresql_active_backends_by_database which shows the active backends broken down by database. --- .../postgresql_active_backends_by_database | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 plugins/postgresql/postgresql_active_backends_by_database diff --git a/plugins/postgresql/postgresql_active_backends_by_database b/plugins/postgresql/postgresql_active_backends_by_database new file mode 100755 index 00000000..c8efb9e9 --- /dev/null +++ b/plugins/postgresql/postgresql_active_backends_by_database @@ -0,0 +1,96 @@ +#!/bin/bash +# +# Plugin to monitor PostgreSQL backends by database. +# (Draws a line for each database and a total with suitable warning and critical values) +# +# Author: +# Dave Fennel +# +# Created: +# 21st Feb 2013 +# +# License: +# GPLv2 +# +# Usage: +# Place in /etc/munin/plugins/ (or link it there using ln -s) +# +# General info: +# Requires permission for database access and read (no writes are processed). +# Recomended user is PostgreSQL database owner (default: postgres). +# + +dbserver='' # '-h localhost' +dbuser='postgres' + +# The psql command to use. +cmd="psql ${dbserver} -U ${dbuser} -tc 'SELECT datname,numbackends FROM pg_stat_database;' | grep -v '^$'" + + +if [ "$1" = "config" ]; then + maximum=$(psql ${dbserver} -U ${dbuser} -tc "SHOW max_connections;" | bc) + reserved=$(psql ${dbserver} -U ${dbuser} -tc "SHOW superuser_reserved_connections;" | bc) + warning=$(((maximum-reserved)*70/100)) + critical=$(((maximum-reserved)*90/100)) + + echo 'graph_args --base 1000 --lower-limit 0' # --upper-limit '${maximum} + echo 'graph_category Postgresql' + echo 'graph_info Shows open backends for each database on the server' + echo 'graph_scale no' + echo 'graph_title PostgreSQL Active Backends By Database' + echo 'graph_vlabel Number of active backends' + + pools="" + + while read pool sep backends junk + do + test -z "${pool}" && continue + + # Skip pgbouncer database itself. + if [ "$pool" = "template0" ]; then + continue + fi + + if [ "$pool" = "template1" ]; then + continue + fi + + echo ${pool}.label ${pool} + echo ${pool}.info ${pool} active backends + echo ${pool}.draw LINE2 + + pools="${pools} $pool" + + done < <(eval ${cmd}) + + echo total.label total + echo total.info total active backends + echo total.draw AREA + echo total.colour AFCACA + echo total.sum ${pools} + echo total.warning ${warning} + echo total.critical ${critical} + + # If dirty config capability is enabled then fall through + # to output the data with the config information. + if [ "$MUNIN_CAP_DIRTYCONFIG" = "" ]; then + exit 0 + fi +fi + +while read pool sep backends junk +do + test -z "${pool}" && continue + + # Skip template databases. + if [ "$pool" = "template0" ]; then + continue + fi + + if [ "$pool" = "template1" ]; then + continue + fi + + echo ${pool}.value ${backends} + +done < <(eval ${cmd})