微信告警

微信告警

zabbix支持自定義報警方式,那麼咱們就能夠藉助各類經常使用通訊工具的api進行告警,這裏我使用比較經常使用的微信做爲報警工具,固然也能夠使用QQ,釘釘等做爲報警工具。api

這裏我使用微信企業號的方式進行告警,註冊微信企業號,和添加bash

報警腳本的編寫

#!/bin/bash
# 公司ID
CropID='xxxxx'
# 密碼
Secret='xxxxx'
# 獲取發送信息所須要token的URL
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
# 獲取token,這個命令隨着微信api的變化須要相應的變化
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F '"' '{print $10}')
# 查看是否正確獲取token
# echo $Gtoken
# 發送消息的URL
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

function body() {
        # 企業號中的應用id
        local int AppID=1000002
        # 部門成員id,zabbix中定義的微信接收者
        local UserID=$1
        # 部門id,定義了範圍,組內成員均可接收到消息
        local PartyID=1
        # 過濾出zabbix傳遞的第三個參數
        local Msg=$(echo "$@" | cut -d" " -f3-)
        printf '{\n'
        printf '\t"touser": "'"$UserID"\"",\n"
        printf '\t"toparty": "'"$PartyID"\"",\n"
        printf '\t"msgtype": "text",\n'
        printf '\t"agentid": "'" $AppID "\"",\n"
        printf '\t"text": {\n'
        printf '\t\t"content": "'"$Msg"\""\n"
        printf '\t},\n'
        printf '\t"safe":"0"\n'
        printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL &> /dev/null

各個ID的位置

CorpID

在‘個人企業中’查找CorpID微信

微信告警

AppID和Secret

AppID對應的是Agentidcurl

微信告警

這個頁面的位置:企業應用->自建應用(建立自建應用的方法很簡單,惟一須要注意的是可見範圍,不在該可見範圍裏的用戶,不能夠給他發送消息)ide

UserID和PartyID

PartyID對應的是部門ID,UserID對應的是帳號工具

微信告警

``
相關文章
相關標籤/搜索