#!/usr/bin/perl -w # # Return a list of hosts which not reachable via ICMP echo # # Jim Trocki, trockij@transmeta.com # # This module is modified to recognize hosts in format # username[:password]@host/database ... # which is used my pgsql.monitor (of course, just host is used) by # Dobrica Pavlinusic, dpavlin@rot13.org # http://www.rot13.org/~dpavlin/sysadm.html # # $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 <&1 |") || die "could not open pipe to fping: $!\n"; my @unreachable; my @alive; my @slow; my @other_prob; # details for other per-host problems my @error; # other errors which I'll give non-zero exit for my @icmp; # ICMP messages output by fping my %addr_unknown; my %want_host = map { $_ => 1 } @hosts; # hosts fping hasn't output yet while () { chomp; if (/^(\S+).*unreachable/) { push (@unreachable, $1); delete $want_host{$1} or push @error, "unreachable host `$1' wasn't asked for"; } elsif (/^(\S+) is alive \((\S+)/) { delete $want_host{$1} or push @error, "reachable host `$1' wasn't asked for"; if ($opt{"s"} && $2 > $opt{"s"}) { push (@slow, [$1, $2]); } else { push (@alive, [$1, $2]); } } elsif (/^(\S+)\s+address\s+not\s+found/) { $addr_unknown{$1} = 1; push @other_prob, "$1 address not found"; push @unreachable, $1; delete $want_host{$1} or push @error, "unknown host `$1' wasn't asked for"; } # ICMP Host Unreachable from 1.2.3.4 for ICMP Echo sent to 2.4.6.8 # (among others) elsif (/^ICMP (.*) for ICMP Echo sent to (\S+)/) { push @icmp, $_; } else { push @error, "unidentified output from fping: [$_]"; } } for my $host (keys %want_host) { push @other_prob, "$host not listed in fping's output"; push @unreachable, $host; } close (IN); $END_TIME = time; my $retval = $? >> 8; if ($retval < 3) { # do nothing } elsif ($retval == 3) { push @error, "fping: invalid cmdline arguments [$CMD @ARGV]"; } elsif ($retval == 4) { push @error, "fping: system call failure"; } else { push @error, "unknown return code ($retval) from fping"; } if (@error) { print "unusual errors\n"; } else { my @fail = sort @unreachable, map { $_->[0] } @slow; # This line is intentionally blank if there are no failures. print "@fail\n"; } print "\n"; print "start time: " . localtime ($START_TIME) . "\n"; print "end time : " . localtime ($END_TIME) . "\n"; print "duration : " . ($END_TIME - $START_TIME) . " seconds\n"; if (@error != 0) { print <&1"); } } exit 1 if @error; # # fail only if all hosts do not respond # if ($opt{"a"}) { exit(@alive ? 0 : 1); } exit 1 if (@slow != 0); exit $retval;