Update BackupPC's discovery and monitoring scripts

tags/zabbix-agent-addons-0.2.68-1
Daniel Berteaud 6 years ago
parent d64fc82d3e
commit 3e464cb4f7
  1. 16
      zabbix_conf/backuppc.conf
  2. 148
      zabbix_scripts/check_backuppc_sudo
  3. 21
      zabbix_scripts/disco_backuppc_sudo

@ -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

@ -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 $json = {};
if ( $host ) {
my $hostConf = $bpc->ConfigDataRead($host); my $hostConf = $bpc->ConfigDataRead($host);
my $conf = { %$mainConf, %$hostConf }; my $conf = { %$mainConf, %$hostConf };
my $fullCnt = $incrCnt = 0; my $age = -1;
my $fullAge = $incrAge = $lastAge = -1;
# Backup frequency
my $freq = ($conf->{FullPeriod} > $conf->{IncrPeriod}) ? $conf->{IncrPeriod} : $conf->{FullPeriod};
my $lastXferErrors = 0; my $lastXferErrors = 0;
my $maxErrors = 0; my $maxErrors = 0;
my $new_size_of_last_full = 0;
if ( @backups ) { foreach my $backup ( $bpc->BackupInfoRead($host) ) {
for ( my $i = 0 ; $i < @backups ; $i++ ) { # Skip partial or active backups
if ( $backups[$i]{type} eq "full" ) { next if ( $backup->{type} !~ m/^full|incr$/ );
$fullCnt++; if ( $backup->{type} eq "full" ) {
if ( $fullAge < 0 || $backups[$i]{startTime} > $fullAge ) { $json->{full_size} = $backup->{size};
$fullAge = $backups[$i]{startTime}; $new_size_of_last_full = $backup->{sizeNew};
$fullSize = $backups[$i]{size};
$fullDur = $backups[$i]{endTime} - $backups[$i]{startTime};
}
} else {
$incrCnt++;
if ( $incrAge < 0 || $backups[$i]{startTime} > $incrAge ) {
$incrAge = $backups[$i]{startTime};
}
} }
$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;
$json->{max_errors} = $conf->{MaxXferError} if (defined $conf->{MaxXferError});
} elsif ( $entity ) {
$json = {
perf => 0,
size => 0,
hosts => 0,
bkp => 0,
ratio => 0
};
my $total_new = 0;
my $total_comp = 0;
foreach my $host ( keys %{ $bpc->HostInfoRead } ) {
next unless $host =~ m/^(vm_)?\Q$entity\E_.*/;
my $full_size;
$json->{hosts}++;
my $hostConf = $bpc->ConfigDataRead($host);
my $conf = { %$mainConf, %$hostConf };
my $freq = ($conf->{FullPeriod} > $conf->{IncrPeriod}) ? $conf->{IncrPeriod} : $conf->{FullPeriod};
my $duration = 0;
my $bkp_num = 0;
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};
} }
if ( $lastAge < 0 ) { $size += $backup->{sizeNew};
$lastAge = ""; $total_new += $backup->{sizeNew};
} else { $total_comp += $backup->{sizeNewComp};
$lastAge = sprintf("%.1f", (time - $lastAge) / (24 * 3600)); $duration += $backup->{endTime} - $backup->{startTime};
$bkp_num++;
$json->{bkp}++;
} }
$lastXferErrors = $backups[@backups-1]{xferErrs}; # Compute the average cost as the number of hours per day spent
$lastSizeNew = $backups[@backups-1]{sizeNew}; # to backup this host
$lastDuration = $backups[@backups-1]{endTime} - $backups[@backups-1]{startTime}; $json->{perf} += ( $bkp_num > 0 ) ? $duration / ( 3600 * $bkp_num * $freq ) : 0;
$lastType = $backups[@backups-1]{type}; $json->{size} += $size + $full_size;
} }
$json->{ratio} = ( $total_new > 0 ) ? 100 - ( $total_comp * 100 / $total_new ) : 0;
$maxErrors = $conf->{MaxXferError} if (defined $conf->{MaxXferError}); # Round some values
foreach my $key ( qw(ratio perf) ) {
if ($what eq 'errors') { $json->{$key} = sprintf( "%.2f", $json->{$key} );
print $lastXferErrors;
} elsif ($what eq 'max_errors') {
print $maxErrors;
} elsif ($what eq 'age') {
print $lastAge;
} elsif ($what eq 'size') {
print $fullSize;
} elsif ($what eq 'duration') {
print $lastDuration;
} elsif ($what eq 'notify') {
print $conf->{EMailNotifyOldBackupDays};
} elsif ($what eq 'all') {
print to_json(
{
max_errors => $maxErrors,
last_age => $lastAge,
full_size => $fullSize,
new_size => $lastSizeNew,
full_duration => $fullDur,
duration => $lastDuration,
last_type => $lastType,
last_errors => $lastXferErrors
} }
);
} 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);

@ -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,13 +29,22 @@ my $mainConf = $bpc->ConfigDataRead();
my $json; my $json;
@{$json->{data}} = (); @{$json->{data}} = ();
if ($entities) {
my %entities = ();
foreach my $host (keys %$hosts){
if ($host =~ m/^(?:vm_)?([^_]+)_.*/) {
$entities{$1}= 1;
}
}
push @{$json->{data}}, { '{#BPC_ENTITY}' => $_ } foreach ( keys %entities );
} elsif ($hosts){
foreach my $host (keys %$hosts){ foreach my $host (keys %$hosts){
my $hostConf = $bpc->ConfigDataRead($host); my $hostConf = $bpc->ConfigDataRead($host);
my $conf = { %$mainConf, %$hostConf }; my $conf = { %$mainConf, %$hostConf };
my $warning = $conf->{EMailNotifyOldBackupDays}; my $warning = $conf->{EMailNotifyOldBackupDays};
my $errors = (defined $conf->{MaxXferError}) ? $conf->{MaxXferError}: '0'; my $errors = (defined $conf->{MaxXferError}) ? $conf->{MaxXferError}: '0';
my $monitoring = $conf->{ZabbixMonitoring} || 1; my $monitoring = $conf->{ZabbixMonitoring} || 1;
my $status = ($conf->{BackupsDisable} eq '1' or $monitoring eq '0') ? '0' : '1'; my $status = ($conf->{BackupsDisable} gt 0 or $monitoring eq '0') ? '0' : '1';
push @{$json->{data}}, push @{$json->{data}},
{ {
"{#BPCHOST}" => $host, "{#BPCHOST}" => $host,
@ -35,5 +53,6 @@ foreach my $host (keys %$hosts){
"{#BPCSTATUS}" => $status, "{#BPCSTATUS}" => $status,
}; };
} }
}
print to_json($json); print to_json($json);
exit(0); exit(0);

Loading…
Cancel
Save