测试机房网络连通性和延迟(shell脚本)
2012-04-17 17:08:57
一、生成本机房到各电信、联通、移动、教育网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 | | dx | 61.130.152.34 | 39.633 || dx | 61.132.128.1 | 24.814 || dx | 202.105.177.100 | 44.165 |-------------------------------------| wt | 116.114.16.58 | 21.583 || wt | 218.56.56.1 | 17.534 || wt | 125.39.101.1 | 28.832 || wt | 218.12.255.186 | 11.877 || wt | 58.23.128.22 | 79.854 || wt | 202.102.245.71 | 13.890 || wt | 120.80.2.106 | 70.563 || wt | 221.10.28.1 | 38.923 || wt | 221.5.247.1 | 38.667 || wt | 58.20.125.210 | 64.651 || wt | 58.19.238.1 | 26.398 |-------------------------------------| cmcc| 218.202.1.188 | 100.263 || cmcc| 218.204.81.1 | 51.917 || cmcc| 218.206.191.1 | 41.055 || cmcc| 221.130.13.13 | 36.846 |-------------------------------------#######################################################Packet Loss22:00 o 'clockcmcc/120.203.214.177201201032200.txt:100 packets transmitted 99 received 1% packet losscmcc/218.201.21.158201201032200.txt:100 packets transmitted 95 received 5% packet lossdx/222.217.99.18201201032200.txt:100 packets transmitted 96 received 4% packet lossdx/61.139.48.66201201032200.txt:100 packets transmitted 0 received 100% packet lossedu/115.156.255.14201201032200.txt:100 packets transmitted 96 received 4% packet lossedu/202.112.49.1201201032200.txt:100 packets transmitted 99 received 1% packet lossedu/202.175.26.214201201032200.txt:100 packets transmitted 99 received 1% packet lossedu/202.179.240.1201201032200.txt:100 packets transmitted 94 received 6% packet lossedu/210.40.119.22201201032200.txt:100 packets transmitted 48 received 52% packet lossedu/59.75.1.1201201032200.txt:100 packets transmitted 99 received 1% packet losswt/120.80.2.106201201032200.txt:100 packets transmitted 99 received 1% packet loss