一、配置邮件报警
1、脚本
# cd /usr/lib/zabbix/alertscripts
# vim Sendmail.sh
#!/bin/bashfile=/tmp/zabbix_mail.txtecho "$3" > $filedos2unix -k $file/bin/mail -s "$2" $1 < $file# echo "$3" | mail -s "$2" $1
2、安装邮件发送软件mailx
# yum install mailx
测试能否发送邮件:# echo "zabbix test mail " |mail -s "zabbix-title"
3、安装dos2unix(用于解决邮件内容变成附件的问题)
# yum install dos2unix
4、创建报警媒介
- {ALERT.SENDTO}
- {ALERT.SUBJECT}
- {ALERT.MESSAGE}
5、配置用户报警媒介
注:多个收件人用空格分隔
6、配置动作
【故障】服务器:{HOST.IP}发生: {TRIGGER.NAME}故障!
告警级别:{TRIGGER.SEVERITY}
告警主机:{HOST.HOST}
告警IP:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警内容:{TRIGGER.NAME}
当前值:{ITEM.VALUE}
告警状态:{TRIGGER.STATUS}
告警ID:{EVENT.ID}
【恢复】服务器:{HOST.IP}: {TRIGGER.NAME}已恢复!
告警级别:{TRIGGER.SEVERITY}
告警主机:{HOST.HOST}
告警IP:{HOST.IP}
恢复时间:{EVENT.RECOVERY.TIME}
告警内容:{TRIGGER.NAME}
当前值:{ITEM.VALUE}
告警状态:{TRIGGER.STATUS}
告警ID:{EVENT.ID}
二、微信告警
配置微信报警
1、注册微信企业号
2、登录企业号
3、创建部门
4、脚本
#!/usr/bin/env python# -*- coding: utf-8 -*-import urllib,urllib2,jsonimport sysreload(sys)sys.setdefaultencoding( "utf-8" )class WeChat(object): __token_id = '' # init attribute def __init__(self,url): self.__url = url.rstrip('/') self.__corpid = 'XXXXXXXX' self.__secret = 'XXXXXXXXXXXXXXXXX' # Get TokenID def authID(self): params = { 'corpid':self.__corpid, 'corpsecret':self.__secret} data = urllib.urlencode(params) content = self.getToken(data) try: self.__token_id = content['access_token'] # print content['access_token'] except KeyError: raise KeyError # Establish a connection def getToken(self,data,url_prefix='/'): url = self.__url + url_prefix + 'gettoken?' try: response = urllib2.Request(url + data) except KeyError: raise KeyError result = urllib2.urlopen(response) content = json.loads(result.read()) return content # Get sendmessage url def postData(self,data,url_prefix='/'): url = self.__url + url_prefix + 'message/send?access_token=%s' % self.__token_id request = urllib2.Request(url,data) try: result = urllib2.urlopen(request) except urllib2.HTTPError as e: if hasattr(e,'reason'): print 'reason',e.reason elif hasattr(e,'code'): print 'code',e.code return 0 else: content = json.loads(result.read()) result.close() return content # send message def sendMessage(self,touser,message): self.authID() data = json.dumps({ 'touser':touser, 'toparty':"1", #部门ID 'msgtype':"text", 'agentid':"1", #应用ID 'text':{ 'content':message }, 'safe':"0" },ensure_ascii=False) response = self.postData(data) print responseif __name__ == '__main__': a = WeChat('https://qyapi.weixin.qq.com/cgi-bin') a.sendMessage(sys.argv[1],sys.argv[3])
5、创建报警媒介类型
- corpid
- secret
- {ALERT.MESSAGE}
6、创建报警媒介
7、配置动作
【故障】服务器:{HOST.IP}发生: {TRIGGER.NAME}故障!
告警级别:{TRIGGER.SEVERITY}
告警主机:{HOST.HOST}
告警IP:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警内容:{TRIGGER.NAME}
当前值:{ITEM.VALUE}
告警状态:{TRIGGER.STATUS}
告警ID:{EVENT.ID}
【恢复】服务器:{HOST.IP}: {TRIGGER.NAME}已恢复!
告警级别:{TRIGGER.SEVERITY}
告警主机:{HOST.HOST}
告警IP:{HOST.IP}
恢复时间:{EVENT.RECOVERY.TIME}
告警内容:{TRIGGER.NAME}
当前值:{ITEM.VALUE}
告警状态:{TRIGGER.STATUS}
告警ID:{EVENT.ID}
注意:zabbix.conf.php为zabbix链接数据库配置文件,zabbix数据库密码修改后,一定要修改该配置文件。