shell IP地址監控

#!/bin/bash
#20190710
#ip.txt存放IP地址 ip2.txt存放項目名稱 IP地址 等信息
#作爲自動化web監控的衍生,針對網站多鏈路的需求,寫了IP地址的監控,需要更高頻率監控,不用crontab,直接腳本內while True無限循環
cur_time(){
        date "+%Y/%m/%d %H:%M:%S"
}
systemname(){
name=`cat ip2.txt|grep $ip|wc -l`
if [ $name -eq 1 ];then
        cat ip2.txt|grep $ip
else
        echo "$ip"
fi
}

[ ! -f /root/ip.txt ] && echo "ip.txt文件不存在" && exit 1

while read ip
do
	[ -z $ip  ] && echo "ip.txt存在空格 檢查文件格式" && exit 1
        for ((i=1;i<6;i++))
        do
                ping -W 1 -n -c 1 $ip &>/dev/null
                if [ $? = 0 ];then
                        echo "$(cur_time) ping $ip Successful" >> connect.log
                        break
                elif [ $i = 5 ];then
                        echo "$(cur_time) $(systemname)  $(expr $i \* 2)秒未迴應PING報文,請檢查!"|mail -s "【重要告警】IP不可達" [email protected]
                        echo "$(cur_time) $(systemname)  $(expr $i \* 2)秒未迴應PING報文" >> connectfail.log
                else
                        echo "$(cur_time) 第$i次檢查$ip未迴應PING報文" >> connectfail.log
                        sleep 2
                fi
        done
done < /root/ip.txt


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