[check_postgres] [commit] Bump version, add first attempt at explicit Cacti support.

check_postgres at bucardo.org check_postgres at bucardo.org
Sun Oct 26 20:34:36 UTC 2008


Committed by Greg Sabino Mullane <greg at endpoint.com>

Bump version, add first attempt at explicit Cacti support.

---
 .perlcriticrc          |    2 +-
 check_postgres.pl      |  147 ++++++++++++++++++++++++++++++++++++++++++++----
 check_postgres.pl.asc  |    6 +-
 check_postgres.pl.html |   95 ++++++++++++++++++++++++++++++-
 index.html             |    8 +-
 t/99_spellcheck.t      |    4 +
 6 files changed, 240 insertions(+), 22 deletions(-)

diff --git a/.perlcriticrc b/.perlcriticrc
index 0e4763d..bea1336 100644
--- a/.perlcriticrc
+++ b/.perlcriticrc
@@ -4,7 +4,7 @@ verbose = 8
 severity = 1
 
 [Documentation::PodSpelling]
-stop_words = Mullane Nagios Slony nols salesrep psql dbname postgres USERNAME usernames dbuser pgpass nagios stderr showperf symlinked timesync criticals quirm lancre exabytes sami includeuser excludeuser flagg tardis WAL tablespaces tablespace perflimit burrick mallory grimm oskar ExclusiveLock garrett artemus queryname speedtest checksum checksums morpork klatch pluto faceoff slon greg watson franklin wilkins scott Sabino Seklecki dbpass autovacuum Astill refactoring NAGIOS localhost cronjob symlink symlinks backends snazzo logfile syslog parens plugin Cwd Ioannis Tambouras schemas SQL MRTG mrtg uptime datallowconn dbhost dbport ok contrib pageslots robert dylan emma fsm minvalue nextval
+stop_words = Mullane Nagios Slony nols salesrep psql dbname postgres USERNAME usernames dbuser pgpass nagios stderr showperf symlinked timesync criticals quirm lancre exabytes sami includeuser excludeuser flagg tardis WAL tablespaces tablespace perflimit burrick mallory grimm oskar ExclusiveLock garrett artemus queryname speedtest checksum checksums morpork klatch pluto faceoff slon greg watson franklin wilkins scott Sabino Seklecki dbpass autovacuum Astill refactoring NAGIOS localhost cronjob symlink symlinks backends snazzo logfile syslog parens plugin Cwd Ioannis Tambouras schemas SQL MRTG mrtg uptime datallowconn dbhost dbport ok contrib pageslots robert dylan emma fsm minvalue nextval dbstats del ret upd
 
 ## Severity 5:
 [-Subroutines::ProhibitNestedSubs]
diff --git a/check_postgres.pl b/check_postgres.pl
index ada6d32..9d28779 100755
--- a/check_postgres.pl
+++ b/check_postgres.pl
@@ -28,7 +28,7 @@ $Data::Dumper::Varname = 'POSTGRES';
 $Data::Dumper::Indent = 2;
 $Data::Dumper::Useqq = 1;
 
-our $VERSION = '2.3.11';
+our $VERSION = '2.4.0';
 
 use vars qw/ %opt $PSQL $res $COM $SQL $db /;
 
