[check_postgres] [commit] Clean up prepared_txns action and make a test for it.
check_postgres at bucardo.org
check_postgres at bucardo.org
Wed Apr 22 21:37:55 UTC 2009
Committed by Greg Sabino Mullane <greg at endpoint.com>
Clean up prepared_txns action and make a test for it.
---
check_postgres.pl | 25 +++++++++++-----
t/02_prepared_txns.t | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
t/CP_Testing.pm | 1 +
3 files changed, 91 insertions(+), 8 deletions(-)
diff --git a/check_postgres.pl b/check_postgres.pl
index 85068a0..567da8d 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, at warn, at 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 0000000..b3c7d8d
--- /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 d617796..af0e005 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};
--
1.6.0.5
More information about the Check_postgres
mailing list