测试机房网络连通性和延迟(shell脚本)
一、生成本机房到各电信、联通、移动、教育网ping和traceroute的数据;另外搜集各个运营商ip全国的ip列表文件为ip.txt;ipedu.txt;ipdx.txt;ipcmcc.txt(移动)
脚本一:
#!/bin/bash
ping -c 100 $1 > test/temp/temp_$1
tail -n 3 test/temp/temp_$1
traceroute $1
脚本二:做任务计划每隔15分钟执行一次:
#!/bin/bash
DateTime=`date "+%Y%m%d%H%M"`
Date=`date "+%Y%m%d"`
cd ~
mkdir -p test/temp
mkdir -p test/${Date}/wt
mkdir -p test/${Date}/dx
mkdir -p test/${Date}/edu
mkdir -p test/${Date}/cmcc
while true; do
read iplist || break ;
sh ping_tr.sh $iplist >> test/${Date}/wt/$iplist${DateTime}.txt &
done < ip.txt
while true; do
read iplist || break ;
sh ping_tr.sh $iplist >> test/${Date}/edu/$iplist${DateTime}.txt &
done < ipedu.txt
while true; do
read iplist || break ;
sh ping_tr.sh $iplist >> test/${Date}/dx/$iplist${DateTime}.txt &
done < ipdx.txt
while true; do
read iplist || exit ;
sh ping_tr.sh $iplist >> test/${Date}/cmcc/$iplist${DateTime}.txt &
done < ipcmcc.txt
二、生成数据报告,发邮件到系统运维部
cront配置:20,50 7-23 * * * /bin/bash /root/net-monitor.sh
#!/bin/bash
#2011/08/04
time=`date +%Y%m%d`
time1=`date +%Y%m%d%H%M -d "20 minutes ago"`
time2=`date +%H:%M -d "20 minutes ago"`
[ -d /home/larry/vnet/${time} ] || mkdir /home/larry/vnet/${time}
path='/root/test'
isp=(dx wt edu cmcc)
wt='/root/ip.txt'
dx='/root/ipdx.txt'
cmcc='/root/ipcmcc.txt'
edu='/root/ipedu.txt'
com(){
awk 'BEGIN{print "-------------------------------------"}'
awk 'BEGIN{printf "| %-3s| %-16s | %-8s |\n","ISP","IP","'$time2'"}'
awk 'BEGIN{print "-------------------------------------"}'
for x in ${isp[@]}
do
for i in `eval cat \\$$x`
do
grep -E \(---\|^rtt\) ${path}/${time}/$x/${i}${time1}.txt | awk 'BEGIN{FS="/| "}NR%2==1{a=$2}NR%2==0{printf "| %-3s| %-16s | %-8s |\n","'$x'",a,$8}'
done
awk 'BEGIN{print "-------------------------------------"}'
done
}
com > /home/larry/vnet/${time}/${time1}.txt
myip=`ifconfig eth0 |grep 'inet addr'|awk '{print $2}'|awk -F':' '{print $2}'`
echo "#########################################################################" >> /home/larry/vnet/${time}/${time1}.txt
echo "Packet Loss" >> /home/larry/vnet/${time}/${time1}.txt
echo "$time2 o 'clock" >> /home/larry/vnet/${time}/${time1}.txt
grep 'packet loss' /root/test/${time}/*/*${time1}.txt |grep -v ' 0% packet loss'|awk -F',' '{print $1 $2 $3}'| awk -F'/' '{print $5 "/" $6}' >> /home/larry/vnet/${time}/${time1}.txt
cat /home/larry/vnet/${time}/${time1}.txt
mail -s "${time1}_$myip network monitor" sys@ilarry.com -- -f cnc.2@ilarry.com < /home/larry/vnet/${time}/${time1}.txt
报过内容:
-------------------------------------
| ISP| IP | 22:00 |
-------------------------------------
| dx | 61.144.56.1 | 38.136 |
| dx | 121.15.148.1 | 41.215 |
| dx | 222.177.80.222 | 56.307 |
| dx | 61.132.78.65 | 27.718 |
| dx | 202.96.112.1 | 37.504 |