shell寫的告警次數(shù)控制及恢復(fù)示例代碼
更新時間:2013年02月04日 17:15:04 作者:
自己寫的一個監(jiān)控腳本,發(fā)現(xiàn)告警短信次數(shù)未控制時,垃圾短信N多。經(jīng)過思考,做了個簡單的控制,同一故障只發(fā)送二次。之后不再發(fā)送?;謴?fù)后發(fā)送恢復(fù)短信
俺也是新手,寫的有點(diǎn)老土,權(quán)當(dāng)練手了,供初學(xué)的朋友們參考吧。
#!/bin/bash
if [ $succ_ping -gt $((ping_count-1)) ];then
if [ -f ./alarm_file/alarm_$host ];then
rm -rf ./alarm_file/alarm_$host
#發(fā)送故障消除短信通知--調(diào)用insert_table
messages="$host服務(wù)器通迅恢復(fù)正常`date "+%Y%m%d %H:%M:%S"`"
echo "$messages" >>./log/ping_err_recover.log
insert_table
else
echo "本次檢測結(jié)果---->$host服務(wù)器ping檢測正常"
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器ping檢測正常,ping次數(shù)$ping_count,接收$succ_ping" >>./log/ping_log
fi
else
echo "本次檢測結(jié)果---->$host服務(wù)器有丟包現(xiàn)象,丟包率為:$loss_ping"
messages="`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,丟包率為:$loss_ping"
#對警次數(shù)進(jìn)行處理,超過2次將不告警
#格式host a(告警次數(shù))
#已經(jīng)出現(xiàn)告警的次數(shù)加1
echo "告警服務(wù)器為$host"
if [ -f ./alarm_file/alarm_$host ];then
#取出當(dāng)前的告警次數(shù)并加1
a=`awk '{print$2}' ./alarm_file/alarm_$host`
b=$(($a+1))
echo "$host $b" >./alarm_file/alarm_$host
alarm_count=`awk '{print$2}' ./alarm_file/alarm_$host`
if [ $alarm_count -gt $sms_count ];then
echo "超過2次不發(fā)送短信"
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,接收$succ_ping,丟包率為:$loss_ping,超過二次不發(fā)送。" >>./log/ping_err_log
else
#未超過2次調(diào)用insert_table函數(shù)發(fā)送短信
echo "未超過2次"
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,接收$succ_ping,丟包率為:$loss_ping">>./log/ping_err_log
insert_table
fi
else
#第一次告警
echo "第一次告警"
echo "$host 1" >./alarm_file/alarm_$host
insert_table
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,接收$succ_ping,丟包率為:$loss_ping" >>./log/ping_err_log
fi
fi
done
復(fù)制代碼 代碼如下:
#!/bin/bash
if [ $succ_ping -gt $((ping_count-1)) ];then
if [ -f ./alarm_file/alarm_$host ];then
rm -rf ./alarm_file/alarm_$host
#發(fā)送故障消除短信通知--調(diào)用insert_table
messages="$host服務(wù)器通迅恢復(fù)正常`date "+%Y%m%d %H:%M:%S"`"
echo "$messages" >>./log/ping_err_recover.log
insert_table
else
echo "本次檢測結(jié)果---->$host服務(wù)器ping檢測正常"
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器ping檢測正常,ping次數(shù)$ping_count,接收$succ_ping" >>./log/ping_log
fi
else
echo "本次檢測結(jié)果---->$host服務(wù)器有丟包現(xiàn)象,丟包率為:$loss_ping"
messages="`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,丟包率為:$loss_ping"
#對警次數(shù)進(jìn)行處理,超過2次將不告警
#格式host a(告警次數(shù))
#已經(jīng)出現(xiàn)告警的次數(shù)加1
echo "告警服務(wù)器為$host"
if [ -f ./alarm_file/alarm_$host ];then
#取出當(dāng)前的告警次數(shù)并加1
a=`awk '{print$2}' ./alarm_file/alarm_$host`
b=$(($a+1))
echo "$host $b" >./alarm_file/alarm_$host
alarm_count=`awk '{print$2}' ./alarm_file/alarm_$host`
if [ $alarm_count -gt $sms_count ];then
echo "超過2次不發(fā)送短信"
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,接收$succ_ping,丟包率為:$loss_ping,超過二次不發(fā)送。" >>./log/ping_err_log
else
#未超過2次調(diào)用insert_table函數(shù)發(fā)送短信
echo "未超過2次"
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,接收$succ_ping,丟包率為:$loss_ping">>./log/ping_err_log
insert_table
fi
else
#第一次告警
echo "第一次告警"
echo "$host 1" >./alarm_file/alarm_$host
insert_table
echo "`date "+%Y%m%d %H:%M:%S"`--->$host服務(wù)器有丟包現(xiàn)象,接收$succ_ping,丟包率為:$loss_ping" >>./log/ping_err_log
fi
fi
done
相關(guān)文章
Shell腳本實(shí)現(xiàn)非法IP登陸自動報警
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)非法IP登陸自動報警,本文實(shí)現(xiàn)非指定IP段登錄服務(wù)器時使用郵件報警,需要的朋友可以參考下2015-02-02
使用ubuntu搭建公網(wǎng)個人郵件服務(wù)器(基于postfix,dovecot,mysql)
這篇文章主要介紹了基于ubuntu搭建公網(wǎng)個人郵件服務(wù)器(基于postfix,dovecot,mysql),免費(fèi)的郵箱每天發(fā)信數(shù)量是有限制的,所以呢就想著搭建一個自己的郵件服務(wù)器,需要的朋友可以參考下2019-06-06
Linux下is not in the sudoers file的解決
當(dāng)我們使用sudo命令切換用戶的時候可能會遇到提示以下錯誤:用戶名 is not in the sudoers file.本文給大家分享原因分析及解決方案,感興趣的朋友跟隨小編一起看看吧2023-02-02
Linux crontab定時執(zhí)行Shell腳本需要執(zhí)行特定的命令時解決思路
今天小編就為大家分享一篇關(guān)于Linux crontab定時執(zhí)行Shell腳本需要執(zhí)行特定的命令時解決思路,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
使用ntpdate工具校正linux服務(wù)器時間(實(shí)現(xiàn)方法)
下面小編就為大家?guī)硪黄褂胣tpdate工具校正linux服務(wù)器時間(實(shí)現(xiàn)方法)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
關(guān)于Linux反空閑設(shè)置的兩種方法總結(jié)
下面小編就為大家?guī)硪黄P(guān)于Linux反空閑設(shè)置的兩種方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03

