~ubuntu-branches/debian/squeeze/smokeping/squeeze

« back to all changes in this revision

Viewing changes to lib/probes/EchoPing.pm

  • Committer: Bazaar Package Importer
  • Author(s): Jose Carlos Garcia Sogo
  • Date: 2002-04-02 15:02:08 UTC
  • Revision ID: james.westby@ubuntu.com-20020402150208-8eycqntc07q6gig5
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package probes::EchoPing;
 
2
 
 
3
my $DEFAULTBIN = "/usr/bin/echoping";
 
4
 
 
5
=head1 NAME
 
6
 
 
7
probes::EchoPing - an echoping(1) probe for SmokePing
 
8
 
 
9
=head1 OVERVIEW
 
10
 
 
11
Measures TCP or UDP echo (port 7) roundtrip times for SmokePing. Can also be 
 
12
used as a base class for other echoping(1) probes.
 
13
 
 
14
=head1 SYNOPSYS
 
15
 
 
16
 *** Probes ***
 
17
 + EchoPing
 
18
 
 
19
 binary = /usr/bin/echoping # default value
 
20
 
 
21
 *** Targets ***
 
22
 
 
23
 probe = EchoPing
 
24
 forks = 10
 
25
 
 
26
 menu = Top
 
27
 title = Top Menu
 
28
 remark = Top Menu Remark
 
29
 
 
30
 + PROBE_CONF
 
31
 
 
32
 # none of these are mandatory
 
33
 timeout = 1
 
34
 waittime = 1
 
35
 udp = no
 
36
 size = 510
 
37
 tos = 0xa0
 
38
 priority = 6
 
39
 
 
40
 + First
 
41
 menu = First
 
42
 title = First Target
 
43
 host = router.example.com
 
44
 
 
45
 # PROBE_CONF can be overridden here
 
46
 ++ PROBE_CONF
 
47
 size = 300
 
48
 
 
49
=head1 DESCRIPTION
 
50
 
 
51
Supported probe-specific variables:
 
52
 
 
53
=over
 
54
 
 
55
=item binary
 
56
 
 
57
The location of your echoping binary.
 
58
 
 
59
=item forks
 
60
 
 
61
The number of concurrent processes to be run. See probes::basefork(3pm) 
 
62
for details.
 
63
 
 
64
=back
 
65
 
 
66
Supported target-level probe variables 
 
67
(see echoping(1) for details of the options):
 
68
 
 
69
=over
 
70
 
 
71
=item timeout
 
72
 
 
73
The "-t" echoping(1) option. 
 
74
 
 
75
=item waittime
 
76
 
 
77
The "-w" echoping(1) option. 
 
78
 
 
79
=item size
 
80
 
 
81
The "-s" echoping(1) option. 
 
82
 
 
83
=item udp
 
84
 
 
85
The "-u" echoping(1) option. Values other than '0' and 'no' enable UDP.
 
86
 
 
87
=item fill
 
88
 
 
89
The "-f" echoping(1) option. 
 
90
 
 
91
=item priority
 
92
 
 
93
The "-p" echoping(1) option.
 
94
 
 
95
=item tos
 
96
 
 
97
The "-P" echoping option.
 
98
 
 
99
=back
 
100
 
 
101
=head1 BUGS
 
102
 
 
103
Should we test the availability of the service at startup? After that it's
 
104
too late to complain.
 
105
 
 
106
The location of the echoping binary should probably be a global variable
 
107
instead of a probe-specific one. As things are, every EchoPing -derived probe 
 
108
has to declare it if the default (/usr/bin/echoping) isn't correct.
 
109
 
 
110
=head1 AUTHOR
 
111
 
 
112
Niko Tyni E<lt>ntyni@iki.fiE<gt>
 
113
 
 
114
=head1 SEE ALSO
 
115
 
 
116
echoping(1), probes::EchoPingHttp(3pm) etc., http://echoping.sourceforge.net/
 
117
 
 
118
=cut
 
119
 
 
120
use strict;
 
121
use base qw(probes::basefork);
 
122
use Carp;
 
123
#
 
124
# derived class will mess with this through the 'features' method below
 
125
my $featurehash = {
 
126
        waittime => "-w",
 
127
        timeout => "-t",
 
128
        size => "-s",
 
129
        tos => "-P",
 
130
        priority => "-p",
 
131
        fill => "-f",
 
132
};
 
133
 
 
134
sub features {
 
135
        my $self = shift;
 
136
        my $newval = shift;
 
137
        $featurehash = $newval if defined $newval;
 
138
        return $featurehash;
 
139
}
 
