Zabbix自动化监控QQ交流群:9888359 欢迎喜欢交流的朋友加入!
一、概述及环境要求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.conf38:LogFile=/tmp/zabbix_server.log87:DBName=zabbix103:DBUser=zabbix111:DBPassword=zabbix126:DBPort=3306239:StartVMwareCollectors=10247:VMwareFrequency=60255:VMwarePerfFrequency=60265:VMwareCacheSize=80M273:VMwareTimeout=10281:SNMPTrapperFile=/tmp/zabbix_traps.tmp289:StartSNMPTrapper=0407:Timeout=4448:AlertScriptsPath=/usr/local/share/zabbix/alertscripts492:LogSlowQueries=30002、如有修改配置,需重启zabbix-server服务。/etc/init.d/zabbix_server restart3、进入Zabbix发邮件脚本目录cd /usr/local/share/zabbix/alertscripts4、创建脚本及配置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.pychown -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])