Attempts to fix bug 59.
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
## 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};
use strict;
use warnings;
use Data::Dumper;
-use Test::More tests => 14;
+use Test::More tests => 15;
use lib 't','.';
use CP_Testing;
$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;
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};
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;
}
$count = $sth->fetchall_arrayref()->[0][0];
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');