(五)zabbix微信报警

1、注册微信企业号

1)注册微信企业号

https://work.weixin.qq.com

2)通讯录添加用户

3)记住部门id

4)创建应用


5)点击刚创建的应用,记住Agentld和secret

6)微信关注这个企业号,获取更多尺寸关注


点击关注

7)测试向关注的用户发送信息


微信能够正常接收到消息

8)获取CorpID

2、python脚本

  • 获取脚本文件目录位置
#grep alertscripts /etc/zabbix/zabbix_server.conf 
AlertScriptsPath=/usr/lib/zabbix/alertscripts
  • 安装simplejson
wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
python setup.py build
python setup.py install
  • python脚本
#!/usr/bin/python
#_*_coding:utf-8 _*_
import urllib,urllib2
import json
import sys
import simplejson
reload(sys)
sys.setdefaultencoding('utf-8')
def gettoken(corpid,corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    print  gettoken_url
    try:
        token_file = urllib2.urlopen(gettoken_url)
    except urllib2.HTTPError as e:
        print e.code
        print e.read().decode("utf8")
        sys.exit()
    token_data = token_file.read().decode('utf-8')
    token_json = json.loads(token_data)
    token_json.keys()
    token = token_json['access_token']
    return token
def senddata(access_token,user,subject,content):
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    send_values = {
        "touser":user,    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty":"2",    #企业号中的部门id。
        "msgtype":"text", #消息类型。
        "agentid":"2",    #企业号中的应用id。
        "text":{
            "content":subject + '\n' + content
           },
        "safe":"0"
        }
#    send_data = json.dumps(send_values, ensure_ascii=False)
    send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
    send_request = urllib2.Request(send_url, send_data)
    response = json.loads(urllib2.urlopen(send_request).read())
    print str(response)
if __name__ == '__main__':
    user = str(sys.argv[1])     #zabbix传过来的第一个参数
    subject = str(sys.argv[2])  #zabbix传过来的第二个参数
    content = str(sys.argv[3])  #zabbix传过来的第三个参数
    corpid =  '11111111111111'   #CorpID是企业号的标识
    corpsecret = '222222222222222222'  #corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)
  • 测试python脚本
# python wechat.py 1 2 3
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwa6962ed44a38221b&corpsecret=qpyrPT4E15FhyvUujJcuyhGU5CczWrAeZAn95lorCNU
{u'invaliduser': u'1', u'errcode': 0, u'errmsg': u'ok'}

验证微信接收到的消息

  • 脚本放到目录和赋予x权限
cd /usr/lib/zabbix/alertscripts/
chmod +x wechat.py
chown -R zabbix.zabbix wechat.py

3、zabbix配置微信报警

1)创建报警媒介

脚本参数
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

2)用户添加报警媒介

4)定义动作

  • 什么条件会触发动作,这里和zabbix3.0版本不同
  • 恢复操作:定义标题,内容,发送给指定的用户,通过微信发送
默认标题:{TRIGGER.STATUS}: {TRIGGER.NAME}
消息内容
告警主机:   {HOST.NAME}                                                                                                  
告警    IP:    {HOST.IP}                                                                                                       
告警时间:   {EVENT.DATE}-{EVENT.TIME}
告警等级:   {TRIGGER.SEVERITY}
告警信息:   {TRIGGER.NAME}
问题详情:   {ITEM.NAME}:{ITEM.VALUE}
事件    ID:   {EVENT.ID}


  • 恢复操作
默认标题:{TRIGGER.STATUS}: {TRIGGER.NAME}
消息内容
告警主机:   {HOST.NAME}                                                                                                  
告警    IP:    {HOST.IP}                                                                                                       
告警时间:   {EVENT.DATE}-{EVENT.TIME}
告警等级:   {TRIGGER.SEVERITY}
告警信息:   {TRIGGER.NAME}
问题详情:   {ITEM.NAME}:{ITEM.VALUE}
事件    ID:   {EVENT.ID}

  • 测试报警:需要提前指定好触发器
    发生问题

    问题恢复

猜你喜欢

转载自www.cnblogs.com/shaonli/p/12085188.html