summaryrefslogtreecommitdiffstats
path: root/lib/Smokeping/probes/IOSPing.pm
blob: 7a10e4ac78ad02f427619be972e7dd863943b31b (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package Smokeping::probes::IOSPing;

=head1 301 Moved Permanently

This is a Smokeping probe module. Please use the command 

C<smokeping -man Smokeping::probes::IOSPing>

to view the documentation or the command

C<smokeping -makepod Smokeping::probes::IOSPing>

to generate the POD document.

=cut

use strict;
use base qw(Smokeping::probes::basefork);
use IPC::Open2;
use Symbol;
use Carp;

my $e = "=";

sub pod_hash {
	return {
		name => <<DOC,
Smokeping::probes::IOSPing - Cisco IOS Probe for SmokePing
DOC
		description => <<DOC,
Integrates Cisco IOS as a probe into smokeping.  Uses the rsh / remsh
protocol to run a ping from an IOS device.
DOC
	notes => <<DOC,
=head2 IOS Configuration

The IOS device must have rsh enabled and an appropriate trust defined,
eg:

    !
    ip rcmd rsh-enable
    ip rcmd remote-host smoke 192.168.1.2 smoke enable
    !

Some IOS devices have a maximum of 5 VTYs available, so be careful not to 
hit a limit with the 'forks' variable.

${e}head2 Password authentication

It is not possible to use password authentication with rsh or remsh
due to fundamental limitations of the protocol.

${e}head2 Ping packet size

The FPing manpage has the following to say on the topic of ping packet
size:

Number of bytes of ping data to send.  The minimum size (normally 12)
allows room for the data that fping needs to do its work (sequence
number, timestamp).  The reported received data size includes the IP
header (normally 20 bytes) and ICMP header (8 bytes), so the minimum
total size is 40 bytes.  Default is 56, as in ping. Maximum is the
theoretical maximum IP datagram size (64K), though most systems limit
this to a smaller, system-dependent number.
DOC
	authors => <<'DOC',
Paul J Murphy <paul@murph.org>

based on L<Smokeping::probes::FPing|Smokeping::probes::FPing> by

Tobias Oetiker <tobi@oetiker.ch>
DOC
	}
}

sub new($$$)
{
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = $class->SUPER::new(@_);

    # no need for this if we run as a cgi
    unless ( $ENV{SERVER_SOFTWARE} ) {
	$self->{pingfactor} = 1000; # Gives us a good-guess default
	print "### assuming you are using an IOS reporting in miliseconds\n";
    };

    return $self;
}

sub ProbeDesc($){
    my $self = shift;
    my $bytes = $self->{properties}{packetsize};
    return "Cisco IOS - ICMP Echo Pings ($bytes Bytes)";
}

sub pingone ($$){
    my $self = shift;
    my $target = shift;
    my $bytes = $self->{properties}{packetsize};
    # do NOT call superclass ... the ping method MUST be overwriten
    my %upd;
    my $inh = gensym;
    my $outh = gensym;
    my @args = ();
    my $pings = $self->pings($target);

    push(@args,$self->{properties}{binary});
    push(@args,'-l',$target->{vars}{iosuser})
	if defined $target->{vars}{iosuser};
    push(@args,$target->{vars}{ioshost});
    push(@args,'ping');

    my $pid = open2($outh,$inh,@args);
    #
    # The following comments are the dialog produced by
    # "remsh <router> ping" to a Cisco 800 series running IOS 12.2T
    #
    # Other hardware or versions of IOS may need adjustments here.
    #
    # Protocol [ip]: 
    print { $inh } "\n";
    # Target IP address: 
    print { $inh } $target->{addr},"\n";
    # Repeat count [5]: 
    print { $inh } $pings,"\n";
    # Datagram size [100]: 
    print { $inh } $bytes,"\n";
    # Timeout in seconds [2]: 
    print { $inh } "\n";
    # Extended commands [n]: 
    print { $inh } "y\n";
    # Source address or interface: 
    print { $inh } "".($target->{vars}{iosint} || "") ,"\n";
         # Added by Mars Wei to make
         # Source address an option
    # Type of service [0]: 
    print { $inh } "\n";
    # Set DF bit in IP header? [no]: 
    print { $inh } "\n";
    # Validate reply data? [no]: 
    print { $inh } "\n";
    # Data pattern [0xABCD]: 
    print { $inh } "\n";
    # Loose, Strict, Record, Timestamp, Verbose[none]: 
    print { $inh } "V\n";
    # Loose, Strict, Record, Timestamp, Verbose[V]: 
    print { $inh } "\n";
    # Sweep range of sizes [n]: 
    print { $inh } "\n";
    #
    # Type escape sequence to abort.
    # Sending 20, 56-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
    # Reply to request 0 (4 ms)
    # Reply to request 1 (4 ms)
    # Reply to request 2 (4 ms)
    # Reply to request 3 (1 ms)
    # Reply to request 4 (1 ms)
    # Reply to request 5 (1 ms)
    # Reply to request 6 (4 ms)
    # Reply to request 7 (4 ms)
    # Reply to request 8 (4 ms)
    # Reply to request 9 (4 ms)
    # Reply to request 10 (1 ms)
    # Reply to request 11 (1 ms)
    # Reply to request 12 (1 ms)
    # Reply to request 13 (1 ms)
    # Reply to request 14 (4 ms)
    # Reply to request 15 (4 ms)
    # Reply to request 16 (4 ms)
    # Reply to request 17 (4 ms)
    # Reply to request 18 (1 ms)
    # Reply to request 19 (1 ms)
    # Success rate is 100 percent (20/20), round-trip min/avg/max = 1/2/4 ms

    my @times = ();
    while (<$outh>){
	chomp;
	/^Reply to request \d+ \((\d+) ms\)/ && push(@times,$1);
    }
    @times = map {sprintf "%.10e", $_ / $self->{pingfactor}} sort {$a <=> $b} @times;

    waitpid $pid,0;
    close $inh;
    close $outh;

    return @times;
}

sub probevars {
	my $class = shift;
        return $class->_makevars($class->SUPER::probevars, {
		_mandatory => ['binary'],
		binary => {
			_doc => <<DOC,
The binary option specifies the path of the binary to be used to
connect to the IOS device.  Commonly used binaries are /usr/bin/rsh
and /usr/bin/remsh, although any script or binary should work if can
be called as 

    /path/to/binary [ -l user ] router ping

to produce the IOS ping dialog on stdin & stdout.
DOC
			_example => '/usr/bin/rsh',
			_sub => sub {
				my $val = shift;
				-x $val or return "ERROR: binary '$val' is not executable";
				return undef;
			},
		},
		packetsize => {
			_doc => <<DOC,
The (optional) packetsize option lets you configure the packetsize for
the pings sent.
DOC
			_default => 56,
			_re => '\d+',
			_sub => sub {
				my $val = shift;
				return "ERROR: packetsize must be between 12 and 64000"
					unless $val >= 12 and $val <= 64000;
				return undef;
			},
		},
	});
}

sub targetvars {
	my $class = shift;
        return $class->_makevars($class->SUPER::targetvars, {
		_mandatory => [ 'ioshost' ],
		ioshost => {
			_doc => <<DOC,
The ioshost option specifies the IOS device which should be used for
the ping.
DOC
			_example => 'my.cisco.router',
		},
		iosuser => {
			_doc => <<DOC,
The (optional) iosuser option allows you to specify the remote
username the IOS device.  If this option is omitted, the username
defaults to the default user used by the remsh command (usually the
user running the remsh command, ie the user running SmokePing).
DOC
			_example => 'admin',
		},
		iosint => {
			_doc => <<DOC,
The (optional) iosint option allows you to specify the source address
or interface in the IOS device. The value should be an IP address or
an interface name such as "Ethernet 1/0". If this option is omitted,
the IOS device will pick the IP address of the outbound interface to
use.
DOC
			_example => 'Ethernet 1/0',
		},
	});
}

1;