Update BackupPC's discovery and monitoring scripts

This commit is contained in:
Daniel Berteaud 2019-04-04 15:32:19 +02:00
parent d64fc82d3e
commit 3e464cb4f7
3 changed files with 136 additions and 87 deletions

View File

@ -1,15 +1,17 @@
# Discovery of configured host # Discovery of configured host
# Key: backuppc.host.discovery # Key: backuppc.host.discovery
# Macro: {#BPCSTATUS} # Macro: {#BPCSTATUS}
# Filter regex: enabled => true # Filter regex: enabled|1 => true
# Other available macros: # Other available macros:
# {#BPCPERIOD}: Max age (in day) the oldest backup should be # {#BPCPERIOD}: Max age (in day) the oldest backup should be
# {#BPCHOST}: name of the backup host # {#BPCHOST}: name of the backup host
UserParameter=backuppc.host.discovery[*],/usr/bin/sudo /var/lib/zabbix/bin/disco_backuppc_sudo UserParameter=backuppc.host.discovery[*],/usr/bin/sudo /var/lib/zabbix/bin/disco_backuppc_sudo --hosts
UserParameter=backuppc.entity.discovery[*],/usr/bin/sudo /var/lib/zabbix/bin/disco_backuppc_sudo --entities
# Item prototypes # Item prototypes
# key: backuppc.host.info[{#BPCHOST},item] # key: backuppc.host[{#BPCHOST}]
# Valide item are: errors, max_errors, size, duration, age, notify # or
UserParameter=backuppc.host.info[*],/usr/bin/sudo /var/lib/zabbix/bin/check_backuppc_sudo $1 $2 # key: backuppc.entity[{#BPC_ENTITY}]
# Same but used for transition to the dependent items # Returns a JSON object, use dependent item to split it
UserParameter=backuppc.host[*],/usr/bin/sudo /var/lib/zabbix/bin/check_backuppc_sudo $1 $2 UserParameter=backuppc.host[*],/usr/bin/sudo /var/lib/zabbix/bin/check_backuppc_sudo --host=$1
UserParameter=backuppc.entity[*],/usr/bin/sudo /var/lib/zabbix/bin/check_backuppc_sudo --entity=$1

View File

