For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > Re: using perl grep to read a log file -









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: using perl grep to read a log file -
seane

2004-08-03, 9:02 am

OK you ever spell check a document and you are so far off that spell
check has no idea what you are trying to spell????

That's what I feel like here.. Am I that far off base?
What I have here works it's just not exactly what I want...

At the end of the script running; I basically want to know if the
agent is still connected or has it disconnected, sometimes it will
drop connection but then reconnect on it's own and each event will
write to the log.

Any pointers/suggestions would be appreciated.




seane@us.ibm.com (seane) wrote in message news:<6634fdf1.0407281010.2f825f8b@posting.google.com>...
> I read this log file twice a day using cron. Normally I should
> see one "connected" message when everything is working and if so exit
> the script. If not send an email to myself and then exit. Sometimes I
> get a following disconnected message and I get sent an email if that
> occurs. However sometimes I get a following connected message after
> the first disconnected message then I am still ok. How can I read this
> log and only get sent an email if the disconnected message is the last
> string found or if no connected string was found at all?
>
>
> I have a log file containing:


> Fri Jul 23 13:53:54 2004: Agent is now connected.
> Fri Jul 23 13:54:54 2004: Agent is now disconnected.
> Fri Jul 23 13:55:54 2004: Agent is now connected.
> Fri Jul 23 13:56:54 2004: Agent is now disconnected.
>
> I have this code:


> #!/usr/bin/perl -w
> $filein=("logfile");
> open (LOG, "<$filein");
> @logarray=<LOG>;
> #######################################
####################################
> print"starting the log search for connected\n\n";
> ########################################
###################################
> $lookfor=("Agent is now connected");
>
> @match=grep{/$lookfor/}@logarray;
> if (@match) {
> foreach(@match)
> {
> print ;
> }
> }
> else
> {
> print"$lookfor was not found, an email has been sent.\n\n";
> # here is where I send myself the email- same as below.
> exit(1);
> }
> print" Going to next step\n\n";
>
> #######################################
####################################
> print"STARTING THE SEARCH FOR DISCONNECTED MESSAGE\n\n";
> ########################################
###################################
> $lookfor=("Agent is now disconnected");
>
> @match=grep{/$lookfor/}@logarray;
> if (@match) {
> foreach(@match) {
> # system("echo $lookfor was found. |mail -s \"$0 - $sysName\"
> user\@network.com");
> print"$lookfor was found, an email has been sent.\n";
> exit(1);
> }
> }
>
> print"EVERYTHING RAN OK\n\n";
>
> exit(0);

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com