首页 > 代码库 > 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.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‘] = 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 ok‘32 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)+‘.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_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
添加user中的media
在action中添加触发器
zabbix的邮件报警
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。