Use statement_timeout in addition to alarm.
authorGreg Sabino Mullane <greg@endpoint.com>
Mon, 11 May 2009 15:29:30 +0000 (11:29 -0400)
committerGreg Sabino Mullane <greg@endpoint.com>
Mon, 11 May 2009 15:29:30 +0000 (11:29 -0400)
Per requests from many people.

MANIFEST
META.yml
Makefile.PL
check_postgres.pl
check_postgres.pl.html
t/04_timeout.t [new file with mode: 0644]

index 06a306c22e5c14f98aca983786ba469388327a5b..62da5af4eda621b56731505d8655cd4da7f28a17 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -47,6 +47,7 @@ t/02_txn_wraparound.t
 t/02_version.t
 t/02_wal_files.t
 t/03_translations.t
+t/04_timeout.t
 t/99_cleanup.t
 t/99_perlcritic.t
 t/99_pod.t
index c159a7f7d6fbc576f4f4769a18abc29751ddaa59..a6d9f9b3dcca010c2e5b7d1792b4e496e1f8fb2f 100644 (file)
--- a/META.yml
+++ b/META.yml
@@ -1,6 +1,6 @@
 --- #YAML:1.0
 name                        : check_postgres.pl
-version                     : 2.8.0
+version                     : 2.8.1
 abstract                    : Postgres monitoring script
 author:              
   - Greg Sabino Mullane <greg@endpoint.com>
@@ -30,7 +30,7 @@ recommends:
 provides:
   check_postgres:
     file                    : check_postgres.pl
-    version                 : 2.8.0
+    version                 : 2.8.1
 
 keywords:
   - Postgres
index 39a80e6f76b16f93d3c435b10ac90180f7d52ca5..031cf68399583d53c11553bb9dea8bb44d89ca77 100644 (file)
@@ -6,7 +6,7 @@ use strict;
 use warnings;
 use 5.006001;
 
