#!/opt/SUNWstade/bin/perl -I/opt/ras/lib:/opt/ras/lib/auto -d
use Getopt::Std;
use BK2;
use Util;
use System;
use FibreGUI;
use FindBin;
use FibreStat;

my($local_p) = $FindBin::Bin;
my($ix)    = rindex($local_p, "/");
$HOME      = substr($local_p,0,$ix);
$RASSTAT   = "$HOME/DATA/Rasstat";
$LOG       = "$HOME/log/rasstat.log";

System->set_home($HOME);

if (!getopts("XC:D:mhds:", \%opts)) {
    die("Abort: $Getopt::Std::ERROR \n");
}
if ($opts{h}) {
  print "Usage: rasstat -C [comp] -D [displaydate] -m [manualmode] -s <sleep> -d [debug] -h [help]\n";
  print "Comp: hba[0/1]|enc|disk/port \n";
}
$debug    = $opts{d} || 1;
$sleep    = $opts{s} || 600;


Debug->level($debug);

$SIG{'HUP'} = 'IGNORE';

# open(STDIN,  "+>/dev/null");
# open(STDOUT, "+>&STDIN");
# open(STDERR, "+>&STDIN");

$KILL = 0;
$PID =  "$HOME/DATA/rasstat.pid";

sub log {
  my($info) = @_;
  open(O, ">>$LOG");
  print `date` . " $info \n";
  close(O);
}


sub bye {
  my($sig) = @_;
  print "Caught $sig \n";
  $KILL = 1;
}
sub exiting {
  unlink $PID;
  print "$$ Exiting.. \n";
  $db->close() if ($db);
  exit;
}
sub catch {}

# TERM = 1
$SIG{CHLD} = \&catch;

for $s ('INT','KILL','HUP','ABRT','USR1','USR2','TERM') {
  $SIG{$s} = \&bye;
}

if ($opts{X}) {
  $date = "0128171610";
  $db = BK2->new({file => $RASSTAT, readonly => 1, debug => 1, len => [20,20] });
  $csr = $db->cursor(1);
  for (($el=$csr->getGT($date,"")) ; $el; $el = $csr->prev()) {
     print "$el->[0], $el->[1] = $el->[2] \n";
  }
  $csr->close();
  $db->close();
  exit;
  

}

if ($opts{C}) {
  $comp = $opts{C};
  $db = BK2->new({file => $RASSTAT, readonly => 1, debug => 1, len => [20,20] });
  $csr = $db->cursor(2);

  for (($el=$csr->getGE("",$comp)) ; $el && substr($el->[1],0,length($comp)) eq $comp; $el = $csr->next() ) {
     print "$el->[0], $el->[1] = $el->[2] \n";
  }
  $csr->close();
  $db->close();
  exit;
}

if ($opts{D}) {
  $date = $opts{D};
  $db = BK2->new({file => $RASSTAT, readonly => 1, debug => 1, len => [20,20] });
  $csr = $db->cursor(1);

  for (($el=$csr->getGE($date,"")) ; $el && substr($el->[0],0,length($date)) eq $date; $el = $csr->next() ) {
     print "$el->[0], $el->[1] = $el->[2] \n";
  }
  $csr->close();
  $db->close();
  exit;

  
}
  
open(O, ">$PID"); print O $$; close(O);
$day0 = "";
# will snapshot at least once a day.
while (1) {

  $start = time;
  if ($KILL) {
    exiting();
  }
  ($a5k, $summ) = FibreStat::a5k_luxdisc($q,{cache => 0, report => 0});

  my($a5, $sw, $t3) = &FibreGUI::findSelected();

  @switch = ();
  foreach my $data (@$sw) {
     my(@ip) = split(/:/, $data);
     ($x0, $summ) = FibreStat::sw_luxdisc($q,{ip => $ip[1], cache => 0, name=> $ip[0]});
     push(@switch, @$x0);
  }
  

  $date = &today();  # MMDDHH:MM:SS
  $day = substr($date,0,4);

  $db = BK2->new({file => $RASSTAT, create => 1, debug => 1, len => [20,20] });
  if (!$db) {
     &log($BK2::Error);

  } else {
    print "Date=$date " if ($debug);
    for ($x = 0; $x <= $#$a5k; $x++) {
       next if (substr($a5k->[$x],0,1) eq "#");
       @a = split(/\t/, $a5k->[$x]);
       $comp = "$a[0]";
       if ($LASTA{$comp} ne $a5k->[$x] || $day0 ne $day) {
         print "ACounters changed: $date\n";
         $db->put($date, $comp, $a5k->[$x]);
         $LASTA{$comp} = $a5k->[$x];
       }
    }

    for ($x = 0; $x <= $#switch; $x++) {
       @a = split(/\t/, $switch[$x]);
       $comp = "$a[0]";
       if ($LASTS{$comp} ne $switch[$x] || $day0 ne $day) {
         print "SCounters changed: $date\n";
         $db->put($date, $comp, $switch[$x]);
         $LASTS{$comp} = $switch[$x];
       }
    }
    $db->close();
    $db = undef;
  }
  print " Iteration" . (++$iter) . "\n"  if ($debug);

  last if ($opts{m});
  $day0 = $day;   
  sleep(5);

#  sleep($sleep - (time - $start)) ;

}

# MMDDHH:MM:SS
sub today {
  my($this) = @_;

  my(@date) = localtime(time);
  $date[4]++; $date[5] += 1900;

  sprintf("%2.2d%2.2d%2.2d%2.2d%2.2d",
    $date[4],$date[3], $date[2], $date[1], $date[0]);
}

  





