测试机房网络连通性和延迟(shell脚本)

2012-04-17 17:08:57

 一、生成本机房到各电信、联通、移动、教育网ping和traceroute的数据;另外搜集各个运营商ip全国的ip列表文件为ip.txt;ipedu.txt;ipdx.txt;ipcmcc.txt(移动)

脚本一:
 
  1. #!/bin/bash 
  2. ping -c 100 $1 > test/temp/temp_$1 
  3. tail -n 3 test/temp/temp_$1 
  4. traceroute $1 

脚本二:做任务计划每隔15分钟执行一次:

 
  1. #!/bin/bash 
  2. DateTime=`date "+%Y%m%d%H%M"` 
  3. Date=`date "+%Y%m%d"` 
  4. cd ~ 
  5. mkdir -p test/temp 
  6. mkdir -p test/${Date}/wt 
  7. mkdir -p test/${Date}/dx 
  8. mkdir -p test/${Date}/edu 
  9. mkdir -p test/${Date}/cmcc 
  10. while true; do 
  11. read iplist || break ; 
  12. sh ping_tr.sh $iplist >> test/${Date}/wt/$iplist${DateTime}.txt & 
  13. done < ip.txt 
  14.  
  15. while true; do 
  16. read iplist || break ; 
  17. sh ping_tr.sh $iplist >> test/${Date}/edu/$iplist${DateTime}.txt & 
  18. done < ipedu.txt 
  19.  
  20. while true; do 
  21. read iplist || break ; 
  22. sh ping_tr.sh $iplist >> test/${Date}/dx/$iplist${DateTime}.txt & 
  23. done < ipdx.txt 
  24.  
  25. while true; do 
  26. read iplist || exit ; 
  27. sh ping_tr.sh $iplist >> test/${Date}/cmcc/$iplist${DateTime}.txt & 
  28. done < ipcmcc.txt 

二、生成数据报告,发邮件到系统运维部

cront配置:20,50 7-23 * * * /bin/bash /root/net-monitor.sh

 
  1. #!/bin/bash 
  2. #2011/08/04 
  3. time=`date +%Y%m%d` 
  4. time1=`date +%Y%m%d%H%M  -d  "20 minutes ago"` 
  5. time2=`date +%H:%M -d "20 minutes ago"` 
  6. [ -d /home/larry/vnet/${time} ] || mkdir /home/larry/vnet/${time} 
  7. path='/root/test' 
  8. isp=(dx wt edu cmcc) 
  9. wt='/root/ip.txt' 
  10. dx='/root/ipdx.txt' 
  11. cmcc='/root/ipcmcc.txt' 
  12. edu='/root/ipedu.txt' 
  13. com(){ 
  14.         awk 'BEGIN{print "-------------------------------------"}' 
  15.         awk 'BEGIN{printf "| %-3s| %-16s | %-8s  |\n","ISP","IP","'$time2'"}' 
  16.         awk 'BEGIN{print "-------------------------------------"}' 
  17. for x in ${isp[@]} 
  18. do 
  19.         for i in `eval cat \\$$x` 
  20.         do 
  21.   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}' 
  22.         done 
  23.         awk 'BEGIN{print "-------------------------------------"}' 
  24. done 
  25.  
  26. com > /home/larry/vnet/${time}/${time1}.txt 
  27.  
  28. myip=`ifconfig eth0 |grep 'inet addr'|awk '{print $2}'|awk -F':' '{print $2}'` 
  29. echo "#########################################################################" >> /home/larry/vnet/${time}/${time1}.txt 
  30. echo "Packet Loss" >> /home/larry/vnet/${time}/${time1}.txt 
  31. echo "$time2 o 'clock"  >> /home/larry/vnet/${time}/${time1}.txt 
  32. 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 
  33. cat /home/larry/vnet/${time}/${time1}.txt 
  34. 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