@@ -146,7 +146,7 @@ our $DEBUG_INFO = '?';
 
 if (!$OUTPUT) {
 	my $dir = getcwd;
-	if ($dir =~ /(nagios|mrtg|simple)/io) {
+	if ($dir =~ /(nagios|mrtg|simple|cacti)/io) {
 		$OUTPUT = lc $1;
 	}
 	elsif ($opt{simple}) {
@@ -163,12 +163,12 @@ $opt{transform} = '';
 if ($OUTPUT =~ /\b(kb|mb|gb|tb|eb)\b/) {
 	$opt{transform} = uc $1;
 }
-if ($OUTPUT =~ /(nagios|mrtg|simple)/io) {
+if ($OUTPUT =~ /(nagios|mrtg|simple|cacti)/io) {
 	$OUTPUT = lc $1;
 }
 ## Check for a valid output setting
-if ($OUTPUT ne 'nagios' and $OUTPUT ne 'mrtg' and $OUTPUT ne 'simple') {
-	die qq{Invalid output: must be 'nagios' or 'mrtg' or 'simple'\n};
+if ($OUTPUT ne 'nagios' and $OUTPUT ne 'mrtg' and $OUTPUT ne 'simple' and $OUTPUT ne 'cacti') {
+	die qq{Invalid output: must be 'nagios' or 'mrtg' or 'simple' or 'cacti'\n};
 }
 
 our $MRTG = ($OUTPUT eq 'mrtg' or $OUTPUT eq 'simple') ? 1 : 0;
@@ -197,6 +197,7 @@ our $action_info = {
  connection          => [0, 'Simple connection check.'],
  custom_query        => [0, 'Run a custom query.'],
  database_size       => [0, 'Report if a database is too big.'],
+ dbstats             => [1, 'Returns stats from pg_stat_database: Cacti output only'],
  disk_space          => [1, 'Checks space of local disks Postgres is using.'],
  fsm_pages           => [1, 'Checks percentage of pages used in free space map.'],
  fsm_relations       => [1, 'Checks percentage of relations used in free space map.'],
@@ -724,6 +725,9 @@ check_fsm_pages() if $action eq 'fsm_pages';
 ## See how many relations we have used up compared to max_fsm_relations
 check_fsm_relations() if $action eq 'fsm_relations';
 
+## Spit back info from the pg_stat_database table. Cacti only
+show_dbstats() if $action eq 'dbstats';
+
 finishup();
 
 exit 0;
@@ -986,10 +990,10 @@ sub run_command {
 
 	## Create a temp file to store our results
 	$tempdir = tempdir(CLEANUP => 1);
-	($tempfh,$tempfile) = tempfile('nagios_psql.XXXXXXX', SUFFIX => '.tmp', DIR => $tempdir);
+	($tempfh,$tempfile) = tempfile('check_postgres_psql.XXXXXXX', SUFFIX => '.tmp', DIR => $tempdir);
 
 	## Create another one to catch any errors
-	($errfh,$errorfile) = tempfile('nagios_psql_stderr.XXXXXXX', SUFFIX => '.tmp', DIR => $tempdir);
+	($errfh,$errorfile) = tempfile('check_postgres_psql_stderr.XXXXXXX', SUFFIX => '.tmp', DIR => $tempdir);
 
 	for $db (@target) {
 
@@ -1010,7 +1014,7 @@ sub run_command {
 
 		if (defined $db->{dbpass} and length $db->{dbpass}) {
 			## Make a custom PGPASSFILE. Far better to simply use your own .pgpass of course
-			($passfh,$passfile) = tempfile('nagios.XXXXXXXX', SUFFIX => '.tmp', DIR => $tempdir);
+			($passfh,$passfile) = tempfile('check_postgres.XXXXXXXX', SUFFIX => '.tmp', DIR => $tempdir);
 			$VERBOSE >= 3 and warn "Created temporary pgpass file $passfile\n";
 			$ENV{PGPASSFILE} = $passfile;
 			printf $passfh "%s:%s:%s:%s:%s\n",
@@ -1479,6 +1483,11 @@ sub validate_range {
 			ndie qq{Invalid 'warning' option: must be number of locks, or "type1=#;type2=#"\n};
 		}
 	}
+	elsif ('cacti' eq $type) { ## Takes no args, just dumps data
+		if (length $warning or length $critical) {
+			die "This action is for cacti use only and takes not warning or critical arguments\n";
+		}
+	}
 	else {
 		ndie qq{validate_range called with unknown type '$type'\n};
 	}
@@ -3668,13 +3677,55 @@ sub check_sequence {
 
 } ## end of check_sequence
 
+sub show_dbstats {
+
+	## Returns values from the pg_stat_database view
+	## Supports: Cacti
+	## Assumes psql and target are the same version for the 8.3 check
+
+	my ($warning, $critical) = validate_range
+		({
+		  type => 'cacti',
+	  });
+
+	my $SQL = q{SELECT datname,numbackends,xact_commit,xact_rollback,blks_read,blks_hit};
+	$psql_version >= 8.3 and $SQL .= q{,tup_returned,tup_fetched,tup_inserted,tup_updated,tup_deleted};
+	$SQL .= q{ FROM pg_catalog.pg_stat_database};
+
+	my $info = run_command($SQL, {regex => qr{\w}} );
+
+	for $db (@{$info->{db}}) {
+	  SLURP: for my $row (split /\n/ => $db->{slurp}) {
+			my @stats = split /\s*\|\s*/ => $row;
+			(my $dbname = shift @stats) =~ s/^\s*//;
+			next SLURP if skip_item($dbname);
+			## If dbnames were specififed, use those for filtering as well
+			if (@{$opt{dbname}}) {
+				my $keepit = 0;
+				for my $drow (@{$opt{dbname}}) {
+					for my $d (split /,/ => $drow) {
+						$d eq $dbname and $keepit = 1;
+					}
+				}
+				next SLURP unless $keepit;
+			}
+			my $template = "backends:%d commits:%d rollbacks:%d read:%d hit:%d ret:%d fetch:%d ins:%d del:%d";
+			my $msg = sprintf "$template", @stats, (0,0,0,0);
+			print "$msg dbname:$dbname\n";
+		}
+	}
+
+	exit 0;
+
+} ## end of show_dbstats
+
 
 =pod
 
 =head1 NAME
 
-B<check_postgres.pl> - a Postgres monitoring script for Nagios, MRTG, and others
-This documents describes check_postgres.pl version 2.3.11
+B<check_postgres.pl> - a Postgres monitoring script for Nagios, MRTG, Cacti, and others
+This documents describes check_postgres.pl version 2.4.0
 
 =head1 SYNOPSIS
 
@@ -3766,6 +3817,13 @@ argument, for example:
 
   --output=simple,MB
 
+=head3 Simple output
+
+The Cacti output consists of one or more items on the same line, with a simple name, a colon, and 
+then a number. At the moment, the only action with explicit Cacti output is 'dbstats', and using 
+the --output option is not needed in this case, Cacti is the only output for this action. For many 
+other actions, using --simple is enough to make Cacti happy.
+
 =head1 DATABASE CONNECTION OPTIONS
 
 All actions accept a common set of database options. At least one is required.
@@ -4138,6 +4196,72 @@ Example 3: Give a warning if any database on host 'tardis' owned by the user 'to
 For MRTG output, returns the size in bytes of the largest database on the first line, 
 and the name of the database on the fourth line.
 
+=head2 B<dbstats>
+
+(C<symlink: check_postgres_dbstats>) Reports information from the pg_stat_database view, 
+and outputs it in a Cacti-friendly manner. No other output are supports, as the output 
+is informational and does not lend itself to alerts, such as used with Nagios. If no 
+options are given, all databases are returned, one per line. You can include a specific 
+database by use of the C<--include> option, or you can use the C<--dbname> option.
+
+Eleven items are returned on each line, in the format name:value, separated by a single 
+space. The items are:
+
+=over 4
+
+=item backends
+
+The number of currently running backends for this database.
+
+=item commits
+
+The total number of commits for this database since it was created or reset.
+
+=item rollbacks
+
+The total number of rollbacks for this database since it was created or reset.
+
+=item read
+
+The total number of disk blocks read.
+
+=item hit
+
+The total number of buffer hits.
+
+=item ret
+
+The total number of rows returned.
+
+=item fetch
+
+The total number of rows fetched.
+
+=item ins
+
+The total number of rows inserted.
+
+=item upd
+
+The total number of rows updated.
+
+=item del
+
+The total number of rows deleted.
+
+=item dbname
+
+The name of the database.
+
+=back
+
+Note that ret, fetch, ins, upd, and del items will always be 0 if Postgres is version 8.2 or lower, as those stats were 
+not available in those versions.
+
+Example 1: Grab the stats for a database named "products" on host "willow":
+
+  check_postgres_dbstats --dbhost willow --dbname products
+
 =head2 B<disk_space>
 
 (C<symlink: check_postgres_disk_space>) Checks on the available physical disk space used by Postgres. This action requires 
@@ -4818,8 +4942,9 @@ Items not specifically attributed are by Greg Sabino Mullane.
 
 =over 4
 
-=item B<Version 2.3.11>
+=item B<Version 2.4.0>
 
+ Add Cacti support with the dbstats action.
  Pretty up the time output for last vacuum and analyze actions.
  Show the percentage of backends on the check_backends action.
 
diff --git a/check_postgres.pl.asc b/check_postgres.pl.asc
index 0345780..a0a1aae 100644
--- a/check_postgres.pl.asc
+++ b/check_postgres.pl.asc
@@ -1,6 +1,6 @@
 -----BEGIN PGP SIGNATURE-----
 
-iEYEABEDAAYFAkkBFAUACgkQvJuQZxSWSsi+vQCgtCrFcTN8cNaSP7P3E0cXT+3y
-UGwAoN0MOBvvy824q0cKPhfADAZoobcF
-=eG9F
+iEYEABEDAAYFAkkE1CQACgkQvJuQZxSWSshOLACfY520+UFybHaBLDg4j9bxdDNR
+wzsAnjo7kFvAWhMZMY19f9zlqu4IjXou
+=VHPI
 -----END PGP SIGNATURE-----
diff --git a/check_postgres.pl.html b/check_postgres.pl.html
index f02c09f..e5e1fc2 100644
--- a/check_postgres.pl.html
+++ b/check_postgres.pl.html
@@ -2,7 +2,7 @@
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
-<title>check_postgres.pl - a Postgres monitoring script for Nagios, MRTG, and others</title>
+<title>check_postgres.pl - a Postgres monitoring script for Nagios, MRTG, Cacti, and others</title>
 
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 
@@ -26,6 +26,7 @@
 			<li><a href="#nagios_output">Nagios output</a></li>
 			<li><a href="#mrtg_output">MRTG output</a></li>
 			<li><a href="#simple_output">Simple output</a></li>
+			<li><a href="#simple_output">Simple output</a></li>
 		</ul>
 
 	</ul>
@@ -41,6 +42,7 @@
 		<li><a href="#connection"><strong>connection</strong></a></li>
 		<li><a href="#custom_query"><strong>custom_query</strong></a></li>
 		<li><a href="#database_size"><strong>database_size</strong></a></li>
+		<li><a href="#dbstats"><strong>dbstats</strong></a></li>
 		<li><a href="#disk_space"><strong>disk_space</strong></a></li>
 		<li><a href="#fsm_pages"><strong>fsm_pages</strong></a></li>
 		<li><a href="#fsm_relations"><strong>fsm_relations</strong></a></li>
@@ -89,8 +91,8 @@
 </p>
 <hr />
 <h1><a name="name">NAME</a></h1>
-<p><strong>check_postgres.pl</strong> - a Postgres monitoring script for Nagios, MRTG, and others
-This documents describes check_postgres.pl version 2.3.10</p>
+<p><strong>check_postgres.pl</strong> - a Postgres monitoring script for Nagios, MRTG, Cacti, and others
+This documents describes check_postgres.pl version 2.4.0</p>
 <p>
 </p>
 <hr />
@@ -184,6 +186,13 @@ argument, for example:</p>
   --output=simple,MB</pre>
 <p>
 </p>
+<h3><a name="simple_output">Simple output</a></h3>
+<p>The Cacti output consists of one or more items on the same line, with a simple name, a colon, and 
+then a number. At the moment, the only action with explicit Cacti output is 'dbstats', and using 
+the --output option is not needed in this case, Cacti is the only output for this action. For many 
+other actions, using --simple is enough to make Cacti happy.</p>
+<p>
+</p>
 <hr />
 <h1><a name="database_connection_options">DATABASE CONNECTION OPTIONS</a></h1>
 <p>All actions accept a common set of database options. At least one is required.</p>
@@ -535,6 +544,78 @@ showing both raw bytes and a &quot;pretty&quot; version of the size.</p>
 and the name of the database on the fourth line.</p>
 <p>
 </p>
+<h2><a name="dbstats"><strong>dbstats</strong></a></h2>
+<p>(<code>symlink: check_postgres_dbstats</code>) Reports information from the pg_stat_database view, 
+and outputs it in a Cacti-friendly manner. No other output are supports, as the output 
+is informational and does not lend itself to alerts, such as used with Nagios. If no 
+options are given, all databases are returned, one per line. You can include a specific 
+database by use of the <code>--include</code> option, or you can use the <code>--dbname</code> option.</p>
+<p>Eleven items are returned on each line, in the format name:value, separated by a single 
+space. The items are:</p>
+<dl>
+<dt><strong><a name="item_backends">backends</a></strong></dt>
+
+<dd>
+<p>The number of currently running backends for this database.</p>
+</dd>
+<dt><strong><a name="item_commits">commits</a></strong></dt>
+
+<dd>
+<p>The total number of commits for this database since it was created or reset.</p>
+</dd>
+<dt><strong><a name="item_rollbacks">rollbacks</a></strong></dt>
+
+<dd>
+<p>The total number of rollbacks for this database since it was created or reset.</p>
+</dd>
+<dt><strong><a name="item_read">read</a></strong></dt>
+
+<dd>
+<p>The total number of disk blocks read.</p>
+</dd>
+<dt><strong><a name="item_hit">hit</a></strong></dt>
+
+<dd>
+<p>The total number of buffer hits.</p>
+</dd>
+<dt><strong><a name="item_ret">ret</a></strong></dt>
+
+<dd>
+<p>The total number of rows returned.</p>
+</dd>
+<dt><strong><a name="item_fetch">fetch</a></strong></dt>
+
+<dd>
+<p>The total number of rows fetched.</p>
+</dd>
+<dt><strong><a name="item_ins">ins</a></strong></dt>
+
+<dd>
+<p>The total number of rows inserted.</p>
+</dd>
+<dt><strong><a name="item_upd">upd</a></strong></dt>
+
+<dd>
+<p>The total number of rows updated.</p>
+</dd>
+<dt><strong><a name="item_del">del</a></strong></dt>
+
+<dd>
+<p>The total number of rows deleted.</p>
+</dd>
+<dt><strong><a name="item_dbname">dbname</a></strong></dt>
+
+<dd>
+<p>The name of the database.</p>
+</dd>
+</dl>
+<p>Note that ret, fetch, ins, upd, and del items will always be 0 if Postgres is version 8.2 or lower, as those stats were 
+not available in those versions.</p>
+<p>Example 1: Grab the stats for a database named &quot;products&quot; on host &quot;willow&quot;:</p>
+<pre>
+  check_postgres_dbstats --dbhost willow --dbname products</pre>
+<p>
+</p>
 <h2><a name="disk_space"><strong>disk_space</strong></a></h2>
 <p>(<code>symlink: check_postgres_disk_space</code>) Checks on the available physical disk space used by Postgres. This action requires 
 that you have the executable &quot;/bin/df&quot; available to report on disk sizes, and it 
@@ -1122,6 +1203,14 @@ feature requests, and commit notices, send email to <a href="mailto:check_postgr
 <h1><a name="history">HISTORY</a></h1>
 <p>Items not specifically attributed are by Greg Sabino Mullane.</p>
 <dl>
+<dt><strong><a name="item_version_2_2e4_2e0"><strong>Version 2.4.0</strong></a></strong></dt>
+
+<dd>
+<pre>
+ Add Cacti support with the dbstats action.
+ Pretty up the time output for last vacuum and analyze actions.
+ Show the percentage of backends on the check_backends action.</pre>
+</dd>
 <dt><strong><a name="item_version_2_2e3_2e10"><strong>Version 2.3.10</strong></a></strong></dt>
 
 <dd>
diff --git a/index.html b/index.html
index 4b5b8b4..cd198b6 100644
--- a/index.html
+++ b/index.html
@@ -21,14 +21,14 @@ h1 {
 
 <h1>check_postgres.pl</h1>
 
-<p><b>check_postgres.pl</b> is a script for checking the state of one or more Postgres databases and reporting back in a Nagios-friendly manner. It was developed by Greg Sabino Mullane of <a href="http://www.endpoint.com/">End Point Corporation</a> and is BSD-licensed. The latest version is <b>2.3.10</b>, and was released on October 23, 2008.</p>
+<p><b>check_postgres.pl</b> is a script for checking the state of one or more Postgres databases and reporting back in a Nagios-friendly manner. It was developed by Greg Sabino Mullane of <a href="http://www.endpoint.com/">End Point Corporation</a> and is BSD-licensed. The latest version is <b>2.4.0</b>, and was released on October 26, 2008.</p>
 
 <ul>
- <li><a href="/check_postgres/check_postgres.pl.html">Documentation for check_postgres 2.3.10</a></li>
+ <li><a href="/check_postgres/check_postgres.pl.html">Documentation for check_postgres 2.4.0</a></li>
 </ul>
 <ul>
- <li><a href="/check_postgres/check_postgres.pl">Download check_postgres.pl 2.3.10</a></li>
- <li><a href="/check_postgres/check_postgres.pl.asc">PGP signature for check_postgres.pl 2.3.10</a></li>
+ <li><a href="/check_postgres/check_postgres.pl">Download check_postgres.pl 2.4.0</a></li>
+ <li><a href="/check_postgres/check_postgres.pl.asc">PGP signature for check_postgres.pl 2.4.0</a></li>
 </ul>
 
 <p>The latest development version can be downloaded via git:</p>
diff --git a/t/99_spellcheck.t b/t/99_spellcheck.t
index e25bfcd..e838328 100644
--- a/t/99_spellcheck.t
+++ b/t/99_spellcheck.t
@@ -193,7 +193,9 @@ dbhost
 dbname
 dbpass
 dbport
+dbstats
 dbuser
+del
 dylan
 emma
 exabytes
@@ -237,6 +239,7 @@ PSQL
 queryname
 quirm
 refactoring
+ret
 robert
 runtime
 salesrep
@@ -259,6 +262,7 @@ Tambouras
 tardis
 timesync
 txn
+upd
 uptime
 USERNAME
 usernames
-- 
1.5.5.4



More information about the Check_postgres mailing list