| 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/Tcp.pm,v $ | ||
| 7 | # $Revision: 1.7 $ | ||
| 8 | # $Date: 2002/03/04 20:53:07 $ | ||
| 9 | # $Author: bhenry $ | ||
| 10 | # $Locker: $ | ||
| 11 | # $State: Exp $ | ||
| 12 | # | ||
| 13 | # Purpose: A common perl library used to monitor TCP listeners. | ||
| 14 | # | ||
| 15 | # Copyright (C) 1996 - 2002 Brent Henry | ||
| 16 | # | ||
| 17 | # This program is free software; you can redistribute it and/or | ||
| 18 | # modify it under the terms of version 2 of the GNU General Public | ||
| 19 | # License as published by the Free Software Foundation available at: | ||
| 20 | # http://www.gnu.org/copyleft/gpl.html | ||
| 21 | # | ||
| 22 | # This program is distributed in the hope that it will be useful, | ||
| 23 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 24 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 25 | # GNU General Public License for more details. | ||
| 26 | # | ||
| 27 | ############################################################################# | ||
| 28 | # | ||
| 29 | # | ||
| 30 | # REVISION HISTORY: | ||
| 31 | # | ||
| 32 | # $Log: Tcp.pm,v $ | ||
| 33 | # Revision 1.7 2002/03/04 20:53:07 bhenry | ||
| 34 | # *** empty log message *** | ||
| 35 | # | ||
| 36 | # Revision 1.6 2002/03/04 16:49:09 bhenry | ||
| 37 | # Changed requirement back to perl 5.6.0 | ||
| 38 | # | ||
| 39 | # Revision 1.5 2002/03/02 00:53:55 bhenry | ||
| 40 | # Documentation updates | ||
| 41 | # | ||
| 42 | # Revision 1.4 2002/02/13 07:38:51 bhenry | ||
| 43 | # Disabled RrdLogRecovery and removed use of @log | ||
| 44 | # | ||
| 45 | # Revision 1.3 2002/01/25 07:15:10 bhenry | ||
| 46 | # Changed to inherit from Parser | ||
| 47 | # | ||
| 48 | # Revision 1.2 2002/01/21 17:07:40 bhenry | ||
| 49 | # Disabled 'uninitialized' warnings | ||
| 50 | # | ||
| 51 | # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry | ||
| 52 | # VBTK Project | ||
| 53 | # | ||
| 54 | |||
| 55 | package VBTK::Tcp; | ||
| 56 | |||
| 57 | use 5.6.0; | ||
| 58 | use strict; | ||
| 59 | use warnings; | ||
| 60 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 61 | no warnings qw(uninitialized); | ||
| 62 | |||
| 63 | use VBTK; | ||
| 64 | use VBTK::Common; | ||
| 65 | use VBTK::Parser; | ||
| 66 | use IO::Socket::INET; | ||
| 67 | |||
| 68 | # Inherit methods from Parser class | ||
| 69 | our @ISA = qw(VBTK::Parser); | ||
| 70 | |||
| 71 | our $VERBOSE = $ENV{VERBOSE}; | ||
| 72 | our $DEFAULT_TIMEOUT=10; | ||
| 73 | |||
| 74 | #------------------------------------------------------------------------------- | ||
| 75 | # Function: new | ||
| 76 | # Description: Object constructor. Allocates memory for all class members, | ||
| 77 | # create an Snmp session, check passed Snmp labels. | ||
| 78 | # | ||
| 79 | # Input Parms: | ||
| 80 | # Output Parms: Pointer to self | ||
| 81 | #------------------------------------------------------------------------------- | ||
| 82 | sub new | ||
| 83 | { | ||
| 84 | my ($type,$self); | ||
| 85 | |||
| 86 | # If we're passed a hash as the first element, then it's probably from an | ||
| 87 | # inheriting class | ||
| 88 | if((defined $_[0])&&(UNIVERSAL::isa($_[0], 'HASH'))) | ||
| 89 | { | ||
| 90 | $self = shift; | ||
| 91 | } | ||
| 92 | # Otherwise, allocate a new hash, bless it and handle any passed parms | ||
| 93 | else | ||
| 94 | { | ||
| 95 | $type = shift; | ||
| 96 | $self = {}; | ||
| 97 | bless $self, $type; | ||
| 98 | |||
| 99 | # Store all passed input name pairs in the object | ||
| 100 | $self->set(@_); | ||
| 101 | } | ||
| 102 | |||
| 103 | # Setup the header and detail formats for the pmobjects and log | ||
| 104 | my $stdHeader = [ 'Time Resp Time (sec)', | ||
| 105 | '----------------- ---------------' ]; | ||
| 106 | my $stdDetail = [ '@<<<<<<<<<<<<<<<< @>>>>>>>>>>>>>>', | ||
| 107 | '$time, $data[0]' ]; | ||
| 108 | |||
| 109 | # Setup a hash of default parameters | ||
| 110 | my $defaultParms = { | ||
| 111 | Interval => 60, | ||
| 112 | Host => $::REQUIRED, | ||
| 113 | Port => $::REQUIRED, | ||
| 114 | VBServerURI => $::VBURI, | ||
| 115 | VBHeader => $stdHeader, | ||
| 116 | VBDetail => $stdDetail, | ||
| 117 | LogFile => undef, | ||
| 118 | LogHeader => $stdHeader, | ||
| 119 | LogDetail => $stdDetail, | ||
| 120 | RotateLogAt => '12:00am', | ||
| 121 | Timeout => $DEFAULT_TIMEOUT, | ||
| 122 | ErrorStatus => 'Warning' | ||
| 123 | }; | ||
| 124 | |||
| 125 | # Run the validation, setting defaults if values are not already set | ||
| 126 | $self->validateParms($defaultParms); | ||
| 127 | |||
| 128 | # Create a parser object to handle the response times. | ||
| 129 | $self->SUPER::new() || return undef; | ||
| 130 | |||
| 131 | # Add self to the list of all defined VBTK::Snmp objects | ||
| 132 | &VBTK::register($self); | ||
| 133 | return $self; | ||
| 134 | } | ||
| 135 | |||
| 136 | #------------------------------------------------------------------------------- | ||
| 137 | # Function: addVBObj | ||
| 138 | # Description: Pass the specified Rules name/value pairs to the Parser object | ||
| 139 | # Input Parms: VBTK::Parser name/value pairs hash | ||
| 140 | # Output Parms: None | ||
| 141 | #------------------------------------------------------------------------------- | ||
| 142 | sub addVBObj | ||
| 143 | { | ||
| 144 | my $self = shift; | ||
| 145 | my $Interval = $self->{Interval}; | ||
| 146 | my $Host = $self->{Host}; | ||
| 147 | my $Port = $self->{Port}; | ||
| 148 | my %args = @_; | ||
| 149 | |||
| 150 | # Setup some reasonable thresholds | ||
| 151 | my $expireAfterSec = int($Interval * 3); | ||
| 152 | my $description = qq( | ||
| 153 | This object monitors the TCP listener on $Host:$Port. If will set the status | ||
| 154 | to warning if it is unable to retrieve the URL or if response times become | ||
| 155 | unacceptable. | ||
| 156 | ); | ||
| 157 | |||
| 158 | # Setup default parms | ||
| 159 | my $defaultRules = { | ||
| 160 | VBObjName => $::REQUIRED, | ||
| 161 | TextHistoryLimit => 100, | ||
| 162 | ReverseText => 1, | ||
| 163 | Rules => undef, | ||
| 164 | Requirements => undef, | ||
| 165 | StatusHistoryLimit => 30, | ||
| 166 | StatusChangeActions => undef, | ||
| 167 | StatusUpgradeRules => [ | ||
| 168 | "Upgrade to Failed if Warning occurs 2 times in $expireAfterSec sec" ], | ||
| 169 | ExpireAfter => "$expireAfterSec seconds", | ||
| 170 | Description => $description, | ||
| 171 | RrdTimeCol => undef, | ||
| 172 | RrdColumns => undef, | ||
| 173 | RrdFilter => undef, | ||
| 174 | RrdMin => 0, | ||
| 175 | RrdMax => undef, | ||
| 176 | RrdXFF => undef, | ||
| 177 | RrdCF => undef, | ||
| 178 | RrdDST => undef, | ||
| 179 | }; | ||
| 180 | |||
| 181 | # Run the validation | ||
| 182 | &validateParms(\%args,$defaultRules) || &fatal("Exiting"); | ||
| 183 | |||
| 184 | # Setup the VB object for response time parser. | ||
| 185 | my $vbObj = $self->SUPER::addVBObj(%args); | ||
| 186 | return undef if (! defined $vbObj); | ||
| 187 | |||
| 188 | ($vbObj); | ||
| 189 | } | ||
| 190 | |||
| 191 | #------------------------------------------------------------------------------- | ||
| 192 | # Function: run | ||
| 193 | # Description: Retrieve and process the specified Snmp values | ||
| 194 | # Input Parms: None | ||
| 195 | # Output Parms: Time till next retrieval | ||
| 196 | #------------------------------------------------------------------------------- | ||
| 197 | sub run | ||
| 198 | { | ||
| 199 | my $self = shift; | ||
| 200 | my $Interval = $self->{Interval}; | ||
| 201 | my $Host = $self->{Host}; | ||
| 202 | my $Port = $self->{Port}; | ||
| 203 | my $Timeout = $self->{Timeout}; | ||
| 204 | my $ErrorStatus = $self->{ErrorStatus}; | ||
| 205 | my $lastTime = $self->{lastTime}; | ||
| 206 | |||
| 207 | my $now = time; | ||
| 208 | my ($sock,$elapsedTime,$sleepTime); | ||
| 209 | |||
| 210 | # If it's not time to run yet, then return | ||
| 211 | if(($sleepTime = $self->calcSleepTime()) > 0) | ||
| 212 | { | ||
| 213 | &log("Not time to check $Host:$Port, wait $sleepTime seconds") | ||
| 214 | if ($VERBOSE > 1); | ||
| 215 | return ($sleepTime,$::NOT_FINISHED); | ||
| 216 | } | ||
| 217 | |||
| 218 | # Run all network operations within an alarmed eval, so that nomatter | ||
| 219 | # where it hangs, if it doesn't finish in $timeout seconds, then it will | ||
| 220 | # just fail gracefully. | ||
| 221 | eval { | ||
| 222 | local $SIG{ALRM} = sub { die "Timed out connecting to $Host:$Port\n"; }; | ||
| 223 | alarm $Timeout; | ||
| 224 | |||
| 225 | &log("Connecting to $Host:$Port") if ($VERBOSE > 1); | ||
| 226 | |||
| 227 | $sock = IO::Socket::INET->new( | ||
| 228 | PeerAddr => $Host, | ||
| 229 | PeerPort => $Port, | ||
| 230 | Proto => 'tcp' ); | ||
| 231 | |||
| 232 | die "$!\n" if (! defined $sock); | ||
| 233 | |||
| 234 | &log("Closing socket") if ($VERBOSE > 1); | ||
| 235 | $sock->close; | ||
| 236 | alarm 0; | ||
| 237 | }; | ||
| 238 | |||
| 239 | alarm 0; | ||
| 240 | |||
| 241 | $elapsedTime = time - $now; | ||
| 242 | |||
| 243 | # Check for errors | ||
| 244 | if($@ ne '') | ||
| 245 | { | ||
| 246 | my $msg = "Can't connect to '$Host:$Port' - $@"; | ||
| 247 | &error($msg); | ||
| 248 | $self->parseData(undef,$ErrorStatus,$msg); | ||
| 249 | } | ||
| 250 | else | ||
| 251 | { | ||
| 252 | # Call the response time parser, with the elapsed time | ||
| 253 | $self->parseData([[$elapsedTime]],$::SUCCESS); | ||
| 254 | } | ||
| 255 | |||
| 256 | $sleepTime = $self->calcSleepTime(1); | ||
| 257 | |||
| 258 | ($sleepTime,$::NOT_FINISHED); | ||
| 259 | } | ||
| 260 | |||
| 261 | 1; | ||
| 262 | __END__ |