#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Getopt::Long;
use YAML 'Dump'; 

#we don't run at the same time as ourselves
use Proc::PID::File;
if (Proc::PID::File->running()){
  print "$0 is already running!\n";
  exit();
}

my $jobsfile = "/var/lib/backup/backup.jobs";
my $cachefile = "/opt/eig_linux/data/backupstate.data";
my $full = 0;
my $fix = 0;
my $verbose = 0;
my $submit = 0;
GetOptions(
        "full"      =>      \$full,
        "fix"       =>      \$fix,
        "verbose"   =>      \$verbose,
        "submit"    =>      \$submit,
);
my $systemdata = {};

resumeifboxisrebooted();

if ( -f "/opt/eig_linux/data/backups/dirs.files.lastrun" ) {
  checklastmodified(\$systemdata,'dirs.files.lastrun-date','/opt/eig_linux/data/backups/dirs.files.lastrun');
  checklastmodified(\$systemdata,'dirs.files.lastrun-epoch','/opt/eig_linux/data/backups/dirs.files.lastrun');
} else {
  checklastmodified(\$systemdata,'dirs.files.lastrun-date','/usr/sec/data/backups/dirs.files.lastrun');
  checklastmodified(\$systemdata,'dirs.files.lastrun-epoch','/usr/sec/data/backups/dirs.files.lastrun');
}
checklastmodified(\$systemdata,'postbackup.log-date','/var/log/postbackup.log');
checklastmodified(\$systemdata,'postbackup.log-epoch','/var/log/postbackup.log');
if ( -f "/opt/eig_linux/data/backups/backupmysql.lastrun" ) {
  checklastmodified(\$systemdata,'mysql-backup.lastrun-date','/opt/eig_linux/data/backups/backupmysql.lastrun');
  checklastmodified(\$systemdata,'mysql-backup.lastrun-epoch','/opt/eig_linux/data/backups/backupmysql.lastrun');
} else {
  checklastmodified(\$systemdata,'mysql-backup.lastrun-date','/usr/sec/data/backups/backupmysql.lastrun');
  checklastmodified(\$systemdata,'mysql-backup.lastrun-epoch','/usr/sec/data/backups/backupmysql.lastrun');
}

check_fc(\$systemdata);

my $userdata = {};
open(PASSWD, "</etc/passwd") or die("failed to open passwd: $!");
while(<PASSWD>){
  my @ent = split(':', $_);
  if($ent[5] =~ m#^/home(\d*)#){
    $userdata->{$ent[0]}->{homedir} = $ent[5];
    if($1 eq ""){
      $userdata->{$ent[0]}->{homenum} = "";
    }else{
      $userdata->{$ent[0]}->{homenum} = $1;
    }
  }
}
close(PASSWD);

my %users;
my %cpanelusers;
$systemdata->{"cpanel-accounts-total"} = 0;
foreach(`ls -A /var/cpanel/users`){
  chomp;
  next if $_ eq 'root';
  next if ( -e "/var/cpanel/suspended/$_" );
  $users{$_} = 1;
  $cpanelusers{$_} = 1;
  $systemdata->{"cpanel-accounts-total"}++;
}
checkrpmversion(\$systemdata);

checkbackuphelperlastuser(\$systemdata);

$systemdata->{"backup-dirs-completed"} = 0;
$systemdata->{"backup-dirs-uncompleted"} = 0;

systemstatus(\$systemdata);


checkbackupmounts(\$systemdata);

checkpkgacctrunner(\%cpanelusers,\$userdata,\$systemdata);

partitiondata(\$systemdata);

#print the data we have collected
if($submit){
  submitdata(\$systemdata);
}else{
  foreach my $key (sort keys %$systemdata){
    if($key =~ m/date$/){
      my $date = localtime($systemdata->{$key});
      printf("%-40s %-40s\n",$key,$date);
    }else{
      printf("%-40s %-40s\n",$key,$systemdata->{$key});
    }
  }
}
update_cache($cachefile, $systemdata);
exit unless $full;

foreach my $user (keys %users){
  checkbackup($user, \$userdata);
}
printbackupstats(\$userdata,\%users);


#### only subs below this line
sub check_fc {
  my $response_ref = shift;
  if( -e "/sys/class/fc_host"){
    my $has_non_soft_raid = 0;
    my $has_old = 0;
    open(MNTS, "</proc/mounts");
    while(<MNTS>){
      my @p = split('\s+');
      if($p[1] =~ m#^/backup#){
        if(substr($p[1],-4,4) eq ".old"){
          $has_old = 1;
          next;
        }
        if(substr($p[0],0,7) ne "/dev/md"){
          $has_non_soft_raid = 1;
        }
      }
    }
    close(MNTS);
    if($has_non_soft_raid){
      $$response_ref->{'has-fc'} = 'part';
    }else{
      $$response_ref->{'has-fc'} = 'yes';
    }
    if($has_old){
       $$response_ref->{'has-fc'} .= '.o';
    }
  }else{
    $$response_ref->{'has-fc'} = 'no';
  }
}


