[check_postgres] [commit] Fix typo in 1.9.0, allow percentages in bloat check, bump version to 1.9.1

check_postgres at bucardo.org check_postgres at bucardo.org
Tue Jun 24 20:31:10 UTC 2008


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

Fix typo in 1.9.0, allow percentages in bloat check, bump version to 1.9.1

---
 .perlcriticrc          |    2 +-
 check_postgres.pl      |  108 +++++++++++++++++++++++++++++++++++++-----------
 check_postgres.pl.html |   23 ++++++++--
 index.html             |    6 +-
 4 files changed, 107 insertions(+), 32 deletions(-)

diff --git a/.perlcriticrc b/.perlcriticrc
index 1aa2cb3..312fa06 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
+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
 
 ## Severity 5:
 [-Subroutines::ProhibitNestedSubs]
diff --git a/check_postgres.pl b/check_postgres.pl
index f67c8ee..f6ea5dc 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 = '1.9.0';
+our $VERSION = '1.9.1';
 
 use vars qw/ %opt $PSQL $res $COM $SQL $db /;
 
@@ -1129,7 +1129,7 @@ sub validate_range {
 			if ($critical =~ $sizere) {
 				$critical = size_in_bytes($1,$2);
 			}
-			elsif ($critical !~ /^\d\d?\%$/) {
+			elsif ($critical !~ /^\d+\%$/) {
 				ndie qq{Invalid 'critical' option: must be size or percentage\n};
 			}
 		}
@@ -1137,7 +1137,7 @@ sub validate_range {
 			if ($warning =~ $sizere) {
 				$warning = size_in_bytes($1,$2);
 			}
-			elsif ($warning !~ /^\d\d?\%$/) {
+			elsif ($warning !~ /^\d+\%$/) {
 				ndie qq{Invalid 'warning' option: must be size or percentage\n};
 			}
 		}
@@ -1327,6 +1327,7 @@ sub check_bloat {
 	## Valid units: b, k, m, g, t, e
 	## All above may be written as plural or with a trailing 'b'
 	## Example: --critical="25 GB" --include="mylargetable"
+	## Can also specify percentages
 
 	## Don't bother with tables or indexes unless they have at least this many bloated pages
 	my $MINPAGES = 0;
@@ -1339,7 +1340,7 @@ sub check_bloat {
 
 	my ($warning, $critical) = validate_range
 		({
-		  type               => 'size',
+		  type               => 'size or percent',
 		  default_warning    => '1 GB',
 		  default_critical   => '5 GB',
 		  });
@@ -1390,7 +1391,7 @@ FROM (
     ) AS foo
   ) AS rs
   JOIN pg_class cc ON cc.relname = rs.tablename
-  JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.relname <> 'information_schema'
+  JOIN pg_namespace nn ON cc.relnamespace = nn.oid AND nn.nspname = rs.schemaname AND nn.nspname <> 'information_schema'
   LEFT JOIN pg_index i ON indrelid = cc.oid
   LEFT JOIN pg_class c2 ON c2.oid = i.indexrelid
 ) AS sml
@@ -1402,6 +1403,10 @@ ORDER BY wastedbytes DESC LIMIT $LIMIT
 
 	my $info = run_command($SQL, { version => {'8.0' => $SQL2}});
 
+	if (defined $info->{db}[0] and exists $info->{db}[0]{error}) {
+		ndie $info->{db}[0]{error};
+	}
+
 	## schema, table, rows, pages, otta, bloat, wastedpages, wastedbytes, wastedsize
 	##         index, ""     "" ...
 	my $N = qr{ (.+?)\s*\|};
@@ -1441,31 +1446,76 @@ ORDER BY wastedbytes DESC LIMIT $LIMIT
 				$db->{perf} .= " $schema.$table=$wb";
 				my $msg = qq{table $schema.$table rows:$tups pages:$pages shouldbe:$otta (${bloat}X)};
 				$msg .= qq{ wasted size:$wb ($ws)};
-				## The key here is the wastedbytes
-				if ($critical and $wb >= $critical) {
-					add_critical $msg;
-				}
-				elsif ($warning and $wb >= $warning) {
-					add_warning $msg;
+				my $ok = 1;
+				my $perbloat = $bloat * 100;
+
+				if (length $critical) {
+					if (index($critical,'%')>=0) {
+						(my $critical2 = $critical) =~ s/\%//;
+						if ($perbloat >= $critical2) {
+							add_critical $msg;
+							$ok = 0;
+						}
+					}
+					elsif ($wb >= $critical) {
+						add_critical $msg;
+						$ok = 0;
+					}
 				}
-				else {
-					($max = $wb, $maxmsg = $msg) if $wb > $max;
+
+				if (length $warning and $ok) {
+					if (index($warning,'%')>=0) {
+						(my $warning2 = $warning) =~ s/\%//;
+						if ($perbloat >= $warning2) {
+							add_warning $msg;
+							$ok = 0;
+						}
+					}
+					elsif ($wb >= $warning) {
+						add_warning $msg;
+						$ok = 0;
+					}
 				}
+				($max = $wb, $maxmsg = $msg) if $wb > $max and $ok;
 			}
+
 			## Now the index, if it exists
 			if ($index ne '?') {
 				$db->{perf} .= " $index=$iwb" if $iwb;
 				my $msg = qq{index $index rows:$irows pages:$ipages shouldbe:$iotta (${ibloat}X)};
 				$msg .= qq{ wasted bytes:$iwb ($iws)};
-				if ($critical and $iwb >= $critical) {
-					add_critical $msg;
-				}
-				elsif ($warning and $iwb >= $warning) {
-					add_warning $msg;
+				my $ok = 1;
+				my $iperbloat = $ibloat * 100;
+
+				if (length $critical) {
+					if (index($critical,'%')>=0) {
+						(my $critical2 = $critical) =~ s/\%//;
+						if ($iperbloat >= $critical2) {
+							add_critical $msg;
+							$ok = 0;
+						}
+					}
+					elsif ($iwb >= $critical) {
+						add_critical $msg;
+						$ok = 0;
+					}
 				}
-				else {
-					($max = $iwb, $maxmsg = $msg) if $iwb > $max;
+
+				if (length $warning and $ok) {
+					if (index($warning,'%')>=0) {
+						(my $warning2 = $warning) =~ s/\%//;
+						if ($iperbloat >= $warning2) {
+							add_warning $msg;
+							$ok = 0;
+						}
+					}
+					elsif ($iwb >= $warning) {
+						add_warning $msg;
+						$ok = 0;
+					}
 				}
+
+				($max = $iwb, $maxmsg = $msg) if $iwb > $max and $ok;
 			}
 		}
 		if ($max == -1) {
@@ -1701,7 +1751,7 @@ sub check_disk_space {
 					$ok = 0;
 				}
 			}
-			if (length $warning) {
+			if (length $warning and $ok) {
 				if (index($warning,'%')>=0) {
 					(my $warning2 = $warning) =~ s/\%//;
 					if ($percent >= $warning2) {
@@ -2863,7 +2913,7 @@ check_postgres.pl - Postgres monitoring script for Nagios
 
 =head1 VERSION
 
-This documents describes B<check_postgres.pl> version 1.9.0
+This documents describes B<check_postgres.pl> version 1.9.1
 
 =head1 SYNOPSIS
 
@@ -3114,8 +3164,8 @@ enabled on the target databases, and requires that ANALYZE is run frequently.
 The B<--include> and B<--exclude> options can be used to filter out which tables 
 to look at. See the L</"BASIC FILTERING"> section for more details.
 
-The B<--warning> and B<--critical> options must be specified as sizes. 
-Valid units are bytes, kilobytes, megabytes, gigabytes, terabytes, and exabytes. 
+The B<--warning> and B<--critical> options can be specified as sizes or percents.
+Valid size units are bytes, kilobytes, megabytes, gigabytes, terabytes, and exabytes. 
 You can abbreviate all of those with the first letter. Items without units are 
 assumed to be 'bytes'. The default values are '1 GB' and '5 GB'. The value 
 represents the number of "wasted bytes", or the difference between what is actually 
@@ -3144,6 +3194,10 @@ Example 2: Give a critical if table 'orders' on host 'sami' has more than 10 meg
 
   check_postgres_bloat --host=sami --include=orders --critical='10 MB'
 
+Example 3: Give a critical if table 'q4' on database 'sales' is over 50% bloated
+
+  check_postgres_bloat --db=sales --include=q4 --critical='50%'
+
 =item B<connection> (symlink: check_postgres_connection)
 
 Simply connects, issues a 'SELECT version()', and leaves.
@@ -3794,6 +3848,12 @@ Items not specifically attributed are by Greg Sabino Mullane.
 
 =over 4
 
+=item B<Version 1.9.1> (June 24, 2008)
+
+Fix an error in the bloat SQL in 1.9.0
+Allow percentage arguments to be over 99%
+Allow percentages in the bloat --warning and --critical (thanks to Robert Treat for the idea)
+
 =item B<Version 1.9.0> (June 22, 2008)
 
 Don't include information_schema in certain checks. (Jeff Frost)
diff --git a/check_postgres.pl.html b/check_postgres.pl.html
index e20d16f..c9bf015 100644
--- a/check_postgres.pl.html
+++ b/check_postgres.pl.html
@@ -51,7 +51,7 @@
 </p>
 <hr />
 <h1><a name="version">VERSION</a></h1>
-<p>This documents describes <strong>check_postgres.pl</strong> version 1.9.0</p>
+<p>This documents describes <strong>check_postgres.pl</strong> version 1.9.1</p>
 <p>
 </p>
 <hr />
@@ -346,8 +346,8 @@ The <strong>--include</strong> and <strong>--exclude</strong> options can be use
 to look at. See the <a href="#basic_filtering">BASIC FILTERING</a> section for more details.</p>
 </dd>
 <dd>
-<p>The <strong>--warning</strong> and <strong>--critical</strong> options must be specified as sizes. 
-Valid units are bytes, kilobytes, megabytes, gigabytes, terabytes, and exabytes. 
+<p>The <strong>--warning</strong> and <strong>--critical</strong> options can be specified as sizes or percents.
+Valid size units are bytes, kilobytes, megabytes, gigabytes, terabytes, and exabytes. 
 You can abbreviate all of those with the first letter. Items without units are 
 assumed to be 'bytes'. The default values are '1 GB' and '5 GB'. The value 
 represents the number of &quot;wasted bytes&quot;, or the difference between what is actually 
@@ -385,6 +385,13 @@ should give a rough idea of how bloated things are.</p>
 <pre>
   check_postgres_bloat --host=sami --include=orders --critical='10 MB'</pre>
 </dd>
+<dd>
+<p>Example 3: Give a critical if table 'q4' on database 'sales' is over 50% bloated</p>
+</dd>
+<dd>
+<pre>
+  check_postgres_bloat --db=sales --include=q4 --critical='50%'</pre>
+</dd>
 </li>
 <dt><strong><a name="connection" class="item"><strong>connection</strong> (symlink: check_postgres_connection)</a></strong>
 
@@ -1191,6 +1198,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="1" class="item"><strong>Version 1.9.1</strong> (June 24, 2008)</a></strong>
+
+<dd>
+<p>Fix an error in the bloat SQL in 1.9.0
+Allow percentage arguments to be over 99%
+Allow percentages in the bloat --warning and --critical (thanks to Robert Treat for the idea)</p>
+</dd>
+</li>
 <dt><strong><a name="0" class="item"><strong>Version 1.9.0</strong> (June 22, 2008)</a></strong>
 
 <dd>
@@ -1225,7 +1240,7 @@ Thanks to Jeff Frost for the bug report.</p>
 <p>Changes to allow working under Nagios' embedded Perl mode. (Ioannis Tambouras)</p>
 </dd>
 </li>
-<dt><strong><a name="1" class="item"><strong>Version 1.8.1</strong> (June 9, 2008)</a></strong>
+<dt><strong><strong>Version 1.8.1</strong> (June 9, 2008)</strong>
 
 <dd>
 <p>Allow check_bloat to work on Postgres version 8.0.
diff --git a/index.html b/index.html
index db487fb..204d529 100644
--- a/index.html
+++ b/index.html
@@ -21,13 +21,13 @@ 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>1.9.0</b>, and was released on June 22, 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>1.9.1</b>, and was released on June 24, 2008.</p>
 
 <ul>
- <li><a href="/check_postgres/check_postgres.pl.html">Documentation for check_postgres 1.9.0</a></li>
+ <li><a href="/check_postgres/check_postgres.pl.html">Documentation for check_postgres 1.9.1</a></li>
 </ul>
 <ul>
- <li><a href="/check_postgres/check_postgres.pl">Download check_postgres.pl 1.9.0</a></li>
+ <li><a href="/check_postgres/check_postgres.pl">Download check_postgres.pl 1.9.1</a></li>
  <li><a href="/check_postgres/check_postgres.pl.asc">PGP signature for check_postgres.pl</a></li>
 </ul>
 
-- 
1.5.5.4



More information about the Check_postgres mailing list