I'm very new to Perl, learning as I go, but I've hit a roadblock and I'm not sure if the problem is with my logic or simple lack of experience in Perl--or both.
Basically, the script opens an error log, reads through each line looking for a specific error message, and then sends an email if the error message exists. That much I have working. But it gets complicated because the plan is to have the script run every X minutes to search for new error messages and ignore past messages.
What I wish to do is pull any error message that occurs on today's date and send an email describing the error. This wouldn't be a problem except that I don't want the script to pick up error messages that occured in the past (after the message was sent) and continue sending messages--the error log could possibly be reporting the same error several times a minute. Further, once the problem is resolved, I don't want the script to continue sending messages because it still sees today's date and the error text in the log file. But I do want it to continue searching for future errors every time it runs.
I've tried using Add_Delta_DHMS to only report errors that occured X minutes ago, but that does no good because what I need is it to look at the range of minutes in between as well.
Here's the code as the script currently exists:
open(PCC_ERR, "ErrLog.PCC") or die "Cannot open ErrLog.PCC"; #PCC_ERR
+ is the filehandle. Opens error log
use Date::Calc(Today); # needed for calculating 'friendly' dates
($year, $month, $day) = Today;
# Go through each line of the error log and only report if a line cont
+ains today's date and error codes 3022 or 3186
for $line (<PCC_ERR>) {
if ($line =~ /$month-$day-$year/ && $line =~/3022|3186/) {
use Net::SMTP; # If the condition is met, send an email with err
+or message
$smtp = Net::SMTP->new('smtp.server'); # connect to an SMTP se
+rver
$smtp->mail('email.address'); # use the sender's address h
+ere
$smtp->to('recipient.address'); # recipient's address
$smtp->data(); # Start the email
$smtp->datasend("To: recipient.address\n");
$smtp->datasend("From: email.address\n");
$smtp->datasend("\n");
$smtp->datasend("Error Report for $month-$day-$year: Errors Fo
+und on Server!\n");
$smtp->dataend(); # Finish sending the mail
$smtp->quit; # Close the SMTP connectio
+n
last; # break loop if error found
}
}
close(PCC_ERR);
I'm hoping someone can help me with the logic if not the code, because I feel like I'm just missing something or making it harder than it is. Thanks!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.