nagios郵件警示指令碼

來源:互聯網
上載者:User

標籤:nagios   監控系統   郵件警示   

目的:主要作用於nagios監控系統郵件警示功能,也可以稍作修改使用於其他地方;

使用方法:

perl notify_service_by_mail.pl -t connact -s subject -m messageperl notify_host_by_mail.pl -t connact -s subject -m message

服務警示代碼:

#!/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; }

主機警示代碼:

#!/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; }

警示結果:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/F1/wKiom1WMJnCDljSQAAEcY6hFCkc070.jpg" title="1.jpg" alt="wKiom1WMJnCDljSQAAEcY6hFCkc070.jpg" />

本文出自 “大風” 部落格,請務必保留此出處http://lansgg.blog.51cto.com/5675165/1665760

nagios郵件警示指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.