利用微信企业号的告警功能,联动检测ICMP的shell脚本

  作者:邓聪聪

  由于设备IP众多,为了及时发现IP地址有不可达现象,利用微信的联动报警,及时发现问题,以下是脚本内容!!!

ping.sh
#!/bin/bash
###SCRIPT_NAME:icmp testing
PATH=$PATH ipfile="ip.txt"
declare -A ip_dic for key in `cat $ipfile |awk '{print $1}'`
do
ip_dic+=([$key]="")
done
ping_ip(){
dic_value=`echo ${ip_dic[$]}`
ping -c -W $ > /dev/null >&
if [ $? -ne $dic_value ]
then
S_IP=`cat $ipfile | grep -w "$1 "`
des=`cat $ipfile | grep ^"$S_ip "`
if [ $dic_value == ]
then
ip_dic[$]=""
/bin/bash /root/wechat.sh "网络中断" "$S_IP" "$des"
else
ip_dic[$]=""
/bin/bash /root/wechat.sh "网络恢复" "$S_IP" "$des"
fi
else
continue
fi
}
while true
do
for ip in `cat $ipfile|awk '{print $1}'`
do
ping_ip $ip
done
sleep
done
wechat.sh
#!/bin/bash
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
###SCRIPT_NAME:weixin.sh###
###send message from weixin for icmp monitor### CropID='your CropID '
Secret='your SecretID'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F'access_token\":\"' '{print $2}' | awk -F'"' '{print $1}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" function body() {
local int AppID=
local UserID=$
local PartyID=
local TagID=
Ent=$'\n'
Date=$(date '+%Y年%m月%d日 %H:%M:%S\n\n')
Tit=$(cat /root/title.txt)
Mes=$Date$-$-$ printf '{\n'
printf '\t"touser": "'"$User"\"",\n"
printf '\t"toparty": "'"$PartyID"\"",\n"
printf '\t"totag": "'"$TagID"\"",\n"
printf '\t"msgtype": "news",\n'
printf '\t"agentid": "'" $AppID "\"",\n"
printf '\t"news": {\n'
printf '\t"articles": [\n'
printf '{\n'
printf '\t\t"title": "'"$Tit"\","\n"
printf '\t"description": "'"$Mes"\","\n"
printf '\t}\n'
printf '\t]\n'
printf '\t}\n'
printf '}\n'
} /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
上一篇:【Linux学习】Vi / Vim编辑器—编辑器工作模式、vi编辑操作


下一篇:sendfile Linux函数