#!/usr/bin/perl

#
#
# Script to grab failures and uptime out of syslog files
# Rev History:
# 0.1 - Initial rev, B. Kieft 11/2011 
#

$usage = "usage: log_failures.pl rootPath [recurseDepth]\n";
die $usage unless @ARGV >= 1; #show usage if no parameters were passed 
use File::Find;
my $baseDepth;
my $recurseDepth = @ARGV >= 2 ? $ARGV[1] : 1000;

my @files;
$failCount = 0; 
$totRun = 0;
$totFail = 0;
$CSVOutput = 1;
$totFiles = 0;


sub process_file 
{
    my $depth = ($name =~ tr!/!!); # count slashes to get depth
    if($depth- $baseDepth <= $recurseDepth && $File::Find::name =~/.*syslog.*/) 
    {
      my $file = $File::Find::name;
      return unless $file =~ /syslog/;

      open F, $file or print "couldn't open $file\n" && return;
      $firstLine = <F>;
      while (<F>) {
      	# Count up the critical failures
#        if (my ($found) = m/\.*(\[\w*\])\(CRITICAL\)/o) {
# CBIT, vertical control, battery, depth, or command line critical count only
        if ( (my ($found) = m/\.*(\[CBIT\])\(CRITICAL\)/o) || ((my ($found) = m/\.*(\[VerticalControl\])\(CRITICAL\)/o)) || ((my ($found) = m/\.*(\[CommandLine\])\(CRITICAL\)/o)) || ((my ($found) = m/\.*(\[Depth_Keller\])\(CRITICAL\)/o)) || ((my ($found) = m/\.*(\(CRITICAL\)\: )SIGSEGV/o)) || ((my ($found) = m/\.*(\[Batt_Ocean_Server\])\(CRITICAL\)/o)) ){
          
          $failCount++;
          $compName = $1;

#           [CBIT](CRITICAL)
          
          # Throw out SBIT for now
          if(my ($found) = m/SBIT]\(CRITICAL\)/o) {
          	$failCount--;
          }
        }
       $lastLine = $_;
      }
      close F;

# No longer checking for this. Not a failure.      
#      # See if the log terminated normally
#      if(!($lastLine =~/.*Uninitialize CBIT Component.$/)) 
#      {
#      	$failCount++;
#      }      
     
      $lastLine =~/.*Z,(.*) \[.*/; 
      $endTime = $1;
      
      $firstLine =~/.*Z,(.*) \[.*/; 
      $startTime = $1;  

      $runTime = ($endTime - $startTime)/3600;
      
      if($runTime > 0) 
      {
        $totFiles += 1;
        if($failCount > 0)
        {
          $mtbcf = $runTime / $failCount;
        }
        else
        {
        	$mtbcf = $runTime;
        }
        $totRun += $runTime;
        $totFail += $failCount;
        if($totFail > 0) 
        {
          $avgMTBCF = $totRun / $totFail;
        }
        if( $CSVOutput )
        {
          print("$file,$failCount,$runTime,$mtbcf\n");      	
        }
        else
        {
          print("\nFOUND: $failCount in: $file\n"); 
          print("RUNTIME: $runTime\n");
          print("MTBCF: $mtbcf\n");
          print("avgMTBCF: $avgMTBCF\n");
        }
      }  
      else
      {
        print("$file does not contain a valid start/end time on the first and last line")
      }
      $failCount = 0;   
    
    }
}


foreach $dir ($ARGV[0]) {
    my $path = "$dir";
    $path=~ s/\/+$//g;
    $baseDepth = ($path =~ tr!/!!);
    print("path=$path, baseDepth=$baseDepth, recurseDepth=$recurseDepth\n");
    find(\&process_file, $path);
    print("Total files processed: $totFiles\n")
}

exit 0;
