一、概述及环境要求 1、概述 zabbix监控也起到重要作用,以下是使用python脚本发送告警邮件配置方法。之前使用过sendemail邮件报警但是发现邮件主题为中文时候会出现乱码的问题。 2、环境安装要求 Zabbix软件版本:zabbix3.2.6 操作系统:CentOS release 6.8 (Final) 二、脚本创建及测试 1、查看配置zabbix_server.conf ,找到AlertScriptsPath,查看路径位置,如被注释,将注释打开,把send.py 文件保存到该目录下。 grep -n '^[a-Z]' /usr/local/etc/zabbix_server.conf 38:LogFile=/tmp/zabbix_server.log 87:DBName=zabbix 103:DBUser=zabbix 111:DBPassword=zabbix 126:DBPort=3306 239:StartVMwareCollectors=10 247:VMwareFrequency=60 255:VMwarePerfFrequency=60 265:VMwareCacheSize=80M 273:VMwareTimeout=10 281:SNMPTrapperFile=/tmp/zabbix_traps.tmp 289:StartSNMPTrapper=0 407:Timeout=4 448:AlertScriptsPath=/usr/local/share/zabbix/alertscripts 492:LogSlowQueries=3000 2、如有修改配置,需重启zabbix-server服务。 /etc/init.d/zabbix_server restart 3、进入Zabbix发邮件脚本目录 cd /usr/local/share/zabbix/alertscripts 4、创建脚本及配置 vim zabbix-mail.py #!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_host = 'smtp.qq.com' mail_user = '10880347' mail_pass = '**********' mail_postfix = 'qq.com' def send_mail(to_list,subject,content): me = "zabbix3.2监控告警平台"+"<"+mail_user+"@"+mail_postfix+">" msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = subject msg['From'] = me msg['to'] = to_list try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me,to_list,msg.as_string()) s.close() return True except Exception,e: print str(e) return False if __name__ == "__main__": send_mail(sys.argv[1], sys.argv[2], sys.argv[3]) 5.增加可执行权限 chmod +x zabbix-mail.py chown -R zabbix.zabbix zabbix-mail.py 6.测试邮件 python zabbix-mail.py 10880347@qq.com test test
三、ZABBIX服务器报警媒介类型 1、web界面设置 http://172.16.8.200/zabbix/ 添加三个参数 {ALERT.SENDTO}收件人地址 {ALERT.SUBJECT}主题 {ALERT.MESSAGE}详细内容
2、创建报警用户
3、配置报警媒介
4、配置权限 现在测试环境所以配置超级管理的权限,如果是生产环境建议使用用户权限
四、配置报警动作 1、创建新的触发动作
2、配置动作
3、配置操作 故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障! 告警主机:{HOSTNAME1} 告警时间:{EVENT.DATE} {EVENT.TIME} 告警等级:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1} 事件ID:{EVENT.ID}
4、配置恢复操作 恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复! 告警主机:{HOSTNAME1} 告警时间:{EVENT.DATE} {EVENT.TIME} 告警等级:{TRIGGER.SEVERITY} 告警信息: {TRIGGER.NAME} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1} 事件ID:{EVENT.ID}
5、邮件告警测试结果
对发邮件脚本进行了优化,解决outlook收邮件标题是乱码的问题。
#!/usr/bin/python #coding:utf-8 import smtplib from email.header import Header from email.mime.text import MIMEText import sys mail_host = 'mail.qq.com' mail_user = '10880347' mail_pass = '********' mail_postfix = 'qq.com' def send_mail(to_list,subject,content): #me = "某某公司zabbix监控告警"+"<"+mail_user+"@"+mail_postfix+">" me = ("%s<10880347@qq.com>")%(Header('某某公司zabbix_监控告警!','utf-8'),) msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = subject msg['From'] = me msg['to'] = to_list try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me,to_list,msg.as_string()) s.close() return True except Exception,e: print str(e) return False if __name__ == "__main__": send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
注:通过zabbix配置给多人发送邮件,貌似只能发送第一个,所以使用脚本来实现。
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import smtplib from email.mime.text import MIMEText def send_mail(sub,content): # 要发给谁 mailto_list=["test1@qq.com","test2@qq.com","test3@qq.com"] # 设置服务器,用户名、口令以及邮箱的后缀 mail_host="smtp.mail.haoyisheng.com" mail_user="admin@mail.haoyisheng.com" mail_pass="********" mail_postfix="mail.haoyisheng.com" me=mail_user+"<"+mail_user+"@"+mail_postfix+">" msg = MIMEText(content,_subtype='plain',_charset='utf-8') msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(mailto_list) try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me, mailto_list, msg.as_string()) s.close() return True except Exception, e: print str(e) return False if __name__ == '__main__': if send_mail(sys.argv[2],sys.argv[3]): print u'发送成功' else: print u'发送失败'