From 6facd3c33183820b13da40cb962dc2f9dee5eb76 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 14 Mar 2012 17:26:15 +0900 Subject: [PATCH 1/3] die commands if no DB connection can be made graceful die script if DB connection cannot be created. Also for prepare & execute command --- plugins/postgresql/pgbouncer_ | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/plugins/postgresql/pgbouncer_ b/plugins/postgresql/pgbouncer_ index 208d663e..610c4834 100755 --- a/plugins/postgresql/pgbouncer_ +++ b/plugins/postgresql/pgbouncer_ @@ -157,13 +157,16 @@ if (defined($ARGV[0])) } # connect to data -my $dbh = DBI->connect("DBI:Pg:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_pass); +my $dbh = DBI->connect("DBI:Pg:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_pass) + or die ("Cannot connect to database"); # go trough each set and get the data foreach my $get ('pools', 'stats') { # prep and execute the show query - my $pre = $dbh->prepare("SHOW $get"); - $pre->execute(); + my $pre = $dbh->prepare("SHOW $get") + or die ("Cannot prepare query"); + $pre->execute() + or die ("Cannot execute statement") while (@data = $pre->fetchrow) { # first defines the pool From 8de0f08367a148761be79b4fe8ce4f5c35131c96 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 14 Mar 2012 19:27:04 +0900 Subject: [PATCH 2/3] postgres database size in detail shows the data, index, sequence and view size of a single database. Uses the default munin postgresql perl plugin. --- plugins/postgresql/postgres_size_detail_ | 83 ++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 plugins/postgresql/postgres_size_detail_ diff --git a/plugins/postgresql/postgres_size_detail_ b/plugins/postgresql/postgres_size_detail_ new file mode 100755 index 00000000..0959e358 --- /dev/null +++ b/plugins/postgresql/postgres_size_detail_ @@ -0,0 +1,83 @@ +#!/usr/bin/perl +# -*- cperl -*- +# +# Copyright (C) 2012, Clemens Schwaighofer +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2 dated June, +# 1991. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 USA. + +=head1 NAME + +postgres_size_detail_ - Plugin to monitor PostgreSQL database size for one database in detail (data, index, sequence, view) + +=head1 CONFIGURATION + +Configuration is done through libpq environment variables, for example +PGUSER, PGDATABASE, etc. For more information, see L. + +To monitor a specific database, link to postgres_size_. +This script cannot monitor all database detail sizes at the same time, a valid database name needs to be given. + +=head1 SEE ALSO + +L + +=head1 MAGIC MARKERS + + #%# family=auto + #%# capabilities=autoconf suggest + +=head1 AUTHOR + +Magnus Hagander , Redpill Linpro AB + +=head1 COPYRIGHT/License. + +Copyright (c) 2012, Clemens Schwaighofer (gullevek@gullevek.org) + +All rights reserved. This program is free software; you can +redistribute it and/or modify it under the terms of the GNU General +Public License as published by the Free Software Foundation; version 2 +dated June, 1991. + +=cut + +use strict; +use warnings; + +use Munin::Plugin::Pgsql; + +my $pg = Munin::Plugin::Pgsql->new( + basename => 'postgres_size_detail_', + title => 'PostgreSQL detail database size', + info => 'Size of database in detail', + vlabel => 'Size', + paramdatabase => 1, + basequery => "SELECT CASE WHEN relkind = 'r' OR relkind = 't' THEN 'db_detail_data' WHEN relkind = 'i' THEN 'db_detail_index' WHEN relkind = 'v' THEN 'db_detail_view' WHEN relkind = 'S' THEN 'db_detail_sequence' ELSE 'db_detail_other' END AS state, + SUM(relpages::bigint * 8 * 1024) AS size + FROM pg_class pg, pg_namespace pgn WHERE pg.relnamespace = pgn.oid AND pgn.nspname NOT IN ('information_schema', 'pg_catalog') GROUP BY state", + configquery => [ + "VALUES ('db_detail_data','Data size'),('db_detail_index','Index size'),('db_detail_sequence','Sequence size'),('db_detail_view','View size')", + ], + suggestquery => + "SELECT datname FROM pg_database WHERE datallowconn AND NOT datistemplate AND NOT datname='postgres' UNION ALL SELECT 'ALL' ORDER BY 1 LIMIT 10", + stack => 1, + graphmin => 0, + graphdraw => 'AREA', + extraconfig => 'graph_total Total' +); + +$pg->Process(); + From f1952dc3b06b8299758d058bedff829c3f32685d Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 14 Mar 2012 19:44:11 +0900 Subject: [PATCH 3/3] postgres size detail Added "other" in case this data exists. --- plugins/postgresql/postgres_size_detail_ | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/postgresql/postgres_size_detail_ b/plugins/postgresql/postgres_size_detail_ index 0959e358..520b3aac 100755 --- a/plugins/postgresql/postgres_size_detail_ +++ b/plugins/postgresql/postgres_size_detail_ @@ -69,7 +69,7 @@ my $pg = Munin::Plugin::Pgsql->new( SUM(relpages::bigint * 8 * 1024) AS size FROM pg_class pg, pg_namespace pgn WHERE pg.relnamespace = pgn.oid AND pgn.nspname NOT IN ('information_schema', 'pg_catalog') GROUP BY state", configquery => [ - "VALUES ('db_detail_data','Data size'),('db_detail_index','Index size'),('db_detail_sequence','Sequence size'),('db_detail_view','View size')", + "VALUES ('db_detail_data','Data size'),('db_detail_index','Index size'),('db_detail_sequence','Sequence size'),('db_detail_view','View size'),('db_detail_other','Other size')", ], suggestquery => "SELECT datname FROM pg_database WHERE datallowconn AND NOT datistemplate AND NOT datname='postgres' UNION ALL SELECT 'ALL' ORDER BY 1 LIMIT 10",