diff options
author | Christoph Berg | 2016-06-08 11:42:44 +0000 |
---|---|---|
committer | Christoph Berg | 2016-06-08 11:42:44 +0000 |
commit | 0984bbc4effc2bcc64ac53c5957ce23e1def832a (patch) | |
tree | ab43838e3c428f3d546c0e65c9c168663a73ac8f | |
parent | c2e378122b5b20abf7ab69476296a5307733def0 (diff) |
connection: Make all errors including timeout from psql CRITICAL
UNKNOWN is not so much useful in the context of basic connection checks.
(The result remains UNKNOWN in case version() returns something fishy.)
Close #100.
-rwxr-xr-x | check_postgres.pl | 18 | ||||
-rw-r--r-- | t/02_connection.t | 11 | ||||
-rw-r--r-- | t/CP_Testing.pm | 25 |
3 files changed, 48 insertions, 6 deletions
diff --git a/check_postgres.pl b/check_postgres.pl index afb3b27a0..84065bad6 100755 --- a/check_postgres.pl +++ b/check_postgres.pl @@ -2624,7 +2624,7 @@ sub run_command { } } - local $SIG{ALRM} = sub { die 'Timed out' }; + local $SIG{ALRM} = sub { die "Timed out\n" }; alarm 0; push @args, '-c', $string; @@ -2642,6 +2642,10 @@ sub run_command { alarm 0; open STDERR, '>&', $oldstderr or ndie msg('runcommand-noerr'); close $oldstderr or ndie msg('file-noclose', 'STDERR copy', $!); + if ($err and $action eq 'connection') { + $info->{fatal} = $err; + return $info; + } if ($err) { if ($err =~ /Timed out/) { ndie msg('runcommand-timeout', $timeout); @@ -2666,8 +2670,8 @@ sub run_command { } ## If we are just trying to connect, failed attempts are critical - if ($action eq 'connection' and $db->{error} =~ /FATAL|could not connect/) { - $info->{fatal} = 1; + if ($action eq 'connection' and $db->{error}) { + $info->{fatal} = $db->{error}; return $info; } @@ -4325,11 +4329,15 @@ sub check_connection { } my $info = run_command('SELECT version() AS v'); + if ($info->{fatal}) { + add_critical $info->{fatal}; + return; + } for $db (@{$info->{db}}) { my $err = $db->{error} || ''; - if ($err =~ /FATAL|could not connect/) { + if ($err) { $MRTG and do_mrtg({one => 0}); add_critical $db->{error}; return; @@ -10321,6 +10329,8 @@ Items not specifically attributed are by GSM (Greg Sabino Mullane). total_relation_size, using the respective pg_indexes_size() and pg_total_relation_size() functions. All size checks will now also check materialized views where applicable. + + Connection errors are now always critical, not unknown. (Christoph Berg) New action replication_slots checking if logical or physical replication diff --git a/t/02_connection.t b/t/02_connection.t index d6fd219e1..20839ae3d 100644 --- a/t/02_connection.t +++ b/t/02_connection.t @@ -6,7 +6,7 @@ use 5.006; use strict; use warnings; use Data::Dumper; -use Test::More tests => 12; +use Test::More tests => 14; use lib 't','.'; use CP_Testing; @@ -52,7 +52,14 @@ is ($cp->run('--output=MRTG'), qq{1\n0\n\n\n}, $t); $cp->fake_version('ABC'); $t=qq{$S fails if there's a fake version function}; -like ($cp->run(), qr{^$label UNKNOWN:}, $t); +like ($cp->run(), qr{^$label UNKNOWN:.*Invalid query}, $t); + +$cp->fake_version_timeout(); +$t=qq{$S fails on timeout}; +like ($cp->run('--timeout 1'), qr{^$label CRITICAL:.*Timed out}, $t); $cp->reset_path(); +$t=qq{$S fails on nonexisting socket}; +like ($cp->run('--port=1023'), qr{^$label CRITICAL: could not connect to server}, $t); + exit; diff --git a/t/CP_Testing.pm b/t/CP_Testing.pm index 0ed72874c..3ff087896 100644 --- a/t/CP_Testing.pm +++ b/t/CP_Testing.pm @@ -767,6 +767,31 @@ SELECT 'PostgreSQL $version on fakefunction for check_postgres.pl testing'::text } ## end of fake version +sub fake_version_timeout { + + my $self = shift; + my $dbh = $self->{dbh} || die; + my $dbuser = $self->{testuser} || die; + + if (! $self->schema_exists($dbh, $fakeschema)) { + $dbh->do("CREATE SCHEMA $fakeschema"); + } + + $dbh->do(qq{ +CREATE OR REPLACE FUNCTION $fakeschema.version() +RETURNS TEXT +LANGUAGE SQL +AS \$\$ +SELECT pg_sleep(10)::text; +\$\$ +}); + $dbh->do("ALTER USER $dbuser SET search_path = $fakeschema, public, pg_catalog"); + $dbh->commit(); + return; + +} ## end of fake version timeout + + sub fake_self_version { ## Look out... |