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_ ...
随机推荐
- linux下的shell操作mysql
(1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...
- Esper学习之一:Esper介绍
CEP即Complex Event Process,中文意思就是“复杂事件处理”.听起来好像很复杂,实际上就是基于事件流进行数据处理,把要分析的数据抽象成事件,然后将数据发送到CEP引擎,引擎就会根据 ...
- MIME类型列表
A Multipurpose Internet Mail Extensions (MIME) type is a standard that indicates the nature and form ...
- mysql客户端工具
MySQL 数据库不仅提供了数据库的服务器端应用程序,同时还提供了大量的客户端工具程序,如 mysql,mysqladmin,mysqldump 等等,都是大家所熟悉的.虽然有些人对这些工具的功能都已 ...
- Unity3D 加密 Assembly-CSharp.dll (Android平台) 防止反编译【转】
转自 http://blog.csdn.net/u013108312/article/details/54234439
- 对Java中使用两个大括号进行初始化的理解
最近重读Java 编程思想,读到有关实例化代码块儿 的内容,使我对于使用两个大括号进行初始化有了更深的理解. 实例化代码块儿: 和静态代码块儿的概念相对应,静态代码块儿是static 关键字 + 大括 ...
- Run-Time Check Failure #2 Stack around the variable ‘xxx’ was corrupted
在改别人代码时,运行报错: Run-Time Check Failure #2 Stack around the variable 'buffer' was corrupted 这表明你对某变量的赋值 ...
- Xcode - 打开工程,提示No Scheme解决
错误提示,如图: 解决思路:
- Thymeleaf 入门
基本项目结构: Thymeleaf配置: spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false spring.thymeleaf ...
- JDBC 连接数据库,包含连接池
1.不使用连接池方式(Jdbc) 1.1 工具类(JdbcUtil.java) package com.jdbc.util; import java.io.IOException;import jav ...