#!/usr/bin/env perl # Script ---------------------------------------------------------------------- # $Id: ldupes,v 1.2 2008/10/09 16:42:22 jayrfink Exp $ # # Description - Grep out one expression from a logfile and do not report dups # # Created # Author: jay.fink@gmail.com # Date: 2007/05/02 # # Last Modified # $Author: jayrfink $ # $Date: 2008/10/09 16:42:22 $ # $State: Exp $ # #------------------------------------------------------------------------------ use strict; $SIG{'INT' }='interrupt'; $SIG{'QUIT'}='interrupt'; $SIG{'TRAP'}='interrupt'; $SIG{'HUP' }='interrupt'; $SIG{'ABRT'}='interrupt'; $SIG{'STOP'}='interrupt'; my $PATTERN; my $LOGFILE; my $MAILTO="root"; sub interrupt { print "caught @_ exiting\n"; die; } #------------------------------------------------------------------------------ # Helper subroutines #------------------------------------------------------------------------------ sub load_file { my ($file) = shift; my @flist; open(FILE, $file) or die "Unable to open logfile $file: $!\n"; @flist = ; close FILE; return(@flist); } sub usage { print "$PROGRAM_NAME: [option][argument]\n"; print "$PROGRAM_NAME: [[-p \"pattern\"][-f path_to_logfile]][-u]\n"; } #------------------------------------------------------------------------------ # Main #------------------------------------------------------------------------------ while (my $opt = shift @ARGV) { $opt =~ s/--//; $opt =~ s/-//; if ($opt eq 'p') { $PATTERN = shift @ARGV; } elsif ($opt eq 'f') { $LOGFILE = shift @ARGV; } elsif ($opt eq 'u') { usage(); exit 0; } else { usage(); exit 1; } } my @log_contents = `grep \"$PATTERN\" $LOGFILE`; my @alarm_list; my $prev = " "; while (my $line = shift @log_contents) { if($line ne $prev) { push(@alarm_list, $line); } $prev=$line; } if (@alarm_list > 1) { my $tmpfile="/tmp/ezfilter.$$"; open(outfile, ">>$tmpfile"); while (my $line = shift @alarm_list) { print outfile $line; } system("cat $tmpfile | mail -s \"Invalid LSF Processes\" $MAILTO"); close(outfile); unlink($tmpfile); } exit 0;