Objective: The main role of the Nagios monitoring System mail alarm function, but also can be slightly modified to use elsewhere;
How to use:
Perl notify_service_by_mail.pl-t connact-s subject-m messageperl notify_host_by_mail.pl-t connact-s subject-m messag E
Service Alarm Code:
#!/usr/bin/env perl#notify_service_by_mail.pluse strict;use warnings;use mail::sender;my ($CONTACTEMAIL, $Subject, $Message, $Monitor, $NOTIFICATIONTYPE, $SERVICEDESC, $HOSTALIAS, $HOSTADDRESS, $ ServiceState, $LONGDATETIME, $SERVICEOUTPUT);my ($sec, $min, $hour, $mday, $mon, $year) = ( localtime) [0..5];($sec, $min, $hour, $mday, $mon, $year) = ( sprintf ("%02d", $sec), sprintf ("%02d", $min), sprintf ("%02d", $hour), sprintf ("%02d", $mday), sprintf ("%02d", $mon + 1), $year + 1900 );my $date = "$year-$mon-$mday $hour: $min: $ SEC "; #Sendermy $send _user= ' [email protected] '; #passwd for Sendermy $send _user_pw= ' Notify_by_monitor '; #smtp addressmy $smtp _server= ' smtp.wo.cn ';while (defined (my $arg = shift)) { if ($arg eq ' t ') { $CONTACTEMAIL = shift; next; }elsif ($arg eq ' s ') { $Subject = shift; next; }elsif ($arg eq '-M ') { $Message = shift; }else { next; } if (defined $Message) { ($Monitor, $NOTIFICATIONTYPE, $SERVICEDESC, $ Hostalias, $HOSTADDRESS, $SERVICESTATE, $LONGDATETIME, $SERVICEOUTPUT) =split (/\|/, $Message); print "$Monitor \n$notificationtype\n$servicedesc\n$hostalias\n$hostaddress\ N$servicestate\n$longdatetime\n$serviceoutput "; sendtestmail (); }}sub sendTestMail { my $sender = new mail::sender{ ctype => ' Text/plain; charset=utf-8 ', encoding => ' Utf-8 ', }; #die "error in mailing : $Mail:: sender::error\n" unless ref $sender;if ($sender->mailmsg ({ smtp => $smtp _server , from => $send _user, to => $CONTACTEMAIL, subject => $Subject, msg => "$Monitor \n$notificationtype\n$servicedesc\n$hostalias\n$ Hostaddress\n$servicestate\n$longdatetime\n$serviceoutput ", auth => ' LOGIN ', authid => $send _user, authpwd => $send _user _pw, charset=> ' Utf-8 ' < 0) { warn "$Mail:: sender::error\n "; return 1; }else{$sender->close (); #print " Test mail Send to $recv _user mail sent ok.\n "; #print " $send _user to $ contactemail smtp success subject : $Subject \ n ";return 0; }}sub Usage { print "Perl notify_ Service_by_mail.pl -t cOnnact -s subject -m message "; exit 2; }
Host alarm Code:
#!/usr/bin/env perl#notify_host_by_mail.pluse strict;use warnings;use mail::sender;my ($CONTACTEMAIL, $Subject, $Message, $Monitor, $NOTIFICATIONTYPE, $HOSTNAME, $HOSTALIAS, $HOSTADDRESS, $HOSTSTATE , $LONGDATETIME, $HOSTOUTPUT);my ($sec, $min, $hour, $mday, $mon, $year) = (localtime) [0..5];($ SEC, $min, $hour, $mday, $mon, $year) = ( sprintf ("%02d", $sec), sprintf ("%02d", $min), sprintf ("%02d", $hour), sprintf (" %02d ", $mday), sprintf ("%02d ", $mon + 1), $year + 1900 );my $date = "$year-$mon-$mday $hour: $min: $sec"; sendermy $send _user= ' [email protected] '; #passwd for Sendermy $send _user_pw= ' notify_by _monitor '; #smtp addressmy $smtp _server= ' smtp.wo.cn ';while (defined (my $arg = shift)) { if ($arg eq ' t ') { $CONTACTEMAIL = shift; next; }elsif ($arg eq ' s ') { $Subject = shift; next; }elsif ($arg eq '-M ') { $Message = shift; }else { next; } if (defined $Message) { ($Monitor, $NOTIFICATIONTYPE, $HOSTNAME, $HOSTALIAS, $ Hostaddress, $HOSTSTATE, $LONGDATETIME, $HOSTOUTPUT) =split (/\|/, $Message); print "$Monitor \n$notificationtype\n$hostname\n$hostalias\n$hostaddress\n$hoststate\n$ longdatetime\n$hostoutput "; sendtestmail (); }}sub sendTestMail { my $sender = new mail::sender{ ctype => ' Text/plain; charset=utf-8 ', encoding => ' Utf-8 ', }; # die "error in mailing : $Mail:: sender::error\n" unless ref $sender; if ($sender->mailmsg ({ smtp => $smtp _server , from => $send _user, to => $CONTACTEMAIL, subject => $Subject, msg => "$Monitor \n$notificationtype\n$hostname\n$hostalias\n$hostaddress\n$hoststate\n$ Longdatetime\n$hostoutput ", auth => ' LOGIN ', authid => $send _user, authpwd => $send _user_pw, charset=> ' Utf-8 ' }) < 0) { warn "$Mail:: sender::error\n"; return 1; }else{$sender->close () #print "test mail send to $recv _user mail sent ok.\n "; #print " $send _user to $CONTACTEMAIL smtp success Subject : $Subject \ n ";return 0; }}sub usage { print "Perl notify_service_by_mail.pl -t connact -s subject -m message "; exit 2; }
Alarm Result:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/F1/wKiom1WMJnCDljSQAAEcY6hFCkc070.jpg "title=" 1.jpg " alt= "Wkiom1wmjncdljsqaaecy6hfckc070.jpg"/>
This article is from the "Big Wind" blog, please be sure to keep this source http://lansgg.blog.51cto.com/5675165/1665760
Nagios Mail Alert script