140
 
 
141
sub new {
 
142
        my $proto = shift;
 
143
        my $class = ref($proto) || $proto;
 
144
        my $self = $class->SUPER::new(@_);
 
145
 
 
146
        $self->_init if $self->can('_init');
 
147
 
 
148
        unless (defined $self->{properties}{binary}) {
 
149
                $self->{properties}{binary} = $DEFAULTBIN;
 
150
        }
 
151
        croak "ERROR: EchoPing 'binary' $self->{properties}{binary} does not point to an executable"
 
152
                unless -f $self->{properties}{binary} and -x $self->{properties}{binary};
 
153
 
 
154
        $self->test_usage;
 
155
 
 
156
        return $self;
 
157
}
 
158
 
 
159
# warn about unsupported features
 
160
sub test_usage {
 
161
        my $self = shift;
 
162
        my $bin = $self->{properties}{binary};
 
163
        my @unsupported;
 
164
 
 
165
        my $arghashref = $self->features;
 
166
        my %arghash = %$arghashref;
 
167
 
 
168
        for my $feature (keys %arghash) {
 
169
                if (`$bin $arghash{$feature} 1 foo 2>&1` =~ /invalid option|usage/i) {
 
170
                        push @unsupported, $feature;
 
171
                        carp("Note: your echoping doesn't support the $feature feature (option $arghash{$feature}), disabling it") unless $ENV{SERVER_SOFTWARE};
 
172
                }
 
173
        }
 
174
        map { delete $arghashref->{$_} } @unsupported;
 
175
 
 
176
        return;
 
177
}
 
178
 
 
179
sub ProbeDesc($) {
 
180
        return "TCP or UDP Echo pings using echoping(1)";
 
181
}
 
182
 
 
183
# This can be overridden to tag the port number to the address
 
184
# in derived classes (namely EchoPingHttp)
 
185
sub make_host {
 
186
        my $self = shift;
 
187
        my $target = shift;
 
188
        return $target->{addr};
 
189
}
 
190
 
 
191
 
 
192
# other than host, count and protocol-specific args come from here
 
193
sub make_args {
 
194
        my $self = shift;
 
195
        my $target = shift;
 
196
        my @args;
 
197
        my %arghash = %{$self->features};
 
198
 
 
199
        for (keys %arghash) {
 
200
                my $val = $target->{vars}{$_};
 
201
                $val = $self->{properties}{$_} unless defined $val;
 
202
                push @args, ($arghash{$_}, $val) if defined $val;
 
203
        }
 
204
        return @args;
 
205
}
 
206
 
 
207
# this is separated to make it possible to test the service
 
208
# at startup, although we don't do it at the moment.
 
209
sub count_args {
 
210
        my $self = shift;
 
211
        my $count = shift;
 
212
 
 
213
        $count = $self->{cfg}{Database}{pings} unless defined $count;
 
214
        return ("-n", $count);
 
215
}
 
216
 
 
217
# This is what derived classes will override
 
218
sub proto_args {
 
219
        my $self = shift;
 
220
        return $self->udp_arg(@_);
 
221
}
 
222
 
 
223
# UDP is defined only for echo and discard
 
224
sub udp_arg {
 
225
        my $self = shift;
 
226
        my $target = shift;
 
227
        my @args;
 
228
 
 
229
        my $udp = $target->{vars}{udp};
 
230
        $udp = $self->{properties}{udp} unless defined $udp;
 
231
        push @args, "-u" if (defined $udp and $udp ne "no" and $udp != 0);
 
232
 
 
233
        return @args;
 
234
}
 
235
 
 
236
sub make_commandline {
 
237
        my $self = shift;
 
238
        my $target = shift;
 
239
        my $count = shift;
 
240
 
 
241
        my @args = $self->make_args($target);
 
242
        my $host = $self->make_host($target);
 
243
        push @args, $self->proto_args($target);
 
244
        push @args, $self->count_args($count);
 
245
        
 
246
        return ($self->{properties}{binary}, @args, $host);
 
247
}
 
248
 
 
249
sub pingone {
 
250
        my $self = shift;
 
251
        my $t = shift;
 
252
 
 
253
        my @cmd = $self->make_commandline($t);
 
254
 
 
255
        my $cmd = join(" ", @cmd);
 
256
 
 
257
        carp "executing cmd $cmd\n" if $self->debug;
 
258
 
 
259
        my @times;
 
260
 
 
261
        open(P, "$cmd 2>&1 |") or croak("fork: $!");
 
262
 
 
263
        # what should we do with error messages?
 
264
        while (<P>) {
 
265
                /^Elapsed time: (\d+\.\d+) seconds/ and push @times, $1;
 
266
        }
 
267
        close P;
 
268
        
 
269
        # carp("Got @times") if $self->debug;
 
270
        return sort { $a <=> $b } @times;
 
271
}
 
272
 
 
273
sub debug {
 
274
        my $self = shift;
 
275
        my $newval = shift;
 
276
        $self->{debug} = $newval if defined $newval;
 
277
        return $self->{debug};
 
278
}
 
279
 
 
280
1;