sub submitdata {
  my $response_ref = shift;
  my $hostname = `hostname`;
  chomp($hostname);
  $$response_ref->{hostname} = $hostname;
  my $message;
  foreach my $key (sort keys %{$$response_ref}){
    $message .= "$key,".$$response_ref->{$key}."\n";
  }
  require LWP::UserAgent;
  my $ua = LWP::UserAgent->new;
  $ua->agent("backupstate/0.1 ");

  my $req = HTTP::Request->new(POST => 'http://74.220.198.194/cgi-bin/backupstate.cgi');
  $req->content_type('application/x-www-form-urlencoded');
  $req->content("data=$message");

  my $res = $ua->request($req);
  my $response = $res->content;
  print "response from server: $response\n" if $verbose;
  unless($response =~ m/report accepted/){
    print "backupmaster didnt accept my report!\n" if $verbose;
  }
}
sub partitiondata {
  my $response_ref = shift;
  my @space = `/bin/df -P`;
  my $tmp = {};
  foreach(@space){
    next unless $_ =~ m#/(home|backup)(\d)#;
    my $type = $1;
    my $part = $2;
    my @p = split('\s+');
    $tmp->{$part}->{$type}->{'size'} = $p[1];
    $tmp->{$part}->{$type}->{'used'} = $p[2];
    if($type eq "home" and -e "/admin/mig$part/df.output"){
      open(DF, "</admin/mig$part/df.output");
      <DF>;
      my $sourcespace = <DF>;
      close(DF);
      my @s = split('\s+', $sourcespace);
      $tmp->{$part}->{$type}->{'used'} = $s[2] if ($s[2] > $p[2]);
    }
  }
  foreach my $key (keys %$tmp){
    $tmp->{$key}->{'home'}->{'size'} = 0 unless $tmp->{$key}->{'home'}->{'size'};
    $tmp->{$key}->{'home'}->{'used'} = 0 unless $tmp->{$key}->{'home'}->{'used'};
    $tmp->{$key}->{'backup'}->{'size'} = 0 unless $tmp->{$key}->{'backup'}->{'size'};
    $tmp->{$key}->{'backup'}->{'used'} = 0 unless $tmp->{$key}->{'backup'}->{'used'};
    $$response_ref->{"partition-data-$key"} = join('-',($tmp->{$key}->{'home'}->{'size'},$tmp->{$key}->{'home'}->{'used'},$tmp->{$key}->{'backup'}->{'size'},$tmp->{$key}->{'backup'}->{'used'}));
  }
}
sub checklastmodified {
  my $response_ref = shift;
  my $label = shift;
  my $file = shift;
  my @stats = stat($file);
  @stats = stat("$file.1") if( -e "$file.1" and $stats[7] == 0);
  @stats = stat("$file.2") if( -e "$file.2" and $stats[7] == 0);
  @stats = stat("$file.3") if( -e "$file.3" and $stats[7] == 0);
  @stats = stat("$file.4") if( -e "$file.4" and $stats[7] == 0);
  $$response_ref->{$label} = 0;
  $$response_ref->{$label} = $stats[9] if $stats[9];

}

sub systemstatus {
  my $response_ref = shift;
  $$response_ref->{"backup-status"} = "";
  $$response_ref->{"backup-stage"} = "";
  my $jobs_in_queue = `ls /var/spool/eig_backup | wc -l` || 0;
  chomp $jobs_in_queue;
  if ( `ps fauwwx | grep datacycl[e]` ) {
    $$response_ref->{"backup-status"} = "Datacycle running";
  }
  if ( -f "/var/run/queuer.pid" ) {
    $$response_ref->{"backup-stage"} = "Active: $jobs_in_queue jobs running";
  } elsif ( $jobs_in_queue > 0 ) {
    $$response_ref->{"backup-stage"} = "Pending: $jobs_in_queue jobs queued";
    my %disabled = ();
    my $failed_count = 0;
    foreach my $user ( `ls /var/spool/eig_backup` ) {
      $failed_count = `cat /var/spool/eig_backup/$user`;
      if ( $failed_count && $failed_count >= 5 ) {
        $disabled{$user} = $failed_count;
      }
    }
    $failed_count = keys %disabled;
    $$response_ref->{"backup-stage"} = "Pending:$jobs_in_queue Failed:$failed_count" if ($failed_count);
  } else {
    $$response_ref->{"backup-stage"} = "No jobs.";
  }
  if ( -f "/var/run/backup_cpanel.pid" ) {
    $$response_ref->{"backup-stage"} = "Active: backup_cpanel";
  } elsif ( -f "/var/run/backup-non-user-mysql" ) {
    $$response_ref->{"backup-stage"} = "Active: non-user-mysql";
  }
  if ( -f "/etc/disable_backups" ) {
    $$response_ref->{"backup-stage"} = "Backups Disabled";
  }
  if ( check_complete() && $$response_ref->{"backup-stage"} !~ m/^Active/ && !-f "/etc/disable_backups") {
    $$response_ref->{"backup-stage"} = "Incomplete";
  }
}


