check_backuppc: critical output if no host was checked

This commit is contained in:
Emmanuel Lacour 2024-10-07 11:45:10 +02:00 committed by Benjamin Renard
parent 7f607c2c7a
commit 6c048b83b7
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -182,7 +182,10 @@ foreach my $host (sort(keys(%Status)))
# Check host error # Check host error
if ($Status{$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 { } else {
$Status{$host}{statusMsg} = "status: ".$Status{$host}{'state'}; $Status{$host}{statusMsg} = "status: ".$Status{$host}{'state'};
} }
@ -194,10 +197,9 @@ foreach my $host (sort(keys(%Status)))
if ( $Status{$host}{'startDays'} > $critDaysOld ) { if ( $Status{$host}{'startDays'} > $critDaysOld ) {
$Status{$host}{statusMsg} .= ", no backups"; $Status{$host}{statusMsg} .= ", no backups";
$Status{$host}{statusCode} = 'CRITICAL'; $Status{$host}{statusCode} = 'CRITICAL';
$statusCode = 'CRITICAL';
} elsif ( $Status{$host}{'startDays'} > $warnDaysOld ) { } elsif ( $Status{$host}{'startDays'} > $warnDaysOld ) {
$Status{$host}{statusMsg} .= ", no backups"; $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' ); $statusCode = 'WARNING' unless ( $statusCode eq 'CRITICAL' );
} }
} else { } else {
@ -223,6 +225,7 @@ foreach my $host (sort(keys(%Status)))
$Status{$host}{statusMsg} .= ", last good backup have ".sprintf("%.1f",$Status{$host}{'lastGoodBackupDays'})." days"; $Status{$host}{statusMsg} .= ", last good backup have ".sprintf("%.1f",$Status{$host}{'lastGoodBackupDays'})." days";
} }
} }
$ok_count++ if ( $Status{$host}{statusCode} eq 'OK' ); $ok_count++ if ( $Status{$host}{statusCode} eq 'OK' );
$unknown_count++ if ( $Status{$host}{statusCode} eq 'UNKNOWN' ); $unknown_count++ if ( $Status{$host}{statusCode} eq 'UNKNOWN' );
$warning_count++ if ( $Status{$host}{statusCode} eq 'WARNING' ); $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"; my $statusMsg = "BACKUPPC $statusCode";
if ( $statusCode eq 'OK' ) { if ( $statusCode eq 'OK' ) {
@ -242,15 +258,19 @@ if ( $statusCode eq 'OK' ) {
if ( $verbose ) { if ( $verbose ) {
$statusMsg .= " ("; $statusMsg .= " (";
my $first_host = 1; my $first_host = 1;
foreach my $host ( keys %Status ) { if ($hostCount == 0 || !scalar(keys %Status)) {
next if (@hostsDesired and not grep {/^$host$/} @hostsDesired); $statusMsg .= "no host checked";
next if (@hostsExcluded and grep {/^$host$/} @hostsExcluded); } else {
next if ( $Status{$host}{BackupsDisable} && $Status{$host}{BackupsDisable} == 2 and not $forceCheckOnDisabledHosts ); foreach my $host ( keys %Status ) {
next if ($Status{$host}{'type'} eq 'archive'); next if (@hostsDesired and not grep {/^$host$/} @hostsDesired);
if ( $Status{$host}{statusCode} && $Status{$host}{statusCode} ne 'OK' ) { next if (@hostsExcluded and grep {/^$host$/} @hostsExcluded);
$statusMsg .= ", " unless ( $first_host ); next if ( $Status{$host}{BackupsDisable} && $Status{$host}{BackupsDisable} == 2 and not $forceCheckOnDisabledHosts );
$statusMsg .= "$host: ".$Status{$host}{statusCode}." - ".$Status{$host}{statusMsg}; next if ($Status{$host}{'type'} eq 'archive');
$first_host = 0 if ( $first_host ); 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 .= ")"; $statusMsg .= ")";