@ -5,6 +5,18 @@ use BackupPC::Lib;
use BackupPC::CGI::Lib; use BackupPC::CGI::Lib;
use POSIX; use POSIX;
use JSON; use JSON;
use Getopt::Long;
use Data::Dumper;
my $host = undef;
my $entity = undef;
my $pretty = 0;
GetOptions(
"host=s" => \$host,
"entity=s" => \$entity,
"pretty" => \$pretty
);
# We need to switch to backuppc UID/GID # We need to switch to backuppc UID/GID
my $uid = getuid(); my $uid = getuid();
@ -13,84 +25,100 @@ my (undef,undef,$bkpuid,$bkpgid) = getpwnam('backuppc');
setuid($bkpuid) if ($uid ne $bkpuid); setuid($bkpuid) if ($uid ne $bkpuid);
setgid($bkpgid) if ($gid ne $bkpgid); setgid($bkpgid) if ($gid ne $bkpgid);
my $host = $ARGV[0];
my $what = $ARGV[1];
my $bpc = BackupPC::Lib->new(); my $bpc = BackupPC::Lib->new();
my @backups = $bpc->BackupInfoRead($host);
my $mainConf = $bpc->ConfigDataRead(); my $mainConf = $bpc->ConfigDataRead();
my $hostConf = $bpc->ConfigDataRead($host); my $json = {};
my $conf = { %$mainConf, %$hostConf };
my $fullCnt = $incrCnt = 0;
my $fullAge = $incrAge = $lastAge = -1;
my $lastXferErrors = 0;
my $maxErrors = 0;
if ( @backups ) { if ( $host ) {
for ( my $i = 0 ; $i < @backups ; $i++ ) { my $hostConf = $bpc->ConfigDataRead($host);
if ( $backups[$i]{type} eq "full" ) { my $conf = { %$mainConf, %$hostConf };
$fullCnt++; my $age = -1;
if ( $fullAge < 0 || $backups[$i]{startTime} > $fullAge ) {
$fullAge = $backups[$i]{startTime}; # Backup frequency
$fullSize = $backups[$i]{size}; my $freq = ($conf->{FullPeriod} > $conf->{IncrPeriod}) ? $conf->{IncrPeriod} : $conf->{FullPeriod};
$fullDur = $backups[$i]{endTime} - $backups[$i]{startTime};
} my $lastXferErrors = 0;
} else { my $maxErrors = 0;
$incrCnt++; my $new_size_of_last_full = 0;
if ( $incrAge < 0 || $backups[$i]{startTime} > $incrAge ) {
$incrAge = $backups[$i]{startTime}; foreach my $backup ( $bpc->BackupInfoRead($host) ) {
} # Skip partial or active backups
next if ( $backup->{type} !~ m/^full|incr$/ );
if ( $backup->{type} eq "full" ) {
$json->{full_size} = $backup->{size};
$new_size_of_last_full = $backup->{sizeNew};
} }
$json->{last_errors} = $backup->{xferErrs};
$json->{new_size} = $backup->{sizeNew};
$json->{total_size} += $backup->{sizeNew};
$json->{duration} = $backup->{endTime} - $backup->{startTime};
$json->{type} = $backup->{type};
$json->{ratio} = ( $backup->{sizeNew} > 0 ) ? sprintf( "%.2f", 100 - ( $backup->{sizeNewComp} * 100 / $backup->{sizeNew} ) ) : 0;
$age = $backup->{startTime};
} }
if ( $fullAge > $incrAge && $fullAge >= 0 ) { $json->{enabled} = ( $conf->{BackupsDisable} > 0 ) ? 0 : 1;
$lastAge = $fullAge; $json->{total_size} += $json->{full_size} - 2 * $new_size_of_last_full;
} else { $json->{age} = time - $age;
$lastAge = $incrAge;
}
if ( $lastAge < 0 ) {
$lastAge = "";
} else {
$lastAge = sprintf("%.1f", (time - $lastAge) / (24 * 3600));
}
$lastXferErrors = $backups[@backups-1]{xferErrs};
$lastSizeNew = $backups[@backups-1]{sizeNew};
$lastDuration = $backups[@backups-1]{endTime} - $backups[@backups-1]{startTime};
$lastType = $backups[@backups-1]{type};
}
$maxErrors = $conf->{MaxXferError} if (defined $conf->{MaxXferError}); $json->{max_errors} = $conf->{MaxXferError} if (defined $conf->{MaxXferError});
} elsif ( $entity ) {
if ($what eq 'errors') { $json = {
print $lastXferErrors; perf => 0,
} elsif ($what eq 'max_errors') { size => 0,
print $maxErrors; hosts => 0,
} elsif ($what eq 'age') { bkp => 0,
print $lastAge; ratio => 0
} elsif ($what eq 'size') { };
print $fullSize;
} elsif ($what eq 'duration') { my $total_new = 0;
print $lastDuration; my $total_comp = 0;
} elsif ($what eq 'notify') {
print $conf->{EMailNotifyOldBackupDays}; foreach my $host ( keys %{ $bpc->HostInfoRead } ) {
} elsif ($what eq 'all') { next unless $host =~ m/^(vm_)?\Q$entity\E_.*/;
print to_json( my $full_size;
{
max_errors => $maxErrors, $json->{hosts}++;
last_age => $lastAge,
full_size => $fullSize, my $hostConf = $bpc->ConfigDataRead($host);
new_size => $lastSizeNew, my $conf = { %$mainConf, %$hostConf };
full_duration => $fullDur, my $freq = ($conf->{FullPeriod} > $conf->{IncrPeriod}) ? $conf->{IncrPeriod} : $conf->{FullPeriod};
duration => $lastDuration, my $duration = 0;
last_type => $lastType, my $bkp_num = 0;
last_errors => $lastXferErrors
foreach my $backup ( $bpc->BackupInfoRead( $host ) ) {
next if ( $backup->{type} !~ m/^full|incr$/ );
# For the last full backup of this host, we do not count
# the new file size, but the total size
# We substract 2 times the new file size because we want the total size
if ( $backup->{type} eq 'full' ) {
$full_size = $backup->{size} - 2 * $backup->{sizeNew};
}
$size += $backup->{sizeNew};
$total_new += $backup->{sizeNew};
$total_comp += $backup->{sizeNewComp};
$duration += $backup->{endTime} - $backup->{startTime};
$bkp_num++;
$json->{bkp}++;
} }
); # Compute the average cost as the number of hours per day spent
# to backup this host
$json->{perf} += ( $bkp_num > 0 ) ? $duration / ( 3600 * $bkp_num * $freq ) : 0;
$json->{size} += $size + $full_size;
}
$json->{ratio} = ( $total_new > 0 ) ? 100 - ( $total_comp * 100 / $total_new ) : 0;
# Round some values
foreach my $key ( qw(ratio perf) ) {
$json->{$key} = sprintf( "%.2f", $json->{$key} );
}
} else { } else {
print<<"EOF"; print<<"EOF";
Usage: $0 <host> [errors|age|size|duration|all] Usage: $0 --host=<host> or --entity=<entity>
EOF EOF
} }
print to_json( $json, { pretty => $pretty } );
exit(0); exit(0);

