zabbix通過微信企業號發送圖文消息

    我有篇博客寫到如何用微信發送告警消息,實現了發送文字消息,不能帶圖片,這樣不是很直觀,最近又研究了一下如何發送圖片,寫了腳本實現了發送文字+圖片的告警。

    效果如下:

wKioL1dFNZTC2wqpAAHBpvuUFEE956.png

先發送文字消息,下面挨着graph。


    這裏只提供腳本和思路,具體配置請看我的另一篇博客:(http://wuhf2015.blog.51cto.com/8213008/1688614#662543)


    實現方式:

  1. 在Action中設置Default Subject的格式爲"狀態:#{TRIGGER.STATUS}#主機:#{HOST.NAME}#鍵名:#{ITEM.KEY}#"。這樣可以在腳本里做判斷,如果狀態爲OK則不發送圖片,如果狀態爲problem則發送圖片。

    wKiom1dFNu7AIttqAABXO-9FBXA484.png

  2. 腳本有了{HOST.NAME}和{ITEM.KEY}這兩個參數後,可以通過查詢Mysql或者調用zabbix_api的方式得到我們必要的變量ItemID,有了這個變量才能獲取圖片。

  3. 通過itemid從zabbix中獲取圖片後,我們需要將圖片上傳到微信企業號的臨時素材裏,上傳後我們會得到一個media_id

  4. 我們將media_id通過image格式發送出來就能收到圖片消息了。


wKiom1dFOZKzuEjuAADj2YUdIxo690.png


    腳本:

#!/bin/bash
#SCRIPT_NAME:weixin.sh
#get graph to you
#V2-2016-05-23
#wuhf
#email:[email protected]

gettoken() {
ID='xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
URL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$ID&corpsecret=$SECRET"
Gtoken=$(/usr/bin/curl -s -G $URL | awk -F\" '{print $4}')
}
gettoken

AppID=3
PartyID=2
UserID="$1"
Title="$2"
Msg="$3"

getitemid() {
DBServer="127.0.0.1"
DBUser="root"
DBName="zabbix"
DBPort="3306"
DBPassword="123456"
Hostname=$(echo $Title |awk -F"#" '{print $4}')
Key=$(echo $Title |awk -F"#" '{print $6}')
Return=$(sudo mysql -u$DBUser -h$DBServer -P$DBPort -p$DBPassword --database=$DBName -e "select itemid from items where hostid=(select hostid from hosts where name = \"$Hostname\" ) and key_ = \"$Key\";")
Itemid=$(echo $Return |awk '{print $2}')
}
#獲取itemid可以通過數據庫查詢,也可以通過zabbix_api,我自己用的是api
#getitemid

getgraph() {
zabbix_user='xxxxxxxxxxxxxxxxxx'
zabbix_pass='xxxxxxxxxxxxxxxxxx'
zabbix_url="http://127.0.0.1/zabbix/"
cookie="/tmp/cookie"
image_path="/tmp/"
STime=$(date +%Y%m%d%H%M%S)
Period=7200
Width=640
High=200
sudo /usr/bin/curl -s -c $cookie -b $cookie -d "name=$zabbix_user&password=$zabbix_pass&autologin=1&enter=Sign+in" $zabbix_url/index.php
sudo /usr/bin/curl -s -b $cookie -F "itemids=$Itemid" -F "period=$Period" -F "stime=$STime" -F "width=$Width" -F "high=$High" $zabbix_url/chart.php > $image_path$Itemid.png
}
#定義這個函數是要利用上面的itemid獲取圖片保存到/tmp下面
#getgraph

postgraph() {
PIC_URL="$image_path$Itemid.png"
M_URL="https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$Gtoken&type=image"
Media=$(sudo curl -s -F media=@"$PIC_URL" "$M_URL" | awk -F"\"" '{print $8}')
}
#定義這個函數是要將/tmp下面的png文件上傳到微信臨時素材接口,永久素材接口有上限5000,所以不建議使用
#postgraph
#debug
#echo $PIC_URL$Media > /tmp/pic.txt

image() {
    printf '{\n'
    printf '\t"touser": "'"$UserID"\"",\n"
    printf '\t"toparty": "'"$PartyID"\"",\n"
    printf '\t"msgtype": "image",\n'
    printf '\t"agentid": "'" $AppID "\"",\n"
    printf '\t"image": {\n'
    printf '\t\t"media_id": "'"$Media"\""\n"
    printf '\t},\n'
    printf '\t"safe":"0"\n'
    printf '}\n'
}

text() {
    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'
}

#我這裏定義image和text兩個格式,是要將一條告警消息分兩次發送,先發送text然後發送圖片,原因就是微信企業號提供的news發送的爲圖片鏈接,如果zabbix是內網監聽,那麼鏈接就無意義了,而mpnews每天最多發送100條,超過就返回錯誤,所以放棄。

url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

if echo $Title | grep "PROBLEM" ;then
getitemid
getgraph
postgraph
sudo /usr/bin/curl --data-ascii "$(text)" $url
sudo /usr/bin/curl --data-ascii "$(image)" $url
sudo rm -f $PIC_URL
else
sudo /usr/bin/curl --data-ascii "$(text)" $url
fi

    互相學習:

    腳本參照了下面這兩篇博客:


    注意:

    1.key_ 中不能帶引號,例如grpsum{"zabbix server",net.if.in[eth0],last,0}這樣就不正確

    2.Action中的Default Subject項一定按照我寫的來

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章