parent
d4dbe7f512
commit
dca3cd398e
3 changed files with 0 additions and 650 deletions
@ -1,341 +0,0 @@ |
||||
#============================================================= -*-perl-*- |
||||
# |
||||
# BackupPC::CGI::Browse package |
||||
# |
||||
# DESCRIPTION |
||||
# |
||||
# This module implements the Browse action for the CGI interface. |
||||
# |
||||
# AUTHOR |
||||
# Craig Barratt <cbarratt@users.sourceforge.net> |
||||
# |
||||
# COPYRIGHT |
||||
# Copyright (C) 2003-2019 Craig Barratt |
||||
# |
||||
# This program is free software: you can redistribute it and/or modify |
||||
# it under the terms of the GNU General Public License as published by |
||||
# the Free Software Foundation, either version 3 of the License, or |
||||
# (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
# |
||||
#======================================================================== |
||||
# |
||||
# Version 4.3.1, released 14 Jul 2019. |
||||
# |
||||
# See http://backuppc.sourceforge.net. |
||||
# |
||||
#======================================================================== |
||||
|
||||
package BackupPC::CGI::Browse; |
||||
|
||||
use strict; |
||||
use Encode qw/decode_utf8/; |
||||
use BackupPC::CGI::Lib qw(:all); |
||||
use BackupPC::View; |
||||
use BackupPC::XS qw(:all); |
||||
|
||||
sub action |
||||
{ |
||||
my $Privileged = CheckPermission($In{host}); |
||||
my($i, $dirStr, $fileStr, $attr); |
||||
my $checkBoxCnt = 0; |
||||
|
||||
if ( !$Privileged ) { |
||||
ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_browse_backup_files}}")); |
||||
} |
||||
my $host = $In{host}; |
||||
my $num = $In{num}; |
||||
my $share = $In{share}; |
||||
my $dir = $In{dir}; |
||||
|
||||
ErrorExit($Lang->{Empty_host_name}) if ( $host eq "" ); |
||||
# |
||||
# Find the requested backup and the previous filled backup |
||||
# |
||||
my @Backups = $bpc->BackupInfoRead($host); |
||||
|
||||
# |
||||
# default to the newest backup |
||||
# |
||||
if ( !defined($In{num}) && @Backups > 0 ) { |
||||
$i = @Backups - 1; |
||||
$num = $Backups[$i]{num}; |
||||
} |
||||
|
||||
for ( $i = 0 ; $i < @Backups ; $i++ ) { |
||||
last if ( $Backups[$i]{num} == $num ); |
||||
} |
||||
if ( $i >= @Backups || $num !~ /^\d+$/ ) { |
||||
ErrorExit("Backup number ${EscHTML($num)} for host ${EscHTML($host)} does" |
||||
. " not exist."); |
||||
} |
||||
my $backupTime = timeStamp2($Backups[$i]{startTime}); |
||||
my $backupAge = sprintf("%.1f", (time - $Backups[$i]{startTime}) |
||||
/ (24 * 3600)); |
||||
my $view = BackupPC::View->new($bpc, $host, \@Backups, {nlink => 1}); |
||||
|
||||
if ( $dir eq "" || $dir eq "." || $dir eq ".." ) { |
||||
$attr = $view->dirAttrib($num, "", ""); |
||||
if ( keys(%$attr) > 0 ) { |
||||
$share = (sort(keys(%$attr)))[-1]; |
||||
$dir = '/'; |
||||
} else { |
||||
ErrorExit(eval("qq{$Lang->{Directory___EscHTML}}")); |
||||
} |
||||
} |
||||
$dir = "/$dir" if ( $dir !~ /^\// ); |
||||
my $relDir = $dir; |
||||
my $currDir = undef; |
||||
if ( $dir =~ m{(^|/)\.\.(/|$)} ) { |
||||
ErrorExit($Lang->{Nice_try__but_you_can_t_put}); |
||||
} |
||||
|
||||
# |
||||
# Loop up the directory tree until we hit the top. |
||||
# |
||||
my(@DirStrPrev); |
||||
while ( 1 ) { |
||||
my($fLast, $fLastum, @DirStr); |
||||
|
||||
$attr = $view->dirAttrib($num, $share, $relDir); |
||||
if ( !defined($attr) ) { |
||||
$relDir = decode_utf8($relDir); |
||||
ErrorExit(eval("qq{$Lang->{Can_t_browse_bad_directory_name2}}")); |
||||
} |
||||
|
||||
my $fileCnt = 0; # file counter |
||||
$fLast = $dirStr = ""; |
||||
|
||||
# |
||||
# Loop over each of the files in this directory |
||||
# |
||||
foreach my $f ( sort {uc($a) cmp uc($b)} keys(%$attr) ) { |
||||
my($dirOpen, $gotDir, $imgStr, $img, $path); |
||||
my $fURI = $f; # URI escaped $f |
||||
my $shareURI = $share; # URI escaped $share |
||||
if ( $relDir eq "" ) { |
||||
$path = "/$f"; |
||||
} else { |
||||
($path = "$relDir/$f") =~ s{//+}{/}g; |
||||
} |
||||
if ( $shareURI eq "" ) { |
||||
$shareURI = $f; |
||||
$path = "/"; |
||||
} |
||||
$path =~ s{^/+}{/}; |
||||
$path =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg; |
||||
$fURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg; |
||||
$shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02X", ord($1))/eg; |
||||
$dirOpen = 1 if ( defined($currDir) && $f eq $currDir ); |
||||
if ( $attr->{$f}{type} == BPC_FTYPE_DIR ) { |
||||
# |
||||
# Display directory if it exists in current backup. |
||||
# First find out if there are subdirs |
||||
# |
||||
my $subDirAttr = $share eq "" ? $view->dirAttrib($num, $f, "/") |
||||
: $view->dirAttrib($num, $share, "$relDir/$f"); |
||||
my $subDirCnt = 0; |
||||
my $tdStyle; |
||||
my $linkStyle = "fview"; |
||||
|
||||
foreach my $sub ( keys(%$subDirAttr) ) { |
||||
next if ( $subDirAttr->{$sub}{type} != BPC_FTYPE_DIR ); |
||||
$subDirCnt++; |
||||
} |
||||
$img |= 1 << 6; |
||||
$img |= 1 << 5 if ( $subDirCnt ); |
||||
if ( $dirOpen ) { |
||||
$linkStyle = "fviewbold"; |
||||
$img |= 1 << 2; |
||||
$img |= 1 << 3 if ( $subDirCnt ); |
||||
} |
||||
my $imgFileName = sprintf("%07b.gif", $img); |
||||
$imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">"; |
||||
if ( "$relDir/$f" eq $dir ) { |
||||
$tdStyle = "fviewon"; |
||||
} else { |
||||
$tdStyle = "fviewoff"; |
||||
} |
||||
my $dirName = $f; |
||||
$dirName =~ s/ / /g; |
||||
$dirName = decode_utf8($dirName); |
||||
push(@DirStr, {needTick => 1, |
||||
tdArgs => " class=\"$tdStyle\"", |
||||
link => <<EOF}); |
||||
<a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$imgStr</a><a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path" class="$linkStyle"> $dirName</a></td></tr> |
||||
EOF |
||||
$fileCnt++; |
||||
$gotDir = 1; |
||||
if ( $dirOpen ) { |
||||
my($lastTick, $doneLastTick); |
||||
foreach my $d ( @DirStrPrev ) { |
||||
$lastTick = $d if ( $d->{needTick} ); |
||||
} |
||||
$doneLastTick = 1 if ( !defined($lastTick) ); |
||||
foreach my $d ( @DirStrPrev ) { |
||||
$img = 0; |
||||
if ( $d->{needTick} ) { |
||||
$img |= 1 << 0; |
||||
} |
||||
if ( $d == $lastTick ) { |
||||
$img |= 1 << 4; |
||||
$doneLastTick = 1; |
||||
} elsif ( !$doneLastTick ) { |
||||
$img |= 1 << 3 | 1 << 4; |
||||
} |
||||
my $imgFileName = sprintf("%07b.gif", $img); |
||||
$imgStr = "<img src=\"$Conf{CgiImageDirURL}/$imgFileName\" align=\"absmiddle\" width=\"9\" height=\"19\" border=\"0\">"; |
||||
push(@DirStr, {needTick => 0, |
||||
tdArgs => $d->{tdArgs}, |
||||
link => $imgStr . $d->{link} |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
if ( $relDir eq $dir ) { |
||||
# |
||||
# This is the selected directory, so display all the files |
||||
# |
||||
my ($attrStr, $iconStr); |
||||
if ( defined($a = $attr->{$f}) ) { |
||||
my $mtimeStr = $bpc->timeStamp($a->{mtime}); |
||||
# UGH -> fix this |
||||
my $typeStr = BackupPC::XS::Attrib::fileType2Text($a->{type}); |
||||
my $modeStr = sprintf("0%o", $a->{mode} & 07777); |
||||
$iconStr = <<EOF; |
||||
<img src="$Conf{CgiImageDirURL}/icon-$typeStr.png" valign="top"> |
||||
EOF |
||||
$attrStr .= <<EOF; |
||||
<td align="center" class="fviewborder">$typeStr</td> |
||||
<td align="center" class="fviewborder">$modeStr</td> |
||||
<td align="center" class="fviewborder">$a->{backupNum}</td> |
||||
<td align="right" class="fviewborder">$a->{size}</td> |
||||
<td align="right" class="fviewborder">$mtimeStr</td> |
||||
</tr> |
||||
EOF |
||||
} else { |
||||
$attrStr .= "<td colspan=\"5\" align=\"center\" class=\"fviewborder\"> </td>\n"; |
||||
} |
||||
(my $fDisp = "${EscHTML($f)}") =~ s/ / /g; |
||||
$fDisp = decode_utf8($fDisp); |
||||
if ( $gotDir ) { |
||||
$fileStr .= <<EOF; |
||||
<tr><td class="fviewborder"> |
||||
<input type="checkbox" name="fcb$checkBoxCnt" value="$path"> $iconStr <a href="$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a> |
||||
</td> |
||||
$attrStr |
||||
</tr> |
||||
EOF |
||||
} else { |
||||
$fileStr .= <<EOF; |
||||
<tr><td class="fviewborder"> |
||||
<input type="checkbox" name="fcb$checkBoxCnt" value="$path"> $iconStr <a href="$MyURL?action=RestoreFile&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$path">$fDisp</a> |
||||
</td> |
||||
$attrStr |
||||
</tr> |
||||
EOF |
||||
} |
||||
$checkBoxCnt++; |
||||
} |
||||
} |
||||
@DirStrPrev = @DirStr; |
||||
last if ( $relDir eq "" && $share eq "" ); |
||||
# |
||||
# Prune the last directory off $relDir, or at the very end |
||||
# do the top-level directory. |
||||
# |
||||
if ( $relDir eq "" || $relDir eq "/" || $relDir !~ /(.*)\/(.*)/ ) { |
||||
$currDir = $share; |
||||
$share = ""; |
||||
$relDir = ""; |
||||
} else { |
||||
$relDir = $1; |
||||
$currDir = $2; |
||||
} |
||||
} |
||||
$share = $currDir; |
||||
my $shareURI = $share; |
||||
$shareURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg; |
||||
|
||||
# |
||||
# allow each level of the directory path to be navigated to |
||||
# |
||||
my($thisPath, $dirDisplay); |
||||
my $dirClean = $dir; |
||||
$dirClean =~ s{//+}{/}g; |
||||
$dirClean =~ s{/+$}{}; |
||||
my @dirElts = split(/\//, $dirClean); |
||||
@dirElts = ("/") if ( !@dirElts ); |
||||
foreach my $d ( @dirElts ) { |
||||
my($thisDir); |
||||
|
||||
if ( $thisPath eq "" ) { |
||||
$thisDir = decode_utf8($share); |
||||
$thisPath = "/"; |
||||
} else { |
||||
$thisPath .= "/" if ( $thisPath ne "/" ); |
||||
$thisPath .= "$d"; |
||||
$thisDir = decode_utf8($d); |
||||
} |
||||
my $thisPathURI = $thisPath; |
||||
$thisPathURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg; |
||||
$dirDisplay .= "/" if ( $dirDisplay ne "" ); |
||||
$dirDisplay .= "<a href=\"$MyURL?action=browse&host=${EscURI($host)}&num=$num&share=$shareURI&dir=$thisPathURI\">${EscHTML($thisDir)}</a>"; |
||||
} |
||||
|
||||
my $filledBackup; |
||||
|
||||
if ( (my @mergeNums = @{$view->mergeNums}) > 1 ) { |
||||
shift(@mergeNums); |
||||
my $numF = join(", #", @mergeNums); |
||||
$filledBackup = eval("qq{$Lang->{This_display_is_merged_with_backup}}"); |
||||
} |
||||
|
||||
foreach my $d ( @DirStrPrev ) { |
||||
$dirStr .= "<tr><td$d->{tdArgs}>$d->{link}\n"; |
||||
} |
||||
|
||||
### hide checkall button if there are no files |
||||
my ($topCheckAll, $checkAll, $fileHeader); |
||||
if ( $fileStr ) { |
||||
$fileHeader = eval("qq{$Lang->{fileHeader}}"); |
||||
|
||||
$checkAll = $Lang->{checkAll}; |
||||
|
||||
# and put a checkall box on top if there are at least 20 files |
||||
if ( $checkBoxCnt >= 20 ) { |
||||
$topCheckAll = $checkAll; |
||||
$topCheckAll =~ s{allFiles}{allFilestop}g; |
||||
} |
||||
} else { |
||||
$fileStr = eval("qq{$Lang->{The_directory_is_empty}}"); |
||||
} |
||||
my $pathURI = $dir; |
||||
$pathURI =~ s/([^\w.\/-])/uc sprintf("%%%02x", ord($1))/eg; |
||||
if ( my @otherDirs = $view->backupList($share, $dir) ) { |
||||
my $otherDirs; |
||||
foreach my $i ( @otherDirs ) { |
||||
my $selected; |
||||
my $showDate = timeStamp2($Backups[$i]{startTime}); |
||||
my $backupNum = $Backups[$i]{num}; |
||||
$selected = " selected" if ( $backupNum == $num ); |
||||
$otherDirs .= "<option value=\"$MyURL?action=browse&host=${EscURI($host)}&num=$backupNum&share=$shareURI&dir=$pathURI\"$selected>#$backupNum - ($showDate)</option>\n"; |
||||
} |
||||
$filledBackup .= eval("qq{$Lang->{Visit_this_directory_in_backup}}"); |
||||
} |
||||
$dir = decode_utf8($dir); |
||||
$share = decode_utf8($share); |
||||
|
||||
my $content = eval("qq{$Lang->{Backup_browse_for__host}}"); |
||||
Header(eval("qq{$Lang->{Browse_backup__num_for__host}}"), $content); |
||||
Trailer(); |
||||
} |
||||
|
||||
1; |
@ -1,149 +0,0 @@ |
||||
#!/usr/bin/perl |
||||
|
||||
use lib "/usr/share/BackupPC/lib"; |
||||
use BackupPC::Lib; |
||||
use BackupPC::CGI::Lib qw(:all); |
||||
use BackupPC::View; |
||||
use BackupPC::XS qw(:all); |
||||
use Encode qw/decode_utf8/; |
||||
use POSIX; |
||||
use JSON; |
||||
use Getopt::Long; |
||||
use Statistics::Descriptive; |
||||
use Data::Dumper; |
||||
|
||||
my $host = undef; |
||||
|
||||
GetOptions( |
||||
"host=s" => \$host |
||||
); |
||||
|
||||
# We need to switch to backuppc UID/GID |
||||
my $uid = getuid(); |
||||
my $gid = getgid(); |
||||
my (undef,undef,$bkpuid,$bkpgid) = getpwnam('backuppc'); |
||||
setuid($bkpuid) if ($uid ne $bkpuid); |
||||
setgid($bkpgid) if ($gid ne $bkpgid); |
||||
|
||||
my $bpc = BackupPC::Lib->new(); |
||||
my $mainConf = $bpc->ConfigDataRead(); |
||||
my $json = {}; |
||||
|
||||
if ( $host ) { |
||||
my $hostConf = $bpc->ConfigDataRead($host); |
||||
my $conf = { %$mainConf, %$hostConf }; |
||||
my $age = -1; |
||||
$json = { |
||||
bkp => 0, |
||||
last_age => 0, |
||||
errors => 0, |
||||
new_size => 0, |
||||
new_size_avg => 0, |
||||
new_size_median => 0, |
||||
new_size_q1 => 0, |
||||
new_size_q3 => 0 |
||||
}; |
||||
|
||||
my @bpc_info = $bpc->BackupInfoRead($host); |
||||
my $sizes = new Statistics::Descriptive::Full; |
||||
|
||||
if ( scalar( @bpc_info ) ){ |
||||
foreach my $backup ( @bpc_info ) { |
||||
# Skip partial or active backups |
||||
next if ( $backup->{type} !~ m/^full|incr$/ ); |
||||
if ( $backup->{type} eq "full" ) { |
||||
$last_full_num = $backup->{num}; |
||||
} |
||||
# Push all the sizes in our data set to compute avg sizes |
||||
# Exclude backup N°0 as it'll always have much more new data than normal backups |
||||
$sizes->add_data($backup->{sizeNew}) unless ( $backup->{num} == 0 ); |
||||
$json->{bkp}++; |
||||
} |
||||
|
||||
# Ignore the last backup if it's not full or incr (which means it's either partial or active) |
||||
my $i = ( $bpc_info[-1]->{type} =~ m/^full|incr$/ ) ? -1 : -2; |
||||
|
||||
$json->{errors} = $bpc_info[$i]->{xferErrs}; |
||||
$json->{new_size} = $bpc_info[$i]->{sizeNew}; |
||||
$json->{new_size_avg} = int $sizes->mean; |
||||
$json->{new_size_median} = int $sizes->median; |
||||
$json->{new_size_q1} = eval { int $sizes->quantile(1) } || 0; |
||||
$json->{new_size_q3} = eval { int $sizes->quantile(3) } || 0; |
||||
$json->{age} = time - $bpc_info[$i]->{startTime}; |
||||
$json->{last_age} = sprintf("%.1f", ($json->{age}) / 84600); |
||||
} |
||||
} |
||||
|
||||
else { |
||||
print<<"EOF"; |
||||
|
||||
Usage: $0 --host=<host> |
||||
|
||||
EOF |
||||
} |
||||
|
||||
# Print results |
||||
print("\n----------------\n"); |
||||
print("Last Backup : $json->{last_age}"); |
||||
print("\n"); |
||||
print("Errors : $json->{errors}"); |
||||
print("\n"); |
||||
print("Size Consistency : "); |
||||
|
||||
# TOO BIG ? |
||||
my $toobig = "1"; |
||||
if ( $json->{new_size} > ($json->{new_size_q3} + $json->{new_size_q3} - $json->{new_size_q1}) * 1.5 or $json->{new_size} > $json->{new_size_avg} * 6 ) { |
||||
$toobig = "1"; |
||||
} |
||||
else { |
||||
$toobig = "0"; |
||||
} |
||||
|
||||
# TOO SMALL ? |
||||
my $toosmall = "1"; |
||||
if ( $json->{new_size} < ($json->{new_size_q1} - $json->{new_size_q3} - $json->{new_size_q1}) * 1.5 or $json->{new_size} < $json->{new_size_avg} / 3 ) { |
||||
$toosmall = "1"; |
||||
} |
||||
else { |
||||
$toosmall = "0"; |
||||
} |
||||
|
||||
# Print result |
||||
if ( $toobig or $toosmall ) { |
||||
print("ANOMALOUS"); |
||||
} |
||||
else { |
||||
print("Normal"); |
||||
} |
||||
print("\n"); |
||||
print("Random file : "); |
||||
|
||||
# Random file |
||||
|
||||
print("Random file"); |
||||
print("\n"); |
||||
|
||||
|
||||
print("\n----------------\n"); |
||||
|
||||
# Debug |
||||
print("\n----------------\n"); |
||||
print("DEBUG"); |
||||
print("\n"); |
||||
print("last_full_num : $last_full_num"); |
||||
print("\n"); |
||||
print("new_size : $json->{new_size}"); |
||||
print("\n"); |
||||
print("new_size_q1 : $json->{new_size_q1}"); |
||||
print("\n"); |
||||
print("new_size_q3 : $json->{new_size_q3}"); |
||||
print("\n"); |
||||
print("new_size_avg : $json->{new_size_avg}"); |
||||
print("\n"); |
||||
print("toobig : $toobig"); |
||||
print("\n"); |
||||
print("toosmall : $toosmall"); |
||||
print("\n----------------\n"); |
||||
# /Debug |
||||
|
||||
exit(0); |
@ -1,160 +0,0 @@ |
||||
#!/usr/bin/perl |
||||
|
||||
use lib "/usr/share/BackupPC/lib"; |
||||
use BackupPC::Lib; |
||||
use BackupPC::CGI::Lib; |
||||
use POSIX; |
||||
use JSON; |
||||
use Getopt::Long; |
||||
use Statistics::Descriptive; |
||||
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 |
||||
my $uid = getuid(); |
||||
my $gid = getgid(); |
||||
my (undef,undef,$bkpuid,$bkpgid) = getpwnam('backuppc'); |
||||
setuid($bkpuid) if ($uid ne $bkpuid); |
||||
setgid($bkpgid) if ($gid ne $bkpgid); |
||||
|
||||
my $bpc = BackupPC::Lib->new(); |
||||
my $mainConf = $bpc->ConfigDataRead(); |
||||
my $json = {}; |
||||
|
||||
if ( $host ) { |
||||
my $hostConf = $bpc->ConfigDataRead($host); |
||||
my $conf = { %$mainConf, %$hostConf }; |
||||
my $age = -1; |
||||
$json = { |
||||
bkp => 0, |
||||
full_size => 0, |
||||
errors => 0, |
||||
new_size => 0, |
||||
new_size_avg => 0, |
||||
new_size_median => 0, |
||||
new_size_q1 => 0, |
||||
new_size_q3 => 0, |
||||
duration => 0, |
||||
comp_ratio => 0 |
||||
}; |
||||
|
||||
my $lastXferErrors = 0; |
||||
my $maxErrors = 0; |
||||
my $new_size_of_last_full = 0; |
||||
my @bpc_info = $bpc->BackupInfoRead($host); |
||||
my $sizes = new Statistics::Descriptive::Full; |
||||
|
||||
if ( scalar( @bpc_info ) ){ |
||||
foreach my $backup ( @bpc_info ) { |
||||
# 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}; |
||||
} |
||||
# Push all the sizes in our data set to compute avg sizes |
||||
# Exclude backup N°0 as it'll always have much more new data than normal backups |
||||
$sizes->add_data($backup->{sizeNew}) unless ( $backup->{num} == 0 ); |
||||
$json->{bkp}++; |
||||
} |
||||
|
||||
# Ignore the last backup if it's not full or incr (which means it's either partial or active) |
||||
my $i = ( $bpc_info[-1]->{type} =~ m/^full|incr$/ ) ? -1 : -2; |
||||
|
||||
$json->{errors} = $bpc_info[$i]->{xferErrs}; |
||||
$json->{new_size} = $bpc_info[$i]->{sizeNew}; |
||||
$json->{duration} = $bpc_info[$i]->{endTime} - $bpc_info[$i]->{startTime}; |
||||
$json->{type} = $bpc_info[$i]->{type}; |
||||
$json->{comp_ratio} = ( $bpc_info[$i]->{sizeNew} > 0 ) ? |
||||
sprintf( "%.2f", 100 - ( $bpc_info[$i]->{sizeNewComp} * 100 / $bpc_info[$i]->{sizeNew} ) ) |
||||
: |
||||
0; |
||||
|
||||
$json->{new_size_avg} = int $sizes->mean; |
||||
$json->{new_size_median} = int $sizes->median; |
||||
# Some old versions of Statistics::Descriptive (eg, on el5) do not support quantile |
||||
$json->{new_size_q1} = eval { int $sizes->quantile(1) } || 0; |
||||
$json->{new_size_q3} = eval { int $sizes->quantile(3) } || 0; |
||||
$json->{enabled} = ( $conf->{BackupsDisable} > 0 ) ? 0 : 1; |
||||
$json->{total_size} = $sizes->sum + $json->{full_size} - 2 * $new_size_of_last_full; |
||||
$json->{age} = time - $bpc_info[$i]->{startTime}; |
||||
|
||||
$json->{max_errors} = $conf->{MaxXferError} || 0; |
||||
} |
||||
} 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; |
||||
my $new_size_of_last_full = 0; |
||||
|
||||
foreach my $backup ( $bpc->BackupInfoRead( $host ) ) { |
||||
next if ( $backup->{type} !~ m/^full|incr$/ ); |
||||
|
||||
# Save the total size of the last full backup |
||||
if ( $backup->{type} eq 'full' ) { |
||||
$full_size = $backup->{size}; |
||||
$new_size_of_last_full = $backup->{sizeNew}; |
||||
} |
||||
|
||||
$json->{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} represent the total size used by this host. |
||||
# But we want to substract the new size of the last full, as for this one we |
||||
# do not count sizeNew but size. As we've already added sizeNew we need to substract it 2 times |
||||
$json->{size} += $full_size - 2 * $new_size_of_last_full; |
||||
} |
||||
$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 { |
||||
print<<"EOF"; |
||||
|
||||
Usage: $0 --host=<host> or --entity=<entity> |
||||
|
||||
EOF |
||||
} |
||||
|
||||
print to_json( $json, { pretty => $pretty } ); |
||||
exit(0); |
Loading…
Reference in new issue