首页 > 代码库 > zabbix的邮件报警

zabbix的邮件报警

邮件报警插件

 1 #!/usr/bin/python 2 #coding:utf-8 3  4 import smtplib 5 from email.MIMEText import MIMEText 6 import os 7 import argparse 8 import logging 9 import datetime10 11 mail_host=smtp.qq.com12 mail_usr=304977438713 mail_pass=12345614 mail_postfix=qq.com15 16 def send_mail(mail_to,subject,content):17     me = mail_user+"<"+mail_user+"@"+mail_postfix+">"18     msg = MIMEText(content)19     msg[Subject] = subject20     msg[From] = me21     msg[to] = mail_to22     global sendstatus23     global senderr24 25     try:26         smtp = smtplib.SMTP()27         smtp.connect(mail_host)28         smtp.login(mail_user,mail_pass)29         smtp.sendmail(me,mail_to,msg,as_string())30         smtp.close()31         print send ok32         sendstatus = True33     except Exception,e:34         senderr=str(e)35         print senderr36         senstatus = False37 38 def logwrite(sendstatus,mail_to,content):39     logpath = /var/log/zabbix_log/40 41     if not sendstatus:42         content = senderr43 44     if not os.path.isdir(logpath):45         os.makedirs(logpath)46 47         t=datetime.datetime.now()48         daytime=t.strftime(%Y-%m-%d)49         daylogfile=logpath+/+str(daytime)+.log50         logging.basicConfig(filename=daylogfile,level=logging.DEBUG)51         logging.info(**130)52         logging.debug(str(t)+mail send to {0},content is : \n {1},format(mail_to,content))53 54         if __name__ == "__main__":55             parser = argparse.ArgumentParser(description=Send mail to user for zabbix )56             parser.add_argument(mail_to,action="store",help=The address of E-mail that send to user)57             parser.add_argument(subject,action="store",help=The subject of E-mail)58             parser.add_argument(content,action="store",help=The content of the Email)59             args = parser.parse_args()60             mail_to=args.mail_to61             subject=args.subject62             content=args.content63 64             send_mail(mail_to,subject,content)65             logwrite(sendstatus,mail_to,content)

 

插件放在scripts目录下,centos6.4以上版本缺少python-argparse模块。

chmod 700 zabbix_sendmail.pychown zabbix:zabbix zabbix_sendmail.py

 添加media type

image

添加user中的media

QQ截图20141113093638

在action中添加触发器

image

image

22

11

zabbix的邮件报警