connection: Make all errors including timeout from psql CRITICAL
authorChristoph Berg <[email protected]>
Wed, 8 Jun 2016 11:42:44 +0000 (13:42 +0200)
committerChristoph Berg <[email protected]>
Wed, 8 Jun 2016 11:42:44 +0000 (13:42 +0200)
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.

check_postgres.pl
t/02_connection.t
t/CP_Testing.pm

index afb3b27a0ded44fa25de843fec01fbd664462b24..84065bad6f311ca9bd2fb9b32842b975c1835a72 100755 (executable)
@@ -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
index d6fd219e1d876110bf68781ab3d5cdb79f105ecb..20839ae3dde0925550402f19b5a457c467635dba 100644 (file)
@@ -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;
index 0ed72874ce55d42680b08290a0ebaa294362a43e..3ff087896df91a93d6b8f1db4b521166e3e0bfa4 100644 (file)
@@ -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...