open(.....) or Error("Didn't want to open: $!\n","ERROR") && die;
my Error sub would send out an email alert depending if it's a Warning
or Error and write to a logs file.
Thanks a lot ebm.
my Error sub would send out an email alert [..]
ooo..ahhhh....I like that.
My original idea was to create a hash or array with a pile of error
messages, reference the messages to the real $! error, and send that
variable value as a subject/body in an email.
But I'd like to see how your idea ties in all together. It seems
simpler.
Do you have that error subroutine you mention?
Thanks again.
This should set you up.
enjoy!
use Net::SMTP;
use Time::Local;
use File:
ath;
use Sys::Hostname;
$critical_email = '(e-mail address removed)';
$warning_email = '(e-mail address removed)';
$smtp='mail.example.com';
$from_email='(e-mail address removed)';
=pod
=item error($errorType, $subject, $description)
Checks to see if it's a criticality and send email message
Input: Error Type, Subject of Error, Description of Error
Return: True (1), False (0)
error("CRITICAL","Some Subject","Details");
error("WARNING","Some Subject","Details");
=cut
sub error{
my ($type,$event,$description) = @_;
chomp ($type, $event, $description);
logEvent("");
logEvent("$type\t$event \t$description\tServer: ".hostname);
print STDERR "$type\t$event \t$description\tServer: ".hostname;
if(uc($type) eq "CRITICAL"){
alertEmail( $critical_email,"LogMover CRITICAL \n$event","CRITICAL
$description\nServer: ".hostname);
return 1;
}elsif(uc($type) eq "WARNING"){
alertEmail($warning_email,"LogMover WARNING \n$event", "WARNING
$description\nServer: ".hostname);
return 1;
}
return 0;
}
=pod
=item checkPath($path)
check that the path exists, Return boolean
if( checkPath( $path ) ){ print "path exists" }
=cut
sub checkPath{
my($path) = @_; # directory path
#check the path exists
if( -e $path ){
return 1;
}
return 0;
}
=pod
=item logEvent($event)
Log an event to the harvester log, this will request the time for each
entry
Input: Event String
Return: True (1)
logEvent("Something to log");
=cut
sub logEvent{
my ($event) = @_; # event line for the log
chomp($event);
#setup log dir
checkPath("Logs") or mkpath("Logs");
my($month, $day, $year, $hrminsec) = todayDate(0);
my $LOG = 'Logs/'. $year.$month.$day . '.log';
open(LOG,">>$LOG" ) ||
die("CRITICAL". "unable to open $LOG","unable to open $LOG
$!");
print(LOG "$month/$day/$year $hrminsec\t ". $event . "\n");
close(LOG);
return 1;
}
=pod
=item alertEmail($toAddressFromConfig, $subject, $messageBody)
Email error message
Input: Address to send message to, Subject, Description of email
Return: N/A
alertEmail('(e-mail address removed)',"Some Subject","Details");
=cut
sub alertEmail{
my ($to,$subject,$messageBody) = @_;
chomp($to, $subject, $messageBody );
my $smtp = Net::SMTP->new($smtp, Debug => 0 );
$smtp->mail($from_email});
$smtp->to($to);
$smtp->datasend("\r\n");
$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: ".$from_email."\n");
$smtp->datasend("Subject: $subject\r\n");
$smtp->datasend("\r\n");
$smtp->datasend("$messageBody\r\n");
$smtp->data();
$smtp->dataend();
$smtp->quit();
return 1;
}