View File

@ -5,6 +5,15 @@ use BackupPC::Lib;
use BackupPC::CGI::Lib; use BackupPC::CGI::Lib;
use POSIX; use POSIX;
use JSON; use JSON;
use Getopt::Long;
my $hosts = 1;
my $entities = 0;
GetOptions(
"hosts" => \$hosts,
"entities" => \$entities
);
# We need to switch to backuppc UID/GID # We need to switch to backuppc UID/GID
my $uid = getuid(); my $uid = getuid();
@ -20,20 +29,30 @@ my $mainConf = $bpc->ConfigDataRead();
my $json; my $json;
@{$json->{data}} = (); @{$json->{data}} = ();
foreach my $host (keys %$hosts){ if ($entities) {
my $hostConf = $bpc->ConfigDataRead($host); my %entities = ();
my $conf = { %$mainConf, %$hostConf }; foreach my $host (keys %$hosts){
my $warning = $conf->{EMailNotifyOldBackupDays}; if ($host =~ m/^(?:vm_)?([^_]+)_.*/) {
my $errors = (defined $conf->{MaxXferError}) ? $conf->{MaxXferError}: '0'; $entities{$1}= 1;
my $monitoring = $conf->{ZabbixMonitoring} || 1; }
my $status = ($conf->{BackupsDisable} eq '1' or $monitoring eq '0') ? '0' : '1'; }
push @{$json->{data}}, push @{$json->{data}}, { '{#BPC_ENTITY}' => $_ } foreach ( keys %entities );
{ } elsif ($hosts){
"{#BPCHOST}" => $host, foreach my $host (keys %$hosts){
"{#BPCNOBACKUPWARNING}" => $warning, my $hostConf = $bpc->ConfigDataRead($host);
"{#BPCMAXERROR}" => $errors, my $conf = { %$mainConf, %$hostConf };
"{#BPCSTATUS}" => $status, my $warning = $conf->{EMailNotifyOldBackupDays};
}; my $errors = (defined $conf->{MaxXferError}) ? $conf->{MaxXferError}: '0';
my $monitoring = $conf->{ZabbixMonitoring} || 1;
my $status = ($conf->{BackupsDisable} gt 0 or $monitoring eq '0') ? '0' : '1';
push @{$json->{data}},
{
"{#BPCHOST}" => $host,
"{#BPCNOBACKUPWARNING}" => $warning,
"{#BPCMAXERROR}" => $errors,
"{#BPCSTATUS}" => $status,
};
}
} }
print to_json($json); print to_json($json);
exit(0); exit(0);