首页 > 微信 > 详细

zabbix 微信脚本(转贴后增加)

时间:2018-07-04 22:37:15      阅读:299      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/python # -*- coding: utf-8 -*- # zabbix notification confirmation script # python2.7 or above    import requests import json import os import sys    Toparty = "13"   #部门id AgentID = 1000008     #应用id    #修改为企业CropID和Secret CropID = '1·#!·#!·#!·#!·#!·3' Secret = '!·#!·#!·#!#!·#!·#'    #获取Token Gtoken ="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+ CropID + "&corpsecret=" + Secret headers = {'Content-Type': 'application/json'} json_data = json.loads(requests.get(Gtoken).content.decode()) token = json_data["access_token"]    #消息发送接口 Purl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token    #消息发送函数 def msg(title,message):     weixin_msg = {          "toparty" : Toparty,          "msgtype" : "textcard",          "agentid" : AgentID,          "textcard" : {              "title" : title,              "description" : message,              "url" : "www.wzlinux.com",              "btntxt":"更多"           }       }     print requests.post(Purl,json.dumps(weixin_msg),headers=headers)    if __name__ == '__main__':     title = sys.argv[1]        #获取第一个参数     message = sys.argv[2]      #获取第二个参数     msg(title,message)
chmod +x /usr/local/zabbix/alertscripts/wechat.py

测试脚本(1为标题 2为内容)

./wechat.py  1 2

如出现以下报错需安装独立模块

[root@localhost /]# ./wechat.py  1 2
Traceback (most recent call last):
  File "./wechat.py", line 6, in <module>
    import requests
ImportError: No module named requests
[root@localhost /]# wget https://bootstrap.pypa.io/get-pip.py
[root@localhost /]# python get-pip.py 
[root@localhost /]# pip install requests

安装完后再次执行脚本

[root@localhost /]# ./wechat.py  1 2
<Response [200]>

次结果为正常,同时微信受到标题1内容2的消息



脚本转自  https://www.linuxidc.com/Linux/2017-11/148418.htm   增加了报错的处理



zabbix 微信脚本(转贴后增加)

原文:http://blog.51cto.com/317487/2136298

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!