From: Greg Sabino Mullane Date: Mon, 24 Mar 2008 01:12:50 +0000 (-0400) Subject: Add "emptyok" concept for queries that may return no rows. X-Git-Tag: 2.9.0~378 X-Git-Url: http://waps.l3s.uni-hannover.de/gitweb/?a=commitdiff_plain;h=d0d7b3e80f3e6f8d87f9cac098a24fa6721edd1c;p=check_postgres.git Add "emptyok" concept for queries that may return no rows. --- diff --git a/check_postgres.pl b/check_postgres.pl index c11e85c49..bde3a8131 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -549,6 +549,8 @@ sub run_command { ## "failok" - don't report if we failed ## "target" - use this targetlist instead of generating one ## "timeout" - change the timeout from the default of $opt{timeout} + ## "regex" - the query must match this or we throw an error + ## "emptyok" - it's okay to not match any rows at all my $string = shift; my $arg = shift || {}; @@ -564,8 +566,8 @@ sub run_command { ## Multi-args are grouped together: host, port, dbuser, dbpass ## Grouped are kept together for first pass ## The final arg in a group is passed on - ## - ## Examples: + ## + ## Examples: ## --host=a,b --port=5433 --db=c ## Connects twice to port 5433, using database c, to hosts a and b ## a-5433-c b-5433-c @@ -740,8 +742,12 @@ sub run_command { $db->{slurp} = <$tempfh>; } $db->{ok} = 1; + + ## Allow an empty query (no matching rows) if requested + if ($arg->{emptyok} and $arg->{slurp} =~ /^\s*$/o) { + } ## If we were provided with a regex, check and bail if it fails - if ($arg->{regex}) { + elsif ($arg->{regex}) { if ($db->{slurp} !~ $arg->{regex}) { add_unknown qq{T-BAD-QUERY $db->{slurp}}; ## Remove it from the returned hash @@ -2133,11 +2139,17 @@ sub check_txn_idle { $SQL = q{SELECT datname, max(COALESCE(ROUND(EXTRACT(epoch FROM now()-xact_start)),0)) }. q{FROM pg_stat_activity WHERE current_query = ' in transaction' GROUP BY 1}; - my $info = run_command($SQL, { regex => qr[\s*.+?\s+\|\s+\d+] } ); + my $info = run_command($SQL, { regex => qr[\s*.+?\s+\|\s+\d+], emptyok => 1 } ); for $db (@{$info->{db}}) { my $max = -1; + + if ($db->{slurp} =~ /^\s*$/o) { + add_ok 'no idle in transaction'; + next; + } + SLURP: while ($db->{slurp} =~ /(.+?)\s+\|\s+(\d+)\s*/gsm) { my ($dbname,$current) = ($1,$2); next SLURP if skip_item($dbname);