2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

Merge pull request #263 from unluckypixie/master

Added a new plugin "postgresql_active_backends_by_database"
This commit is contained in:
Kenyon Ralph 2013-03-03 17:16:24 -08:00
commit fe319da656
3 changed files with 103 additions and 5 deletions

View File

@ -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)

View File

@ -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 <dave@microtux.co.uk>
#
# 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})

View File

@ -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;"