#!/usr/bin/perl -w

############################################################################
# testSnooperConns.pl
#  Takes casnooper output and for each PV in the Top PVs list, runs caget to 
#  check whether or not the PV is connected.
#
#  Can either take input from stdin (i.e. piped from casnooper) or
#  from an argument which is a file containing casnooper output.
#
#  Examples:
#   piped from caSnoooper: 
#         caSnooper -d3 -p20 -t10 | testSnooperConns.pl
#
#   from a file:
#         caSnooper -d3 -p20 -t10 > snoop.out
#         testSnooperConns.pl snoop.out
#
#  31-Jan-2011 jrock
#    only output channels not connected
#
############################################################################


use strict;

sub tryConnection($)
{
   # print "trying connections...\n";
   my $PV = shift;
   my $caGetCommand = "caget $PV > /tmp/cagetResults.txt 2>&1";
   system $caGetCommand;
   # system "cat /tmp/cagetResults.txt";
   if (!open(RESULTS, "/tmp/cagetResults.txt")) 
      {
      print "*** failed to read caget results file\n";
      return 2;
      }
      my @lines = <RESULTS>;
      close RESULTS;

      foreach my $line (@lines) 
           {
           if ($line =~ /Channel connect timed out/)
              {  return 0; }
           }
      return 1;
}

sub checkConnections($)
{
	my $fil = shift;
        my @lines;
	
        if (defined $fil)
           {
           print "input from $fil\n";
	   if (!open(INP, $fil)) 
              {
  	      print "*** $fil: failed to read \n";
	      return;
              }
	   @lines = <INP>;
	   close INP;
           }
        else
           {
        
           print "input from pipe\n";
	   @lines = <>;
           }

        print "===> WILL CHECK CONNECTIONS FOR CASNOOPER OUTPUT\n";
        foreach my $prline (@lines)
           { print "$prline"; }
 
        print "------------------------------------------------------------------------------------\n";
        print "===>  NOW CHECKING FOR PVS THAT ARE NOT CONNECTING...\n";

        my @connChecks;
        
        my $inPVs = 0;
        my $disconnPVCount = 0;
	foreach my $line (@lines) 
           {
	   if ($line =~ /PVs with top/) 
              { $inPVs = 1; }
	   elsif ($line =~ /CAS: Connection Terminated/) 
              { $inPVs = 0; }
           elsif ($inPVs)
              { 
              $line =~ s/^\s+//;
              my ($num, $host, $PVname, $time) = split/\s+/, $line;
              # print "**n:$num h:$host p:$PVname t:$time\n";

              my $connected = tryConnection($PVname);
#              if ($connected) { push @connChecks, "$PVname connected\n"; }
#              else            { push @connChecks, "$PVname NOT connected\n"; }
#              if ($connected) { print "$num  $PVname connected\n"; }
#              else            { print "$num  $PVname NOT connected\n"; }
#  only output channels not connected
              if (!($connected))  
                 { 
                 print "$num  $PVname NOT connected\n"; 
                 ++$disconnPVCount;
                 }

              }
           }
        print "------------------------------------------------------------------------------------\n";
        print "TOTAL COUNT OF DISCONNECTED PVS IN SNOOPER SAMPLE: $disconnPVCount\n";

#        foreach my $connCheck (@connChecks)
#           {  print "$connCheck"; }
}


##################################################

# code start

##################################################

my $fileName = $ARGV[0];

print "Checking connections for PVs from caSnooper output \n";

checkConnections($fileName);

