/[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

Contents of /fping+args.monitor

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Fri Jul 26 10:43:47 2002 UTC (21 years, 8 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +6 -0 lines
better instructions

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

  ViewVC Help
Powered by ViewVC 1.1.26