参照: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
到此配置完成。