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