zabbix微信告警脚本

# yum install -y git python-pip

# pip install requests simplejson

# git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git

查看脚本的存放目录

# cat /etc/zabbix/zabbix_server.conf |grep alertAlertScriptsPath=/usr/lib/zabbix/alertscripts


# cp ~/Zabbix-Alert-WeChat/wechat.py /usr/lib/zabbix/alertscripts/

# vi /usr/lib/zabbix/alertscripts/wechat.py 

#!/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":user,    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。

"toparty":"2",    #企业号中的部门id。

"msgtype":"text", #消息类型。

"agentid":"6",    #企业号中的应用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 =  ‘wx5c55b26119f82df5‘   #CorpID是企业号的标识

corpsecret = ‘XqHkOZfq9NpiXRU1Z6Vh0aRlZstG0o8LnclmzGJ45VqofUGvf3f8JedJ-XgMsWCy‘  #corpsecretSecret是管理组凭证密钥

accesstoken = gettoken(corpid,corpsecret)

senddata(accesstoken,user,subject,content)

给权限否则无法使用

chmod +x /usr/lib/zabbix/alertscripts/wechat.py

# chown zabbix:zabbix /usr/lib/zabbix/alertscripts/wechat.py

https://github.com/X-Mars/Zabbix-Alert-WeChat

时间: 2024-08-04 23:21:33

zabbix微信告警脚本的相关文章

zabbix 微信告警

搭建微信告警参考: https://www.abcdocker.com/abcdocker/2472 http://www.oschina.net/news/75588/zabbix-3.0.3 http://www.iyunv.com/thread-166308-1-1.html 微信告警的python脚本: #!/usr/bin/python #_*_coding:utf-8 _*_ import urllib,urllib2 import json import sys import si

Zabbix微信报警脚本及写触发记录

一.Zabbix微信报警 默认路径:/usr/lib/zabbix/scripts/weixin.py #!/usr/bin/python # -*- coding: utf-8 -*- import urllib,json import urllib2 import sys import time reload(sys) #print sys.getdefaultencoding() sys.setdefaultencoding('utf8') #print sys.getdefaultenc

Zabbix微信告警 iptables output 链设置

查找微信使用的IP地址: 1. 域名查找 # nslookup > weixin.qq.com Server:114.114.114.114 Address:114.114.114.114#53 Non-authoritative answer: weixin.qq.comcanonical name = minorshort.weixin.qq.com. Name:minorshort.weixin.qq.com Address: 140.206.160.234 Name:minorshort

zabbix 微信报警脚本

#! /bin/bash userid=$1 content=$2 python /data/zabbix/alertscripts/weixin.py $userid $content #!/usr/bin/env python # _*_ coding:utf8 _*_ import urllib2 import simplejson as json import sys class weChat: def __init__(self,url,Corpid,Secret): url = '%

【微信告警脚本】python3企业微信告警脚本

#!/usr/bin/python # --*-- encoding=utf-8 --*-- import urllib.request import json import sys import simplejson def gettoken(corpid, corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecr

splunk实现微信告警

splunk微信告警脚本 功能介绍 用于将splunk告警信息推送到微信企业号 https://github.com/XWJR-Ops/Splunk-WeChat-Alert 安装使用 环境要求 需要注册微信企业号点击注册 使用 下载 git clone https://github.com/XWJR-Ops/Splunk-WeChat-Alert.git 复制脚本到执行位置 cd Splunk-WeChat-Alert cp splunk.sh splunk.py /opt/splunk/bi

cacti实现微信告警功能

背景 近来受朋友所托,为他实现cacti的微信告警功能,但是在印象之中,cacti的thold插件貌似只有邮件告警功能吧?于是花了点时间在网络上搜索了相关信息,发现cacti的微信告警的信息着实很少,唯一看似可用的文章尝试了下却无法成功,在那篇文章的基础上,我修改了一下微信脚本的代码,测试成功了.故在此整理一下,供各位参看. 网上参考的原文章:http://www.itnpc.com/news/web/145117534132561.html 原理 由于cacti的thold插件本身并没有微信告

zabbix变量与shell脚本实现微信告警

内容来自网络,感谢分享 故障:{TRIGGER.STATUS},服务器:{HOSTNAME1}发生:{TRIGGER.NAME}故障!告警主机:{HOSTNAME1},IP地址:{HOST.CONN}告警时间:{EVENT.DATE}{EVENT.TIME}告警等级:{TRIGGER.SEVERITY}告警信息:{TRIGGER.NAME}告警项目:{TRIGGER.KEY1}问题详情:{ITEM.NAME}:{ITEM.VALUE}当前状态:{TRIGGER.STATUS}:{ITEM.VAL

zabbix详解:(五)添加微信告警配置

新时代,新事物,前两年大伙还在想着怎么用短信告警比较合理的时候,这回微信告警已经悄悄进入人们视线,邮件报警就变得落后了,甚至有些大牛公司,微信告警还能做成图形告警,把监控图形也发上去,想当NB的架构. 添加微信告警配置: 微信告警的原理其实是利用微信企业号接口,把告警信息发送到微信企业号发布内容,然后关注这个企业号的微信用户就能收到微信信息了,达到了微信告警的结果. 所以我们要做的事情是: 第一,创建一个微信公众号 第二,写一个调用微信公众号接口的脚本 第三,在zabbix_web配置相关信息