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

Latest from the blog

服务器ping监控脚本

标签: 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 {} \;

Linux检查网络并自动重启网卡

标签: Shell Linux ping

添加crontab定时任务,可以用作心跳检测.


[root@mail src]# cat check_network.sh 
#!/bin/bash
date="`date '+%Y-%m-%d %H:%M:%S'`"
ip="116.205.11.145"
nic="eth0"
lost_rate=`ping -c 8 -w 8 $ip | grep 'packet loss' \
                | awk -F'packet loss' '{ print $1 }' \
                | awk '{ print $NF }' | sed 's/%//g'`

if [ $lost_rate -eq 0 ]
then
echo "network_ok $date $ip $nic lose $lost_rate" >>/var/log/network_ok.log
elif [ $lost_rate -le 100 ]
then
echo "network_error $date $ip $nic lose $lost_rate" >>/var/log/network_error.log 
/sbin/service network restart
else
echo "network_error $date $ip $nic lose $lost_rate" >>/var/log/network_error.log
/sbin/service network restart
fi