邮件报警插件
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 : {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
添加user中的media
在action中添加触发器