Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use IO::Handle; use File::stat; $sleep = 1; # time to sleep between re-sampling file $counter = 0; while ( $_ = shift(@ARGV)) { if ( /^-f/ ) { $file = shift (@ARGV); } # log file to monitor if ( /^-m/ ) { $max = shift (@ARGV); } # ceiling limit to delt +a update rates if ( /^-r/ ) { $max_exceeds = shift (@ARGV); } # number of tim +es delta ceiling limit is ignored until being reported, optional if ( /^-l/ ) { $log = shift (@ARGV); } # log file to write to, + optional if ( /^-c/ ) { $config = shift (@ARGV); } # use config file } if ( $config ) { open (CFG, "$config") or die "Cannot open config file $config: +$!\n"; while (<CFG>) { chomp; s/#.//; s/^\s+//; s/\s+$//; next unless length; my ($var, $value) = split (/\s*=\s*/, $_, 2); no strict 'refs'; $$var = $value; } close (CFG); } unless ( $max ) { $max = 1500; } # default limit for delta unless ( $limit ) { $limit = 300; } # default to 5 minutes unless ( $max_exceeds ) { $max_exceeds = 5; } # default limit if ( $log ) { open (STDOUT, "> $log") or die "Cannot write to $log:$!\n"; STDOUT->autoflush(1); } if ( $verbose and $config ) { print "Using configuration file $config\ +n"; } if ( $verbose ) { print "delta limit set to $max updates per sample pe +riod (30 seconds)\ndelta exceed timer set to $ limit seconds\nmaximum number of exceeded delta's before reporting is +$max_exceeds\n"; } open (FILE, "$file") or die "Cannot open file $file: $!\n"; for (;;) { while (<FILE>) { if ( $_ =~ "Delta" ) { @line = split, $_; if ( $verbose ) { print "\nDelta found:\n@line +\n"; } $delta = $line[4]; if ( $delta >= $max ) { if ( $verbose ) { print "Current delta + $delta exceeding $max and counter is $counter \n"; } $now_time = time; # if start time not set or zero, set t +ime and alarm if ( ! $s_time or $s_time == 0 ) { $s_time = time; $alarm = $s_time + $limit; if ( $verbose ) { print "Alarm + set to $alarm, current time $s_time\n"; } $counter=0; } # if counter is greater than max_excee +ds and the current time is <= alarm elsif ( $counter >= $max_exceeds and $ +now_time <= $alarm ) { print "$delta exceeds threshol +d of $max $counter times\n"; $s_time=0; $alarm=0; $counter=0; } # if current time >= alarm reset some +variables back to 0, as alarm exceeded elsif ( $now_time >= $alarm ) { if ( $verbose ) { print "Alarm + timer exceeded.....resetting counter, time an d alarms\n"; } $counter=0; $alarm=0; $s_time=0; } else { $counter++; } } } } sleep $sleep; last if stat(*FILE)->nlink == 0; FILE->clearerr(); } close (FILE); if ( $log ) { close (STDOUT); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Tailing rolling logs
by merlyn (Sage) on Jan 30, 2004 at 12:08 UTC | |
by RMGir (Prior) on Jan 30, 2004 at 13:17 UTC | |
by Anonymous Monk on Jan 30, 2004 at 13:08 UTC | |
by merlyn (Sage) on Jan 30, 2004 at 13:43 UTC | |
by coec (Chaplain) on Jan 30, 2004 at 12:45 UTC | |
by hmerrill (Friar) on Jan 30, 2004 at 12:59 UTC | |
by merlyn (Sage) on Jan 30, 2004 at 13:40 UTC | |
|
Re: Tailing rolling logs
by coec (Chaplain) on Jan 30, 2004 at 10:16 UTC | |
by Anonymous Monk on Jan 30, 2004 at 10:32 UTC | |
by pelagic (Priest) on Jan 30, 2004 at 10:48 UTC |