linux 测节点脚本

作者: admin 分类: linux 发布时间: 2010-11-14 10:25 ė 6 没有评论

#!/usr/bin/perl
#####################################
# version 0.02
# Last Update: 2008-06-06
#####################################
# History:
# 0.02 添加注释,并支持平均速度输出排序
# 0.01 完成代码
#####################################
#
# Copyright 2008, Mumonitor <mumonitor@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# 设定输出结果的文件名
$result_filename=’./ping_result.log’;

$argv=@ARGV;
if (($argv != 2) && ($argv != 3)) {
   print “Useage: ./ping.pl ctc|cnc|edu minutes [-s]n”;
   print “n”;
   print “For Example: n”;
   print “t ./ping.pl ctc 10n”;
   print “n”;
   print “Please report bug <mumonitor@gmail.com>, Thank you!n”;
   print “n”;
   exit;
}
$idc=$ARGV[0];

# 转换为妙
$time=$ARGV[1]*60;

# 设定测试点
if ( $idc eq ‘cnc’ ) {
    %ping_list=(
 ‘219.158.28.121’=>’中国网通骨干网’,
 ‘202.108.100.166’=>’中国网通北京网通’,
 ‘218.69.33.1’=>’天津网通’,
 ‘220.248.112.1’=>’上海网通’,
 ‘221.192.1.1’=>’河北石家庄网通’,
 ‘218.26.176.1’=>’山西太原网通’,
 ‘60.31.195.1’=>’内蒙古呼和浩特网通’,
 ‘218.25.255.1’=>’辽宁大连网通’,
 ‘221.8.96.1’=>’吉林长春网通’,
 ‘218.7.249.154’=>’黑龙江哈尔滨网通’,
 ‘218.59.169.109’=>’山东威海网通’,
 ‘202.111.148.1’=>’河南淮阳网通’,
 ‘58.240.48.43’=>’江苏南京网通’,
 ‘60.12.193.1’=>’浙江湖州网通’,
 ‘221.3.131.1’=>’云南昆明网通’,
 ‘221.10.239.1’=>’四川成都网通’,
 ‘221.11.1.1’=>’陕西西安网通’,
 ‘221.7.129.163’=>’广西南宁网通’,
 ‘58.22.97.1’=>’福建福州网通’,
 ‘221.5.196.1’=>’重庆网通’,
 ‘221.8.18.8’=>’长春网通’,
 ‘202.97.224.68’=>’黑龙江网通’,
 ‘202.99.160.1’=>’河北网通1′,
 ‘202.96.69.38’=>’大连网通’,
 ‘221.10.216.98’=>’四川网通’,
 ‘60.210.17.1’=>’淄博网通’,
 ‘60.209.5.254’=>’青岛网通’,
 ‘60.12.166.1’=>’金华网通’,
 ‘58.19.183.1’=>’襄樊网通’,
 ‘221.208.172.1’=>’哈尔滨网通’,
 ‘60.31.255.115’=>’内蒙古网通’,
 ‘202.99.192.68’=>’山西网通’,
 ‘202.99.160.68’=>’河北网通2′,
 ‘218.104.32.106’=>’江苏网通’,
 ‘222.138.109.1’=>’开封网通’,
 ‘218.107.56.1’=>’广东网通’,
 ‘210.52.149.2’=>’湖北网通’,
 ‘60.18.94.1’=>’辽宁网通’,
);
} elsif ( $idc eq ‘ctc’ ) {
    %ping_list=(
 ‘61.152.188.1’=>’上海电信1′,
 ‘61.129.51.254’=>’上海电信2′,
 ‘218.1.64.33’=>’上海热线’,
 ‘61.145.125.229’=>’广东互联星空_广州电信’,
 ‘61.140.60.90’=>’广东电信’,
 ‘218.18.104.1’=>’深圳电信 ‘,
 ‘218.16.239.129’=>’汕头电信’,
 ‘218.75.107.60’=>’浙江互联星空_杭州电信’,
 ‘61.186.95.92’=>’湖南互联星空_长沙电信’,
 ‘221.236.17.1’=>’四川互联星空_成都电信’,
 ‘61.139.33.1’=>’绵阳电信 ‘,
 ‘222.176.2.214’=>’重庆电信’,
 ‘220.179.251.234’=>’安徽电信’,
 ‘202.102.192.80’=>’安徽合肥电信’,
 ‘219.148.197.6’=>’辽宁沈阳电信’,
 ‘219.146.11.20’=>’山东互联星空_济南电信’,
 ‘58.56.19.129’=>’山东电信’,
 ‘222.173.123.1’=>’青岛电信 ‘,
 ‘219.148.62.194’=>’河北石家庄电信’,
 ‘58.53.192.150’=>’湖北互联星空_湖北电信’,
 ‘218.77.178.229’=>’海南互联星空_海口电信’,
 ‘219.141.62.111’=>’贵州互联星空_贵阳电信’,
 ‘61.166.150.110’=>’云南互联星空_曲靖电信’,
 ‘218.30.85.83’=>’陕西互联星空_西安电信’,
 ‘218.30.64.121’=>’北京互联星空_西安电信’,
 ‘61.134.40.138’=>’陕西渭南 ‘,
 ‘60.164.225.1’=>’甘肃互联星空_甘肃电信’,
 ‘219.159.67.2’=>’广西互联星空_南宁电信’,
 ‘202.100.128.201’=>’青海互联星空_西宁电信’,
 ‘202.100.109.157’=>’宁夏互联星空_石嘴山电信’,
 ‘219.150.150.1’=>’河南互联星空_郑州电信’,
 ‘221.238.193.1’=>’天津互联星空_天津电信’,
 ‘202.109.204.150’=>’福建福州电信’,
 ‘61.131.11.1’=>’福建泉州 ‘,
 ‘220.175.8.1’=>’江西电信’,
 ‘218.65.103.201’=>’江西南昌电信’,
 ‘61.186.95.92’=>’湖南电信’,
 ‘60.190.223.1’=>’绍兴电信 ‘,
 ‘219.150.150.150’=>’河南电信 ‘,
 ‘219.150.32.184’=>’天津电信  ‘,
 ‘61.139.37.1’=>’南充电信’,
 ‘219.149.194.31’=>’吉林互联星空_长春电信’,
 ‘219.148.162.4’=>’内蒙古互联星空_呼和浩特电信’,
 ‘219.147.130.76’=>’黑龙江互联星空_哈尔滨电信’,
 ‘220.182.54.199’=>’西藏互联星空_拉萨电信’,
 ‘61.128.101.1’=>’新疆互联星空_阿克苏电信’,
        ‘220.178.124.16’=>’….’,
        ‘202.97.40.13’=>’….’,
        ‘219.153.13.1’=>’….’,
        ‘202.109.251.32’=>’….’,
        ‘61.178.8.242’=>’….’,
        ‘58.61.157.141’=>’….’,
        ‘59.36.96.1’=>’….’,
        ‘61.145.112.8’=>’….’,
        ‘58.59.134.240’=>’….’,
        ‘219.151.1.15’=>’….’,
        ‘219.148.62.194’=>’….’,
        ‘219.147.130.76’=>’…..’,
        ‘219.150.155.1’=>’….’,
        ‘61.183.0.36’=>’….’,
        ‘222.240.142.148’=>’….’,
        ‘202.102.34.254’=>’….’,
        ‘218.65.103.1’=>’….’,
        ‘219.149.194.31’=>’….’,
        ‘219.148.197.154’=>’….’,
        ‘202.100.100.169’=>’….’,
        ‘219.150.32.184’=>’….’,
        ‘219.146.19.2’=>’….’,
        ‘222.173.1.66’=>’….’,
        ‘61.152.87.78’=>’….’,
        ‘61.157.77.1’=>’….’,
        ‘219.150.32.190’=>’….’,
        ‘220.182.54.1’=>’….’,
        ‘222.83.4.50’=>’….’,
        ‘222.221.5.1’=>’….’,
        ‘202.96.118.167’=>’….’,
        ‘218.30.15.12’=>’….’,
);
} elsif ($idc eq ‘edu’ ) {
    %ping_list=(
 ‘202.112.128.1’=>’北京航空航天大学’,
 ‘166.111.8.28’ =>’北京清华大学’,
 ‘202.114.0.242’ =>’湖北武汉华中科技大学’,
 ‘202.117.0.20’ =>’陕西西安交通大学’,
 ‘202.112.26.34’ =>’上海交通大学’,
 ‘202.203.128.33’ =>’云南昆明教育网’,
 ‘202.115.64.33’ =>’四川成都西南交通大学’,
 ‘202.201.48.2’ =>’甘肃兰州西北师范大学’,
 ‘202.116.160.33’ =>’广州华南农业大学’,
);
}

