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_ ...
随机推荐
- 原生js(一)
Element对象有以下重要属性: 1.style. a) Element的css样式 b) 可以通过elem.style.backgroundColor = "red"的形式才动 ...
- Spark学习笔记--Spark在Windows下的环境搭建
本文主要是讲解Spark在Windows环境是如何搭建的 一.JDK的安装 1.1 下载JDK 首先需要安装JDK,并且将环境变量配置好,如果已经安装了的老司机可以忽略.JDK(全称是JavaTM P ...
- Promise {<pending>
场景:在create-react-app whatwg-fetch的项目中,想获取请求返回的数据, componentWillMount() { console.log(this.props) con ...
- 剑指offer题目记录
1.如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStri ...
- Particle 粒子效果使用(适配微信小游戏,particle is not defined)
在微信小游戏中使用粒子效果 参考: 1. 粒子库下载地址 2. 粒子官方使用教程 3. 水友解决微信小游戏particle is not defined 一.下载第三方库 Git地址:https:// ...
- Canvas裁剪Clip和Region、RegionIterator
extends:http://blog.csdn.net/lonelyroamer/article/details/8349601 裁剪功能由Canvas提供的一系列的clip...方法 和quick ...
- Unity3D笔记十四 力
Unity中力的方式有两种:第一种为普通力,需要设定力的方向与大小:第二种为目标位置力,需要设定目标点的位置,该物体将朝向这个目标位置施加力. 1.案例 添加两个球体和一个立方体,另外还要给两个球体添 ...
- Thymeleaf 入门
基本项目结构: Thymeleaf配置: spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false spring.thymeleaf ...
- MFC如何获取控件相对于窗口的左边,以及鼠标相对于控件的位置
CRect rect; CWnd *pWnd = GetDlgItem(IDC_STATIC_PIC); pWnd->GetWindowRect(&rect); ScreenToClie ...
- django之admin设置
Django自带的后台管理是Django明显特色之一,可以让我们快速便捷管理数据.后台管理可以在各个app的admin.py文件中进行控制.以下是我最近摸索总结出比较实用的配置.若你有什么比较好的配置 ...