!/usr/sbin/perl
my $fudge = .06;  #fudge factor for new estimate;
my $hconf = "/etc/holland/holland.conf";
my $bsdir =	"/etc/holland/backupsets";
#======
my $co="\033[0m"; my $cb="\033[1;34m"; my $cr="\033[1;31m"; my $cg="\033[1;32m"; my $cp="\033[1;35m";
my $alert = "\033[41;4;33m/!\\\033[0m "; my $info = "\033[44;1;37m[i]\033[0m ";
my $cursize; my $newsize;

print $cb ."-=$cp Lolland v1.2: Holland estimation refactoring tool $cb=-$co\n";
my $config = qx(grep "^backupsets" $hconf);
if (!$config) { print $alert . $cr ."Couldnt get Backupsets.\n$co"; exit 1; };
$config =~ s/.*=\s*//g;
my @backupsets = split(", ", $config);
chomp (@backupsets);
if (scalar(@backupsets) > 1) { 
 print $alert . $cr ."Multiple Backupsets found!\n$co"; 
 print "Lolland supports single backupsets only.\n"; 
 exit 1; 
};
my $bs = $backupsets[0];
$bs =~ s/\s*$//;
print "=" x 60 ."\n";
print "Current Backupset:\t$bs\n";
my $cfile = $bsdir ."/". $bs .".conf";
if (!-e $cfile) { print $cr ."[ERROR] $cfile not found!$co"; exit 1; };
if (lc($bs) != "default" or lc($bs) != "mysql-lvm") { print $alert . $cr ."Nonstandard backupset found.\n$co"; };
print "\nConfiguration file:\t$cfile\n";

my @log = qx(zcat /var/log/holland/holland.log*.gz 2>/dev/null | cat - /var/log/holland/holland.log 2>/dev/null | grep -Ei "of estimated size" | tail -n 10);
if (@log) { print "\nPrevious ". scalar @log ." backup sizes: \n @log"; } else { print $alert . $cp ."No Previous holland data found.$co\n"; };
unless (!@log) {
 $log[(scalar @log)-1] =~ m/.*]\s(.*)%.*/;
 $cursize = sprintf("%.2f", $1 *.01);
 $newsize = $cursize + $fudge;
 if ($newsize > 1) { $newsize = 1.0};
};

my $ok;
my $s = qx(grep -iE "^estimated-size-factor" $cfile 2>/dev/null);
if (!$s) { print $alert . $cr ."Couldnt get Current multiplier.$co"; exit 1; };
$s =~ s/.*=\s*//g; chomp $s;
print "\nCurrent Multiplier:\t\t$s\n";
if ($newsize){
 if( $newsize == $s) {
  print $alert . $cr. "Recommended new Multiplier was the same as current.\nThere is nothing we should adjust here, it is simply failing for disk space.$co\n";
 } else {
  print "Recommended new Multiplier:\t";
  if ($newsize >= $s) { print $cr; } else { print $cg; $ok = 1;};
  print $newsize . $co ."\t($cursize + $fudge fudge)\n";
  if ($newsize > $s) { print $alert . $cp ."Recommended new multiplier is$cr larger$cp than current setting.$co\n"; };
 };
};

my $backupdir = qx(grep "^backup_directory" $hconf 2>/dev/null);
if (!$backupdir) { print $alert . $cr ."Couldnt get Current backup directory.\n$co"; exit 1; };
$backupdir =~ s/.*=\s*//g; chomp $backupdir;
if (-l $backupdir) { 
 print "\n" . $info . "$cp Backup directory $backupdir is a softlink:$co \n"; 
 my $p = qx(stat $backupdir | grep ">"); print $p;
 print "You may want to double check the CommVault configuration.\n";
 $p =~ m/.*\->\s(.*)/;
 $backupdir = substr($1, 1, length($1)-2); #lol, thanks bash;
};
print "\nCurrent Size of backup directory, $backupdir:\n";
my @bss = qx(df -h $backupdir);
if (!@bss) { print $alert . $cr. "Had a problem getting the data for $backupdir$co\n"; exit 1; };
my $tmp = $bss[1] . $bss[2]; $tmp =~ s/\n/ /g;
my @bss2 = split(" ", $tmp);
print "Mounted on ". $bss2[5] .", it has ". $bss2[3] ." avaliable, ". $bss2[2] ." Used/". $bss2[1] ." Total (". $bss2[4] ." Used).\n";

print "=" x 60 ."\n";

if($ok) {
 print "Would you like to make the change now? [y/n] ";
 my $p = <STDIN>; chomp $p; 
 if($p =~ m/^y/i) { 
  my $cmd = "sed -i ". chr(39) ."s/estimated-size-factor\\s*=.*/estimated-size-factor = $newsize/". chr(39). " $cfile";
  qx($cmd);
  print "Changed estimation factor to recommended setting.\n";
  print $cg . "New setting: ";  print qx(grep "estimated" $cfile | cut -d " " -f 3 ); print $co;
  print "=" x 60 ."\n";
 };
};
