VBTK/Wrapper/Ping.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 122 CC 8 SCAR 36.7
CC
8
Cov
0%
new
72.0
addVBObj
2.0
BEGIN
2.0
CRAP 39.3
1 #############################################################################
2 #
3 # NOTE: This file under revision control using RCS
4 # Any changes made without RCS will be lost
5 #
6 # $Source: /usr/local/cvsroot/vbtk/VBTK/Wrapper/Ping.pm,v $
7 # $Revision: 1.10 $
8 # $Date: 2002/03/04 20:53:08 $
9 # $Author: bhenry $
10 # $Locker: $
11 # $State: Exp $
12 #
13 # Purpose: An extension of the Wrapper library which defaults the
14 # proper settings necessary to run a ping test.
15 #
16 # Depends on: VBTK::Common, VBTK::Wrapper
17 #
18 # Copyright (C) 1996 - 2002 Brent Henry
19 #
20 # This program is free software; you can redistribute it and/or
21 # modify it under the terms of version 2 of the GNU General Public
22 # License as published by the Free Software Foundation available at:
23 # http://http://www.gnu.org/copyleft/gpl.html
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 #
30 #############################################################################
31 #
32 #
33 # REVISION HISTORY:
34 #
35 # $Log: Ping.pm,v $
36 # Revision 1.10 2002/03/04 20:53:08 bhenry
37 # *** empty log message ***
38 #
39 # Revision 1.9 2002/03/04 16:49:10 bhenry
40 # Changed requirement back to perl 5.6.0
41 #
42 # Revision 1.8 2002/03/02 00:53:56 bhenry
43 # Documentation updates
44 #
45 # Revision 1.7 2002/02/13 07:36:14 bhenry
46 # Disabled RrdLogRecovery and removed use of @log
47 #
48 #
49  
50 package VBTK::Wrapper::Ping;
51  
52 use 5.6.0;
53 use strict;
54 use warnings;
55 # I like using undef as a value so I'm turning off the uninitialized warnings
56 no warnings qw(uninitialized);
57  
58 use VBTK::Common;
59 use VBTK::Wrapper;
60  
61 # Inherit methods from VBTK::Wrapper;
62 our @ISA=qw(VBTK::Wrapper);
63  
64 our $VERBOSE = $ENV{VERBOSE};
65  
66 #-------------------------------------------------------------------------------
67 # Function: new
68 # Description: Object constructor. Allocates memory for all class members
69 # Input Parms:
70 # Output Parms: Pointer to class
71 #-------------------------------------------------------------------------------
72 sub new
73 {
74 my $type = shift;
75 my $self = {};
76 bless $self, $type;
77  
78 # Store all passed input name pairs in the object
79 $self->set(@_);
80  
81 # Make sure the 'Interval' has a value before we try to do defaults, since some
82 # of them depend on the interval
83 $self->{Interval} ||= 60;
84  
85 # Setup formatting defaults
86 my $stdHeader =
87 [ ' Resp Pkt',
88 'Time HostName IP Time (ms) Loss (%)',
89 '------------------ -------------------------- --------------- --------- --------' ];
90 my $stdDetail =
91 [ '@<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @>>>>>>>> @>>>>>>>',
92 '$time, @data[0..3]' ];
93  
94  
95 # Define a subroutine which will parse the multi-line output of the ping
96 # command and return a single row with the hostname, ip, reponse time, and
97 # packet loss.
98 my $preProcessor = sub {
99 my($data) = @_;
100 my($respTime,$pktLoss,$host,$ip,$msg) = (0,100);
101 foreach (@{$data}) {
102 if(/bytes from (\S+) \(([\d\.]+)\): icmp_seq=\d+.+time=(\d+)/) {
103 ($host,$ip,$respTime) = ($1,$2,$3); next; }
104 if(/Pinging (\S+) \[([\d\.]+)\]/) { ($host,$ip) = ($1,$2); next; }
105 if(/Average =\s+(\d+)ms/) { ($respTime) = ($1); next; }
106 if(/(\d+)\% (packet )?loss/) { $pktLoss = $1; next; }
107 if(/PING|^\s*$|^round-trip|Approximate|statistics|Reply from/) { next; }
108 }
109 # Now replace everything in the $data array with a single line
110 @{$data} = ([ $host, $ip, $respTime, $pktLoss ]);
111 };
112  
113 # Setup a hash of default parameters
114 my $defaultParms = {
115 Interval => 60,
116 Host => $::REQUIRED,
117 Execute => "ping -s $self->{Host} 56 1",
118 SourceList => undef,
119 VBServerURI => $::VBURI,
120 VBHeader => $stdHeader,
121 VBDetail => $stdDetail,
122 LogFile => undef,
123 LogHeader => $stdHeader,
124 LogDetail => $stdDetail,
125 RotateLogAt => undef,
126 RotateLogOnEOF => undef,
127 PreProcessor => $preProcessor,
128 Filter => undef,
129 Ignore => undef,
130 Split => undef,
131 SkipLines => undef,
132 Timeout => 20,
133 TimeoutStatus => 'Warn',
134 Follow => undef,
135 FollowTimeout => undef,
136 FollowHeartbeat => undef,
137 SetRunStatus => undef,
138 NonZeroExitStatus => 'Warn',
139 SuppressStdout => undef,
140 SuppressMessages => undef,
141 DebugHeader => "Ping $self->{Host}"
142 };
143  
144 # Do some O/S specific overrides
145 if ($::OS =~ /win/i)
146 {
147 $defaultParms->{Execute} = "ping -n 1 $self->{Host}";
148 }
149 elsif ($::OS =~ /linux/i)
150 {
151 $defaultParms->{Execute} = "ping -c 1 $self->{Host}";
152 }
153
154 # Run a validation on the passed parms, using the default parms
155 $self->validateParms($defaultParms) || &fatal("Exiting");
156  
157 # Save the host value and then delete it out of self temporarily, so that it
158 # doesn't get passed to the Wrapper object.
159 my $host = $self->{Host};
160 delete $self->{Host};
161  
162 # Initialize a wrapper object.
163 $self->SUPER::new() || return undef;
164  
165 # Store the defaults for later
166 $self->{Host} = $host;
167 $self->{defaultParms} = $defaultParms;
168  
169 ($self);
170 }
171  
172  
173 #-------------------------------------------------------------------------------
174 # Function: addVBObj
175 # Description: Add rules to the wrapper object.
176 # Input Parms:
177 # Output Parms: Pointer to class
178 #-------------------------------------------------------------------------------
179 sub addVBObj
180 {
181 my $self = shift;
182 my $Interval = $self->{Interval};
183 my $Host = $self->{Host};
184 my %args = @_;
185  
186 # Setup some reasonable thresholds
187 my $expireAfterSec = int($Interval * 3);
188 my $description = qq(
189 This object uses the 'ping' command to monitor '$Host'. It will set the
190 status to 'Warning' if the ping command loses any packets or if the response
191 time is greater than 250ms.
192 );
193  
194 # Setup a hash of rules to be returned
195 my $defaultRules = {
196 VBObjName => ".$Host.ping",
197 TextHistoryLimit => 30,
198 ReverseText => 1,
199 # Set to Warn if any packets are lost or if response time > 250
200 Rules => {
201 '(($data[2] > 250)||($data[3] > 0))' => 'Warn' },
202 Requirements => undef,
203 StatusHistoryLimit => 30,
204 StatusChangeActions => undef,
205 StatusUpgradeRules => [
206 "Upgrade to Failed if Warning occurs 2 times in $expireAfterSec sec" ],
207 ExpireAfter => "$expireAfterSec seconds",
208 Description => $description,
209 RrdTimeCol => undef,
210 RrdColumns => [ '$data[2]','$data[3]' ],
211 RrdFilter => undef,
212 RrdMin => 0,
213 RrdMax => undef,
214 RrdXFF => undef,
215 RrdCF => undef,
216 RrdDST => undef,
217 };
218  
219 # Run the validation
220 &validateParms(\%args,$defaultRules) || &fatal("Exiting");
221  
222 # Add the rule
223 my $vbObj = $self->SUPER::addVBObj(%args) || return undef;
224  
225 # Now define what graphs to show on this object's page
226 $vbObj->addGraphGroup (
227 GroupNumber => 1,
228 DataSourceList => ':0',
229 Labels => 'Response Time (ms)',
230 Title => "Ping $Host",
231 CF => undef,
232 );
233  
234 # Now define what graphs to show on this object's page
235 $vbObj->addGraphGroup (
236 GroupNumber => 2,
237 DataSourceList => ':1',
238 Labels => 'Packet Loss %',
239 Title => "Ping $Host",
240 CF => undef,
241 );
242  
243 ($vbObj);
244 }
245  
246 1;
247 __END__