sub checkrpmversion {
  my $response_ref = shift;
  my $version = $1 if ( `rpm -q eig_backup` =~ m/(eig_backup-.*)\.el\d+\.noarch/ );
  chomp $version;
  $$response_ref->{"rpm-version"} = $version;
}

sub checkbackuphelperlastuser {
  my $response_ref = shift;
  if ( -f "/var/log/eig_backup/backuphelper_last_command" ) {
    my ($lastuser,$lastcmd) = ($1,$2) if ( `tail -n 1 /var/log/eig_backup/backuphelper_last_command` =~ m/^(\w+)\:(\w+)\:.*$/ );
    $lastuser = '' unless ( $lastuser );
    $lastcmd = '' unless ( $lastcmd );
    chomp $lastuser;
    chomp $lastcmd;
    $$response_ref->{"backuphelper-lastuser"} = $lastuser;
    $$response_ref->{"backuphelper-lastcmd"} = $lastcmd;
  }
}

sub checkpkgacctrunner {
  my $users_ref = shift;
  my $userdata_ref = shift;
  my $response_ref = shift;
  my $now = time;
  my $twodays = $now - (60 * 60 * 24 * 2);
  my $oneweek = $now - (60 * 60 * 24 * 7);
  $$response_ref->{"cpanel-accounts-two-days-behind"} = 0;
  $$response_ref->{"cpanel-accounts-one-week-behind"} = 0;
  $$response_ref->{"cpanel-accounts-not-backuped-up"} = 0;
  $$response_ref->{"cpanel-accounts-with-backups"} = 0;
  foreach my $user ( keys %{$users_ref}){
    my $mostrecent = getmostrecentbackupdate($user,$response_ref);
    if($mostrecent == 0){
      print "$user not pkgaccted\n" if $verbose;
      $$response_ref->{"cpanel-accounts-not-backuped-up"}++ if ( !-f "/var/cpanel/suspended/$user" );
    }elsif($mostrecent < $oneweek){
      $$response_ref->{"cpanel-accounts-with-backups"}++;
      $$response_ref->{"cpanel-accounts-two-days-behind"}++;
      $$response_ref->{"cpanel-accounts-one-week-behind"}++;
      print "$user 7+ days behind\n" if $verbose;
    }elsif($mostrecent < $twodays){
      $$response_ref->{"cpanel-accounts-with-backups"}++;
      $$response_ref->{"cpanel-accounts-two-days-behind"}++;
      print "$user 2+ days behind\n" if $verbose;
    }else{
      $$response_ref->{"cpanel-accounts-with-backups"}++;
    }
  }

}
sub getmostrecentbackupdate {
  my $user = shift;
  my $data_ref = shift;
  my $mostrecent = 0;
  return $mostrecent if ( -f "/var/cpanel/suspended/$user" );
  foreach my $type (qw{daily weekly monthly}){
    if ( $type =~ m/^daily$/ and my @attrib = stat "/backup/lastgenerated/$user" ) {
      $mostrecent = $attrib[9] if($attrib[9] > $mostrecent);
      $$data_ref->{"backups-$type"}++;
    } else {
      if(my @attrib = stat "/backup/cpbackup/$type/$user/lastgenerated"){
        $mostrecent = $attrib[9] if($attrib[9] > $mostrecent);
        $$data_ref->{"backups-$type"}++;
      }
    }
  }
  return $mostrecent;
}


