Some improvements

This commit is contained in:
Benjamin Renard 2015-11-12 15:16:19 +01:00
parent 6ed85bafad
commit 9e507bc48b

View file

@ -54,6 +54,13 @@ my $goodOpt = 0;
my @ownerOnly;
my @hostsDesired;
my @hostsExcluded;
my %Status;
my $statusCode = 'OK';
my $ok_count = 0;
my $unknown_count = 0;
my $warning_count = 0;
my $critical_count = 0;
# Process options
$goodOpt = GetOptions(
@ -114,8 +121,6 @@ if ($err)
exit $ERRORS{'UNKNOWN'};
}
my %Status;
# query the BackupPC server for host status
my $status_raw = $server->ServerMesg('status hosts');
my $hosts_infos = $server->HostInfoRead();
@ -125,19 +130,16 @@ eval $status_raw;
# check the dumped output
my $hostCount = 0;
my $errorLevel='OK';
foreach my $host (@hostsDesired, @hostsExcluded)
{
if (not grep {/$host/} keys(%Status))
if (not grep {/^$host$/} keys(%Status))
{
print("BACKUPPC UNKNOWN - Unknown host ($host)\n");
exit $ERRORS{'UNKNOWN'};
}
}
my @problems;
# host status checks
foreach my $host (sort(keys(%Status)))
{
@ -145,10 +147,12 @@ foreach my $host (sort(keys(%Status)))
my $owner = $hosts_infos->{$host}->{user};
next if (@ownerOnly and not grep {/$owner/} @ownerOnly);
my %host_conf = %{$server->ConfigDataRead($host)};
next if ( $host_conf{BackupsDisable} );
next if (@hostsDesired and not grep {/$host/} @hostsDesired);
next if (@hostsExcluded and grep {/$host/} @hostsExcluded);
$Status{$host}{BackupsDisable} = $host_conf{BackupsDisable};
next if ( $Status{$host}{BackupsDisable} );
next if (@hostsDesired and not grep {/^$host$/} @hostsDesired);
next if (@hostsExcluded and grep {/^$host$/} @hostsExcluded);
next if ($Status{$host}{'type'} eq 'archive');
$Status{$host}{'statusCode'} = 'OK';
$hostCount++;
# Debug
if ($verbose == 2)
@ -161,50 +165,73 @@ foreach my $host (sort(keys(%Status)))
# Check host error
if ($Status{$host}{'error'})
{
# Check connectivity errors with greater care
if ($Status{$host}{'error'} ne 'ping too slow' &&
$Status{$host}{'error'} ne 'no ping response' &&
$Status{$host}{'error'} ne 'no ping response' &&
$Status{$host}{'error'} ne 'host not found' &&
$Status{$host}{'reason'} !~ /Reason_restore_failed/) {
push @problems, "$host error : ".$Status{$host}{'error'}." / ".$Status{$host}{'reason'};
next;
}
$Status{$host}{statusMsg} = "error: ".$Status{$host}{'error'}." / ".$Status{$host}{'reason'};
} else {
$Status{$host}{statusMsg} = "status: ".$Status{$host}{'state'};
}
# Check last good backup time
my $difftime=difftime(time(), $Status{$host}{'lastGoodBackupTime'});
my $diffdays=$difftime/(3600 * 24);
$Status{$host}{'lastGoodBackupTime'} = $Status{$host}{'startTime'} if (not $Status{$host}{'lastGoodBackupTime'});
if ($difftime > ($critDaysOld * 3600 * 24))
{
push @problems, "$host : last good backup have ".sprintf("%.1f",$diffdays)." days";
$errorLevel='CRITICAL';
}
elsif ($difftime > ($warnDaysOld * 3600 * 24))
{
push @problems, "$host : last good backup have ".sprintf("%.1f",$diffdays)." days";
$errorLevel='WARNING' if ($errorLevel eq 'OK');
}
}
my $problemTxt="";
if (scalar(@problems) > 0) {
if ($verbose > 0) {
foreach my $pbl (@problems) {
if ($problemTxt ne "") {
$problemTxt.=" , ";
}
else {
$problemTxt=" ( ";
}
$problemTxt.=$pbl;
$Status{$host}{'lastGoodBackupDays'} = difftime(time(), $Status{$host}{'lastGoodBackupTime'}) / (3600 * 24) if ( defined $Status{$host}{'lastGoodBackupTime'} );
if ( ! $Status{$host}{'lastGoodBackupDays'} ) {
$Status{$host}{'startDays'} = difftime(time(), $Status{$host}{'startTime'}) / (3600 * 24);
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' );
$statusCode = 'WARNING' unless ( $statusCode eq 'CRITICAL' );
}
$problemTxt.=" )";
} elsif ( $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 )
{
$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' );
} else {
$Status{$host}{statusMsg} .= ", last good backup have ".sprintf("%.1f",$Status{$host}{'lastGoodBackupDays'})." days";
}
else {
$problemTxt=" (".scalar(@problems)." problems)";
$ok_count++ if ( $Status{$host}{statusCode} eq 'OK' );
$unknown_count++ if ( $Status{$host}{statusCode} eq 'UNKNOWN' );
$warning_count++ if ( $Status{$host}{statusCode} eq 'WARNING' );
$critical_count++ if ( $Status{$host}{statusCode} eq 'CRITICAL' );
}
my $statusMsg = "BACKUPPC $statusCode";
if ( $statusCode eq 'OK' ) {
if ( $verbose && scalar(@hostsDesired) == 1 ) {
$statusMsg .= " (".$Status{$hostsDesired[0]}{statusMsg}.")";
} else {
$statusMsg .= " ($ok_count OK)";
}
} else {
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} );
next if ($Status{$host}{'type'} eq 'archive');
if ( $Status{$host}{statusCode} ne 'OK' ) {
$statusMsg .= ", " unless ( $first_host );
$statusMsg .= "$host: ".$Status{$host}{statusCode}." - ".$Status{$host}{statusMsg};
$first_host = 0 if ( $first_host );
}
}
$statusMsg .= ")";
} else {
$statusMsg .= " ( $ok_count OK, $unknown_count UNKNOWN, $warning_count WARNING, $critical_count CRITICAL)";
}
}
print "BACKUPPC $errorLevel$problemTxt\n";
exit $ERRORS{$errorLevel};
print "$statusMsg\n";
exit $ERRORS{$statusCode};