# 计数器,用来区分第一次记录ping值和丢包率
$count = 1;

chomp ($now_time=`/bin/date +%s`);

# 设定测试结束时间
$end_time=$now_time+$time;

# 判断现在时间是否小于结束时间
while ($now_time <= $end_time) {
  foreach $i (keys %ping_list) {
    if ($count == 1) {
         $server_count{$ping_list{$i}}=1;
    }

    # 测试ping数据
    open(PROCESS, “/bin/ping -c 1 $i | grep icmp_seq |”);
    $ping = <PROCESS>;
    close(PROCESS);
   
    # 判断计数单位ms和usec
    if ( $ping =~ m/(.*time=)(.*) (ms|usec)/ ) {
        # 如果非常非常非常快,到了微妙级别,则…
        if ($3 eq “usec”) {
            # 如果是第一次的数值,则直接记录
            if ($count==1) {
                $average_num=($2/1000+$result{$ping_list{$i}});
                $rate=($rate_tmp+$result_rate{$ping_list{$i}});
            } else {
                # 两次的结果相加然后除以2
                $average_num=($2/1000+$result{$ping_list{$i}})/2;
                $rate_tmp=0;
                $rate=($rate_tmp+$result_rate{$ping_list{$i}})/2;
            }
            $result{$ping_list{$i}}=$average_num;
            $result_rate{$ping_list{$i}}=$rate;
        }else {
            # 如果是第一次的数值,则直接记录
            if ($count==1) {
                $average_num=($2+$result{$ping_list{$i}});
                $rate=($rate_tmp+$result_rate{$ping_list{$i}});
            } else {
                # 两次的结果相加然后除以2
                $average_num=($2+$result{$ping_list{$i}})/2;
                $rate_tmp=0;
                $rate=($rate_tmp+$result_rate{$ping_list{$i}})/2;
            }
            $result{$ping_list{$i}}=$average_num;
            $result_rate{$ping_list{$i}}=$rate;
        }
    }else{
        # ping没有输出结果,或者是timeout
        # 第一次则直接记录
         if ($count==1) {
             $result{$ping_list{$i}}=0;
             $rate=100;
         } else {
             # 丢包率需要两次结果相加,平均值则不作记录
             $rate_tmp=100;
             $rate=($rate_tmp+$result_rate{$ping_list{$i}})/2;
         }
         $result_rate{$ping_list{$i}}=$rate;
    }
  }

  $count += 1;
  chomp ($now_time=`/bin/date +%s`);

  # 输出一次的结果到文件
  open LOG, “>$result_filename”;
  if ($ARGV[2] eq ‘-s’) {
      foreach $i (sort (sort_values (keys %result))) {
          printf LOG “%s %.2f %.2f%n” , $i,$result{$i},$result_rate{$i};
      }
  } else {
      foreach $i (sort (keys %result)) {
          printf LOG “%s %.2f %.2f%n” , $i,$result{$i},$result_rate{$i};
      }
  }

  close LOG;
}

sub sort_values {
    $result{$a} <=> $result{$b}
}

下面输出到HTML中

#!/bin/sh
date=$(date +%Y%m%d_%T)
echo $date
cd /root
perl ping.pl cnc 10
/bin/awk ‘{print $1″t”$2″,” $3″………………………………………………………………………………………………………………………………………………………………………………………”}’ ping_result.log > /var/www/html/cnc.html && rm -rf ping_result.log
perl ping.pl ctc 10
/bin/awk ‘{print $1″t”$2″,” $3″………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………….”}’ ping_result.log > /var/www/html/ctc.html && rm -rf ping_result.log
#crontab -e
#0 10,15,20 * * * sh /root/idc.sh

本文出自 小Q,转载时请注明出处及相应链接。

本文永久链接: http://www.linuxqq.com/archives/243.html

0
更多
Ɣ回顶部