根據公司業務寫的檢測監控腳本,計劃任務每分鐘執行一次,有節點掛掉每20分鐘發送一次釘釘告警,連續5次告警就從均衡下掉該節點
但是沒有恢復通知
#!/bin/bash
#Description:yw_node_monitor,send to dingtalk
#Author:xxx
#version:1.1
source /etc/profile
URL='https://oapi.dingtalk.com/robot/send?access_token=xxx'
#URL='https://oapi.dingtalk.com/robot/send?access_token=xxx'
BASEDIR='/App/apache-jmeter-jjh-1.0.0'
remove_node(){
echo "auto remove node "$x""
}
jishu(){
t_s=`date +%s`
t_s2=`date -d "2 hours ago" +%s`
#臨時記錄時間的文件
if [ ! -f /tmp/$x ]
then
echo $t_s2 > /tmp/$x
fi
t_s2=`tail -1 /tmp/$x|awk '{print $1}'` #上次告警發生時間
echo $t_s > /tmp/$x #當前告警發生時間
v=$[$t_s-$t_s2] #時間差
echo $v
#如果距離上次告警大於1小時,說明是新的告警(不準確)
if [ $v -gt 3600 ]
then
send_dingtalk #發送通知,計數文件清0
echo "0" > /tmp/$x.txt
else
if [ ! -f /tmp/$x.txt ]
then
echo "0" > /tmp/$x.txt
fi
nu=`cat /tmp/$x.txt`
nu2=$((${nu}+1))
echo $nu2 > /tmp/$x.txt
#連續五次失敗,調用下節點函數
if [ $nu2 -gt 5 ]
then
remove_node
fi
if [ $nu2 -gt 20 ] #告警20次發一次通知
then
send_dingtalk
echo "0" > /tmp/$x.txt
fi
fi
}
send_dingtalk(){
curl ''$URL'' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "'$falure_name' 宕, IP '$x',請及時處理\n測試。。測試。。"
},
"at": {
"atMobiles": [
"xxx"
],
}
}'
}
cd $BASEDIR/log/
if [ -f result.ftl ];then
mv result.ftl result.ftl.$(date "+%Y%m%d-%H:%M:%S")
fi
$BASEDIR/bin/jmeter -n -t $BASEDIR/bin/check.jmx -l $BASEDIR/log/result.ftl
falure_ip=`grep -C 3 "<failure>true</failure>" $BASEDIR/log/result.ftl | egrep -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" | sort -n | uniq `
echo $falure_ip > $BASEDIR/log/falure_ip
if [[ ! -z $falure_ip ]];then
#遍歷每個失敗的IP
for x in $falure_ip;do
falure_name=`grep $x $BASEDIR/bin/dlg.txt | awk -F "," '{print $2}' | tr -d '\r'`
jishu
done
fi
新手,請多多指教!