联系我们:416326801#qq.com(#改成@)

img1

服务器ping监控脚本

1179 Views 标签: Shell Linux ping

[root@zzfwb src]# cat monitor_ping.sh
#!/bin/sh

#ping count
COUNT=30

#if lose% > max%
MAX=80

#nowtime
LogFileName=/var/ping/monitor_$(date "+%Y%m%d%H%M%S").log
HostFileName=/var/ping/host_$(date "+%Y%m%d%H%M%S").txt

#list_hosts
curl  http://api.zzfwb.com/monitor/list_hosts.php >  $HostFileName

for ipadd in `cat $HostFileName`
do
  timing=`date "+%Y-%m-%d %H:%M:%S"`
  PingFileName=/var/ping/ping_${ipadd}_$(date "+%Y%m%d%H%M%S").log
  ping -i 0.2 $ipadd -c $COUNT > $PingFileName
  losspag=`grep "packet loss" $PingFileName |awk '{print $6}' |sed 's/%//g'`
  FLAG=`grep "min/avg/max" $PingFileName -c`
  if [ $losspag -ge $MAX ] ;
  then
    STR=`echo -n "$timing|$ipadd|$losspag|$COUNT|down|$FLAG" | base64`
    echo "$timing|$ipadd|$losspag|$COUNT|down|$FLAG" >> $LogFileName
  else
    STR=`echo -n "$timing|$ipadd|$losspag|$COUNT|up|$FLAG" | base64`
    echo "$timing|$ipadd|$losspag|$COUNT|up|$FLAG" >> $LogFileName
  fi

 /usr/bin/curl  http://api.zzfwb.com/monitor/send_ping.php?str=$STR >> $LogFileName

 echo '' >>  $LogFileName
 echo '' >>  $LogFileName

done

cd /var/ping
find /var/ping -name "*.log" -ctime +1 -exec rm -f {} \;

如果觉得我的文章对您有用,打赏一下呗.

wxpay.png