#!/usr/bin/perl 

#ModulePurpose: List current CT and PA sessions 
#ModuleName:    list_ct/pa_sessions
#TimeRequied:   1 min

# Details Report
#    -- CT/PA Sessions -08.15.2009 
#    Session User Name      Session Name   Tool Type      Status         Priority  Creation Time                 Last Access Time
#    4       uno7411        090814142354   CALL TRACE     STOP_REALTIME   181      Fri Aug 14 14:23:54 2009
#    5       usc484x        090807103831   CALL TRACE     STOP_REALTIME   180      Fri Aug  7 10:38:31 2009
#    1       admin          090812060732   PROT ANAL      UNCONFIGURED    183      Wed Aug 12 06:07:32 2009
#    3       spcsbiho       090814000415   PROT ANAL      RUNNING         184      Fri Aug 14 00:04:15 2009
#    8       admin          090807100348   PROT ANAL      UNCONFIGURED    180      Fri Aug  7 10:03:48 2009


use lib '/h/bmetzger/s/p/hc/';
use HC qw(:ALL);  #functions available to all modules 

use English;
use Carp;
use Cwd;
use Date::Pcalc qw(Delta_Days); 
use File::Basename;
use File::Glob;
use File::Copy;

###############3###
# define location of, and open files
##################

  my ($TmpDir,$sitelist,$global_problems,$global_details,$global_summary) = InitVars();

  $SSH_OPTIONS=" -o BatchMode=yes -o ConnectTimeout=2";
  $ENV{'PATH'} = 'misc_tools:/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:/opt/platform7/lbin/:.';
  &initvars();


### checks
 ($header, $local_problems, $local_details)= &check(); 
  summarize($header, $local_problems, $local_details);

###################33
sub initvars {

   ($sys_day,$sys_mon,$sys_year) = (localtime)[3,4,5];
   $sys_mon += 1;  #jan is 0
   $sys_year += 1900;
   $today = sprintf "%02d.%02d.%d",$sys_mon,$sys_day,$sys_year;  # zero fill date

   $SSH_OPTIONS=" -o BatchMode=yes -o ConnectTimeout=2";
   $ENV{'PATH'} = '/usr/bin:/usr/local/bin:/usr/sbin/:/usr/ucb:/etc/:.';
} #initvars

####################
sub line_count {
# perform a  wc -l

  my $file = shift;
    open my $fh, "<", $file or die "could not open $file:$!";
    my $lines = 0;
    $lines++ while <$fh>;
    return $lines;
    close ($fh);
} #line_count

#########
sub check {
# List CT and PA sessions.  Look for unused sessions to kill off

     print "list pa/ct sessions \n";

     my $g25_file = "/var/opt/platform7/tmp/eng_logging_G25_0";  

     my $local_problems = "$TmpDir/sessions_problems";
     my $local_details  = "$TmpDir/sessions_details";
     my $local_junk1    = "$TmpDir/junk1";    # temp file for problems
     my $local_junk2    = "$TmpDir/junk2";    # temp file for details

    # open local output files
    open PROBLEMS, ">>$local_problems" or croak "Can't open file $local_problems for write: $!\n";
    open DETAILS, ">>$local_details" or croak "Can't open file $local_details for write: $!\n";

    # dump current 
    unlink("$g25_file");
    qx[. /opt/platform7/lbin/profile; /opt/platform7/bin/p7dumpstatus g25];

    open (G25, "<$g25_file") or croak "Can't open g25 for read: $!\n";
   
    while (my $line = <G25>) {
        chomp($line);

         $line =~ s/^\s+//,$line;
#         $line =~ s/\s+/ /g,$line;
         my @arr = split(/\s+/,$line);

         if ( ($arr[0] =~ /\d+/) && ($line =~ /PROT ANAL/) ){
             printf DETAILS "$line\n";
         } 
      
         if ( ($arr[0] =~ /\d+/) && ($line =~ /CALL TRACE/) ){
             print DETAILS "$line\n";
         } 
   }
    #gotta close, so lines can be counted
    close(DETAILS);  

    my $detailcount = line_count($local_details);

    # open local output files
    open JUNK1, ">>$local_junk1" or croak "Can't open file $local_junk1 for write: $!\n";
    open JUNK2, ">>$local_junk2" or croak "Can't open file $local_junk2 for write: $!\n";

    my $header = "\n--- Curren CT/PA Sessions -$today \n";

    #  Format local_details, if needed
    if ( $detailcount > 0 ){
         printf JUNK2 $header;
         printf JUNK2 "Session User Name      Session Name   Tool Type      Status         Priority  Creation Time                 Last Access Time\n";
         qx[grep CALL $local_details >> $local_junk2];
         qx[grep PROT $local_details >> $local_junk2];
    }
    close(JUNK2);  

   copy($local_junk1,$local_problems) or die "junk1 cannot be copied";
   copy($local_junk2,$local_details) or die "junk2 cannot be copied";

   unlink("$local_junk1") || print $!;
   unlink("$local_junk2") || print $!;

  return($header, $local_problems, $local_details);
} #check

