#!/opt/SUNWstadh/bin/perl -I/opt/SUNWstadh/lib
use System;
use strict;
use Getopt::Std;
use PDM;
use Agent::HOST;
use HBA;
use Events;
use PDM::ConfigFile;
use Modules;
use Client;
use Agent;
use Data::Dumper;
use Mail;

sub usage {
  print <<EOF;
  Usage  : ras_datahost -m master_ip 
         -m IP      : send to master

  Example:
         ras_datahost -m host1:7654
EOF
}
use vars qw (%opts $HOME);

if (!getopts("m:", \%opts)) {
    &usage();
    exit(1);
}
$HOME    = System->home_dir();
my $IP   = $opts{m};
my $PORT = 7654;

if (!$IP) {
   open(O, "$HOME/DATA/MASTER"); 
   $IP = <O>; close(O);
   if (open(O, "$HOME/DATA/MASTER_PORT")) {
      $PORT = <O>; close(O);
   }
}

if (index($IP, ":") > 0) {
  ($IP, $PORT) = split(/\:/, $IP);
}

System->set_home(System->home_dir());

my($renv, $devs, $hosts, $notifs) = PDM::ConfigFile->read();
System->set_renv($renv);
my $hostname = $renv->{hostname};
System->set_rasport($PORT);

my $admin_email = $renv->{admin_email};

my $LUXADM  = $renv->{use_luxadm};
my $LOG     = System->get_home() . "/log/cron.log";
my $LOGSIZE = (stat($LOG))[7];
rename "$LOG", "$LOG.2" if ($LOGSIZE > 1000000); 

&report();

sub report {
  PDM->readAllCache();
  #my $renv    = System->get_renv();
  my $logfile   = $renv->{logfile};
  my $t3logfile = $renv->{t300logfile};

  if ($LUXADM) {
      my $report = {};
      my $to     = {};
      Agent::HOST->readPorts($report);
      if ($LUXADM > 1) {
        Agent::HOST->readLuns($report);
        Agent::HOST->readFreeSpace($report);
      }
      if ($LUXADM > 2) {
        $to = TO->getCurrentTopo(undef, 0, 1);
        if ($to) {
          my $hosts = $to->hostList();
          my $host = $hosts->[0];
          $host->{info}{datahost} = 1;
        }
      }
      my $date = Util->get_today();
      $report->{key}  = System->hostname();
      $report->{topo} = $to;
      $report->{time} = time;
      $report->{date} = $date;

      $Data::Dumper::Indent = 1;
      my($err, $ans) = Util::Http->saveFile(  $IP, "DataHost/$hostname", Dumper($report));
      &sendEmail($err, $IP);
  }

  my($log_err, $lines) = Agent->read_log_file($logfile,'NEW');
  if ($#$lines >= 0) {
      my($err, $ans) = Util::Http->appendFile($IP, "DataHost/$hostname.loglines", 
                              join("\n", @$lines));
  }

  my($log_err, $lines) = Agent->read_log_file($t3logfile,'NEW');
  if ($#$lines >= 0) {
      my($err, $ans) = Util::Http->appendFile($IP, "DataHost/$hostname.t3loglines", 
                              join("\n", @$lines));
  }

  PDM->writeAllCache();
}



sub sendEmail {
  my($err, $IP) = @_;
  my $renv = System->get_renv();
  my $h = PDM->getCacheHandle("datahost_email");
  my $hn = System->hostname();
  my $time = Util->get_today();
  my($version) = $renv->{version};
  if ($err)  {
    print "$time: DataHost Error : $err\n";
    if ($admin_email) {
      if (!$h->{last_email} || time - $h->{last_email} > (60*60*8)) {
         $h->{last_email} = time;
         Mail->mail($admin_email, "Storage A.D.E DataHost", 
            "Error(s) from '$hn' ($version)", 
            " You requested the following errors be forwarded to you from $hn\n\nAgent    : $hn\nSeverity : Error\nCategory : DataHost\nEventTime: $time\n\nDESCRIPTION:\nThe Storage A.D.E Datahost package failed to transmit to $IP:\n $err");
      }
    }
  } else {
    $h->{last_email} = 0;
  }
}


sub dumper {
  my($report) = @_;

  my $out;

  foreach my $e (keys %$report) {
      my $v = $report->{$e};
      if (ref($v) eq "HASH") {
         foreach my $e1 (keys %$v) {
              $out .= "$e.$e1\t$v->{$e1}\n";
         }
      } else {
         $out .= "$e\t$report->{$e}\n";
      }
  }  
  return $out;
}





