using perl grep to read a log file - need some sort of loop
From: seane (seane_at_us.ibm.com)
Date: 07/28/04
- Next message: Jeff 'Japhy' Pinyan: "Re: sort files by extension"
- Previous message: James Edward Gray II: "Re: sort files by extension"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Jul 2004 11:10:22 -0700
I read this log file twice a day using cron. Normally I should just
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);
- Next message: Jeff 'Japhy' Pinyan: "Re: sort files by extension"
- Previous message: James Edward Gray II: "Re: sort files by extension"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|