/[mon-modules]/fping+args.monitor
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /fping+args.monitor

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Wed Jul 10 10:04:48 2002 UTC (16 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.2: +4 -3 lines
add each host to test list just once

1 dpavlin 1.3 #!/usr/bin/perl -w
2 dpavlin 1.1 #
3     # Return a list of hosts which not reachable via ICMP echo
4     #
5     # Jim Trocki, trockij@transmeta.com
6     #
7     # $Id: fping.monitor 1.7 Mon, 27 Aug 2001 14:22:45 -0400 trockij $
8     #
9     # Copyright (C) 1998, Jim Trocki
10     #
11     # This program is free software; you can redistribute it and/or modify
12     # it under the terms of the GNU General Public License as published by
13     # the Free Software Foundation; either version 2 of the License, or
14     # (at your option) any later version.
15     #
16     # This program is distributed in the hope that it will be useful,
17     # but WITHOUT ANY WARRANTY; without even the implied warranty of
18     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     # GNU General Public License for more details.
20     #
21     # You should have received a copy of the GNU General Public License
22     # along with this program; if not, write to the Free Software
23     # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24     #
25     use strict;
26    
27     use Getopt::Std;
28    
29     my %opt;
30     getopts ("ahr:s:t:T", \%opt);
31    
32     sub usage
33     {
34     print <<EOF;
35     usage: fping.monitor [-a] [-r num] [-s num] [-t num] [-T] host [host...]
36    
37     -a only report failure if all hosts are unreachable
38     -r num retry "num" times for each host before reporting failure
39     -s num consider hosts which respond in over "num" msecs failures
40     -t num wait "num" msecs before sending retries
41     -T traceroute to each failed host. CAUTION: this may cause
42     this monitor to hang for a very long time
43    
44     EOF
45    
46     exit;
47     }
48    
49     usage if ($opt{"h"});
50    
51     my $TIMEOUT = $opt{"t"} || 2000;
52     my $RETRIES = $opt{"r"} || 3;
53     my $CMD = "fping -e -r $RETRIES -t $TIMEOUT";
54     my $START_TIME = time;
55     my $END_TIME;
56    
57     exit 0 if (@ARGV == 0);
58    
59 dpavlin 1.2 my @hosts;
60    
61     # you can use hosts in format host:optional configuration parameters and
62     # this part will strip everything after hostname
63     foreach (@ARGV) {
64     if (m/^[^:]+:?[^\@]*\@([^\/]+)\/?.*/) {
65 dpavlin 1.3 my $host = $1;
66     push @hosts,$host if (! grep /^$host$/,@hosts);
67 dpavlin 1.2 } else {
68 dpavlin 1.3 push @hosts,$_ if (! grep /^$_$/,@hosts);
69 dpavlin 1.2 }
70     }
71    
72     open (IN, "$CMD @hosts 2>&1 |") ||
73 dpavlin 1.1 die "could not open pipe to fping: $!\n";
74    
75     my @unreachable;
76     my @alive;
77     my @slow;
78     my @other_prob; # details for other per-host problems
79     my @error; # other errors which I'll give non-zero exit for
80     my @icmp; # ICMP messages output by fping
81     my %addr_unknown;
82    
83 dpavlin 1.2 my %want_host = map { $_ => 1 } @hosts; # hosts fping hasn't output yet
84 dpavlin 1.1
85     while (<IN>)
86     {
87     chomp;
88     if (/^(\S+).*unreachable/)
89     {
90     push (@unreachable, $1);
91     delete $want_host{$1}
92     or push @error, "unreachable host `$1' wasn't asked for";
93     }
94    
95     elsif (/^(\S+) is alive \((\S+)/)
96     {
97     delete $want_host{$1}
98     or push @error, "reachable host `$1' wasn't asked for";
99    
100     if ($opt{"s"} && $2 > $opt{"s"})
101     {
102     push (@slow, [$1, $2]);
103     }
104    
105     else
106     {
107     push (@alive, [$1, $2]);
108     }
109     }
110    
111     elsif (/^(\S+)\s+address\s+not\s+found/)
112     {
113     $addr_unknown{$1} = 1;
114     push @other_prob, "$1 address not found";
115     push @unreachable, $1;
116     delete $want_host{$1}
117     or push @error, "unknown host `$1' wasn't asked for";
118     }
119    
120     # ICMP Host Unreachable from 1.2.3.4 for ICMP Echo sent to 2.4.6.8
121     # (among others)
122    
123     elsif (/^ICMP (.*) for ICMP Echo sent to (\S+)/)
124     {
125     push @icmp, $_;
126     }
127    
128     else
129     {
130     push @error, "unidentified output from fping: [$_]";
131     }
132     }
133    
134     for my $host (keys %want_host) {
135     push @other_prob, "$host not listed in fping's output";
136     push @unreachable, $host;
137     }
138    
139     close (IN);
140    
141     $END_TIME = time;
142    
143     my $retval = $? >> 8;
144    
145     if ($retval < 3)
146     {
147     # do nothing
148     }
149    
150     elsif ($retval == 3)
151     {
152     push @error, "fping: invalid cmdline arguments [$CMD @ARGV]";
153     }
154    
155     elsif ($retval == 4)
156     {
157     push @error, "fping: system call failure";
158     }
159    
160     else
161     {
162     push @error, "unknown return code ($retval) from fping";
163     }
164    
165     if (@error) {
166     print "unusual errors\n";
167     }
168     else {
169     my @fail = sort @unreachable, map { $_->[0] } @slow;
170     # This line is intentionally blank if there are no failures.
171     print "@fail\n";
172     }
173    
174     print "\n";
175     print "start time: " . localtime ($START_TIME) . "\n";
176     print "end time : " . localtime ($END_TIME) . "\n";
177     print "duration : " . ($END_TIME - $START_TIME) . " seconds\n";
178    
179     if (@error != 0)
180     {
181     print <<EOF;
182    
183     ------------------------------------------------------------------------------
184     unusual errors
185     ------------------------------------------------------------------------------
186     EOF
187     print join ("\n", @error), "\n";
188     }
189    
190     if (@unreachable != 0)
191     {
192     print <<EOF;
193    
194     ------------------------------------------------------------------------------
195     unreachable hosts
196     ------------------------------------------------------------------------------
197     EOF
198     print join ("\n", @unreachable), "\n";
199    
200     print "\nother problems:\n", join "\n", @other_prob, ''
201     if @other_prob;
202     }
203    
204     if (@icmp != 0)
205     {
206     print <<EOF;
207    
208     ------------------------------------------------------------------------------
209     ICMP messages
210     ------------------------------------------------------------------------------
211     EOF
212     print join "\n", @icmp, '';
213     }
214    
215    
216     if (@slow != 0)
217     {
218     print <<EOF;
219    
220     ------------------------------------------------------------------------------
221     slow hosts (response time which exceeds $opt{s}ms)
222     ------------------------------------------------------------------------------
223     EOF
224    
225     foreach my $host (@slow)
226     {
227     printf ("%-40s %.2f ms\n", @{$host});
228     }
229     }
230    
231    
232    
233     if (@alive != 0)
234     {
235     print <<EOF;
236    
237     ------------------------------------------------------------------------------
238     reachable hosts rtt
239     ------------------------------------------------------------------------------
240     EOF
241    
242     for (my $i = 0; $i < @alive; $i++)
243     {
244     printf ("%-40s %.2f ms\n", @{$alive[$i]});
245     }
246     }
247    
248     #
249     # traceroute
250     #
251     if ($opt{"T"} && @unreachable)
252     {
253     my $header_output = 0;
254     foreach my $host (@unreachable)
255     {
256     next if $addr_unknown{$host};
257     print $header_output++ ? "\n" : <<EOF;
258    
259     ------------------------------------------------------------------------------
260     traceroute to unreachable hosts
261     ------------------------------------------------------------------------------
262     EOF
263     system ("traceroute -w 3 $host 2>&1");
264     }
265     }
266    
267     exit 1 if @error;
268    
269     #
270     # fail only if all hosts do not respond
271     #
272     if ($opt{"a"})
273     {
274     exit(@alive ? 0 : 1);
275     }
276    
277     exit 1 if (@slow != 0);
278    
279     exit $retval;

  ViewVC Help
Powered by ViewVC 1.1.26