Compare commits

..

6 commits

Author SHA1 Message Date
Emmanuel Lacour
76a91c37e5
check_backuppc: improve debug output 2024-10-07 11:48:34 +02:00
Emmanuel Lacour
ad909157e9
check_backuppc: critical output if no host was checked 2024-10-07 11:48:28 +02:00
Emmanuel Lacour
bb7b6969f7
check_backuppc: add a check on how many time an host is in backup_in_progress state 2024-10-07 11:48:22 +02:00
Emmanuel Lacour
e0714b51dc
check_backuppc: force check on disabled host if host is specified 2024-10-07 11:48:16 +02:00
Emmanuel Lacour
969b0ca1e3
check_backuppc: Use UNKNOWN as default state 2024-10-07 11:47:56 +02:00
Benjamin Renard
a97930fdca
Use spaces instead of tabs for indentation 2024-10-07 11:17:46 +02:00
2 changed files with 223 additions and 176 deletions

View file

@ -47,6 +47,8 @@ use BackupPC::Lib;
my $version = '1.1.3';
my $warnDaysOld = 2;
my $critDaysOld = 7;
my $warnDaysInProgress = 0.5;
my $critDaysInProgress = 1;
my $verbose = 0;
my $opt_V = 0;
my $opt_h = 0;
@ -56,7 +58,7 @@ my @hostsDesired;
my @hostsExcluded;
my $forceCheckOnDisabledHosts;
my %Status;
my $statusCode = 'OK';
my $statusCode = 'UNKNOWN';
my $ok_count = 0;
my $unknown_count = 0;
my $warning_count = 0;
@ -68,6 +70,8 @@ $goodOpt = GetOptions(
'v+' => \$verbose, 'verbose+' => \$verbose,
'c=f' => \$critDaysOld, 'critical=f' => \$critDaysOld,
'w=f' => \$warnDaysOld, 'warning=f' => \$warnDaysOld,
'C=f' => \$critDaysInProgress, 'critprog=f' => \$critDaysInProgress,
'W=f' => \$warnDaysInProgress, 'warnprog=f' => \$warnDaysInProgress,
'o=s' => \@ownerOnly, 'owner=s' => \@ownerOnly,
'V' => \$opt_V, 'version' => \$opt_V,
'h' => \$opt_h, 'help' => \$opt_h,
@ -78,6 +82,9 @@ $goodOpt = GetOptions(
@hostsDesired = () if $#hostsDesired < 0;
@hostsExcluded = () if $#hostsExcluded < 0;
# Always check disabled host if a named host is provided (when backup is trigerred by cron)
$forceCheckOnDisabledHosts = 1 if ( @hostsDesired && scalar(@hostsDesired) );
if ($opt_V)
{
print "check_backuppc - " . $version . "\n";
@ -94,6 +101,8 @@ if ($opt_h or not $goodOpt)
print " --force,-f force check even if host is disabled\n";
print " --warning,-w days old of last good backup to cause a warning\n";
print " --critical,-c days old of last good backup to be critical\n";
print " --warnprog,-W duration (in days) for state backup_in_progress to cause a warning\n";
print " --critprog,-C duration (in days) for state backup_in_progress to be critical\n";
print " --verbose,-v increase verbosity\n";
print " --version,-V display plugin version\n";
print " --help,-h display this message\n\n";
@ -105,6 +114,11 @@ if ($warnDaysOld > $critDaysOld)
print("BACKUPPC UNKNOWN - Warning threshold must be <= critical\n");
exit $ERRORS{'UNKNOWN'};
}
if ($warnDaysInProgress > $critDaysInProgress)
{
print("BACKUPPC UNKNOWN - Warning in progress threshold must be <= critical in progress\n");
exit $ERRORS{'UNKNOWN'};
}
# Connect to BackupPC
my $server;
@ -151,7 +165,7 @@ foreach my $host (sort(keys(%Status)))
next if (@ownerOnly and not grep {/$owner/} @ownerOnly);
my %host_conf = %{$server->ConfigDataRead($host)};
$Status{$host}{BackupsDisable} = $host_conf{BackupsDisable};
next if ( $Status{$host}{BackupsDisable} eq 2 and not $forceCheckOnDisabledHosts);
next if ( $Status{$host}{BackupsDisable} && $Status{$host}{BackupsDisable} == 2 and not $forceCheckOnDisabledHosts );
next if (@hostsDesired and not grep {/^$host$/} @hostsDesired);
next if (@hostsExcluded and grep {/^$host$/} @hostsExcluded);
next if ($Status{$host}{'type'} eq 'archive');
@ -160,6 +174,9 @@ foreach my $host (sort(keys(%Status)))
# Debug
if ($verbose == 2)
{
while (my ($key, $value) = each %{$Status{$host}}) {
print "$key:\t$value\n";
}
print "Host $host state " . $Status{$host}{'state'} . "\n";
print " with reason: " . $Status{$host}{'reason'} . "\n";
print " with error: " . $Status{$host}{'error'} . "\n";
@ -168,7 +185,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'};
}
@ -180,26 +200,35 @@ 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' );
}
} elsif ( $Status{$host}{'lastGoodBackupDays'} > $critDaysOld )
{
} else {
if ($Status{$host}{state} eq 'Status_backup_in_progress') {
$Status{$host}{'startDays'} = difftime(time(), $Status{$host}{'startTime'}) / (3600 * 24);
if ( $Status{$host}{'startDays'} > $critDaysInProgress ) {
$Status{$host}{statusMsg} .= " for " . sprintf("%.1f", $Status{$host}{'startDays'} * 24) . " hours";
$Status{$host}{statusCode} = 'CRITICAL';
} elsif ( $Status{$host}{'startDays'} > $warnDaysInProgress ) {
$Status{$host}{statusMsg} .= " for " . sprintf("%.1f", $Status{$host}{'startDays'} * 24) . " hours";
$Status{$host}{statusCode} = 'WARNING' unless ( $Status{$host}{statusCode} eq 'CRITICAL' );
}
}
if ( $Status{$host}{'lastGoodBackupDays'} > $critDaysOld ) {
$Status{$host}{statusMsg} .= ", last good backup have ".sprintf("%.1f", $Status{$host}{'lastGoodBackupDays'})." days";
$Status{$host}{statusCode} = 'CRITICAL';
$statusCode = 'CRITICAL';
}
elsif ( $Status{$host}{'lastGoodBackupDays'} > $warnDaysOld )
{
elsif ( $Status{$host}{'lastGoodBackupDays'} > $warnDaysOld ) {
$Status{$host}{statusMsg} .= ", last good backup have ".sprintf("%.1f",$Status{$host}{'lastGoodBackupDays'})." days";
$Status{$host}{statusCode} = 'WARNING' unless ( $Status{$host}{statusCode} = 'CRITICAL' );
$statusCode = 'WARNING' unless ( $statusCode eq 'CRITICAL' );
$Status{$host}{statusCode} = 'WARNING' unless ( $Status{$host}{statusCode} eq 'CRITICAL' );
} else {
$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' );
@ -207,6 +236,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' ) {
@ -219,10 +261,13 @@ if ( $statusCode eq 'OK' ) {
if ( $verbose ) {
$statusMsg .= " (";
my $first_host = 1;
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} );
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 );
@ -230,6 +275,7 @@ if ( $statusCode eq 'OK' ) {
$first_host = 0 if ( $first_host );
}
}
}
$statusMsg .= ")";
} else {
$statusMsg .= " ( $ok_count OK, $unknown_count UNKNOWN, $warning_count WARNING, $critical_count CRITICAL)";
@ -238,3 +284,4 @@ if ( $statusCode eq 'OK' ) {
print "$statusMsg\n";
exit $ERRORS{$statusCode};
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:

View file

@ -175,4 +175,4 @@ print $per_host_detail;
exit $ERRORS{$statusCode};
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab: