diff options
author | Greg Sabino Mullane | 2009-04-22 21:04:16 +0000 |
---|---|---|
committer | Greg Sabino Mullane | 2009-04-22 21:04:16 +0000 |
commit | c535dc650ce047098c4e5c9e26f5c39c08ac6dea (patch) | |
tree | e6c6c1bfa71c57ba52b1ecc91358402051d60bbc | |
parent | 1ef5d4b452785241323157be56d261f023d11737 (diff) |
Clean up prepared_txns action and make a test for it.
-rwxr-xr-x | check_postgres.pl | 25 | ||||
-rw-r--r-- | t/02_prepared_txns.t | 73 | ||||
-rw-r--r-- | t/CP_Testing.pm | 1 |
3 files changed, 91 insertions, 8 deletions
diff --git a/check_postgres.pl b/check_postgres.pl index 85068a05a..567da8dd0 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -163,6 +163,7 @@ our %msg = ( 'opt-psql-nofind' => q{Could not find a suitable psql executable}, 'opt-psql-nover' => q{Could not determine psql version}, 'opt-psql-restrict' => q{Cannot use the --PSQL option when NO_PSQL_OPTION is on}, + 'preptxn-none' => q{No prepared transactions found}, 'qtime-fail' => q{Cannot run the txn_idle action unless stats_command_string is set to 'on'!}, 'qtime-msg' => q{longest query: $1s}, 'range-badcs' => q{Invalid '$1' option: must be a checksum}, @@ -346,6 +347,7 @@ our %msg = ( 'opt-psql-nofind' => q{N'a pas pu trouver un psql exécutable}, 'opt-psql-nover' => q{N'a pas pu déterminer la version de psql}, 'opt-psql-restrict' => q{Ne peut pas utiliser l'option --PSQL si NO_PSQL_OPTION est activé}, +'preptxn-none' => q{No prepared transactions found}, 'qtime-fail' => q{Ne peut pas exécuter l'action txn_idle si stats_command_string est désactivé !}, 'qtime-msg' => q{requête la plus longue : $1s}, 'range-badcs' => q{Option « $1 » invalide : doit être une somme de contrôle}, @@ -4514,19 +4516,21 @@ sub check_prepared_txns { default_critical => '30', }); - my $SQL = q{SELECT datname, ROUND(EXTRACT(epoch FROM now()-started)), started}. - q{ FROM pg_prepared_xact() AS (t xid, g text, started timestamptz, u oid, db oid)}. - q{ JOIN pg_database d ON (d.oid = db)}. - q{ ORDER BY started ASC}; + my $SQL = q{SELECT database, ROUND(EXTRACT(epoch FROM now()-prepared)), prepared}. + q{ FROM pg_prepared_xacts ORDER BY prepared ASC}; - my $info = run_command($SQL, {regex => qr[\w+] } ); + my $info = run_command($SQL, {regex => qr[\w+], emptyok => 1 } ); + my $msg = msg('prepared-txn-none'); + my $found = 0; for $db (@{$info->{db}}) { my (@crit,@warn,@ok); my ($maxage,$maxdb) = (0,''); ## used by MRTG only SLURP: while ($db->{slurp} =~ /\s*(.+?) \|\s+(\d+) \|\s+(.+?)$/gsm) { my ($dbname,$age,$date) = ($1,$2,$3); + $found = 1 if ! $found; next SLURP if skip_item($dbname); + $found = 2; if ($MRTG) { if ($age > $maxage) { $maxdb = $dbname; @@ -4538,7 +4542,7 @@ sub check_prepared_txns { next; } - my $msg = "$dbname=$date ($age)"; + $msg = "$dbname=$date ($age)"; $db->{perf} .= " $msg"; if (length $critical and $age >= $critical) { push @crit => $msg; @@ -4553,7 +4557,13 @@ sub check_prepared_txns { if ($MRTG) { do_mrtg({one => $maxage, msg => $maxdb}); } - if (@crit) { + elsif (0 == $found) { + add_ok msg('preptxn-none'); + } + elsif (1 == $found) { + add_unknown msg('no-match-db'); + } + elsif (@crit) { add_critical join ' ' => @crit; } elsif (@warn) { @@ -4564,7 +4574,6 @@ sub check_prepared_txns { } } - return; } ## end of check_prepared_txns diff --git a/t/02_prepared_txns.t b/t/02_prepared_txns.t new file mode 100644 index 000000000..b3c7d8d1e --- /dev/null +++ b/t/02_prepared_txns.t @@ -0,0 +1,73 @@ +#!perl + +## Test the "prepare_txns" action + +use strict; +use warnings; +use Data::Dumper; +use DBI; +use Test::More tests => 10; +use lib 't','.'; +use CP_Testing; + +use vars qw/$dbh $SQL $t $info/; + +my $cp = CP_Testing->new( {default_action => 'prepared_txns'} ); + +$dbh = $cp->test_database_handle(); + +my $S = q{Action 'prepare_txns'}; +my $label = 'POSTGRES_PREPARED_TXNS'; + +$t=qq{$S fails when called with an invalid option}; +like ($cp->run('foobar=12'), qr{^\s*Usage:}, $t); + +## Clear any outstanding transactions +$info = $dbh->selectall_arrayref('SELECT gid FROM pg_prepared_xacts'); +local $dbh->{AutoCommit} = 1; +for (@$info) { + my $gid = $_->[0]; + $dbh->do("ROLLBACK PREPARED '$gid'"); +} +local $dbh->{AutoCommit} = 0; + +$t=qq{$S works when called without warning or critical}; +like ($cp->run(''), qr{^$label OK: .+No prepared transactions found}, $t); + +$dbh->do("PREPARE TRANSACTION '123'"); + +$t=qq{$S gives correct message when all databases excluded}; +like ($cp->run('--include=sbsp'), qr{^$label UNKNOWN: .+No matching databases found due to exclusion}, $t); + +$t=qq{$S fails when called with invalid warning}; +like ($cp->run('-w foo'), qr{ERROR: Invalid argument}, $t); + +$t=qq{$S fails when called with invalid critical}; +like ($cp->run('-c foo'), qr{ERROR: Invalid argument}, $t); + +$t=qq{$S gives correct output with warning}; +like ($cp->run('-w 0'), qr{^$label WARNING}, $t); + +$t=qq{$S gives correct output with warning}; +like ($cp->run('-w 30'), qr{^$label OK}, $t); + +$t=qq{$S gives correct output with critical}; +like ($cp->run('-c 0'), qr{^$label CRITICAL}, $t); + +$t=qq{$S gives correct output with critical}; +like ($cp->run('-c 30'), qr{^$label OK}, $t); + +$t=qq{$S gives correct output for MRTG output}; +like ($cp->run('--output=MRTG'), qr{^\d\n0\n\npostgres\n$}, $t); + +## Clear any outstanding transactions +$info = $dbh->selectall_arrayref('SELECT gid FROM pg_prepared_xacts'); +local $dbh->{AutoCommit} = 1; +for (@$info) { + my $gid = $_->[0]; + $dbh->do("ROLLBACK PREPARED '$gid'"); +} +local $dbh->{AutoCommit} = 0; + +exit; + diff --git a/t/CP_Testing.pm b/t/CP_Testing.pm index d617796b2..af0e005a0 100644 --- a/t/CP_Testing.pm +++ b/t/CP_Testing.pm @@ -64,6 +64,7 @@ sub test_database_handle { print $cfh qq{\n\n## check_postgres.pl testing parameters\n}; print $cfh qq{listen_addresses = ''\n}; print $cfh qq{max_connections = 10\n}; + print $cfh qq{max_prepared_transactions = 5\n}; print $cfh qq{autovacuum = off\n}; print $cfh "\n"; close $cfh or die qq{Could not close "$cfile": $!\n}; |