mon fping.monitor 已修改为可供monitor调用20121116在

本文介绍了一个Perl脚本,用于通过ICMP回显检查主机可达性。该脚本可以根据不同选项配置重试次数、超时时间等参数,并报告不可达、响应缓慢及存活的主机列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/perl
#
# Return a list of hosts which not reachable via ICMP echo
#
# Jim Trocki, trockij@transmeta.com
#
# $Id: fping.monitor 1.7 Mon, 27 Aug 2001 14:22:45 -0400 trockij $
#
#    Copyright (C) 1998, Jim Trocki
#
#    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
#
use strict;


use Getopt::Std;


my %opt;
getopts ("ahr:s:t:T", \%opt);


sub usage
{
    print <<EOF;
usage: fping.monitor [-a] [-r num] [-s num] [-t num] [-T] host [host...]


    -a          only report failure if all hosts are unreachable
    -r num      retry "num" times for each host before reporting failure
    -s num      consider hosts which respond in over "num" msecs failures
    -t num      wait "num" msecs before sending retries
    -T          traceroute to each failed host. CAUTION: this may cause
                this monitor to hang for a very long time


EOF


    exit 0;
}


usage if ($opt{"h"});


my $TIMEOUT = $opt{"t"} || 2000;
my $RETRIES = $opt{"r"} || 3;
my $CMD = "/usr/local/sbin/fping -e -r $RETRIES -t $TIMEOUT";
my $START_TIME = time;
my $END_TIME;
my @hosts;
my $hostnum=@ARGV;
 $hostnum--;
exit 0 if ($hostnum == 0);


my $count=0;
while ($count<$hostnum) {
    @hosts[$count]=@ARGV[$count];
        $count++;
    }


open (IN, "$CMD @hosts 2>&1 |") ||
        die "could not open pipe to fping: $!\n";


my @unreachable;
my @alive;
my @addr_not_found;
my @slow;


