| 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/Pop3.pm,v $ | ||
| 7 | # $Revision: 1.10 $ | ||
| 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 POP3 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: Pop3.pm,v $ | ||
| 33 | # Revision 1.10 2002/03/04 20:53:07 bhenry | ||
| 34 | # *** empty log message *** | ||
| 35 | # | ||
| 36 | # Revision 1.9 2002/03/04 16:49:09 bhenry | ||
| 37 | # Changed requirement back to perl 5.6.0 | ||
| 38 | # | ||
| 39 | # Revision 1.8 2002/03/02 00:53:55 bhenry | ||
| 40 | # Documentation updates | ||
| 41 | # | ||
| 42 | # Revision 1.7 2002/02/20 19:25:18 bhenry | ||
| 43 | # *** empty log message *** | ||
| 44 | # | ||
| 45 | # Revision 1.6 2002/02/19 19:08:38 bhenry | ||
| 46 | # Added 'total mailbox count' to data gathered | ||
| 47 | # | ||
| 48 | # Revision 1.5 2002/02/13 07:38:51 bhenry | ||
| 49 | # Disabled RrdLogRecovery and removed use of @log | ||
| 50 | # | ||
| 51 | # Revision 1.4 2002/01/28 19:35:14 bhenry | ||
| 52 | # Bug Fixes | ||
| 53 | # | ||
| 54 | # Revision 1.3 2002/01/25 07:15:25 bhenry | ||
| 55 | # Changed to inherit from Parser | ||
| 56 | # | ||
| 57 | # Revision 1.2 2002/01/21 17:07:40 bhenry | ||
| 58 | # Disabled 'uninitialized' warnings | ||
| 59 | # | ||
| 60 | # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry | ||
| 61 | # VBTK Project | ||
| 62 | # | ||
| 63 | |||
| 64 | package VBTK::Pop3; | ||
| 65 | |||
| 66 | use 5.6.0; | ||
| 67 | use strict; | ||
| 68 | use warnings; | ||
| 69 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 70 | no warnings qw(uninitialized); | ||
| 71 | |||
| 72 | use VBTK; | ||
| 73 | use VBTK::Common; | ||
| 74 | use VBTK::Parser; | ||
| 75 | use Mail::POP3Client; | ||
| 76 | use Date::Manip; | ||
| 77 | |||
| 78 | # Inherit methods from Parser class | ||
| 79 | our @ISA = qw(VBTK::Parser); | ||
| 80 | |||
| 81 | our $VERBOSE = $ENV{VERBOSE}; | ||
| 82 | our $DEFAULT_TIMEOUT=30; | ||
| 83 | |||
| 84 | #------------------------------------------------------------------------------- | ||
| 85 | # Function: new | ||
| 86 | # Description: Object constructor. Allocates memory for all class members, | ||
| 87 | # | ||
| 88 | # Input Parms: | ||
| 89 | # Output Parms: Pointer to self | ||
| 90 | #------------------------------------------------------------------------------- | ||
| 91 | sub new | ||
| 92 | { | ||
| 93 | my ($type,$self); | ||
| 94 | |||
| 95 | # If we're passed a hash as the first element, then it's probably from an | ||
| 96 | # inheriting class | ||
| 97 | if((defined $_[0])&&(UNIVERSAL::isa($_[0], 'HASH'))) | ||
| 98 | { | ||
| 99 | $self = shift; | ||
| 100 | } | ||
| 101 | # Otherwise, allocate a new hash, bless it and handle any passed parms | ||
| 102 | else | ||
| 103 | { | ||
| 104 | $type = shift; | ||
| 105 | $self = {}; | ||
| 106 | bless $self, $type; | ||
| 107 | |||
| 108 | # Store all passed input name pairs in the object | ||
| 109 | $self->set(@_); | ||
| 110 | } | ||
| 111 | |||
| 112 | # Setup the header and detail formats for the pmobjects and log | ||
| 113 | my $stdHeader = [ ' Retrieval Round-trip Retrieved MailBox', | ||
| 114 | 'Time (sec) (sec) Count Count Error', | ||
| 115 | '----------------- --------- ---------- --------- ------- -----' ]; | ||
| 116 | my $stdDetail = [ '@<<<<<<<<<<<<<<<< @>>>>>>>> @>>>>>>>>> @>>>>>>>> @>>>>>> @>>>>', | ||
| 117 | '$time,@data[0..4]' ]; | ||
| 118 | |||
| 119 | # Setup a hash of default parameters | ||
| 120 | my $defaultParms = { | ||
| 121 | Interval => 300, | ||
| 122 | Host => $::REQUIRED, | ||
| 123 | Port => 110, | ||
| 124 | User => $::REQUIRED, | ||
| 125 | Password => $::REQUIRED, | ||
| 126 | RetrieveFilter => undef, | ||
| 127 | VBServerURI => $::VBURI, | ||
| 128 | VBHeader => $stdHeader, | ||
| 129 | VBDetail => $stdDetail, | ||
| 130 | LogFile => undef, | ||
| 131 | LogHeader => $stdHeader, | ||
| 132 | LogDetail => $stdDetail, | ||
| 133 | RotateLogAt => '12:00am', | ||
| 134 | Timeout => $DEFAULT_TIMEOUT, | ||
| 135 | ErrorStatus => 'Warning' | ||
| 136 | }; | ||
| 137 | |||
| 138 | # Run the validation, setting defaults if values are not already set | ||
| 139 | $self->validateParms($defaultParms); | ||
| 140 | |||
| 141 | # Create a parser object to handle the response times. | ||
| 142 | $self->SUPER::new() || return undef; | ||
| 143 | |||
| 144 | # Add self to the list of all defined VBTK objects | ||
| 145 | &VBTK::register($self); | ||
| 146 | return $self; | ||
| 147 | } | ||
| 148 | |||
| 149 | #------------------------------------------------------------------------------- | ||
| 150 | # Function: addVBObj | ||
| 151 | # Description: Pass the specified Rules name/value pairs to the Parser object | ||
| 152 | # Input Parms: VBTK::Parser name/value pairs hash | ||
| 153 | # Output Parms: None | ||
| 154 | #------------------------------------------------------------------------------- | ||
| 155 | sub addVBObj | ||
| 156 | { | ||
| 157 | my $self = shift; | ||
| 158 | my $Interval = $self->{Interval}; | ||
| 159 | my $Host = $self->{Host}; | ||
| 160 | my $Port = $self->{Port}; | ||
| 161 | my %args = @_; | ||
| 162 | |||
| 163 | # Setup some reasonable thresholds | ||
| 164 | my $expireAfterSec = int($Interval * 4); | ||
| 165 | my $description = qq( | ||
| 166 | This object monitors the SMTP server '$Host:$Port'. If will set the status | ||
| 167 | to warning if it is unable to connect to the POP3 server or if response | ||
| 168 | times become unacceptable. | ||
| 169 | ); | ||
| 170 | |||
| 171 | # Setup default parms | ||
| 172 | my $defaultRules = { | ||
| 173 | VBObjName => $::REQUIRED, | ||
| 174 | TextHistoryLimit => 30, | ||
| 175 | ReverseText => 1, | ||
| 176 | Rules => undef, | ||
| 177 | Requirements => undef, | ||
| 178 | StatusHistoryLimit => 30, | ||
| 179 | StatusChangeActions => undef, | ||
| 180 | StatusUpgradeRules => [ | ||
| 181 | "Upgrade to Failed if Warning occurs 3 times in $expireAfterSec sec" ], | ||
| 182 | ExpireAfter => "$expireAfterSec seconds", | ||
| 183 | Description => $description, | ||
| 184 | RrdTimeCol => undef, | ||
| 185 | RrdColumns => [ '$data[0]', '$data[1]', '$data[4]' ], | ||
| 186 | RrdFilter => undef, | ||
| 187 | RrdMin => 0, | ||
| 188 | RrdMax => undef, | ||
| 189 | RrdXFF => undef, | ||
| 190 | RrdCF => undef, | ||
| 191 | RrdDST => undef, | ||
| 192 | }; | ||
| 193 | |||
| 194 | # Run the validation | ||
| 195 | &validateParms(\%args,$defaultRules) || &fatal("Exiting"); | ||
| 196 | |||
| 197 | # Setup the VB object for response time parser. | ||
| 198 | my $vbObj = $self->SUPER::addVBObj(%args); | ||
| 199 | return undef if (! defined $vbObj); | ||
| 200 | |||
| 201 | # Now define what graphs to show on this object's page, but only if | ||
| 202 | # a value was specified for 'RrdColumns' | ||
| 203 | if(($args{RrdColumns})&&(@{$args{RrdColumns}} > 0)) | ||
| 204 | { | ||
| 205 | $vbObj->addGraphGroup ( | ||
| 206 | GroupNumber => 1, | ||
| 207 | DataSourceList => ':0,:1', | ||
| 208 | Labels => 'retrieval time (sec),round-trip time (sec)', | ||
| 209 | Title => "POP3 Svr $Host:$Port", | ||
| 210 | ); | ||
| 211 | |||
| 212 | $vbObj->addGraphGroup ( | ||
| 213 | GroupNumber => 2, | ||
| 214 | DataSourceList => ':2', | ||
| 215 | Labels => 'errors', | ||
| 216 | Title => "POP3 Svr $Host:$Port", | ||
| 217 | ); | ||
| 218 | } | ||
| 219 | |||
| 220 | ($vbObj); | ||
| 221 | } | ||
| 222 | |||
| 223 | #------------------------------------------------------------------------------- | ||
| 224 | # Function: run | ||
| 225 | # Description: Retrieve and process the specified Snmp values | ||
| 226 | # Input Parms: None | ||
| 227 | # Output Parms: Time till next retrieval | ||
| 228 | #------------------------------------------------------------------------------- | ||
| 229 | sub run | ||
| 230 | { | ||
| 231 | my $self = shift; | ||
| 232 | my $Interval = $self->{Interval}; | ||
| 233 | my $Host = $self->{Host}; | ||
| 234 | my $Port = $self->{Port}; | ||
| 235 | my $User = $self->{User}; | ||
| 236 | my $Password = $self->{Password}; | ||
| 237 | my $Timeout = $self->{Timeout}; | ||
| 238 | my $ErrorStatus = $self->{ErrorStatus}; | ||
| 239 | my $RetrieveFilter = $self->{RetrieveFilter}; | ||
| 240 | my $lastTime = $self->{lastTime}; | ||
| 241 | |||
| 242 | my $now = time; | ||
| 243 | my ($sock,$elapsedTime,$pop,$roundTrip,$count,$sleepTime,$totMessages); | ||
| 244 | |||
| 245 | # If it's not time to run yet, then return | ||
| 246 | if(($sleepTime = $self->calcSleepTime()) > 0) | ||
| 247 | { | ||
| 248 | &log("Not time to check $Host:$Port, wait $sleepTime seconds") | ||
| 249 | if ($VERBOSE > 1); | ||
| 250 | return ($sleepTime,$::NOT_FINISHED); | ||
| 251 | } | ||
| 252 | |||
| 253 | # Run all network operations within an alarmed eval, so that nomatter | ||
| 254 | # where it hangs, if it doesn't finish in $timeout seconds, then it will | ||
| 255 | # just fail gracefully. | ||
| 256 | eval { | ||
| 257 | local $SIG{ALRM} = sub { die "Timed out connecting to $Host:$Port\n"; }; | ||
| 258 | alarm $Timeout; | ||
| 259 | |||
| 260 | &log("Connecting to $Host:$Port") if ($VERBOSE > 1); | ||
| 261 | |||
| 262 | $pop = new Mail::POP3Client( | ||
| 263 | USER => $User, | ||
| 264 | PASSWORD => $Password, | ||
| 265 | HOST => $Host, | ||
| 266 | PORT => $Port, | ||
| 267 | TIMEOUT => $Timeout | ||
| 268 | ); | ||
| 269 | |||
| 270 | $pop->Alive() || die "Can't connect\n"; | ||
| 271 | |||
| 272 | # If a processFilter was specified, then look for email which | ||
| 273 | # have a subject matching the filter, and process them. | ||
| 274 | ($roundTrip,$count,$totMessages) = $self->processEmail($pop) | ||
| 275 | if (defined $RetrieveFilter); | ||
| 276 | |||
| 277 | $pop->Close(); | ||
| 278 | |||
| 279 | alarm 0; | ||
| 280 | }; | ||
| 281 | |||
| 282 | alarm 0; | ||
| 283 | |||
| 284 | $elapsedTime = time - $now; | ||
| 285 | |||
| 286 | # Check for errors | ||
| 287 | if($@ ne '') | ||
| 288 | { | ||
| 289 | my $msg = "Error retrieving mail from '$Host:$Port' - $@"; | ||
| 290 | &error($msg); | ||
| 291 | $self->parseData([[$elapsedTime,0,0,0,1]],$ErrorStatus,$msg); | ||
| 292 | } | ||
| 293 | # If the RetrieveFilter is specified, then only report a status if the | ||
| 294 | # count is > 0. If the RetrieveFilter is not specified, then always report it. | ||
| 295 | elsif(($count > 0)||(! defined $RetrieveFilter)) | ||
| 296 | { | ||
| 297 | # Call the response time parser, with the elapsed time | ||
| 298 | $self->parseData([[$elapsedTime,$roundTrip,$count,$totMessages,0]],$::SUCCESS); | ||
| 299 | } | ||
| 300 | |||
| 301 | $sleepTime = $self->calcSleepTime(1); | ||
| 302 | |||
| 303 | ($sleepTime,$::NOT_FINISHED); | ||
| 304 | } | ||
| 305 | |||
| 306 | #------------------------------------------------------------------------------- | ||
| 307 | # Function: processEmail | ||
| 308 | # Description: Look through the email in the passed pop account and find any which | ||
| 309 | # have a subject matching the specified RetrieveFilter. If they match, | ||
| 310 | # then get their headers and calculate the round-trip time. | ||
| 311 | # Input Parms: None | ||
| 312 | # Output Parms: Email average round-trip time | ||
| 313 | #------------------------------------------------------------------------------- | ||
| 314 | sub processEmail | ||
| 315 | { | ||
| 316 | my $self = shift; | ||
| 317 | my $pop = shift; | ||
| 318 | my $RetrieveFilter = $self->{RetrieveFilter}; | ||
| 319 | |||
| 320 | my ($roundTrip,$count,$i,@headers,@subject,@startDateLine,@endDateLine); | ||
| 321 | my ($startTime,$endTime,$roundTripTotal); | ||
| 322 | |||
| 323 | my $totMessages = $pop->Count(); | ||
| 324 | for($i = 1; $i <= $totMessages; $i++) | ||
| 325 | { | ||
| 326 | @headers = $pop->Head($i); | ||
| 327 | @subject = grep(/^Subject:\s+/i,@headers); | ||
| 328 | |||
| 329 | next unless(grep(/$RetrieveFilter/,@subject)); | ||
| 330 | |||
| 331 | $pop->Delete($i); | ||
| 332 | |||
| 333 | @startDateLine = grep(/^Date:\s+/,@headers); | ||
| 334 | @endDateLine = grep(/^Received:\s+/,@headers); | ||
| 335 | |||
| 336 | # Convert the 'Date' entry to unix time. Skip this email if the | ||
| 337 | # conversion fails. | ||
| 338 | # unless(($startDateLine[0] =~ /^Date:\s*(.+)\s*$/)&& | ||
| 339 | # (($startTime = &Date::Manip::UnixDate($1, "%s")) > 0)) | ||
| 340 | # { | ||
| 341 | # &error("Can't determine unix time from '$startDateLine[0]'"); | ||
| 342 | # next; | ||
| 343 | # } | ||
| 344 | |||
| 345 | # Convert the most recent 'Retrieved' entry to unix time. Skip this | ||
| 346 | # email if the conversion fails. | ||
| 347 | # unless(($endDateLine[0] =~ /^Received:.*(\d{1,2}\s+\w{3,3}\s+\d{4,4}\s+.+)\s*$/)&& | ||
| 348 | # (($endTime = &Date::Manip::UnixDate($1, "%s")) > 0)) | ||
| 349 | # { | ||
| 350 | # &error("Can't determine unix time from '$endDateLine[0]'"); | ||
| 351 | # next; | ||
| 352 | # } | ||
| 353 | |||
| 354 | $count++; | ||
| 355 | $roundTripTotal += $endTime - $startTime; | ||
| 356 | } | ||
| 357 | |||
| 358 | # Calculate the average round-trip time. | ||
| 359 | $roundTrip = ($count > 0) ? $roundTripTotal/$count : 0; | ||
| 360 | |||
| 361 | ($roundTrip,$count,$totMessages); | ||
| 362 | } | ||
| 363 | |||
| 364 | # Put in a stub for handleSignal | ||
| 365 | sub handleSignal { (0); } | ||
| 366 | |||
| 367 | |||
| 368 | 1; | ||
| 369 | __END__ |