zabbix5.0配置企业微信报警

1. 登录企业微信管理后台

https://work.weixin.qq.com/wework_admin/loginpage_wx?etype=expired&redirect_uri=https://work.weixin.qq.com/wework_admin/frame#contacts

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这个是测试的地址 测试是否正常获取 https://work.weixin.qq.com/api/devtools/devtool.php

在这里插入图片描述

修改zabbix_server.conf

[root@localhost ~]# grep alertscripts /etc/zabbix/zabbix_server.conf
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/opt/scripts/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

安装时可能会报个错误’greenlet.h:8:20: 致命错误: Python.h:没有那个文件或目录’

解决办法

解决方法是安装python-dev,这是Python的头文件和静态库包:
sudo apt-get install python-dev

配置监控脚本

#!/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":"test",    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty":"2",    #企业号中的部门id。
        "msgtype":"text", #消息类型。
        "agentid":"1000002",    #企业号中的应用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 =  'ww80706331ded1d29b'   #CorpID是企业号的标识
    corpsecret = 'oxmHg2s_ANAYmASTtpLzaANVyGxdveMKnG_llHg86go'  #corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)

命令行测试是否可以发送

[root@localhost alertscripts]# ./wechat.py user 报警测试 error
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww13e3038f3427b098&corpsecret=GUQCMJAHGnpotGZqmWCXC-ULnjgLH6NyO7adJUpUYik
{u'invalidparty': u'2', u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}

不出问题应该已经可以接收到信息了

现在可以在zabbix上设置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# 服务故障: <font color="warning">{EVENT.NAME}</font>
{
> 告警主机: **{HOST.NAME}**
> 主机地址: **{HOST.IP}**
> 监控项目: {ITEM.NAME}
> 当前取值: {ITEM.LASTVALUE}
> 告警等级: {TRIGGER.SEVERITY}
> 告警时间: {EVENT.DATE}-{EVENT.TIME}
> 事件ID: {EVENT.ID}
}

在这里插入图片描述

# 故障恢复: <font color="info">{EVENT.NAME}</font>
{
> 主机名称: **{HOST.NAME}**
> 主机地址: **{HOST.IP}**
> 告警名称: {EVENT.NAME}
> 持续时长: {EVENT.DURATION}
> 恢复时间: {EVENT.RECOVERY.DATE}-{EVENT.RECOVERY.TIME} 
> 当前状态: {TRIGGER.STATUS}
> 当前取值: {ITEM.LASTVALUE}
> 事件ID: {EVENT.ID}
}

在这里插入图片描述

创建用户

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

创建动作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
下来基本上没问题了,如果接收不到消息的话可以进行以下测试
在这里插入图片描述
在这里插入图片描述
进行测试操作如果可以接收到那就看看触发器那些东西,如果报错可以看看报错内容,有一个错误是路径错误,解决办法就是将py脚本cp到相应目录下。

需要markdown格式的可用此代码

注:这个需要用微信机器人,之前那个不能是同,因为此代码需要用到微信机器人的webhook

#!/usr/local/python3/bin/python3
# coding:utf-8
# zabbix企业微信报警
import requests, json, sys, os, datetime
 
#微信钩子机器人地址
webhook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxxxxxxxxxxxxxxx"   #在此设置即可
 
#zabbix传过来的三个变量
user = sys.argv[1]
title = sys.argv[2]
text = sys.argv[3]
 
#每发送一次报警,就记一次日志
file = r"/var/log/zabbix/send2wechat.log"
 
#报警的信息
data = {
    "msgtype": "markdown",
    "markdown": {
        "content": title + "\n" + text
    }
}
 
#请求这个钩子机器人
headers = {'Content-Type': 'application/json'}
x = requests.post(url=webhook, data=json.dumps(data), headers=headers)
if os.path.exists(file):
    f = open(file, "a+")
else:
    f = open(file, "w+")
    f.write(" " + " - -" * 30)
    f.write("\n")
     
#记录日志
if x.json()["errcode"] == 0:
    f.write(" "+ str(datetime.datetime.now()) + " " + str(user) + " " + "发送成功" + " " + str(title) + "\n")
    f.close()
else:
    f.write(" "+ str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + " " + str(title) + "\n")
    f.close() 

在这里插入图片描述

  • 0
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值