Skip to content

Instantly share code, notes, and snippets.

@kyle-johnson
Created October 8, 2012 21:23
Show Gist options
  • Save kyle-johnson/3855067 to your computer and use it in GitHub Desktop.
Save kyle-johnson/3855067 to your computer and use it in GitHub Desktop.
Munin PostgreSQL db size (Bash + psql)
#!/bin/bash
dbserver='localhost'
dbuser='postgres'
psql='/usr/local/pgsql/bin/psql'
if [ "$1" = "config" ]; then
echo 'graph_args --base 1024 --lower-limit 0'
echo 'graph_category Postgresql'
echo 'graph_info Shows each database size on the PostgreSQL Server.'
echo 'graph_title PostgreSQL Database Sizes'
echo 'graph_vlabel Size (bytes)'
${psql} -h ${dbserver} -U ${dbuser} -tc "SELECT datname FROM pg_database ORDER BY 1;" | while read name
do
test -z "${name}" && continue
echo ${name}'.label '${name}
echo ${name}'.type GAUGE'
echo ${name}'.draw AREASTACK'
if [ "${name}" == "template0" ]; then
echo ${name}'.info PostgreSQL template database.'
elif [ "${name}" == "template1" ]; then
echo ${name}'.info PostgreSQL and/or user template database.'
elif [ "${name}" == "postgres" ]; then
echo ${name}'.info User postgres database.'
else
echo ${name}'.info User defined database.'
fi
done
exit 0
fi
${psql} -h ${dbserver} -U ${dbuser} -tc "SELECT datname, PG_DATABASE_SIZE(oid) FROM pg_database ORDER BY 1;" | while read name sep num
do
test -z "${name}" && continue
echo ${name}'.value '${num}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment