| 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/Smtp.pm,v $ | ||
| 7 | # $Revision: 1.8 $ | ||
| 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 SMTP servers. | ||
| 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: Smtp.pm,v $ | ||
| 33 | # Revision 1.8 2002/03/04 20:53:07 bhenry | ||
| 34 | # *** empty log message *** | ||
| 35 | # | ||
| 36 | # Revision 1.7 2002/03/04 16:49:09 bhenry | ||
| 37 | # Changed requirement back to perl 5.6.0 | ||
| 38 | # | ||
| 39 | # Revision 1.6 2002/03/02 00:53:55 bhenry | ||
| 40 | # Documentation updates | ||
| 41 | # | ||
| 42 | # Revision 1.5 2002/02/13 07:38:51 bhenry | ||
| 43 | # Disabled RrdLogRecovery and removed use of @log | ||
| 44 | # | ||
| 45 | # Revision 1.4 2002/01/28 19:35:14 bhenry | ||
| 46 | # Bug Fixes | ||
| 47 | # | ||
| 48 | # Revision 1.3 2002/01/25 07:15:11 bhenry | ||
| 49 | # Changed to inherit from Parser | ||
| 50 | # | ||
| 51 | # Revision 1.2 2002/01/21 17:07:40 bhenry | ||
| 52 | # Disabled 'uninitialized' warnings | ||
| 53 | # | ||
| 54 | # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry | ||
| 55 | # VBTK Project | ||
| 56 | # | ||
| 57 | |||
| 58 | package VBTK::Smtp; | ||
| 59 | |||
| 60 | use 5.6.0; | ||
| 61 | use strict; | ||
| 62 | use warnings; | ||
| 63 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 64 | no warnings qw(uninitialized); | ||
| 65 | |||
| 66 | use VBTK; | ||
| 67 | use VBTK::Common; | ||
| 68 | use VBTK::Parser; | ||
| 69 | use Mail::Sendmail; | ||
| 70 | |||
| 71 | # Inherit methods from Parser class | ||
| 72 | our @ISA = qw(VBTK::Parser); | ||
| 73 | |||
| 74 | our $VERBOSE = $ENV{VERBOSE}; | ||
| 75 | our $DEFAULT_TIMEOUT=10; | ||
| 76 | |||
| 77 | #------------------------------------------------------------------------------- | ||
| 78 | # Function: new | ||
| 79 | # Description: Object constructor. Allocates memory for all class members, | ||
| 80 | # create an Snmp session, check passed Snmp labels. | ||
| 81 | # | ||
| 82 | # Input Parms: | ||
| 83 | # Output Parms: Pointer to self | ||
| 84 | #------------------------------------------------------------------------------- | ||
| 85 | sub new | ||
| 86 | { | ||
| 87 | my ($type,$self); | ||
| 88 | |||
| 89 | # If we're passed a hash as the first element, then it's probably from an | ||
| 90 | # inheriting class | ||
| 91 | if((defined $_[0])&&(UNIVERSAL::isa($_[0], 'HASH'))) | ||
| 92 | { | ||
| 93 | $self = shift; | ||
| 94 | } | ||
| 95 | # Otherwise, allocate a new hash, bless it and handle any passed parms | ||
| 96 | else | ||
| 97 | { | ||
| 98 | $type = shift; | ||
| 99 | $self = {}; | ||
| 100 | bless $self, $type; | ||
| 101 | |||
| 102 | # Store all passed input name pairs in the object | ||
| 103 | $self->set(@_); | ||
| 104 | } | ||
| 105 | |||
| 106 | # Setup the header and detail formats for the pmobjects and log | ||
| 107 | my $stdHeader = [ 'Time Resp Time (sec) Error', | ||
| 108 | '----------------- --------------- -----' ]; | ||
| 109 | my $stdDetail = [ '@<<<<<<<<<<<<<<<< @>>>>>>>>>>>>>> @>>>>', | ||
| 110 | '$time, $data[0], $data[1]' ]; | ||
| 111 | |||
| 112 | # Setup a hash of default parameters | ||
| 113 | my $defaultParms = { | ||
| 114 | Interval => 300, | ||
| 115 | Host => $::REQUIRED, | ||
| 116 | Port => 25, | ||
| 117 | To => $::REQUIRED, | ||
| 118 | From => $self->{To}, | ||
| 119 | Subject => "VBTK Test from $::HOST", | ||
| 120 | Message => undef, | ||
| 121 | VBServerURI => $::VBURI, | ||
| 122 | VBHeader => $stdHeader, | ||
| 123 | VBDetail => $stdDetail, | ||
| 124 | LogFile => undef, | ||
| 125 | LogHeader => $stdHeader, | ||
| 126 | LogDetail => $stdDetail, | ||
| 127 | RotateLogAt => '12:00am', | ||
| 128 | Timeout => $DEFAULT_TIMEOUT, | ||
| 129 | ErrorStatus => 'Warning' | ||
| 130 | }; | ||
| 131 | |||
| 132 | # Run the validation, setting defaults if values are not already set | ||
| 133 | $self->validateParms($defaultParms); | ||
| 134 | |||
| 135 | # Create a parser object to handle the response times. | ||
| 136 | $self->SUPER::new(); | ||
| 137 | |||
| 138 | # Setup the mail hash | ||
| 139 | $self->{mailHash} = { | ||
| 140 | To => $self->{To}, | ||
| 141 | From => $self->{From}, | ||
| 142 | Subject => $self->{Subject}, | ||
| 143 | Message => $self->{Message}, | ||
| 144 | Smtp => $self->{Host} . ":" . $self->{Port}, | ||
| 145 | }; | ||
| 146 | |||
| 147 | # Add self to the list of all defined VBTK objects | ||
| 148 | &VBTK::register($self); | ||
| 149 | return $self; | ||
| 150 | } | ||
| 151 | |||
| 152 | #------------------------------------------------------------------------------- | ||
| 153 | # Function: addVBObj | ||
| 154 | # Description: Pass the specified Rules name/value pairs to the Parser object | ||
| 155 | # Input Parms: VBTK::Parser name/value pairs hash | ||
| 156 | # Output Parms: None | ||
| 157 | #------------------------------------------------------------------------------- | ||
| 158 | sub addVBObj | ||
| 159 | { | ||
| 160 | my $self = shift; | ||
| 161 | my $Interval = $self->{Interval}; | ||
| 162 | my $Host = $self->{Host}; | ||
| 163 | my $Port = $self->{Port}; | ||
| 164 | my %args = @_; | ||
| 165 | |||
| 166 | # Setup some reasonable thresholds | ||
| 167 | my $expireAfterSec = int($Interval * 4); | ||
| 168 | my $description = qq( | ||
| 169 | This object monitors the SMTP server '$Host:$Port'. If will set the status | ||
| 170 | to warning if it is unable to send an email or if response times become | ||
| 171 | unacceptable. | ||
| 172 | ); | ||
| 173 | |||
| 174 | # Setup default parms | ||
| 175 | my $defaultRules = { | ||
| 176 | VBObjName => ".$Host.smtp", | ||
| 177 | TextHistoryLimit => 30, | ||
| 178 | ReverseText => 1, | ||
| 179 | Rules => undef, | ||
| 180 | Requirements => undef, | ||
| 181 | StatusHistoryLimit => 30, | ||
| 182 | StatusChangeActions => undef, | ||
| 183 | StatusUpgradeRules => [ | ||
| 184 | "Upgrade to Failed if Warning occurs 3 times in $expireAfterSec sec" ], | ||
| 185 | ExpireAfter => "$expireAfterSec seconds", | ||
| 186 | Description => $description, | ||
| 187 | RrdTimeCol => undef, | ||
| 188 | RrdColumns => [ '$data[0]', '$data[1]' ], | ||
| 189 | RrdFilter => undef, | ||
| 190 | RrdMin => 0, | ||
| 191 | RrdMax => undef, | ||
| 192 | RrdXFF => undef, | ||
| 193 | RrdCF => undef, | ||
| 194 | RrdDST => undef, | ||
| 195 | }; | ||
| 196 | |||
| 197 | # Run the validation | ||
| 198 | &validateParms(\%args,$defaultRules) || &fatal("Exiting"); | ||
| 199 | |||
| 200 | # Setup the VB object for response time parser. | ||
| 201 | my $vbObj = $self->SUPER::addVBObj(%args); | ||
| 202 | return undef unless ($vbObj); | ||
| 203 | |||
| 204 | # Now define what graphs to show on this object's page, but only if | ||
| 205 | # a value was specified for 'RrdColumns' | ||
| 206 | if(($args{RrdColumns})&&(@{$args{RrdColumns}} > 0)) | ||
| 207 | { | ||
| 208 | $vbObj->addGraphGroup ( | ||
| 209 | GroupNumber => 1, | ||
| 210 | DataSourceList => ':0', | ||
| 211 | Labels => 'resp time (sec)', | ||
| 212 | Title => "SMTP Svr $Host:$Port", | ||
| 213 | ); | ||
| 214 | |||
| 215 | $vbObj->addGraphGroup ( | ||
| 216 | GroupNumber => 2, | ||
| 217 | DataSourceList => ':1', | ||
| 218 | Labels => 'errors', | ||
| 219 | Title => "SMTP Svr $Host:$Port", | ||
| 220 | ); | ||
| 221 | } | ||
| 222 | |||
| 223 | ($vbObj); | ||
| 224 | } | ||
| 225 | |||
| 226 | #------------------------------------------------------------------------------- | ||
| 227 | # Function: run | ||
| 228 | # Description: Retrieve and process the specified Snmp values | ||
| 229 | # Input Parms: None | ||
| 230 | # Output Parms: Time till next retrieval | ||
| 231 | #------------------------------------------------------------------------------- | ||
| 232 | sub run | ||
| 233 | { | ||
| 234 | my $self = shift; | ||
| 235 | my $Interval = $self->{Interval}; | ||
| 236 | my $Host = $self->{Host}; | ||
| 237 | my $Port = $self->{Port}; | ||
| 238 | my $Timeout = $self->{Timeout}; | ||
| 239 | my $ErrorStatus = $self->{ErrorStatus}; | ||
| 240 | my $lastTime = $self->{lastTime}; | ||
| 241 | my $mailHash = $self->{mailHash}; | ||
| 242 | my $To = $self->{To}; | ||
| 243 | |||
| 244 | my ($sock,$elapsedTime,$sleepTime); | ||
| 245 | |||
| 246 | # If it's not time to run yet, then return | ||
| 247 | if(($sleepTime = $self->calcSleepTime()) > 0) | ||
| 248 | { | ||
| 249 | &log("Not time to check $Host:$Port, wait $sleepTime seconds") | ||
| 250 | if ($VERBOSE > 1); | ||
| 251 | return ($sleepTime,$::NOT_FINISHED); | ||
| 252 | } | ||
| 253 | |||
| 254 | my $now = time; | ||
| 255 | |||
| 256 | # Run all network operations within an alarmed eval, so that nomatter | ||
| 257 | # where it hangs, if it doesn't finish in $timeout seconds, then it will | ||
| 258 | # just fail gracefully. | ||
| 259 | eval { | ||
| 260 | local $SIG{ALRM} = sub { die "Timed out while connecting\n"; }; | ||
| 261 | alarm $Timeout; | ||
| 262 | |||
| 263 | &log("Sending email for '$To' to $Host:$Port") if ($VERBOSE > 1); | ||
| 264 | |||
| 265 | &sendmail(%{$mailHash}) || die "$Mail::Sendmail::error\n"; | ||
| 266 | |||
| 267 | alarm 0; | ||
| 268 | }; | ||
| 269 | |||
| 270 | alarm 0; | ||
| 271 | |||
| 272 | $elapsedTime = time - $now; | ||
| 273 | |||
| 274 | # Check for errors | ||
| 275 | if($@ ne '') | ||
| 276 | { | ||
| 277 | my $msg = "Error sending mail to '$Host:$Port' - $@"; | ||
| 278 | &error($msg); | ||
| 279 | $self->parseData([[$elapsedTime,1]],$ErrorStatus,$msg); | ||
| 280 | } | ||
| 281 | else | ||
| 282 | { | ||
| 283 | # Call the response time parser, with the elapsed time | ||
| 284 | $self->parseData([[$elapsedTime,0]],$::SUCCESS); | ||
| 285 | } | ||
| 286 | |||
| 287 | $sleepTime = $self->calcSleepTime(1); | ||
| 288 | |||
| 289 | ($sleepTime,$::NOT_FINISHED); | ||
| 290 | } | ||
| 291 | |||
| 292 | 1; | ||
| 293 | __END__ |