sub checkbackupmounts {
  my $response_ref = shift;
  my $mountpointproblems = 0;
  my @homefstab = `grep /home /etc/fstab`;
  my %homes;
  foreach(@homefstab){
    chomp;
    next if m/^\s*#/;
    next if m/swap/;
    my @parts = split('\s+');
    $homes{$parts[1]} = 1;
  }
  my @fstab = `grep ' /backup' /etc/fstab`;
  my %should;
  foreach(@fstab){
    chomp;
    next if m/^\s*#/;
    my @parts = split('\s+');
    $should{$parts[1]} = 1;
  }
  foreach my $home (keys %homes){
    my $h = $home;
    $h =~ s#/home#/backup#;
    unless ($should{$h}){
      print "mountpoint: There is no $h mount for $home\n";
      $mountpointproblems++;
    }
  }
  my @mounts = `grep " /backup" /proc/mounts |grep -v fake`;
  if(@mounts != 0){
    foreach(@mounts){
      chomp;
      my @parts = split('\s+');
      if($should{$parts[1]} >= 1){
	$should{$parts[1]} = 2;
      }else{
	print "mountpoint: ".$parts[1]." should not be mounted?\n";
	$mountpointproblems++;
      }
    }
  }
  foreach(keys %should){
    if($should{$_} == 1){
      print "mountpoint: $_ not mounted\n";
      $mountpointproblems++;
    }elsif($should{$_} == 2){
      #this is the expected state
    }else{
      print "mountpoint: $_ uncaught state, let robert know about this!\n";
      $mountpointproblems++;
    }
  }
  if(@mounts == 0){
    unless($mountpointproblems){
      print "mountpoint: no backups mounted and no /backup in fstab\n";
      $mountpointproblems = 1;
    }
  }
  $$response_ref->{"mountpoint-problems"} = $mountpointproblems;

}


sub printbackupstats {
  my $userdata_ref = shift;
  my $users_ref = shift;
  foreach my $username (keys %$users_ref){
    foreach(qw{ numcpbackups numhomedirbackups numbackups} ){
      if(defined($$userdata_ref->{$username}->{$_})){
        $$userdata_ref->{TOTALS}->{"userswith-".$$userdata_ref->{$username}->{$_}."-$_"}++;
      }else{
        $$userdata_ref->{TOTALS}->{"userswith-0-$_"}++;
      }
    }
  }
  #  foreach my $num (0 ... 4){
  foreach my $type (qw{ numcpbackups numhomedirbackups numbackups} ){
    foreach my $num (0 ... 4){
      my $key = "userswith-$num-$type";
      print "$num $type ".$$userdata_ref->{TOTALS}->{$key}."\n" if defined($$userdata_ref->{TOTALS}->{$key});
    }
  }
}

sub checkbackup {
  my $username = shift;
  my $userdata_ref = shift;

#  checkbackuptype($username, $userdata_ref, "daily");
  foreach my $type ("daily", "weekly", "monthly"){
#    print "\t$type\n";
    checkbackuptype($username, $userdata_ref, $type);
  }
}
sub checkbackuptype {
  my $username = shift;
  my $userdata_ref = shift;
  my $type = shift;
  unless(-e "/backup".$$userdata_ref->{$username}->{homenum}."/cpbackup/$type/$username"){
    return 0;#    print "$username does not have a $type backup\n";
  }
  my @bstat = stat("/backup/cpbackup/$type/$username");
  if($$userdata_ref->{$username}->{homenum} ne ""){
    my @bnumstat = stat("/backup".$$userdata_ref->{$username}->{homenum}."/cpbackup/$type/$username");
    unless($bstat[0] == $bnumstat[0] and $bstat[1] == $bnumstat[1]){
      print "$username: /backup does not link properly to /backup".$$userdata_ref->{$username}->{homenum}."\n";
    }
  }
  $$userdata_ref->{$username}->{"cpbackup$type"} = 1 if( -d "/backup".$$userdata_ref->{$username}->{homenum}."/cpbackup/$type/$username/mysql");
  $$userdata_ref->{$username}->{numcpbackups}++ if( $$userdata_ref->{$username}->{"cpbackup$type"} );
  $$userdata_ref->{$username}->{"homedirbackup$type"} = 1 if( -d "/backup".$$userdata_ref->{$username}->{homenum}."/cpbackup/$type/$username/homedir");
  $$userdata_ref->{$username}->{numhomedirbackups}++ if( $$userdata_ref->{$username}->{"homedirbackup$type"} );
  $$userdata_ref->{$username}->{"date$type"} =  $bstat[9] if $$userdata_ref->{$username}->{"cpbackup$type"} and $$userdata_ref->{$username}->{"newbackup$type"};
  $$userdata_ref->{$username}->{numbackups}++;

}

sub resumeifboxisrebooted {
  my $response = `cat /proc/uptime`;
  my @p = split('\s+', $response);
  if($p[0] > 900 and $p[1] < 1800){
    print "resuming backups\n";
    system("/usr/sbin/backupctl --resume");
  }
}

sub check_complete {
  system("/opt/eig_linux/bin/check_backups");
  return "Incomplete Backups" if ( $? != 0 );
  return 0;
}

sub update_cache {
    my $cache_file = shift;
    my $userdata_ref = shift;
    open (FH, ">$cache_file" ) or die "Can't open $cachefile : $! \n";         
    print FH Dump( $userdata_ref );
    close FH; 
}
