The current company Nagios has been normal use, can also alarm, but the mail alarm is sendmail, the senderalways [email protected], received after often added to the mail in the Trash box , and some mailbox servers have anti-spam detection, which causes users to not receive mail. To solve this problem, decide to write a mail-sending program yourself. The process is as follows:
1, developing the Send mail script
[[email protected] python]# cat sendmail.py#!/usr/bin/python#-*-coding:utf-8-*-import smtplibimport sysfrom Email.mime.text Import Mimetextimport time# Gets the current time Current_time=time.strftime ('%y-%m-%d%h:%m ', Time.localtime ( Time.time ())) #获取用户名密码登陆远程再也箱服务器mail_host = ' smtp.exmail.qq.com ' mail_user = ' [email protected] ' mail_pwd = ' 12345 ' #此处密码为假def send_email (content, mailto, get_sub): #获取邮件内容, followed by support for Chinese msg = mimetext (content,_subtype= ' plain ', _char set= ' gb2312 ') #获取收件人, with the title msg[' from '] = mail_user msg[' Subject '] =get_sub msg[' to '] = ",". Join (mailto) Try: #连接上远程邮件发送服务器 s = smtplib. Smtp_ssl (Mail_host, 465) #登陆 S.login (Mail_user, mail_pwd) #发送邮件 S.sendmail (Mail_user, mailto, msg.as_string ()) S.close () except Exception as E:print ' Exception: ', e# the first message header TITLE=SYS.ARGV [1] #传参第二个为邮件内容, here defines the content format cont= ""----------------------------------| Cloud Nest operation and maintenance management platform----------------------------------| Alarm information:%s----------------------------------| Send time:%s----------------------------------""% (sys.argv[2],current_time) #指定收件人to_list = [# ' [email protected ] ', # ' [email protected] ', # ' [email protected] ', ' [email protected] ',] #执行上述类 Send_email (cont, to_list, title)
2, defineNagiosexecute a command to send a message
edit The files of the Nagios Definition command and log off the original send Mail command. Added as follows:
#vi/usr/local/nagios/etc/objects/commands.cfg
Define Command{
Command_name Notify-host-by-email
command_line/usr/bin/python/python/sendmail.py "$HOSTNAME $_$hostaddress$ $NOTIFICATIONTYPE $, $HOSTNAME $ is $ hoststate$ "" $HOSTNAME $ $HOSTADDRESS $ is$hoststate$, $HOSTOUTPUT $. "
}
Define Command{
Command_name Notify-service-by-email
command_line/usr/bin/python/python/sendmail.py "$HOSTNAME $_$hostaddress$ service is $SERVICESTATE $" "$HOSTALIAS $_$ Hostaddress$state is $SERVICESTATE $, $SERVICEOUTPUT $. "
#执行这个程序, the host name alarm information can be passed in.
}
Restart Nagios
#/etc/init.d/nagios Chekconfig
#/etc/init.d/nagios restart
3, mail alarm phenomenon
down a 192.167.6.136 in Nagios monitoring , the alarm information is as follows:
Note: template content can be changed on its own.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/26/44/wKiom1NrFouitEGNAAINglHSfec659.jpg "title=" l~t%0 4A) 2SQ) ' 28[p%905y.jpg ' alt= "wkiom1nrfouitegnaainglhsfec659.jpg"/>
This article from "This peace of mind is my Hometown" blog, reproduced please contact the author!