zabbix 微信报警脚本
不知道是什么原因直接用Python脚本zabbix无法执行脚本,需要一个shell来启动

#! /bin/bash
userid=$
content=$
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 = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url,Corpid,Secret)
res = self.url_req(url)
self.token = res['access_token'] def url_req(self,url,method='get',data={}):
if method == 'get':
req = urllib2.Request(url)
res = json.loads(urllib2.urlopen(req).read())
elif method == 'post':
req = urllib2.Request(url,data)
res = json.loads(urllib2.urlopen(req).read())
else:
print 'error request method...exit'
sys.exit()
return res
def send_message(self,userlist,content,agentid=):
self.userlist = userlist
self.content = content
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % self.token
data = {
"touser": "",
"toparty": "",
"totag": "",
"msgtype": "text",
"agentid": "",
"text": {
"content": ""
},
"safe":""
}
data['touser'] = userlist
data['agentid'] = agentid
data['text']['content'] = content
data = json.dumps(data,ensure_ascii=False)
# print data
res = self.url_req(url,method='post',data=data)
if res['errmsg'] == 'ok':
print 'send sucessed!!!'
else:
print 'send failed!!'
print res if __name__ == '__main__':
userlist = sys.argv[]
# userlist = 'ZhangSen'
content = sys.argv[:]
content = '\n'.join(content)
# content = 'test1'
Corpid = 'xxx' #此处对应修改
Secret = 'xxx' #此处对应修改
url = 'https://qyapi.weixin.qq.com' wechat = weChat(url,Corpid,Secret)
wechat.send_message(userlist,content)
上面这脚本有个问题不能发中文消息,下面这个可以发中文
#!/usr/bin/python
# _*_coding:utf- _*_
import urllib2
import json
import sys reload(sys)
sys.setdefaultencoding('utf-8') def gettoken(corpid, corpsecret):
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
try:
token_file = urllib2.urlopen(gettoken_url)
except urllib2.HTTPError as e:
e.code
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, party, agent, subject, content):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
send_values = "{\"touser\":\"" + user + "\",\"toparty\":\"" + party + "\",\"totag\":\"\",\"msgtype\":\"text\",\"agentid\":\"" + agent + "\",\"text\":{\"content\":\"" + subject + content + "\"},\"safe\":\"0\"}"
send_request = urllib2.Request(send_url, send_values)
response = json.loads(urllib2.urlopen(send_request).read())
print "send sucessed!!!"
str(response) if __name__ == '__main__':
user = str(sys.argv[]) # 参数1:发送给用户的账号,必须关注企业号,并对企业号有发消息权限
party = str('') # 参数2:发送给组的id号,必须对企业号有权限
agent = str('') # 参数3:企业号中的应用id
subject = str('') # 参数4:标题【消息内容的一部分】
content = str(sys.argv[]) # 参数5:文本具体内容
corpid = 'xxx' # CorpID是企业号的标识
corpsecret = 'xxx' # corpsecretSecret是管理组凭证密钥
try:
accesstoken = gettoken(corpid, corpsecret)
senddata(accesstoken, user, party, agent, subject, content)
except Exception, e:
print "error"
str(e) + "Error Please Check \"corpid\" or \"corpsecret\" Config"
zabbix 微信报警脚本的更多相关文章
- Zabbix微信报警脚本及写触发记录
一.Zabbix微信报警 默认路径:/usr/lib/zabbix/scripts/weixin.py #!/usr/bin/python # -*- coding: utf-8 -*- import ...
- 通过脚本发送zabbix微信报警
实现zabbix通过微信报警的方式也是通过脚本来实现,与邮件报警不同的是,脚本调用的微信的相关接口的获取相对复杂一点 1.申请一个微信公众号(企业号) 申请方法不多说,如果已申请请忽略 2.在微信企业 ...
- Zabbix微信报警触发
(1) 企业应用-创建应用 1.除了对个人添加微信报警之外,还可以添加不同管理组,接受同一个应用推送的消息, 成员账号,组织部门ID,应用Agent ID,CorpID和Secret, ...
- (五)zabbix微信报警
1.注册微信企业号 1)注册微信企业号 https://work.weixin.qq.com 2)通讯录添加用户 3)记住部门id 4)创建应用 5)点击刚创建的应用,记住Agentld和secret ...
- Zabbix 微信报警Python版(带监控项波动图片)
#!/usr/bin/python # -*- coding: UTF- -*- #Function: 微信报警python版(带波动图) #Environment: python import ur ...
- zabbix 微信报警
http://blog.csdn.net/wh211212/article/details/52735236 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越 ...
- filebeat+logstash通过zabbix微信报警
一.安装软件: 1.在要收集日志的机器上安装filebeat: 1).下载安装: cd /usr/local/src wget https://artifacts.elastic.co/downloa ...
- zabbix微信报警
[root@LinuxS04 jiaoben]# ./weixin 联系人 baojing baojingok[root@LinuxS04 jiaoben]# pwd/usr/local/zabbix ...
- zabbix邮件报警脚本(Python)
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...
随机推荐
- 【Spring源码分析系列】bean的加载
前言 以 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过 ...
- Telnet是什么意思又是什么协议 Telnet有什么作用及功能
Telnet是teletype network的缩写,专业的说,Telnet是Internet上远程登录的一种程序:它可以让您的电脑通过网络登录到网络另一端的电脑上,甚至还可以存取那台电脑上的文件. ...
- html5media 视频
官网: https://html5media.info/ 二.引入script <script src="//api.html5media.info/1.1.8/html5media. ...
- Android短信发送器_08
1.string xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...
- [Log]ASP.NET之HttpModule 事件执行顺序
ASP.Net下的HttpModule是基于事件的处理模型,这使得我们在选择事件监听和处理的时候有更多选择.下面是对HttpModule有关事件被触发的监测: 有关代码如下 using System; ...
- mysql补充(4)数据完整性
数据完整性(Data Integrity)是指数据的精确性(Accuracy) 和可靠性(Reliability).(补充mysql数据完整性和约束) 它是应防止数据库中存在不符合语义规定的数据和防止 ...
- Coding 代码管理快速入门(转)
当项目创建好了之后,我们该如何上传代码到 coding 上呢? Coding 网站使用“ Git 仓库”(类似 github )来管理代码. 其操作原理在于:利用 git 服务,将本地的项目目录下的文 ...
- gnuplot生成gif动画
最近有个任务需要生成一个动态变化的图,然后突然发现gnuplot竟然可以生成gif动画,当真是应正了博客Gnuplot surprising的子标题: I always tell myself: &q ...
- Solr学习笔记之6、Solr学习资源
一.官方资源 1.官网:http://lucene.apache.org/solr/ 2.wiki:http://wiki.apache.org/solr/FrontPage 3.solr中文网:ht ...
- Java编程思想中关于闭包的一个例子
Java编程思想中的一个例子,不是很理解使用闭包的必要性,如果不使用闭包,是不是有些任务就不能完成?继续探索. package InnerClass; interface Incrementable ...