Zabbix 微信報警Python版(帶監控項波動圖片)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#Function: 微信報警python版(帶波動圖)
#Environment: python 2.7.6

import urllib2,chardet
import sys,json,re

def send_alter():             #發送報警到微信
    AppID = *****         #應用ID
    UserID = *****         #用戶ID,以|間隔可發送多人
    PartyID = 1                 #組ID
    
    GURL = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + CropID + '&corpsecret=' + Secret
    Request = urllib2.Request(GURL)
    Response = urllib2.urlopen(Request)
    access_token = eval(Response.read())['access_token']
    PURL = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    data = {
        "touser" : UserID,
        "toparty": PartyID,
        "msgtype": "text",
        "agentid": AppID,
        "text": {
            "content": Msg
        },
        "safe": 0
    }
    encode_data = json.dumps(data, encoding='utf-8', ensure_ascii=False)    #要將數據encode在json.dumps,否則微信沒法識別
    wx_req = urllib2.Request(PURL, encode_data)
    
    f = open('/home/zabbix/test','w')
    print >> f, urllib2.urlopen(wx_req).read()

def get_return(data):
    request = urllib2.Request(zabbix_url + 'api_jsonrpc.php',data)
    for key in zabbix_header:
        request.add_header(key,zabbix_header[key])

    try:
        result = urllib2.urlopen(request)
    except HTTPError, e:
        print 'The server couldn\'t fulfill the request, Error code: ', e.code
    except URLError, e:
        print 'We failed to reach a server.Reason: ', e.reason
    else:
        response=json.loads(result.read())
        return response
        result.close()

def get_authcode():                    #獲取zabbix的auth_code
    auth_data = json.dumps(
        {   
            "jsonrpc":"2.0",
            "method":"user.login",
            "params":   
                    {   
                        "user":zabbix_user,
                        "password":zabbix_passwd
                    },
            "id":0
        })
    return get_return(auth_data)['result']

def get_imgurl():        #獲取監控項波動圖片地址
    ItemID = re.search('(告警監控項:.*\()(.*)(\))', sys.argv[1]).group(2)
    png_url = zabbix_url + "chart.php?period=3600&isNow=1&itemids%5B0%5D=" + ItemID + "&type=0&profileIdx=web.item.graph&profileIdx2=29123&width=1387"
    return png_url

if __name__ == '__main__':

    defaultencoding = 'utf-8'
    if sys.getdefaultencoding() != defaultencoding:
        reload(sys)
        sys.setdefaultencoding(defaultencoding)

    CropID='*******'      #這兩個要在微信企業號裏找
    Secret='********'      #企業號裏找
    
    zabbix_url = 'http://****/zabbix/'      #zabbix路徑
    
    new_details = '<a href=\"' + get_imgurl()  + '\">點擊查看圖片</a>'
    Msg = re.sub('DETAILS', new_details, sys.argv[1])
    
    send_alter()
    

推薦的動做格式:php

產生告警!
告警主機: {HOST.NAME}
告警時間: {EVENT.DATE} 
告警監控項: {ITEM.NAME}({ITEM.ID})
告警觸發器: {TRIGGER.NAME}
告警狀態: {TRIGGER.STATUS}
告警級別: {TRIGGER.SEVERITY}
告警詳情: DETAILS

監控項值:
1.{ITEM.NAME1} ({HOST.NAME1}:{ITEM.KEY1}):{ITEM.VALUE1}
2.{ITEM.NAME2} ({HOST.NAME2}:{ITEM.KEY2}):{ITEM.VALUE2}
3.{ITEM.NAME3} ({HOST.NAME3}:{ITEM.KEY3}):{ITEM.VALUE3}
事件 ID: {EVENT.ID}    #推薦動做

接收到的信息:python

點擊查看圖片會直接看到報警監控項的波動圖(須要事先訪問過該zabbix後臺並留下cookie),如圖:web

相關文章
相關標籤/搜索