zabbix使用python mail报警

zabbix使用python mail报警

zabbix使用python mail报警

zabbix使用python mail报警

前提:

好久不弄zabbix了,刚换工作,我司运维情况还在百废待兴,在zabiix使用邮件报警的适合我们使用python写得邮件报警,以前使用的是shell编写的脚本.之前的邮件报警和配置可以参考我以前写得文章.

具体相关配置请参考我以前写得文章:

http://opsnotes.net/2013/12/31/install_zabbix_06/
http://opsnotes.net/2014/01/21/install_zabbix_07/

如下是python mail脚本文件:

    root@dgz-zabbixserver01:/usr/lib/zabbix/alertscripts# cat mail.py
    #!/usr/bin/env python
    #coding:utf-8

    import smtplib
    from email.mime.text import MIMEText
    import sys

    mail_host = 'smtp.qq.com'
    mail_user = 'username'
    mail_pass = 'passwd'
    mail_postfix = 'qq.com'

    def send_mail(to_list,subject,content):
        me = mail_user+"<"+mail_user+"@"+mail_postfix+">"
        msg = MIMEText(content)
        msg['Subject'] = subject
        msg['From'] = me
        msg['to'] = to_list

        try:
            s = smtplib.SMTP()
            s.connect(mail_host)
            s.login(mail_user,mail_pass)
            s.sendmail(me,to_list,msg.as_string())
            s.close()
            return True
        except Exception,e:
            print str(e)
            return False

    if __name__ == "__main__":
        send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
    root@dgz-zabbixserver01:/usr/lib/zabbix/alertscripts#
zabbix 

See also