#! /usr/bin/perl

#
# Analyze the previous hours' directory server access logs
#
# Terry.Gardner@Sun.COM
# 19-APR-2005
# 12-FEB-2008
# 13-FEB-2008
#

#
# returns the number of days in the month
# of February
#
sub febDays() {
  if(    ( $year % 400 ) != 0 ) { return 28; }
  elsif( ( $year % 400 ) == 0 ) { return 29; }
  elsif( ( $year % 100 ) == 0 ) { return 28; }
  else                          { return 28; }
}

#
# seed the random number generator
#
srand();

#
# Get current time
#
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

#
# Construct filter for previous hour
#
if($hour == 0) {
  $hour = 23;
  $mday = $mday - 1;
} else {
  $hour = $hour - 1;
}
$year += 1900;


#
# set the month name string
#
if( $mday == 0 ) {
  
  if(    $mon == 0 )  { $month = "Dec"; $day = 31; $year = $year - 1; }
  elsif( $mon == 1 )  { $month = "Jan"; $day = 31;                    }
  elsif( $mon == 2 )  { $month = "Feb"; $day = febDays;               }
  elsif( $mon == 3 )  { $month = "Mar"; $day = 31;                    }
  elsif( $mon == 4 )  { $month = "Apr"; $day = 30;                    }
  elsif( $mon == 5 )  { $month = "May"; $day = 31;                    }
  elsif( $mon == 6 )  { $month = "Jun"; $day = 30;                    }
  elsif( $mon == 7 )  { $month = "Jul"; $day = 31;                    }
  elsif( $mon == 8 )  { $month = "Aug"; $day = 31;                    }
  elsif( $mon == 9 )  { $month = "Sep"; $day = 30;                    }
  elsif( $mon == 10 ) { $month = "Oct"; $day = 31;                    }
  elsif( $mon == 11 ) { $month = "Nov"; $day = 31;                    }

} else {

  if(    $mon == 0 )  { $month = "Jan"; $day = $mday; }
  elsif( $mon == 1 )  { $month = "Feb"; $day = $mday; }
  elsif( $mon == 2 )  { $month = "Mar"; $day = $mday; }
  elsif( $mon == 3 )  { $month = "Apr"; $day = $mday; }
  elsif( $mon == 4 )  { $month = "May"; $day = $mday; }
  elsif( $mon == 5 )  { $month = "Jun"; $day = $mday; }
  elsif( $mon == 6 )  { $month = "Jul"; $day = $mday; }
  elsif( $mon == 7 )  { $month = "Aug"; $day = $mday; }
  elsif( $mon == 8 )  { $month = "Sep"; $day = $mday; }
  elsif( $mon == 9 )  { $month = "Oct"; $day = $mday; }
  elsif( $mon == 10 ) { $month = "Nov"; $day = $mday; }
  elsif( $mon == 11 ) { $month = "Dec"; $day = $mday; }

}

#
# Where logconv.pl resides
#
$logAnalyzerDirectory = "/opt/sun2/tools";

#
# Where to place temporary files
#
$tmpfileDirectory = "/tmp";

#
# The file containing access log entries from the previous day
#
$tmpfile = sprintf "%s/analyzer.%f.logs",$tmpfileDirectory,rand() * 1000;

#
# options passed to logconv.pl
#
$logAnalyzerOptions = " -s 125 -V -N";

#
# Where the access logs are located
#
$logDirectory = "";

#
# where the final reports are placed
#
$reportDirectory = "/opt/sun2/access-log-analysis";

#
# list of email addys that receive a copy of the report
#
$email = "";

#
# $maxAge is the maximum age of files in the
# $reportDirectory. The default is 31, change
# it for the current run with --maxAge <ageInDays>
#
$maxAge = 31;

#
# $conv2csv is a flag indicating whether the 
# report file is converted to CSV. A value of
# non-zero means create a CSV file.
#
$conv2csv = 1;

$sn = 0;
while ($sn < $#ARGV) {
  if ("$ARGV[$sn]" eq "-d") {
    $logDirectory = $ARGV[++$sn];
  } elsif ("$ARGV[$sn]" eq "--conv2csv") {
    $conv2csv = 1;
  } elsif ("$ARGV[$sn]" eq "--maxAge") {
    $maxAge = $ARGV[++$sn];
    if($maxAge<=0) { $maxAge = 31; }
  } elsif ("$ARGV[$sn]" eq "-a") {
    $logAnalyzerDirectory = $ARGV[++$sn];
  } elsif ("$ARGV[$sn]" eq "-r") {
    $reportDirectory = $ARGV[++$sn];
  } elsif ("$ARGV[$sn]" eq "-m") {
    $email = $ARGV[++$sn];
  } elsif ("$ARGV[$sn]" eq "-i") {
    $instance = $ARGV[++$sn];
  } else {
    ++$sn;
  }
}

if( length( $logDirectory ) == 0 ) {
  printf "specify the directory for the logs with -d and try again.\n";
  exit;
}

if( length( $reportDirectory ) == 0 ) {
  printf "empty report directory not allowed.\n";
  printf "specify the directory for the report with -r and try again.\n";
  exit;
}

#
# construct the full path to logconv.pl
#
$logAnalyzer = $logAnalyzerDirectory . "/logconv.pl";

# construct path to access.pl
$accessTool = $logAnalyzerDirectory . "/access.pl";

#
# construct the full path to logconv2csv.awk
#
$logconv2csv = $logAnalyzerDirectory . "/logconv2csv.awk";

#
# make the report directory 
#
`mkdir -p $reportDirectory > /dev/null 2>&1`;

#
# construct a filter for grep
#
$filter = sprintf "$mday/$month/$year:%2.2d:",$hour;

#
# remove older files
#
`find $reportDirectory -type f -ctime +${maxAge} | xargs /bin/rm -f`;

#
# Construct a file $tmpfile containing access log entries from the previous day
#
$command = "fgrep -h $filter $logDirectory/access $logDirectory/access.2[0-9][0-9][0-9]* > $tmpfile 2>/dev/null";
`$command`;

#
# Construct filenames for the logconv.pl output and the logconv2csv.awk output
#
$hostname = `uname -n`;
chop($hostname);
$reportFile       = sprintf "%s/logconv.%s.%s.%d-%s-%d-%2.2d.out",$reportDirectory,$hostname,$instance,$day,$month,$year,$hour;
$reportFileCSV    = sprintf "%s/logconv.%s.%s.%d-%s-%d-%2.2d.csv",$reportDirectory,$hostname,$instance,$day,$month,$year,$hour;
$opsPerSecondFile = sprintf "%s/opsPerSecond.%s.%s.%d-%s-%d-%2.2d.txt",$reportDirectory,$hostname,$instance,$day,$month,$year,$hour;

#
# execute the log analysis perl script logconv.pl
#
`$logAnalyzer $logAnalyzerOptions $tmpfile > $reportFile`;

#
# calculate ops per second
#
`$accessTool $tmpfile > $opsPerSecondFile`;

#
# Convert to CSV
#
if($conv2csv != 0) {
  `$logconv2csv < $reportFile > $reportFileCSV`;
}

#
# remove the tmpfile
#
`rm -f $tmpfile`;

if( length( $email ) > 0 ) {
  `mailx -s $hostname-$reportFile $email < $reportFile`;
  `mailx -s $hostname-$reportFileCSV $email < $reportFileCSV`;
  `mailx -s $hostname-$opsPerSecondFile $email < $opsPerSecondFile`;
}
