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