1
|
#!/usr/bin/perl
|
2
|
#
|
3
|
# Return a list of hosts which not reachable via ICMP echo
|
4
|
#
|
5
|
# Jim Trocki, trockij@arctic.org
|
6
|
#
|
7
|
# $Id: fping.monitor,v 1.3.2.1 2007/05/11 01:00:26 trockij Exp $
|
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
|
my %details;
|
57
|
|
58
|
exit 0 if (@ARGV == 0);
|
59
|
|
60
|
open (IN, "$CMD @ARGV 2>&1 |") ||
|
61
|
die "could not open pipe to fping: $!\n";
|
62
|
|
63
|
my @unreachable;
|
64
|
my @alive;
|
65
|
my @addr_not_found;
|
66
|
my @slow;
|
67
|
|
68
|
while (<IN>)
|
69
|
{
|
70
|
chomp;
|
71
|
if (/^(\S+) is unreachable/)
|
72
|
{
|
73
|
push (@unreachable, $1);
|
74
|
}
|
75
|
|
76
|
elsif (/^(\S+) is alive \((\S+)/)
|
77
|
{
|
78
|
if ($opt{"s"} && $2 > $opt{"s"})
|
79
|
{
|
80
|
push (@slow, [$1, $2]);
|
81
|
}
|
82
|
|
83
|
else
|
84
|
{
|
85
|
push (@alive, [$1, $2]);
|
86
|
}
|
87
|
}
|
88
|
|
89
|
elsif (/^(\S+)\s+address\s+not\s+found/)
|
90
|
{
|
91
|
push @addr_not_found, $1;
|
92
|
push @unreachable, $1;
|
93
|
}
|
94
|
|
95
|
#
|
96
|
# fping can output a number of messages in addition to the eventual
|
97
|
# reachable/unreachable. Ignore them since we'll also get the main
|
98
|
# "unreachable" message).
|
99
|
#
|
100
|
elsif (/^ICMP .+ from \S+ for ICMP Echo sent to /)
|
101
|
{
|
102
|
# do nothing
|
103
|
}
|
104
|
|
105
|
#
|
106
|
# ICMP Host Unreachable from 1.2.3.4 for ICMP Echo sent to 2.4.6.8
|
107
|
#
|
108
|
elsif (/^ICMP (.*) for ICMP Echo sent to (\S+)/)
|
109
|
{
|
110
|
if (! exists $details{$2})
|
111
|
{
|
112
|
$details{$2}= $_;
|
113
|
}
|
114
|
}
|
115
|
|
116
|
elsif (/^ICMP Time Exceeded from \S+ for ICMP Echo sent to (\S+) /)
|
117
|
{
|
118
|
push @unreachable, $1;
|
119
|
}
|
120
|
|
121
|
else
|
122
|
{
|
123
|
print STDERR "unidentified output from fping: [$_]\n";
|
124
|
}
|
125
|
}
|
126
|
|
127
|
close (IN);
|
128
|
|
129
|
$END_TIME = time;
|
130
|
|
131
|
my $retval = $? >> 8;
|
132
|
|
133
|
if ($retval == 3)
|
134
|
{
|
135
|
print "fping: invalid cmdline arguments [$CMD @ARGV]\n";
|
136
|
exit 1;
|
137
|
}
|
138
|
|
139
|
elsif ($retval == 4)
|
140
|
{
|
141
|
print "fping: system call failure\n";
|
142
|
exit 1;
|
143
|
}
|
144
|
|
145
|
elsif ($retval == 1 || $retval == 2 || @slow != 0)
|
146
|
{
|
147
|
print join (" ", sort (@unreachable, map { $_->[0] } @slow)), "\n\n";
|
148
|
}
|
149
|
|
150
|
elsif ($retval == 0)
|
151
|
{
|
152
|
print "\n";
|
153
|
}
|
154
|
|
155
|
else
|
156
|
{
|
157
|
print "unknown return code ($retval) from fping\n";
|
158
|
}
|
159
|
|
160
|
print "start time: " . localtime ($START_TIME) . "\n";
|
161
|
print "end time : " . localtime ($END_TIME) . "\n";
|
162
|
print "duration : " . ($END_TIME - $START_TIME) . " seconds\n\n";
|
163
|
|
164
|
if (@unreachable != 0)
|
165
|
{
|
166
|
print <<EOF;
|
167
|
------------------------------------------------------------------------------
|
168
|
unreachable hosts
|
169
|
------------------------------------------------------------------------------
|
170
|
EOF
|
171
|
print join ("\n", @unreachable), "\n\n";
|
172
|
|
173
|
if (@addr_not_found != 0)
|
174
|
{
|
175
|
print "address not found for @addr_not_found\n";
|
176
|
}
|
177
|
|
178
|
print "\n";
|
179
|
|
180
|
foreach my $ipnum (@unreachable)
|
181
|
{
|
182
|
print $ipnum, " : ", $details{$ipnum}, "\n" if exists $details{$ipnum};
|
183
|
}
|
184
|
}
|
185
|
|
186
|
|
187
|
if (@slow != 0)
|
188
|
{
|
189
|
print <<EOF;
|
190
|
------------------------------------------------------------------------------
|
191
|
slow hosts (response time which exceeds $opt{s}ms)
|
192
|
------------------------------------------------------------------------------
|
193
|
EOF
|
194
|
|
195
|
foreach my $host (@slow)
|
196
|
{
|
197
|
printf ("%-40s %.2f ms\n", @{$host});
|
198
|
}
|
199
|
}
|
200
|
|
201
|
|
202
|
|
203
|
if (@alive != 0)
|
204
|
{
|
205
|
print <<EOF;
|
206
|
------------------------------------------------------------------------------
|
207
|
reachable hosts rtt
|
208
|
------------------------------------------------------------------------------
|
209
|
EOF
|
210
|
|
211
|
for (my $i = 0; $i < @alive; $i++)
|
212
|
{
|
213
|
printf ("%-40s %.2f ms\n", @{$alive[$i]});
|
214
|
}
|
215
|
}
|
216
|
|
217
|
#
|
218
|
# traceroute
|
219
|
#
|
220
|
if ($opt{"T"} && @unreachable)
|
221
|
{
|
222
|
foreach my $host (@unreachable)
|
223
|
{
|
224
|
system ("traceroute -w 3 $host 2>&1");
|
225
|
}
|
226
|
|
227
|
print "\n";
|
228
|
}
|
229
|
|
230
|
#
|
231
|
# fail only if all hosts do not respond
|
232
|
#
|
233
|
if ($opt{"a"})
|
234
|
{
|
235
|
if (@unreachable == @ARGV)
|
236
|
{
|
237
|
exit 1;
|
238
|
}
|
239
|
|
240
|
exit 0;
|
241
|
}
|
242
|
|
243
|
exit 1 if (@slow != 0);
|
244
|
|
245
|
exit $retval;
|