zabbixUser=‘Admin’
zabbixPass=‘zabbix’
zabbixUrl=“yourURL”
corpID=‘xxxx’
secret=‘xxxx’
这几个地方要改下
cat > /usr/lib/zabbix/alertscripts/image2.sh <<'EOF'
#!/bin/bash
#微信发送消息参数
appID=7
userID=$1
subject=$2
message=$3
#获取发送消息凭证access_token
getToken(){
corpID='xxxx'
secret='xxxx'
token_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpID&corpsecret=$secret"
accessToken=$(curl -s -G $token_url | awk -F "\"" '{ print $10}' )
}
#获取itemID
itemID=$(echo $subject | awk -F "#" '{print $6}')
#获取图片并保存至/tmp目录下
getGraph(){
zabbixUser='Admin'
zabbixPass='zabbix'
zabbixUrl="yourURL"
cookie="/tmp/cookie"
imagePath="/tmp/"
imageUrl="$imagePath$itemID.png"
width=640
high=220
curl -s -c $cookie -b $cookie -d "name=$zabbixUser & password=$zabbixPass & autologin=1 &enter=Sign+in" $zabbixUrl/zabbix/index.php
curl -s -b $cookie -F "itemids=$itemID" -F "width=$width" -F "high=$high" $zabbixUrl/zabbix/chart.php>$imageUrl
}
#上传图片至微信临时素材库并获取mediaID
postGraph(){
murl="https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$accessToken&type=image"
mediaID=$(curl -s -F media=@"$imageUrl" $murl | awk -F "\"" '{ print $14}')
}
#发送告警信息
getToken
url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$accessToken"
getGraph
postGraph
if echo $subject | grep "PROBLEM";then
curl --data-ascii "{\"touser\": \"$1\",\"msgtype\":\"text\",\"agentid\":\"$appID\",\"text\":{\"content\":\"$3\"},\"safe\":\"0\"}" $url
curl --data-ascii "{\"touser\": \"$1\",\"msgtype\":\"image\",\"agentid\":\"$appID\",\"image\":{\"media_id\":\"$mediaID\"},\"safe\":\"0\"}" $url
rm -f $imageUrl
else
curl --data-ascii "{\"touser\": \"$1\",\"msgtype\":\"text\",\"agentid\":\"$appID\",\"text\":{\"content\":\"$3\"},\"safe\":\"0\"}" $url
fi
EOF