-my $VERSION = '2.8.0';
+my $VERSION = '2.8.1';
 
 if ($VERSION =~ /_/) {
        print "WARNING! This is a test version ($VERSION) and should not be used in production!\n";
index c351b1dc1ea5de66abe1aebc2b13d958473e48a7..dabd3e8f1ea1f983028af84d821a76d5337e1088 100755 (executable)
@@ -29,7 +29,7 @@ $Data::Dumper::Varname = 'POSTGRES';
 $Data::Dumper::Indent = 2;
 $Data::Dumper::Useqq = 1;
 
-our $VERSION = '2.8.0';
+our $VERSION = '2.8.1';
 
 use vars qw/ %opt $PSQL $res $COM $SQL $db /;
 
@@ -506,7 +506,7 @@ our $nohost = 0;
 our $ERROR = '';
 
 $opt{test} = 0;
-$opt{timeout} = 10;
+$opt{timeout} = 30;
 
 die $USAGE unless
        GetOptions(
@@ -699,7 +699,7 @@ Other options:
   --PSQL=FILE        location of the psql executable; avoid using if possible
   -v, --verbose      verbosity level; can be used more than once to increase the level
   -h, --help         display this help information
-  -t X, --timeout=X  how long in seconds before we timeout. Defaults to 10 seconds.
+  -t X, --timeout=X  how long in seconds before we timeout. Defaults to 30 seconds.
   --symlinks         create named symlinks to the main program for each action
 
 Actions:
@@ -1585,14 +1585,17 @@ sub run_command {
                        }
                }
 
-               push @args, '-c', $string;
-
-               $VERBOSE >= 3 and warn Dumper \@args;
-
                local $SIG{ALRM} = sub { die 'Timed out' };
                my $timeout = $arg->{timeout} || $opt{timeout};
+               my $dbtimeout = $timeout * 1000;
                alarm 0;
 
+               $string = "BEGIN;SET statement_timeout=$dbtimeout;COMMIT;$string";
+
+               push @args, '-c', $string;
+
+               $VERBOSE >= 3 and warn Dumper \@args;
+
                my $start = $opt{showtime} ? [gettimeofday()] : 0;
                open my $oldstderr, '>&', \*STDERR or ndie msg('runcommand-nodupe');
                open STDERR, '>', $errorfile or ndie msg('runcommand-noerr');
@@ -1608,7 +1611,7 @@ sub run_command {
                        if ($err =~ /Timed out/) {
                                ndie msg('runcommand-timeout', $timeout);
                        }
-                       else {
+                       else {ndie $res;
                                ndie msg('runcommand-err');
                        }
                }
@@ -1631,6 +1634,10 @@ sub run_command {
                                ndie "$db->{error}";
                        }
 
+                       elsif ($db->{error} =~ /statement timeout/) {
+                               ndie msg('runcommand-timeout', $timeout);
+                       }
+
                        if (!$db->{ok} and !$arg->{failok} and !$arg->{noverify}) {
 
                                ## Check if problem is due to backend being too old for this check
@@ -4747,7 +4754,7 @@ sub show_dbstats {
 
 B<check_postgres.pl> - a Postgres monitoring script for Nagios, MRTG, Cacti, and others
 
-This documents describes check_postgres.pl version 2.8.0
+This documents describes check_postgres.pl version 2.8.1
 
 =head1 SYNOPSIS
 
@@ -6092,6 +6099,10 @@ Items not specifically attributed are by Greg Sabino Mullane.
 
 =over 4
 
+=item B<Version 2.8.1> (May, 2009)
+
+  Added timeout via statement_timeout in addition to perl alarm (Greg)
+
 =item B<Version 2.8.0> (May 4, 2009)
 
   Added internationalization support (Greg)
index 6fc2470ecd49150fca842999966e004f56abef82..b0ece06590bc6b1ae47fbd18d533735c7eed7eb9 100644 (file)
@@ -96,7 +96,7 @@
 <hr />
 <h1><a name="name">NAME</a></h1>
 <p><strong>check_postgres.pl</strong> - a Postgres monitoring script for Nagios, MRTG, Cacti, and others</p>
-<p>This documents describes check_postgres.pl version 2.8.0</p>
+<p>This documents describes check_postgres.pl version 2.8.1</p>
 <p>
 </p>
 <hr />
@@ -1328,13 +1328,19 @@ 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_0"><strong>Version 2.8.0</strong> (May ??, 2009)</a></strong></dt>
+<dt><strong><a name="item_1"><strong>Version 2.8.1</strong> (May, 2009)</a></strong></dt>
+
+<dd>
+<pre>
+  Added timeout via statement_timeout in addition to perl alarm (Greg)</pre>
+</dd>
+<dt><strong><a name="item_0"><strong>Version 2.8.0</strong> (May 4, 2009)</a></strong></dt>
 
 <dd>
 <pre>
   Added internationalization support (Greg)
   Added the 'disabled_triggers' check (Greg)
-  Added the prepared_txns' check (Greg)
+  Added the 'prepared_txns' check (Greg)
   Added the 'new_version_cp' and 'new_version_pg' checks (Greg)
   French translations (Guillaume Lelarge)
   Make the backends search return ok if no matches due to inclusion rules,
@@ -1350,7 +1356,8 @@ feature requests, and commit notices, send email to <a href="mailto:check_postgr
   Set stats to zero if we bail early due to USERWHERECLAUSE (Andras Fabian)
   Add additional items to dbstats output (Andras Fabian)
   Remove --schema option from the fsm_ checks. (Greg Mullane and Robert Treat)
-  Various fixes. (Jeff Boes)
+  Handle case when ENV{PGUSER} is set. (Andy Lester)
+  Many various fixes. (Jeff Boes)
   Fix --dbservice: check version and use ENV{PGSERVICE} for old versions (Cédric Villemain)</pre>
 </dd>
 <dt><strong><a name="item_3"><strong>Version 2.7.3</strong> (February 10, 2009)</a></strong></dt>
@@ -1366,7 +1373,7 @@ feature requests, and commit notices, send email to <a href="mailto:check_postgr
 <pre>
   Fix to prevent multiple groupings if db arguments given.</pre>
 </dd>
-<dt><strong><a name="item_1"><strong>Version 2.7.1</strong> (February 6, 2009)</a></strong></dt>
+<dt><strong><strong>Version 2.7.1</strong> (February 6, 2009)</strong></dt>
 
 <dd>
 <pre>
diff --git a/t/04_timeout.t b/t/04_timeout.t
new file mode 100644 (file)
index 0000000..64e6857
--- /dev/null
@@ -0,0 +1,27 @@
+#!perl
+
+## Test the timeout functionality
+
+use 5.006;
+use strict;
+use warnings;
+use Data::Dumper;
+use Test::More tests => 2;
+use lib 't','.';
+use CP_Testing;
+
+use vars qw/$dbh $SQL $t $res/;
+
+my $cp = CP_Testing->new( {default_action => 'custom_query'} );
+
+$dbh = $cp->test_database_handle();
+
+$t=q{Setting the --timeout flag works as expected};
+$res = $cp->run('--query="SELECT pg_sleep(2)" -w 7 --timeout=1');
+like ($res, qr{Command timed out}, $t);
+
+$t=q{Setting the --timeout flag works as expected};
+$res = $cp->run('--query="SELECT pg_sleep(1)" -w 7 --timeout=2');
+like ($res, qr{Invalid format}, $t);
+
+exit;