#!/usr/bin/perl -w # # RCS ID: $Id: scan_historylog.pl,v 1.5 2003/04/02 22:47:44 rolf Exp $ # # This script will parse the Urchin 4 scheduler history file for errors # for a particular date (default today) and print a notification if # any profile exits with a non-zero exit status. If desired, the runtime # output from each failed task can be printed inline. # # Usage: scan_historylog.pl [--date CCYYMMDD] [--urchinpath /path/to/urchin] # [--showrunlog] [--summary] [--warn] | [--help] # # Where: # --help shows this message # --urchinpath specifies the path of the Urchin 4 distribution # Default: /usr/local/urchin4 # --date specifies the date of tasks to look for in CCYYMMDD format # Default: today # --showrunlog causes the runtime log for profiles with errors to be # printed along with the error notification # Default: disabled # --summary causes the total number of profiles processed for the # requested date to be printed # --warn causes a warning to be printed if no history entries are # found with the date requested. # Default: off # # Exit status codes: # 0: Profiles processed for the given date, but no errors detected # 1: No profiles processed for the given date # 2: Invalid command line or other fatal error encountered # 3: Errors found for profiles processed on the given date # # Copyright (c) 2002, Urchin Software Corporation. use strict; use Getopt::Long; # Initialize variables my $help = 0; my $gstatus = 0; my $date = "unset"; my $showrunlog = 0; my $summary = 0; my $themonth; my $urchinpath = "/usr/local/urchin"; my $historylog; my $logfileline; my $starttime; my $endtime; my @LOGENTRIES = (); my $profile_name; my $profile_id; my $task_id; my $gid; my $completion_percent; my $begin_time; my $end_time; my $processing_time; my $bytes_processed; my $do_warn = 0; my $lines_processed; my $hits_processed; my $exit_status; my $exit_code; my $stat; my $profile; my $joberrors = 0; my $jobsfound = 0; my ($jobstarttime, $jobdate, $jobday, $jobmonth, $jobyear); # Set output to flush after each print line $| = 1; # Read in the options $gstatus = GetOptions('urchinpath=s' => \$urchinpath, 'date=s' => \$date, 'showrunlog' => \$showrunlog, 'summary' => \$summary, 'warn' => \$do_warn, 'help' => \$help); if ($help) { &Usage; exit(0); } if ($gstatus ne 1) { &Usage; exit(2); } # Calculate the date string in CCYYMMDD format for 'yesterday' my ($day, $month, $year) = (localtime(time()))[3,4,5]; my $today = ($year + 1900)*10000 + ($month + 1)*100 + $day; if ($date eq "unset") { $themonth = substr($today,0,6); $date = $today; } else { $themonth = substr($date,0,6); } $historylog = "$urchinpath/history/UT_$themonth.log"; # Open remote host config file and loop through each configured host:log # entry and go grab the log. open(URCHINDLOG, $historylog) || die "FATAL error: can't open history log file $historylog\n"; @LOGENTRIES = ; foreach $logfileline (@LOGENTRIES){ if (substr($logfileline,0,1) eq '#') { next }; chop($logfileline); # Determine if we're looking at a history entry generated by Urchin 4.0xxx # or Urchin 4.1xxx if ( index($logfileline,"ct_gid") eq -1 ) { ($profile_name, $profile_id, $task_id, $completion_percent, $begin_time, $end_time, $processing_time, $bytes_processed, $lines_processed, $hits_processed, $exit_status, $exit_code) = split(/\&/, $logfileline); if (!defined($profile_name) || !defined($profile_id) || !defined($task_id) || !defined($completion_percent) || !defined($begin_time) || !defined($end_time) || !defined($processing_time) || !defined($bytes_processed) || !defined($lines_processed) || !defined($hits_processed) || !defined($exit_status) || !defined($exit_code)) { print "+ ERROR: bad line in history file, skipping it: \'$logfileline\'\n"; print "###############################################################################\n"; next; } } else { ($profile_name, $profile_id, $task_id, $gid, $completion_percent, $begin_time, $end_time, $processing_time, $bytes_processed, $lines_processed, $hits_processed, $exit_status, $exit_code) = split(/\&/, $logfileline); if (!defined($profile_name) || !defined($profile_id) || !defined($task_id) || !defined ($gid) || !defined($completion_percent) || !defined($begin_time) || !defined($end_time) || !defined($processing_time) || !defined($bytes_processed) || !defined($lines_processed) || !defined($hits_processed) || !defined($exit_status) || !defined($exit_code)) { print "+ ERROR: bad line in history file, skipping it: \'$logfileline\'\n"; print "###############################################################################\n"; next; } } # Calculate the date string in CCYYMMDD format for this job entry $jobstarttime = substr($begin_time,rindex($begin_time,"=")+1); ($jobday, $jobmonth, $jobyear) = (localtime($jobstarttime))[3,4,5]; $jobdate = ($jobyear + 1900)*10000 + ($jobmonth + 1)*100 + $jobday; $stat = substr($exit_status,rindex($exit_status,"=")+1); if ($jobdate eq $date) {$jobsfound ++}; # Uncomment for debugging #$profile = substr($profile_name,rindex($profile_name,"=")+1); #printf("Profile: %s processed on %s\n",$profile,$jobdate); if ($jobdate eq $date && $stat ne 0) { $joberrors = 1; $profile = substr($profile_name,rindex($profile_name,"=")+1); if ( ! $showrunlog ) { print "Failed execution: profile \'$profile\'; exit status: $stat\n"; } else { print "###############################################################################\n"; print "##### FAILED EXECUTION: PROFILE \'$profile\'; EXIT STATUS: $stat\n"; print "##### =====> RUNTIME LOG FOLLOWS <=====\n"; print "###############################################################################\n"; system("cat $urchinpath/data/history/$profile/$jobstarttime.log"); print "###############################################################################\n"; } } } close(URCHINDLOG); # Exit with a status of 1 if no profile runs were found for the specified date. if ($jobsfound eq 0) { if ($do_warn) { print ("+ WARNING: No profiles run on $date\n"); } exit(1); } if ($summary) { print ("Total Profiles processed on $date: $jobsfound\n"); } # Exit with a status of 3 if errors were found in the profile runs if ($joberrors) { exit(3); } # All's well that ends well. exit(0); ############################################################################# ## SUBROUTINES ############################################################################# # Subroutine to show the usage of this script sub Usage { print "Usage: $0 [--date CCYYMMDD] [--urchinpath /path/to/urchin] [--showrunlog] [--summary] [--warn] | [--help] Where: --help shows this message --urchinpath specifies the path of the Urchin 4 distribution Default: /usr/local/urchin4 --date specifies the date of tasks to look for in CCYYMMDD format Default: today --showrunlog causes the runtime log for profiles with errors to be printed along with the error notification Default: disabled --summary causes the total number of profiles processed for the requested date to be printed --warn causes a warning to be printed if no history entries are found with the date requested. Default: off\n\n"; }