1. 程式人生 > >zabbix 微信腳本(轉貼後增加)

zabbix 微信腳本(轉貼後增加)

gen con name access extc ins 一個 tca 微信

#!/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 微信腳本(轉貼後增加)