File: fping.monitor

package info (click to toggle)
mon 1.2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,860 kB
  • ctags: 538
  • sloc: perl: 15,847; ansic: 774; sh: 330; makefile: 46
file content (245 lines) | stat: -rwxr-xr-x 5,076 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/perl
#
# Return a list of hosts which not reachable via ICMP echo
#
# Jim Trocki, trockij@arctic.org
#
# $Id: fping.monitor,v 1.3.2.1 2007/05/11 01:00:26 trockij Exp $
#
#    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;
my %details;

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+) is 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;
    }

    #
    # fping can output a number of messages in addition to the eventual
    # reachable/unreachable.  Ignore them since we'll also get the main
    # "unreachable" message).
    #
    elsif (/^ICMP .+ from \S+ for ICMP Echo sent to /)
    {
       # do nothing
    }

    #
    # ICMP Host Unreachable from 1.2.3.4 for ICMP Echo sent to 2.4.6.8
    #
    elsif (/^ICMP (.*) for ICMP Echo sent to (\S+)/)
    {
	    if (! exists $details{$2})
	    {
		    $details{$2}= $_;
	    }
    }

    elsif (/^ICMP Time Exceeded from \S+ for ICMP Echo sent to (\S+) /)
    {
	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";

	foreach my $ipnum (@unreachable)
	{
		print $ipnum, " : ", $details{$ipnum}, "\n" if exists $details{$ipnum};
	}
}


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;