From 6c048b83b7c557331b24fdb4a3c43a773b35f3d8 Mon Sep 17 00:00:00 2001 From: Emmanuel Lacour Date: Mon, 7 Oct 2024 11:45:10 +0200 Subject: [PATCH] check_backuppc: critical output if no host was checked --- check_backuppc | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/check_backuppc b/check_backuppc index 146e5d1..1b1dc27 100755 --- a/check_backuppc +++ b/check_backuppc @@ -182,7 +182,10 @@ foreach my $host (sort(keys(%Status))) # Check host error if ($Status{$host}{'error'}) { - $Status{$host}{statusMsg} = "error: ".$Status{$host}{'error'}." / ".$Status{$host}{'reason'}; + $Status{$host}{statusCode} = 'CRITICAL'; + $Status{$host}{statusMsg} = "error: " .$Status{$host}{'error'} . " /" + . ( $Status{$host}{reason} ne '' ? " reason: " . $Status{$host}{reason} . "/" : '' ) + . " status: " . $Status{$host}{'state'}; } else { $Status{$host}{statusMsg} = "status: ".$Status{$host}{'state'}; } @@ -194,10 +197,9 @@ foreach my $host (sort(keys(%Status))) if ( $Status{$host}{'startDays'} > $critDaysOld ) { $Status{$host}{statusMsg} .= ", no backups"; $Status{$host}{statusCode} = 'CRITICAL'; - $statusCode = 'CRITICAL'; } elsif ( $Status{$host}{'startDays'} > $warnDaysOld ) { $Status{$host}{statusMsg} .= ", no backups"; - $Status{$host}{statusCode} = 'WARNING' unless ( $Status{$host}{statusCode} = 'CRITICAL' ); + $Status{$host}{statusCode} = 'WARNING' unless ( $Status{$host}{statusCode} eq 'CRITICAL' ); $statusCode = 'WARNING' unless ( $statusCode eq 'CRITICAL' ); } } else { @@ -223,6 +225,7 @@ foreach my $host (sort(keys(%Status))) $Status{$host}{statusMsg} .= ", last good backup have ".sprintf("%.1f",$Status{$host}{'lastGoodBackupDays'})." days"; } } + $ok_count++ if ( $Status{$host}{statusCode} eq 'OK' ); $unknown_count++ if ( $Status{$host}{statusCode} eq 'UNKNOWN' ); $warning_count++ if ( $Status{$host}{statusCode} eq 'WARNING' ); @@ -230,6 +233,19 @@ foreach my $host (sort(keys(%Status))) } +# Ensure we checked at least one host +if ( $hostCount == 0 || !scalar(keys %Status) ) { + $statusCode = 'CRITICAL'; +} elsif ( grep { $Status{$_}{statusCode} eq 'CRITICAL' } keys %Status ) { + $statusCode = 'CRITICAL'; +} elsif ( grep { $Status{$_}{statusCode} eq 'WARNING' } keys %Status ) { + $statusCode = 'WARNING'; +} elsif ( grep { $Status{$_}{statusCode} eq 'UNKNOWN' } keys %Status ) { + $statusCode = 'UNKNOWN'; +} else { + $statusCode = 'OK'; +} + my $statusMsg = "BACKUPPC $statusCode"; if ( $statusCode eq 'OK' ) { @@ -242,15 +258,19 @@ if ( $statusCode eq 'OK' ) { if ( $verbose ) { $statusMsg .= " ("; my $first_host = 1; - foreach my $host ( keys %Status ) { - next if (@hostsDesired and not grep {/^$host$/} @hostsDesired); - next if (@hostsExcluded and grep {/^$host$/} @hostsExcluded); - next if ( $Status{$host}{BackupsDisable} && $Status{$host}{BackupsDisable} == 2 and not $forceCheckOnDisabledHosts ); - next if ($Status{$host}{'type'} eq 'archive'); - if ( $Status{$host}{statusCode} && $Status{$host}{statusCode} ne 'OK' ) { - $statusMsg .= ", " unless ( $first_host ); - $statusMsg .= "$host: ".$Status{$host}{statusCode}." - ".$Status{$host}{statusMsg}; - $first_host = 0 if ( $first_host ); + if ($hostCount == 0 || !scalar(keys %Status)) { + $statusMsg .= "no host checked"; + } else { + foreach my $host ( keys %Status ) { + next if (@hostsDesired and not grep {/^$host$/} @hostsDesired); + next if (@hostsExcluded and grep {/^$host$/} @hostsExcluded); + next if ( $Status{$host}{BackupsDisable} && $Status{$host}{BackupsDisable} == 2 and not $forceCheckOnDisabledHosts ); + next if ($Status{$host}{'type'} eq 'archive'); + if ( $Status{$host}{statusCode} && $Status{$host}{statusCode} ne 'OK' ) { + $statusMsg .= ", " unless ( $first_host ); + $statusMsg .= "$host: ".$Status{$host}{statusCode}." - ".$Status{$host}{statusMsg}; + $first_host = 0 if ( $first_host ); + } } } $statusMsg .= ")";