zabbix监控报警: 钉钉、企业微信、Email方式

  1、添加报警媒介:

  管理-->报警媒介类型-->创建媒介类型:  

    

    

    

   编辑Email:

     

 

2、添加监控脚本: 

 钉钉: dingding.py

  参考官方开放平台文档:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.6ab04a97J3fY3Y&treeId=257&articleId=105733&docType=1

 

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys,os,requests,json

headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "https://oapi.dingtalk.com/robot/send?access_token=钉钉获取"
 
def sendmsg(text):
    json_text= {
     "msgtype": "text",
        "at": {
            "atMobiles": [
                "手机号"
            ],
            "isAtAll": False
        },
        "text": {
            "content": text
        }
    }
    print requests.post(api_url,json.dumps(json_text),headers=headers).content

if __name__ == '__main__':
    text = sys.argv[1]
    sendmsg(text)
View Code
  企业微信: weixin.py

  参考官方文档:https://work.weixin.qq.com/api/doc#10167

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys,os,requests,json,logging

logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
                datefmt = '%a, %d %b %Y %H:%M:%S',
                filename = "/tmp/zabbix_server.log",
                filemode = 'a')

#企业微信信息: 申请企业微信后,去企业微信管理后台获取  
corpid = ''
appsecret = ''
agentid = 

'''
获取access_token:
请求方式:GET(HTTPS)
请求URL:https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT
'''
token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" %(corpid,appsecret)
res = requests.get(token_url)
errcode = res.json()['errcode']

# 脚本传入参数
touser = sys.argv[1]
subject = sys.argv[2]
message = sys.argv[3]

if errcode == 0:
    access_token = res.json()['access_token']

    #发送消息
    msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token

    params={
            "touser": touser,
            "msgtype": "text",
            "agentid": agentid,
            "text": {
                    "content": message
            },
            "safe":0
    }

    req=requests.post(msgsend_url, data=json.dumps(params))
    logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)

else:
    errmsg = res.json()['errmsg']
    logging.info("get access_token failed, errcode=%s, errmsg=%s" %(errcode,errmsg))
weixin.py

将脚本上传至zabbix_server端口:/usr/local/zabbix/alertscripts 目录下。

3、创建触发器:

  配置-->动作-->事件源选:触发器-->创建动作:

    

     

    

  故障恢复添加:

恢复主机:{HOST.NAME}
主机地址:{HOST.IP}
恢复时间:{EVENT.DATE} {EVENT.TIME}
恢复等级:{TRIGGER.SEVERITY}
恢复信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}

  创建微信和Email触发器同上。

4、配置接收报警信息的用户:

  管理-->用户-->选择Admin:  报警媒介-->添加钉钉、企业微信和Email。添加完点更新即可。

 

posted @ 2018-11-07 15:36  领导来视察  阅读(395)  评论(0编辑  收藏  举报