首页 > 其他 > 详细

zabbix的邮件报警

时间:2014-11-13 10:31:36      阅读:363      评论:0      收藏:0      [点我收藏+]

邮件报警插件

 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 datetime
10 
11 mail_host=smtp.qq.com
12 mail_usr=3049774387
13 mail_pass=123456
14 mail_postfix=qq.com
15 
16 def send_mail(mail_to,subject,content):
17     me = mail_user+"<"+mail_user+"@"+mail_postfix+">"
18     msg = MIMEText(content)
19     msg[Subject] = subject
20     msg[From] = me
21     msg[to] = mail_to
22     global sendstatus
23     global senderr
24 
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 ok
32         sendstatus = True
33     except Exception,e:
34         senderr=str(e)
35         print senderr
36         senstatus = False
37 
38 def logwrite(sendstatus,mail_to,content):
39     logpath = /var/log/zabbix_log/
40 
41     if not sendstatus:
42         content = senderr
43 
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)+.log
50         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_to
61             subject=args.subject
62             content=args.content
63 
64             send_mail(mail_to,subject,content)
65             logwrite(sendstatus,mail_to,content)

 

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

chmod 700 zabbix_sendmail.py
chown zabbix:zabbix zabbix_sendmail.py

 添加media type

bubuko.com,布布扣

添加user中的media

bubuko.com,布布扣

在action中添加触发器

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

zabbix的邮件报警

原文:http://www.cnblogs.com/cornell/p/4094167.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!