asdrtu 发表于 2019-1-24 10:55:11

zabbix监控的微信报警设置线上落地经验

  微信接收zabbix监控信息脚本和配置
  微信企业号的申请
1、注册的地址https://qy.weixin.qq.com/
  2、创建用户和应用
http://i2.运维网.com/images/blog/201807/26/5c2c428a9d25fe0013322b409e7fc795.png
  zabbix server配置脚本的路径
#vim zabbix_server.conf
AlertScriptsPath=/data/zabbix/alertscripts
  $ mkdir /data/zabbix/alertscripts -p
$ chown zabbix.zabbix /data/zabbix/alertscripts -R
  测试接口是否正常
调用微信接口需要一个调用接口的凭证:access_token
通过:GropID,Secret才能获取到access_token,但是获取到的token有效期为两分钟
微信企业号接口调试工具地址:http://qydev.weixin.qq.com/debug
打开上面的url,使用上面monitops企业号的monit-manager管理组的CorpID和Secret号获得调用接口凭证access_token,如下:
http://i2.运维网.com/images/blog/201807/26/7e936bb2ba8081bcfdf31840c4671fd2.png
http://i2.运维网.com/images/blog/201807/26/16da0915e4e3ea58736653043c09fc86.png
这个脚本是放置在server端的,在配置文件”AlertScriptsPath”中指定脚本的路径,并赋予执行权限。
  #!/bin/bash
#SCRIPT_NAME: weixin.sh
#send message from weixin for zabbix monitor
  CropID='wx8f104fc9b86b393f'
Secret='TvCUZBGCTQE3nwM94VeoOpBpTbpvjW0cRd35Kh4HMUA'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')
  PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
  function body() {
local int AppID=1#企业号中的应用id
local UserID=$1    #部门成员id,zabbix中定义的微信接收者
local PartyID=4    #部门id,定义了范围,组内成员都可接收到消息
local Msg=$(echo "$@" | cut -d" " -f3-)
printf '{\n'
printf '\t"touser": "'"$UserID"\"",\n"
printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"msgtype": "text",\n'
printf '\t"agentid": "'" $AppID "\"",\n"
printf '\t"text": {\n'
printf '\t\t"content": "'"$Msg"\""\n"
printf '\t},\n'
printf '\t"safe":"0"\n'
printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
  或者使用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
printgettoken_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":"xxxx",   #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
"toparty":"xxxx",      #企业号中的部门id。
"msgtype":"text",   #消息类型。
"agentid":"xxxxx",#企业号中的应用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)   #zabbix传过来的第一个参数
subject = str(sys.argv)#zabbix传过来的第二个参数
content = str(sys.argv)#zabbix传过来的第三个参数

corpid ='xxxxxx'   #企业号的标识(上文中提到获取位置)
corpsecret = 'xxxx'    #管理组凭证密钥(上文中提到获取位置)
accesstoken = gettoken(corpid,corpsecret)
  senddata(accesstoken,user,subject,content)
  测试
http://i2.运维网.com/images/blog/201807/26/da67f686821bc4f63b7e085f54322d78.png
  zabbix配置
1)创建报警媒介(这里我选用python脚本:weixin.py)
注意下面要填写的脚本参数:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
http://i2.运维网.com/images/blog/201807/26/5bd696181134543869e097d594bde1af.png
http://i2.运维网.com/images/blog/201807/26/47922b37004c380edbdeae98b16ecc05.png
http://i2.运维网.com/images/blog/201807/26/c9b43d7aeef2d2093dca26e0249580f6.png
http://i2.运维网.com/images/blog/201807/26/03ec4e28101fa69f868894479a08b286.png
http://i2.运维网.com/images/blog/201807/26/5aed890505e11f159d57b60c90194dfb.png
  配置告警信息:
  告警操作:
{TRIGGER.STATUS}: {TRIGGER.NAME}
告警主机:{HOST.NAME}
主机IP: {HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态: {TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID: {EVENT.ID}
  恢复操作:
{TRIGGER.STATUS}: {TRIGGER.NAME}
告警主机:{HOST.NAME}
主机IP: {HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态: {TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID: {EVENT.ID}



页: [1]
查看完整版本: zabbix监控的微信报警设置线上落地经验