while (<IN>)
{
    chomp;
    if (/^(\S+).*unreachable/)
    {
        push (@unreachable, $1);
    }


    elsif (/^(\S+) is alive \((\S+)/)
    {
        if ($opt{"s"} && $2 > $opt{"s"})
        {
            push (@slow, [$1, $2]);
        }


        else
        {
            push (@alive, [$1, $2]);
        }
    }


    elsif (/^(\S+)\s+address\s+not\s+found/)
    {
        push @addr_not_found, $1;
        push @unreachable, $1;
    }


    else
    {
        print STDERR "unidentified output from fping: [$_]\n";
    }
}


close (IN);


$END_TIME = time;


my $retval = $? >> 8;


if ($retval == 3)
{
    print "fping: invalid cmdline arguments [$CMD @hosts]\n";
    exit 1;
}


elsif ($retval == 4)
{
    print "fping: system call failure\n";
    exit 1;
}


elsif ($retval == 1 || $retval == 2 || @slow != 0)
{
    print join (" ", sort (@unreachable, map { $_->[0] } @slow)), "\n\n";
}


elsif ($retval == 0)
{
    print "\n";
}


else
{
    print "unknown return code ($retval) from fping\n";
}


print "start time: " . localtime ($START_TIME) . "\n";
print "end time  : " . localtime ($END_TIME) . "\n";
print "duration  : " . ($END_TIME - $START_TIME) . " seconds\n\n";


if (@unreachable != 0)
{
    print <<EOF;
------------------------------------------------------------------------------
unreachable hosts
------------------------------------------------------------------------------
EOF
    print join ("\n", @unreachable), "\n\n";


    if (@addr_not_found != 0)
    {
        print "address not found for @addr_not_found\n";
    }


    print "\n";
}




if (@slow != 0)
{
    print <<EOF;
------------------------------------------------------------------------------
slow hosts (response time which exceeds $opt{s}ms)
------------------------------------------------------------------------------
EOF


    foreach my $host (@slow)
    {
        printf ("%-40s %.2f ms\n", @{$host});
    }
}






if (@alive != 0)
{
    print <<EOF;
------------------------------------------------------------------------------
reachable hosts                          rtt
------------------------------------------------------------------------------
EOF
    
    for (my $i = 0; $i < @alive; $i++)
    {
        printf ("%-40s %.2f ms\n", @{$alive[$i]});
    }
}


#
# traceroute
#
if ($opt{"T"} && @unreachable)
{
    foreach my $host (@unreachable)
    {
        system ("traceroute -w 3 $host 2>&1");
    }


    print "\n";
}


#
# fail only if all hosts do not respond
#
if ($opt{"a"})
{
    if (@unreachable == @hosts)
    {
#       printf("---a1:&&&**(@unreachable)&&& == ******(@hosts)**********\n");
        exit 1;
    }
#       printf("\r\n---a0:****(@unreachable)&&&&& == *****(@hosts)*******\r\n");


    exit 0;
}


exit 1 if (@slow != 0);
#printf("retval=%d\n",$retval);
exit $retval;

使用 monitor 调用时 由于多了本机host,所以需要去掉 见上面的程序.
#!/usr/bin/perl
#
# Return a list of hosts which not reachable via ICMP echo
#
# Jim Trocki, trockij@transmeta.com
#
# $Id: fping.monitor 1.7 Mon, 27 Aug 2001 14:22:45 -0400 trockij $
#
#    Copyright (C) 1998, Jim Trocki
#
#    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
#
use strict;

use Getopt::Std;

my %opt;
getopts ("ahr:s:t:T", \%opt);

sub usage
{
    print <<EOF;
usage: fping.monitor [-a] [-r num] [-s num] [-t num] [-T] host [host...]

    -a		only report failure if all hosts are unreachable
    -r num	retry "num" times for each host before reporting failure
    -s num	consider hosts which respond in over "num" msecs failures
    -t num	wait "num" msecs before sending retries
    -T		traceroute to each failed host. CAUTION: this may cause
    		this monitor to hang for a very long time

EOF

    exit;
}

usage if ($opt{"h"});

my $TIMEOUT = $opt{"t"} || 2000;
my $RETRIES = $opt{"r"} || 3;
my $CMD = "fping -e -r $RETRIES -t $TIMEOUT";
my $START_TIME = time;
my $END_TIME;

exit 0 if (@ARGV == 0);

open (IN, "$CMD @ARGV 2>&1 |") ||
	die "could not open pipe to fping: $!\n";

my @unreachable;
my @alive;
my @addr_not_found;
my @slow;

while (<IN>)
{
    chomp;
    if (/^(\S+).*unreachable/)
    {
    	push (@unreachable, $1);
    }

    elsif (/^(\S+) is alive \((\S+)/)
    {
	if ($opt{"s"} && $2 > $opt{"s"})
	{
	    push (@slow, [$1, $2]);
	}

	else
	{
	    push (@alive, [$1, $2]);
	}
    }

    elsif (/^(\S+)\s+address\s+not\s+found/)
    {
    	push @addr_not_found, $1;
	push @unreachable, $1;
    }

    else
    {
    	print STDERR "unidentified output from fping: [$_]\n";
    }
}

close (IN);

$END_TIME = time;

my $retval = $? >> 8;

if ($retval == 3)
{
    print "fping: invalid cmdline arguments [$CMD @ARGV]\n";
    exit 1;
}

elsif ($retval == 4)
{
    print "fping: system call failure\n";
    exit 1;
}

elsif ($retval == 1 || $retval == 2 || @slow != 0)
{
    print join (" ", sort (@unreachable, map { $_->[0] } @slow)), "\n\n";
}

elsif ($retval == 0)
{
    print "\n";
}

else
{
    print "unknown return code ($retval) from fping\n";
}

print "start time: " . localtime ($START_TIME) . "\n";
print "end time  : " . localtime ($END_TIME) . "\n";
print "duration  : " . ($END_TIME - $START_TIME) . " seconds\n\n";

if (@unreachable != 0)
{
    print <<EOF;
------------------------------------------------------------------------------
unreachable hosts
------------------------------------------------------------------------------
EOF
    print join ("\n", @unreachable), "\n\n";

    if (@addr_not_found != 0)
    {
	print "address not found for @addr_not_found\n";
    }

    print "\n";
}


if (@slow != 0)
{
    print <<EOF;
------------------------------------------------------------------------------
slow hosts (response time which exceeds $opt{s}ms)
------------------------------------------------------------------------------
EOF

    foreach my $host (@slow)
    {
    	printf ("%-40s %.2f ms\n", @{$host});
    }
}



if (@alive != 0)
{
    print <<EOF;
------------------------------------------------------------------------------
reachable hosts                          rtt
------------------------------------------------------------------------------
EOF
    
    for (my $i = 0; $i < @alive; $i++)
    {
    	printf ("%-40s %.2f ms\n", @{$alive[$i]});
    }
}

#
# traceroute
#
if ($opt{"T"} && @unreachable)
{
    foreach my $host (@unreachable)
    {
    	system ("traceroute -w 3 $host 2>&1");
    }

    print "\n";
}

#
# fail only if all hosts do not respond
#
if ($opt{"a"})
{
    if (@unreachable == @ARGV)
    {
    	exit 1;
    }

    exit 0;
}

exit 1 if (@slow != 0);

exit $retval;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值