Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Search logs with crontab

by VincentK (Beadle)
on Nov 19, 2013 at 17:03 UTC ( #1063361=note: print w/ replies, xml ) Need Help??


in reply to Search logs with crontab

This is overkill, but I think there are some pieces here that you can use. I used temporary and semaphore files.

use strict; use warnings; use Fcntl qw(:flock); use Time::localtime; $| = 1; my $logFile = "mainLog.LOG"; my $SEMAPHORE = '~'. $logFile . '.lck'; my $was_awaiting = 0; sub WRITE_TO_LOGFILE(); sub READ_LOGFILE_FOR_SENDMAIL (); sub IS_FILE_LOCKED($); sub TIMESTAMP (); ################################################################### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ################################################################### print "\nWRITE TO LOGFILE .... \n"; WRITE_TO_LOGFILE (); print "\nWRITE TO LOGFILE .... [DONE]\n"; print "\nREAD LOGFILE FOR SENDMAIL .... \n"; READ_LOGFILE_FOR_SENDMAIL (); print "\nREAD LOGFILE FOR SENDMAIL .... [DONE]\n"; ################################################################### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ################################################################### sub WRITE_TO_LOGFILE () { open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; print "[Semaphore open]\n"; while ( IS_FILE_LOCKED($SEMAPHORE) ) { print "\r\t[Awaiting Semaphore lock]"; $was_awaiting = 1; } if ($was_awaiting) { print "\n"; $was_awaiting = 0; } print "\t[Semaphore lock] \n"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; print "\t\t[Logfile open]\n"; open (FH, ">>$logFile") or die "Can't open $logFile: $!"; print FH "I have written from PID ($$)::[".TIMESTAMP() +."]::PHY-4-EXCESSIVE_ERRORS\n"; close FH; print "\t\t[Logfile closed]\n"; print "\t\tGoing to sleep...\n"; sleep 10; print "\t\tWoken up...\n"; close S; print "[Semaphore unlock]\n"; print "[Semaphore closed]\n"; unlink($SEMAPHORE); } sub READ_LOGFILE_FOR_SENDMAIL () { open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; print "[Semaphore open]\n"; while ( IS_FILE_LOCKED($SEMAPHORE) ) { print "\r\t[Awaiting Semaphore lock]"; $was_awaiting = 1; } if ($was_awaiting) { print "\n"; $was_awaiting = 0; } print "\t[Semaphore lock] \n"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; print "\t\t[Logfile open]\n"; open (FH, "<$logFile") or die "Can't open $logFile: $!"; open (FHT, ">", '~'."$logFile") or die "Can't open temp $l +ogFile: $!"; while(<FH>) { chomp; if ($_ =~ m/PHY-4-EXCESSIVE_ERRORS/) { ## SENDMAIL() $_ =~ s/PHY-4-EXCESSIVE_ERRORS/PHY_4_EXCESSIVE +-ERRORS -- SENDMAIL SENT/g; print FHT "$_\n"; } else { print FHT "$_\n"; } } close FH; close FHT; unlink($logFile); ## DELETE PREVIOUS LOGFILE rename('~'."$logFile",$logFile); ## RENAME TEMP LOG +FILE TO REPLACE PREVIOUS LOG FILE print "\t\t[Logfile closed]\n"; close S; print "[Semaphore unlock]\n"; print "[Semaphore closed]\n"; unlink($SEMAPHORE); } sub IS_FILE_LOCKED ($) { my $theFile; my $theRC; ($theFile) = @_; $theRC = open(my $HANDLE, ">>", $theFile); $theRC = flock($HANDLE, LOCK_EX|LOCK_NB); close($HANDLE); return !$theRC; } sub TIMESTAMP () { my $t = localtime; return sprintf( "%04d-%02d-%02d_%02d-%02d-%02d", $t->year + 1900, $t->mon + 1, $t->mday, $t->hour, $t->min, $t->sec ); } __END__ -RUNTIME- C:\monks>perl loggie.pl WRITE TO LOGFILE .... [Semaphore open] [Semaphore lock] [Logfile open] [Logfile closed] Going to sleep... Woken up... [Semaphore unlock] [Semaphore closed] WRITE TO LOGFILE .... [DONE] READ LOGFILE FOR SENDMAIL .... [Semaphore open] [Semaphore lock] [Logfile open] [Logfile closed] [Semaphore unlock] [Semaphore closed] READ LOGFILE FOR SENDMAIL .... [DONE] C:\monks> - Log File - I have written from PID (7892)::[2013-11-19_11-52-41]::PHY_4_EXCES +SIVE-ERRORS -- SENDMAIL SENT I have written from PID (7452)::[2013-11-19_11-53-33]::PHY_4_EXCES +SIVE-ERRORS -- SENDMAIL SENT


Comment on Re: Search logs with crontab
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063361]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-12-28 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls