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


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