summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGreg Sabino Mullane2011-07-04 18:37:04 +0000
committerGreg Sabino Mullane2011-07-04 18:37:04 +0000
commitc5c4f994ab214513f1d0ae86d5b88cc38ddae30d (patch)
tree62fc3179292daafb0c405dce4a580dc5fcb3a016
parent5f9eac01e750764f072f33ee9f0efb6cc584b5ba (diff)
Better tests for non-superuser in txn_idle and friends.
Attempts to fix bug 59.
-rwxr-xr-xcheck_postgres.pl33
-rw-r--r--t/02_txn_idle.t7
-rw-r--r--t/CP_Testing.pm20
3 files changed, 44 insertions, 16 deletions
diff --git a/check_postgres.pl b/check_postgres.pl
index 6a8863e7b..3be87c1ae 100755
--- a/check_postgres.pl
+++ b/check_postgres.pl
@@ -7162,7 +7162,7 @@ sub check_txn_idle {
my $thing = shift || msg('transactions');
my $perf = shift || msg('txn-time');
my $start = shift || 'query_start';
- my $clause = shift || q{current_query = '<IDLE> in transaction'};
+ my $clause = shift || q{current_query ~ '^<'};
## Extract the warning and critical seconds and counts.
## If not given, items will be an empty string
@@ -7196,25 +7196,30 @@ sub check_txn_idle {
## Skip if we don't care about this database
next if skip_item($r->{datname});
- ## Detect cases where pg_stat_activity is not fully populated
- if (length $r->{xact_start} and $r->{xact_start} !~ /\d/o) {
- ## Perhaps this is a non-superuser?
- if ($r->{current_query} =~ /insufficient/) {
- add_unknown msg('psa-nosuper');
- return;
- }
+ ## We do a lot of filtering based on the current_query
+ my $cq = $r->{current_query};
- ## Perhaps stats_command_string / track_activities is off?
- if ($r->{current_query} =~ /disabled/) {
- add_unknown msg('psa-disabled');
- return;
- }
+ ## Return unknown if we cannot see because we are a non-superuser?
+ if ($cq =~ /insufficient/o) {
+ add_unknown msg('psa-nosuper');
+ return;
+ }
+
+ ## Return unknown if stats_command_string / track_activities is off?
+ if ($cq =~ /disabled/o) {
+ add_unknown msg('psa-disabled');
+ return;
+ }
- ## Something else is going on
+ ## Detect other cases where pg_stat_activity is not fully populated
+ if (length $r->{xact_start} and $r->{xact_start} !~ /\d/o) {
add_unknown msg('psa-noexact');
return;
}
+ ## Filter out based on the action
+ next if $action eq 'txn_idle' and $cq ne '<IDLE> in transaction';
+
## Keep track of the longest overall time
$maxr = $r if $r->{seconds} >= $maxr->{seconds};
diff --git a/t/02_txn_idle.t b/t/02_txn_idle.t
index f5a32a89c..598cc5619 100644
--- a/t/02_txn_idle.t
+++ b/t/02_txn_idle.t
@@ -6,7 +6,7 @@ use 5.006;
use strict;
use warnings;
use Data::Dumper;
-use Test::More tests => 14;
+use Test::More tests => 15;
use lib 't','.';
use CP_Testing;
@@ -78,6 +78,11 @@ sleep(1);
$t = qq{$S identifies idle using '1 for 2s'};
like ($cp->run(q{-w '1 for 2s'}), qr{1 idle transactions longer than 2s, longest: \d+s}, $t);
+$t = qq{$S returns an unknown if running as a non-superuser};
+my $olduser = $cp->{testuser};
+$cp->{testuser} = 'powerless_pete';
+like ($cp->run('-w 0'), qr{^$label UNKNOWN: .+superuser}, $t);
+
$idle_dbh->commit;
exit;
diff --git a/t/CP_Testing.pm b/t/CP_Testing.pm
index ee9e9e68e..f9d71903e 100644
--- a/t/CP_Testing.pm
+++ b/t/CP_Testing.pm
@@ -24,6 +24,7 @@ sub new {
started => time(),
dbdir => $arg->{dbdir} || 'test_database_check_postgres',
testuser => $arg->{testuser} || 'check_postgres_testing',
+ testuser2 => $arg->{testuser2} || 'powerless_pete',
};
if (exists $arg->{default_action}) {
$self->{action} = $arg->{default_action};
@@ -228,7 +229,18 @@ sub test_database_handle {
if ($res !~ /$newuser/) {
$COM = qq{psql -d template1 -q -h "$host" -c "CREATE USER $newuser"};
system $COM;
- $SQL = q{UPDATE pg_shadow SET usesuper='t' WHERE usename = 'check_postgres_testing'};
+ $SQL = q{UPDATE pg_shadow SET usesuper='t' WHERE usename = '$newuser'};
+ $COM = qq{psql -d postgres -q -h "$host" -c "$SQL"};
+ system $COM;
+ }
+
+ $newuser = $self->{testuser2};
+ $SQL = qq{SELECT * FROM pg_user WHERE usename = '$newuser'};
+ $res = qx{psql -Ax -qt -d template1 -q -h "$host" -c "$SQL"};
+ if ($res !~ /$newuser/) {
+ $COM = qq{psql -d template1 -q -h "$host" -c "CREATE USER $newuser"};
+ system $COM;
+ $SQL = q{UPDATE pg_shadow SET usesuper='t' WHERE usename = '$newuser'};
$COM = qq{psql -d postgres -q -h "$host" -c "$SQL"};
system $COM;
}
@@ -309,6 +321,12 @@ sub test_database_handle {
if (!$count) {
$dbh->do("CREATE USER $dbuser SUPERUSER");
}
+ my $user2 = $self->{testuser2};
+ $sth->execute($user2);
+ $count = $sth->fetchall_arrayref()->[0][0];
+ if (!$count) {
+ $dbh->do("CREATE USER $user2");
+ }
}
$dbh->do('CREATE DATABASE beedeebeedee');
$dbh->do('CREATE DATABASE ardala');