不知道是什么原因直接用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 微信报警脚本的更多相关文章

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

    一.Zabbix微信报警 默认路径:/usr/lib/zabbix/scripts/weixin.py #!/usr/bin/python # -*- coding: utf-8 -*- import ...

  2. 通过脚本发送zabbix微信报警

    实现zabbix通过微信报警的方式也是通过脚本来实现,与邮件报警不同的是,脚本调用的微信的相关接口的获取相对复杂一点 1.申请一个微信公众号(企业号) 申请方法不多说,如果已申请请忽略 2.在微信企业 ...

  3. Zabbix微信报警触发

    (1)         企业应用-创建应用 1.除了对个人添加微信报警之外,还可以添加不同管理组,接受同一个应用推送的消息, 成员账号,组织部门ID,应用Agent ID,CorpID和Secret, ...

  4. (五)zabbix微信报警

    1.注册微信企业号 1)注册微信企业号 https://work.weixin.qq.com 2)通讯录添加用户 3)记住部门id 4)创建应用 5)点击刚创建的应用,记住Agentld和secret ...

  5. Zabbix 微信报警Python版(带监控项波动图片)

    #!/usr/bin/python # -*- coding: UTF- -*- #Function: 微信报警python版(带波动图) #Environment: python import ur ...

  6. zabbix 微信报警

    http://blog.csdn.net/wh211212/article/details/52735236 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越 ...

  7. filebeat+logstash通过zabbix微信报警

    一.安装软件: 1.在要收集日志的机器上安装filebeat: 1).下载安装: cd /usr/local/src wget https://artifacts.elastic.co/downloa ...

  8. zabbix微信报警

    [root@LinuxS04 jiaoben]# ./weixin 联系人 baojing baojingok[root@LinuxS04 jiaoben]# pwd/usr/local/zabbix ...

  9. zabbix邮件报警脚本(Python)

    #!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...

随机推荐

  1. Android设计和开发系列第一篇:Notifications通知(Develop—API Guides)

    Notifications IN THIS DOCUMENT Design Considerations Creating a Notification Required notification c ...

  2. WP8.1学习系列(第十七章)——Windows Phone重要图形、视觉指示器和通知

    美感在手机应用中是不可或缺的,它是直观操作的代名词.在 Windows Phone 中,你的磁贴.初始屏幕.图标.控件和导航的视觉元素会引起用户对应用程序内的相关任务.优先事项或操作的注意,并采用新颖 ...

  3. 【PHP】 php 解析 base64图片上传

    base64 图片编码格式: 类似如下 data:image/JPG;base64,/9j/4S/+RXhpZgAATU0AKgAAAAgACwEPAAIAAAAG php 解析代码如下:  基于tp ...

  4. Apache服务器SSL双向认证配置

    以Win32版Apache与OpenSSL为例,介绍从创建数字证书到Apache配置的整个过程,希望对读者有所帮助. Apache是目前最流行的WEB服务器之一,借助OpenSSL库,我们可以在Apa ...

  5. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十:PS/2模块④ — 普通鼠标

    实验十:PS/2模块④ - 普通鼠标 学习PS/2键盘以后,接下来就要学习 PS/2 鼠标.PS/2鼠标相较PS/2键盘,驱动难度稍微高了一点点,因为FPGA(从机)不仅仅是从PS/2鼠标哪里读取数据 ...

  6. Visual Studio 2012创建SQL Server Database Project提示失败解决方法

    新建一个SQL Server Database Project,提示: Unable to open Database project This version of SQL Server Data ...

  7. Setting up Unicorn with Nginx

    gem install unicorn or gem 'unciron' 1 install Nginx yum install ... 2 Configuration vi /etc/nginx/n ...

  8. 深入浅出WPF之Binding的使用(一)

    在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象:这 ...

  9. Linux(CentOS)安装JDK(.tar.gz)并配置

    本文思路转自http://blog.sina.com.cn/s/blog_81631744010137iy.html 点击(此处)折叠或打开 1.到 甲骨文(oracle)下载jdk不用多说 tar ...

  10. jmeter中Implementation中几个选项的区别

    在jmeter发送http请求时,Implementation下拉框中有几个选项,如下: 那到底有什么区别呢?发送http请求改用哪种方法呢.百度后查之,没答案.我们还是看官方文档吧.官方文档解释如下 ...