zabbix3.X 4.X 接入微信报警(6个外部参数)适合用于给多、单个成员发送告警信息

参照:https://blog.csdn.net/yanggd1987/article/details/80093310

微信作为日常使用最频繁的工具,因此希望将微信接入zabbix报警。

本文分享微信告警脚本,其他请参照上文。

zabbix3.X 4.X 接入微信报警(6个外部参数)适合用于给部门中单个成员发送告警信息。

zabbix server端配置

一.微信报警脚本 

登录zabbix主机,查看配置zabbix_server.conf ,找到AlertScriptsPath,查看路径位置,如被注释,将注释打开,把send.py 文件保存到该目录下。
 

# cat /etc/zabbix/zabbix_server.conf |grep AlertScriptsPath
### Option: AlertScriptsPath
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts

如有修改配置,需重启zabbix-server服务。

以下是脚本内容(python2)

#!/usr/bin/python
#_*_coding:utf-8 _*_


import urllib,urllib2
import json
import sys
import os
import simplejson
import logging
import argparse
reload(sys)
sys.setdefaultencoding('utf-8')

logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
                datefmt = '%a, %d %b %Y %H:%M:%S',
                filename = os.path.join('/var/log/zabbix/','weixin.log'),
                filemode = 'a')


parser = argparse.ArgumentParser()
parser.add_argument('--agentid', type=str, default=None)
parser.add_argument('--corpid', type=str, default=None)
parser.add_argument('--corpsecret', type=str, default=None)
parser.add_argument('--touser', type=str, default=None)
parser.add_argument('--subject', type=str, default=None)
parser.add_argument('--content', type=str, default=None)

parser_parameters = parser.parse_args()
agentid = parser_parameters.agentid
corpid = parser_parameters.corpid
corpsecret = parser_parameters.corpsecret
touser = parser_parameters.touser
subject = parser_parameters.subject
content = parser_parameters.content
#touser = 'huangshumao'
print touser

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,touser,subject,content):
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    send_values = {
        "touser": touser,
        "toparty":"2",
        "msgtype":"text",
        "agentid":agentid,
        "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])
#    subject = str(sys.argv[2])
#    content = str(sys.argv[3])
#    agentid = 1000002
#    corpid =  'ww48f2e8ce0xxxxxx'
#    corpsecret = 'hy8doOgY2Whb_BXxpaeB0qFqpoz3PNvxuZb_xxxxx'
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,touser,subject,content)
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + content)
#添加权限
chmod +x weixin.py
touch /var/log/zabbix/weixin.log
chown zabbix.zabbix /var/log/zabbix/weixin.log

说明:该脚本需要传6个参数,测试方法:

#当前目录/usr/lib/zabbix/alertscripts
./weixin.py --agentid="1000002" --corpid="ww6539930f20f96beb" --corpsecret="PIXJW4JZmRxLzTEb84ackkwNinrMztUyiZ-tLs8JRO0" --touser="huangshumao" --subject test --content tes66666

如果 运行出 错,提示

Traceback (most recent call last):
  File "./weixin.py", line 9, in <module>
    import simplejson
ImportError: No module named simplejson

只要安装下simplejson

 yum install python-simplejson -y 

 

 

手机企业微信可以收到:

 在zabbix服务器web界面设置,添加告警类型

6个参数:

--agentid=1000002

--corpid=ww6539930f20f96beb

--corpsecret=PIXJW4JZmRxLzTEb84ackkwNinrMztUyiZ-tLs8JRO0

--touser={ALERT.SENDTO}

--subject={ALERT.SUBJECT}

--content={ALERT.MESSAGE}

添加动作:

 

标题:

{TRIGGER.STATUS}:{TRIGGER.SEVERITY}: {TRIGGER.NAME}:{HOST.IP}

内容:

告警主机:{HOST.NAME}
主机IP: {HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态: {TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID: {EVENT.ID}

选发到admin,sendonlyto --all--

最后在用户admin-media

 

到此配置完成。 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值