zabbix的邮件报警

时间:2021-05-01 22:30:57

邮件报警插件

 #!/usr/bin/python
#coding:utf-8 import smtplib
from email.MIMEText import MIMEText
import os
import argparse
import logging
import datetime mail_host='smtp.qq.com'
mail_usr=''
mail_pass=''
mail_postfix='qq.com' def send_mail(mail_to,subject,content):
me = mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = me
msg['to'] = mail_to
global sendstatus
global senderr try:
smtp = smtplib.SMTP()
smtp.connect(mail_host)
smtp.login(mail_user,mail_pass)
smtp.sendmail(me,mail_to,msg,as_string())
smtp.close()
print 'send ok'
sendstatus = True
except Exception,e:
senderr=str(e)
print senderr
senstatus = False def logwrite(sendstatus,mail_to,content):
logpath = '/var/log/zabbix_log/' if not sendstatus:
content = senderr if not os.path.isdir(logpath):
os.makedirs(logpath) t=datetime.datetime.now()
daytime=t.strftime('%Y-%m-%d')
daylogfile=logpath+'/'+str(daytime)+'.log'
logging.basicConfig(filename=daylogfile,level=logging.DEBUG)
logging.info('*'*130)
logging.debug(str(t)+'mail send to {0},content is : \n {1}',format(mail_to,content)) if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Send mail to user for zabbix ')
parser.add_argument('mail_to',action="store",help='The address of E-mail that send to user')
parser.add_argument('subject',action="store",help='The subject of E-mail')
parser.add_argument('content',action="store",help='The content of the Email')
args = parser.parse_args()
mail_to=args.mail_to
subject=args.subject
content=args.content send_mail(mail_to,subject,content)
logwrite(sendstatus,mail_to,content)

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

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

添加media type

zabbix的邮件报警

添加user中的media

zabbix的邮件报警

在action中添加触发器

zabbix的邮件报警

zabbix的邮件报警

zabbix的邮件报警

zabbix的邮件报警