| 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/Parser.pm,v $ | ||
| 7 | # $Revision: 1.9 $ | ||
| 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 parse passed data, | ||
| 14 | # logging results to a log file, and setting VBServer | ||
| 15 | # objects based on a passed rule set. | ||
| 16 | # | ||
| 17 | # Invoked by: VBTK::Oracle.pm, VBTK::Snmp.pm, VBTK::Wrapper.pm | ||
| 18 | # | ||
| 19 | # Depends on: VBTK::Common.pm, VBTK::Client.pm | ||
| 20 | # | ||
| 21 | # Copyright (C) 1996 - 2002 Brent Henry | ||
| 22 | # | ||
| 23 | # This program is free software; you can redistribute it and/or | ||
| 24 | # modify it under the terms of version 2 of the GNU General Public | ||
| 25 | # License as published by the Free Software Foundation available at: | ||
| 26 | # http://www.gnu.org/copyleft/gpl.html | ||
| 27 | # | ||
| 28 | # This program is distributed in the hope that it will be useful, | ||
| 29 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 30 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 31 | # GNU General Public License for more details. | ||
| 32 | # | ||
| 33 | ############################################################################# | ||
| 34 | # | ||
| 35 | # | ||
| 36 | # REVISION HISTORY: | ||
| 37 | # | ||
| 38 | # $Log: Parser.pm,v $ | ||
| 39 | # Revision 1.9 2002/03/04 20:53:07 bhenry | ||
| 40 | # *** empty log message *** | ||
| 41 | # | ||
| 42 | # Revision 1.8 2002/03/04 16:49:09 bhenry | ||
| 43 | # Changed requirement back to perl 5.6.0 | ||
| 44 | # | ||
| 45 | # Revision 1.7 2002/03/02 00:53:55 bhenry | ||
| 46 | # Documentation updates | ||
| 47 | # | ||
| 48 | # Revision 1.6 2002/02/19 19:07:56 bhenry | ||
| 49 | # Changed 'lastTime' counter to 'lastRunTime' in calcSleepTime method, to | ||
| 50 | # avoid conflict with another 'lastTime' counter recently added. | ||
| 51 | # | ||
| 52 | # Revision 1.5 2002/02/13 07:40:12 bhenry | ||
| 53 | # Disabled RrdLogRecovery and removed use of @log | ||
| 54 | # | ||
| 55 | # | ||
| 56 | |||
| 57 | package VBTK::Parser; | ||
| 58 | |||
| 59 | use 5.6.0; | ||
| 60 | use strict; | ||
| 61 | use warnings; | ||
| 62 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 63 | no warnings qw(uninitialized); | ||
| 64 | |||
| 65 | use VBTK::Common; | ||
| 66 | use VBTK::Client; | ||
| 67 | use VBTK::ClientObject; | ||
| 68 | use VBTK::Objects::UpgradeRules; | ||
| 69 | use VBTK::Objects::ChangeActions; | ||
| 70 | use VBTK::Objects::Rrd; | ||
| 71 | use Date::Manip; | ||
| 72 | use FileHandle; | ||
| 73 | use POSIX ":sys_wait_h"; | ||
| 74 | |||
| 75 | $SIG{INT} = 'fatal'; | ||
| 76 | |||
| 77 | our $VERBOSE = $ENV{VERBOSE}; | ||
| 78 | |||
| 79 | our $MAX_LOG_RECOVERY_INTERVAL = 1000; | ||
| 80 | our $FORK_SUPPORTED = 1; | ||
| 81 | |||
| 82 | #------------------------------------------------------------------------------- | ||
| 83 | # Function: new | ||
| 84 | # Description: Object constructor. Allocates memory for all class members, | ||
| 85 | # initializes a VBClient object, opens log files, and checks for | ||
| 86 | # required name/value hash pairs. | ||
| 87 | # Input Parms: VBTK::Parser name/value hash | ||
| 88 | # Output Parms: Pointer to class | ||
| 89 | #------------------------------------------------------------------------------- | ||
| 90 | sub new | ||
| 91 | { | ||
| 92 | my ($type,$self); | ||
| 93 | |||
| 94 | # If we're passed a hash, then it's probably from an inheriting class | ||
| 95 | if((defined $_[0])&&(UNIVERSAL::isa($_[0], 'HASH'))) | ||
| 96 | { | ||
| 97 | $self = shift; | ||
| 98 | } | ||
| 99 | # Otherwise, allocate a new hash, bless it and handle any passed parms | ||
| 100 | else | ||
| 101 | { | ||
| 102 | $type = shift; | ||
| 103 | $self = {}; | ||
| 104 | bless $self, $type; | ||
| 105 | |||
| 106 | # Set any passed parms | ||
| 107 | $self->set(@_); | ||
| 108 | |||
| 109 | # Setup a hash of default parameters | ||
| 110 | my $defaultParms = { | ||
| 111 | Interval => undef, | ||
| 112 | VBServerURI => $::VBURI, | ||
| 113 | VBHeader => undef, | ||
| 114 | VBDetail => [ '$data' ], | ||
| 115 | LogFile => undef, | ||
| 116 | LogHeader => undef, | ||
| 117 | LogDetail => undef, | ||
| 118 | RotateLogAt => '12:00am', | ||
| 119 | RotateLogOnEOF => undef, | ||
| 120 | PreProcessor => undef, | ||
| 121 | Split => undef, | ||
| 122 | Filter => undef, | ||
| 123 | Ignore => undef | ||
| 124 | }; | ||
| 125 | |||
| 126 | # Validate the passed parms against the default parms. | ||
| 127 | $self->validateParms($defaultParms) || &fatal("Exiting"); | ||
| 128 | } | ||
| 129 | |||
| 130 | # Allocate some internal structures | ||
| 131 | $self->{vbObjects} = []; | ||
| 132 | $self->{lastRows} = {}; | ||
| 133 | $self->{parseCount} = 0; | ||
| 134 | |||
| 135 | # Default 'Split' to '\s+' unless Filter contains a '(' which means it will | ||
| 136 | # be doing the splitting as part of the pattern match. | ||
| 137 | $self->{Split} ||= '\s+' unless ($self->{Filter} =~ /\(/); | ||
| 138 | |||
| 139 | # Create a VBClient object to be used for reporting status changes to the main | ||
| 140 | # VBServer process. | ||
| 141 | if($self->{VBServerURI} ne '') | ||
| 142 | { | ||
| 143 | &log("Setting up VBClient for $self->{VBServerURI}") if ($VERBOSE); | ||
| 144 | $self->{vbClientObj} = new VBTK::Client(RemoteURI => $self->{VBServerURI}); | ||
| 145 | |||
| 146 | fatal("Can't setup vbclient object for '$self->{VBServerURI}") | ||
| 147 | if ($self->{vbClientObj} eq ''); | ||
| 148 | } | ||
| 149 | |||
| 150 | # Set the logRecoveryDone flag if no log file is specified or if no log | ||
| 151 | # detail format is specified. | ||
| 152 | $self->{logRecoveryDone} = 1 if (! defined $self->{LogFile}); | ||
| 153 | |||
| 154 | # This disables log recovery. I'm going to yank this out at some point | ||
| 155 | # because it's too confusing. I'll add a separate utility to do log | ||
| 156 | # importing. For now, this will disable it. | ||
| 157 | $self->{logRecoveryDone} = 1; | ||
| 158 | |||
| 159 | $self->openLog; | ||
| 160 | |||
| 161 | $self; | ||
| 162 | } | ||
| 163 | |||
| 164 | #------------------------------------------------------------------------------- | ||
| 165 | # Function: checkLogRotate | ||
| 166 | # Description: Check to see if it's time to rotate the log. If so, then | ||
| 167 | # Close the log file and rename it if it's open. Then open a new | ||
| 168 | # log file. If the log file name contains whitespace, then assume | ||
| 169 | # it's actually a command to be executed. | ||
| 170 | # Input Parms: None | ||
| 171 | # Output Parms: None | ||
| 172 | #------------------------------------------------------------------------------- | ||
| 173 | sub checkLogRotate | ||
| 174 | { | ||
| 175 | my $self = shift; | ||
| 176 | my $nextLogRotate = $self->{nextLogRotate}; | ||
| 177 | |||
| 178 | return (0) unless ($nextLogRotate); | ||
| 179 | |||
| 180 | my $now = &datestamp(); | ||
| 181 | |||
| 182 | &log("Checking next=$nextLogRotate to decide if log" . | ||
| 183 | " should be rotated") if ($VERBOSE > 1); | ||
| 184 | |||
| 185 | $self->rotateLog if ($nextLogRotate le $now); | ||
| 186 | |||
| 187 | (0); | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | #------------------------------------------------------------------------------- | ||
| 192 | # Function: rotateLog | ||
| 193 | # Description: Close the log file and rename it if it's open. Then open a new | ||
| 194 | # log file. If the log file name contains whitespace, then assume | ||
| 195 | # it's actually a command to be executed. | ||
| 196 | # Input Parms: None | ||
| 197 | # Output Parms: None | ||
| 198 | #------------------------------------------------------------------------------- | ||
| 199 | sub rotateLog | ||
| 200 | { | ||
| 201 | my $self = shift; | ||
| 202 | my $LogFile = $self->{LogFile}; | ||
| 203 | my $LogHandle = $self->{LogHandle}; | ||
| 204 | |||
| 205 | &log("Rotating log '$LogFile'"); | ||
| 206 | |||
| 207 | # Close the log handle if it's open. | ||
| 208 | $LogHandle->close() if ($LogHandle ne ''); | ||
| 209 | |||
| 210 | # Rename the log to append a date/timestamp | ||
| 211 | if (-f $LogFile) | ||
| 212 | { | ||
| 213 | my $timestamp = &log_datestamp(); | ||
| 214 | rename($LogFile,"$LogFile.$timestamp") || | ||
| 215 | error("Unable to rename logfile to '$LogFile.$timestamp'"); | ||
| 216 | } | ||
| 217 | |||
| 218 | $self->openLog; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | #------------------------------------------------------------------------------- | ||
| 223 | # Function: openLog | ||
| 224 | # Description: Open a new log file. If the log file name contains whitespace, | ||
| 225 | # then assume it's actually a command to be executed. | ||
| 226 | # Input Parms: None | ||
| 227 | # Output Parms: None | ||
| 228 | #------------------------------------------------------------------------------- | ||
| 229 | sub openLog | ||
| 230 | { | ||
| 231 | my $self = shift; | ||
| 232 | my $LogFile = $self->{LogFile}; | ||
| 233 | my $LogHeader = $self->{LogHeader}; | ||
| 234 | my $RotateLogAt = $self->{RotateLogAt}; | ||
| 235 | my ($cmdChar,$nextLogRotate,$tmp,$now,$header); | ||
| 236 | |||
| 237 | return if ($LogFile eq ''); | ||
| 238 | |||
| 239 | # Make sure the standard perf directory exists for this host | ||
| 240 | mkdir "$::VBHOME/perf", 0755 if(! -d "$::VBHOME/perf"); | ||
| 241 | mkdir "$::VBHOME/perf/$::HOST", 0755 if(! -d "$::VBHOME/perf/$::HOST"); | ||
| 242 | |||
| 243 | # If the LogFile name string begins with '|', then assume it's a stream and | ||
| 244 | # don't append a '>>' to the front of it. | ||
| 245 | $cmdChar = '>> ' if ($LogFile !~ /^\|/); | ||
| 246 | |||
| 247 | my $handle = new FileHandle; | ||
| 248 | $handle->open("$cmdChar$LogFile") || | ||
| 249 | fatal("Cannot open LogFile stream to '$cmdChar$LogFile'"); | ||
| 250 | $self->{LogHandle} = $handle; | ||
| 251 | $handle->autoflush(1); | ||
| 252 | |||
| 253 | if($RotateLogAt) | ||
| 254 | { | ||
| 255 | $now = &DateCalc("today",""); | ||
| 256 | |||
| 257 | # Calculate the difference between now and the RotateLogAt string | ||
| 258 | $tmp = &DateCalc($now,$RotateLogAt); | ||
| 259 | |||
| 260 | # If it's a time of day which has already passed, then add 1 day to it | ||
| 261 | $tmp = &DateCalc($tmp,"+ 1 day") if ($tmp =~ /^\-/); | ||
| 262 | |||
| 263 | # If it comes out as relative (starts with a +) then add it to $now. | ||
| 264 | $tmp = &DateCalc($now,$tmp) if ($tmp =~ /^\+/); | ||
| 265 | $self->{nextLogRotate} = $tmp; | ||
| 266 | &log("Log was scheduled to be rotated at '$RotateLogAt', $tmp") | ||
| 267 | if ($VERBOSE); | ||
| 268 | } | ||
| 269 | |||
| 270 | if ($LogHeader ne '') | ||
| 271 | { | ||
| 272 | $header = swrite($LogHeader); | ||
| 273 | print $handle $header; | ||
| 274 | } | ||
| 275 | |||
| 276 | (0); | ||
| 277 | } | ||
| 278 | |||
| 279 | #------------------------------------------------------------------------------- | ||
| 280 | # Function: addVBObj | ||
| 281 | # Description: Read from a passed name/value hash a VBServer object name | ||
| 282 | # and associated rules for determining how to set the status of | ||
| 283 | # that object. | ||
| 284 | # Input Parms: Name/Value hash | ||
| 285 | # Output Parms: None | ||
| 286 | #------------------------------------------------------------------------------- | ||
| 287 | sub addVBObj | ||
| 288 | { | ||
| 289 | my $self = shift; | ||
| 290 | my $vbObjects = $self->{vbObjects}; | ||
| 291 | my $LogFile = $self->{LogFile}; | ||
| 292 | |||
| 293 | &fatal("VBTK::Parser::addRules: Must specify VBServerURI before calling addVBObj") | ||
| 294 | if ($self->{vbClientObj} eq ''); | ||
| 295 | |||
| 296 | my %args = @_; | ||
| 297 | |||
| 298 | # Retrieve the VBServer object name | ||
| 299 | my $VBObjName = $args{VBObjName}; | ||
| 300 | |||
| 301 | # Do validations related to graphing | ||
| 302 | &fatal("VBTK::Parser::addVBObj: Can't use Rrd/Graphing functionality without " . | ||
| 303 | "specifying 'VBDetail' for '$VBObjName'") | ||
| 304 | if((($args{RrdColumns} ne '')||($args{RrdTimeCol} ne ''))&& | ||
| 305 | ($self->{VBDetail} eq '')); | ||
| 306 | |||
| 307 | # Now create a corresponding VBTK::ClientObject | ||
| 308 | my $vbObj = new VBTK::ClientObject ( | ||
| 309 | VBClientObj => $self->{vbClientObj}, | ||
| 310 | Interval => $self->{Interval}, | ||
| 311 | LogFile => $self->{LogFile}, | ||
| 312 | %args | ||
| 313 | ); | ||
| 314 | |||
| 315 | # Now store the new object in an array | ||
| 316 | push(@{$vbObjects},$vbObj); | ||
| 317 | |||
| 318 | ($vbObj); | ||
| 319 | } | ||
| 320 | |||
| 321 | #------------------------------------------------------------------------------- | ||
| 322 | # Function: addGraphGroup | ||
| 323 | # Description: Call 'addGraphGroup' for each VBObject associated with this parser, | ||
| 324 | # passing in the passed parms. | ||
| 325 | # Input Parms: Graph Group Parms | ||
| 326 | # Output Parms: None | ||
| 327 | #------------------------------------------------------------------------------- | ||
| 328 | sub addGraphGroup | ||
| 329 | { | ||
| 330 | my $self = shift; | ||
| 331 | my $vbObjects = $self->{vbObjects}; | ||
| 332 | |||
| 333 | foreach my $vbObj (@{$vbObjects}) | ||
| 334 | { | ||
| 335 | $vbObj->addGraphGroup(@_); | ||
| 336 | } | ||
| 337 | |||
| 338 | (0); | ||
| 339 | } | ||
| 340 | |||
| 341 | #------------------------------------------------------------------------------- | ||
| 342 | # Function: parseData | ||
| 343 | # Description: Parse a 2 dimensional array of values. If a pre-processor | ||
| 344 | # subroutine is defined, then call it. For each row in the | ||
| 345 | # 2D array, call the parse_row method. At the end, call the | ||
| 346 | # setVBObjects method. | ||
| 347 | # Input Parms: Pointer to 2D array, override status, and override message. | ||
| 348 | # Output Parms: None | ||
| 349 | #------------------------------------------------------------------------------- | ||
| 350 | sub parseData | ||
| 351 | { | ||
| 352 | my $self = shift; | ||
| 353 | my $PreProcessor = $self->{PreProcessor}; | ||
| 354 | my $logRecoveryDone = $self->{logRecoveryDone}; | ||
| 355 | my $lastTime = $self->{lastTime}; | ||
| 356 | my ($dataPtr,$status,$msg) = @_; | ||
| 357 | my ($pos,$retval,$currTime,$timeDelta); | ||
| 358 | |||
| 359 | my $time = &log_datestamp(); | ||
| 360 | |||
| 361 | # Check to see if the log needs to be rotated | ||
| 362 | $self->checkLogRotate(); | ||
| 363 | |||
| 364 | # Try to read all existing logs for this wrapper object to catch up on | ||
| 365 | # missing log data. | ||
| 366 | $self->recoverGraphDataFromLog($time) if (! $logRecoveryDone); | ||
| 367 | |||
| 368 | # If a pre-processor subroutine is defined, then make a copy of the | ||
| 369 | # dataPtr array and pass it to the pre-processor. | ||
| 370 | if(($PreProcessor)&&(defined $dataPtr)) | ||
| 371 | { | ||
| 372 | &log("Passing " . (@{$dataPtr} + 0) . " rows to PreProcessor") if ($VERBOSE > 2); | ||
| 373 | $dataPtr = [ @{$dataPtr} ]; | ||
| 374 | &$PreProcessor($dataPtr); | ||
| 375 | &log("PreProcessor returned " . (@{$dataPtr} + 0) . " rows") if ($VERBOSE > 2); | ||
| 376 | } | ||
| 377 | |||
| 378 | if(defined $dataPtr) | ||
| 379 | { | ||
| 380 | log("Parsing " . (@{$dataPtr} + 0) . " rows") if ($VERBOSE > 2); | ||
| 381 | |||
| 382 | $currTime = time; | ||
| 383 | $timeDelta = $currTime - $lastTime if ($lastTime); | ||
| 384 | foreach $pos (0..(@{$dataPtr}-1)) | ||
| 385 | { | ||
| 386 | $retval += $self->parseRow($dataPtr->[$pos],$time,$pos,$timeDelta); | ||
| 387 | } | ||
| 388 | $self->{lastTime} = $currTime; | ||
| 389 | } | ||
| 390 | |||
| 391 | $self->setVBObjects($status,$msg,$time); | ||
| 392 | } | ||
| 393 | |||
| 394 | #------------------------------------------------------------------------------- | ||
| 395 | # Function: parseRow | ||
| 396 | # Description: Parse a row of values. Calculate the delta between the current | ||
| 397 | # rows values and the previous rows values if the delta keyword | ||
| 398 | # is used in any of the clauses or the format statements. Format | ||
| 399 | # the VBServer messages and Log message. Step through rules for | ||
| 400 | # each VBServer object, setting statuses appropriately. | ||
| 401 | # Input Parms: Pointer to array, timestamp value | ||
| 402 | # Output Parms: Return value | ||
| 403 | #------------------------------------------------------------------------------- | ||
| 404 | sub parseRow | ||
| 405 | { | ||
| 406 | my $self = shift; | ||
| 407 | my ($row,$time,$rowPos,$timeDelta) = @_; | ||
| 408 | my $deltaKey = $rowPos; | ||
| 409 | my $vbObjects = $self->{vbObjects}; | ||
| 410 | my $VBDetail = $self->{VBDetail}; | ||
| 411 | my $LogDetail = $self->{LogDetail}; | ||
| 412 | my $LogHandle = $self->{LogHandle}; | ||
| 413 | my $lastRow = $self->{lastRows}->{$deltaKey}; | ||
| 414 | |||
| 415 | my ($pos,$line,@data,@delta,@rate,$prevValue,$delta,$vbMessage,$logMessage,$vbObj); | ||
| 416 | |||
| 417 | # If we were passed a reference to an array, then the line has already been | ||
| 418 | # split, so just copy the data into @data. | ||
| 419 | if (ref($row) eq 'ARRAY') | ||
| 420 | { | ||
| 421 | @data = @{$row}; | ||
| 422 | $row = join(' ',@data); | ||
| 423 | } | ||
| 424 | # Otherwise if it's a scalar, then run it through the filter and splitter. | ||
| 425 | elsif (ref($row) eq '') | ||
| 426 | { | ||
| 427 | # Strip the whitespace off the end of the fulltext line | ||
| 428 | $row =~ s/\s+$//g; | ||
| 429 | |||
| 430 | &log("Processing row: $row") if ($VERBOSE > 3); | ||
| 431 | |||
| 432 | # Run this row through the filters and skip it if it doesn't pass | ||
| 433 | @data = &filterString($row,$self->{Filter},$self->{Ignore},$self->{Split}); | ||
| 434 | return 0 if (@data == 0); | ||
| 435 | } | ||
| 436 | else | ||
| 437 | { | ||
| 438 | &fatal("Invalid reference " . ref($row) . " passed to parseRow"); | ||
| 439 | } | ||
| 440 | |||
| 441 | # Save the data for comparison on the next round | ||
| 442 | $self->{lastRows}->{$deltaKey} = [ @data ]; | ||
| 443 | |||
| 444 | # Calculate the delta and rate for each column | ||
| 445 | foreach $pos (0..$#data) | ||
| 446 | { | ||
| 447 | $prevValue = $lastRow->[$pos]; | ||
| 448 | $delta = ($prevValue =~ /^\d+$/) ? $data[$pos] - $prevValue : undef; | ||
| 449 | $delta[$pos] = $delta; | ||
| 450 | $rate[$pos] = ($timeDelta > 0) ? $delta/$timeDelta : undef; | ||
| 451 | } | ||
| 452 | |||
| 453 | &log("Parsing row: " . join(':',@data)) if ($VERBOSE > 2); | ||
| 454 | |||
| 455 | # Format the VBServer object message and the Log message | ||
| 456 | $vbMessage = swrite($VBDetail,$time,$row,\@data,\@delta,\@rate) if ($VBDetail ne ''); | ||
| 457 | $logMessage = swrite($LogDetail,$time,$row,\@data,\@delta,\@rate) if ($LogDetail ne ''); | ||
| 458 | print $LogHandle $logMessage if ($LogHandle ne ''); | ||
| 459 | &log("Extracting graph data from row - $logMessage") if ($VERBOSE > 3); | ||
| 460 | |||
| 461 | # For each VBServer object, process the data for graphing, using the log message | ||
| 462 | # as the input. We parse the log output as input for the graph data, so that we | ||
| 463 | # can read backwards and catch-up on data which was missed for some reason. Also | ||
| 464 | # test the objects rule clauses, setting statuses as specified | ||
| 465 | foreach $vbObj (@{$vbObjects}) | ||
| 466 | { | ||
| 467 | $vbObj->processText($time,$row,\@data,\@delta,\@rate,$vbMessage); | ||
| 468 | $vbObj->processGraphData($time,$row,\@data,\@delta,\@rate); | ||
| 469 | } | ||
| 470 | |||
| 471 | (0); | ||
| 472 | } | ||
| 473 | |||
| 474 | #------------------------------------------------------------------------------- | ||
| 475 | # Function: recoverGraphDataFromLog | ||
| 476 | # Description: If the graphing option is enabled for the object, try to read | ||
| 477 | # back over the existing log files to recover any data which has | ||
| 478 | # not yet made it to the VBServer graph database. | ||
| 479 | # Input Parms: None | ||
| 480 | # Output Parms: None | ||
| 481 | #------------------------------------------------------------------------------- | ||
| 482 | sub recoverGraphDataFromLog | ||
| 483 | { | ||
| 484 | my $self = shift; | ||
| 485 | my $time = shift; | ||
| 486 | my $LogFile = $self->{LogFile}; | ||
| 487 | my $vbObjects = $self->{vbObjects}; | ||
| 488 | my $logRecoveryPid = $self->{logRecoveryPid}; | ||
| 489 | |||
| 490 | # Just return if we've already processed the logs | ||
| 491 | if(($self->{logRecoveryDone})||(! defined $LogFile)) | ||
| 492 | { | ||
| 493 | $self->{logRecoveryDone} = 1; | ||
| 494 | return undef; | ||
| 495 | } | ||
| 496 | |||
| 497 | my ($fh,$file,$timestamp,$unixTimestamp,$reapedPid,$pid); | ||
| 498 | my $startTime = time; | ||
| 499 | |||
| 500 | # Check to see if we've already forked off the recovery process and if so, | ||
| 501 | # check to see if the pid has completed. | ||
| 502 | if (defined $logRecoveryPid) | ||
| 503 | { | ||
| 504 | &log("Checking to see if log recovery pid '$logRecoveryPid' has finished") | ||
| 505 | if ($VERBOSE); | ||
| 506 | $reapedPid = waitpid($logRecoveryPid,&WNOHANG); | ||
| 507 | |||
| 508 | if(($reapedPid == $logRecoveryPid)||($reapedPid == -1)) | ||
| 509 | { | ||
| 510 | &log("Log recovery process has completed") if ($VERBOSE); | ||
| 511 | $self->{logRecoveryPid} = undef; | ||
| 512 | $self->{logRecoveryDone} = 1; | ||
| 513 | } | ||
| 514 | return undef; | ||
| 515 | } | ||
| 516 | |||
| 517 | &log("Attempting to recover graphing data from past logs") if ($VERBOSE); | ||
| 518 | |||
| 519 | my $earliestTimestamp = $self->getEarliestLastGraphDbTimestamp($time); | ||
| 520 | |||
| 521 | # Don't bother to continue if we can't at least talk to the VB server. | ||
| 522 | if (! defined $earliestTimestamp) | ||
| 523 | { | ||
| 524 | $self->{logRecoveryDone} = 1; | ||
| 525 | return undef; | ||
| 526 | } | ||
| 527 | |||
| 528 | # If we've made it this far, then if forking is allowed, fork off a process | ||
| 529 | # to handle the log recovery. | ||
| 530 | if ($FORK_SUPPORTED) | ||
| 531 | { | ||
| 532 | if ($pid = fork) | ||
| 533 | { | ||
| 534 | &log("Forked off log recovery process, pid = $pid") if ($VERBOSE); | ||
| 535 | $self->{logRecoveryPid} = $pid; | ||
| 536 | return undef; | ||
| 537 | } | ||
| 538 | elsif(! defined $pid) | ||
| 539 | { | ||
| 540 | &fatal("Could not fork log recovery process"); | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | # If we've made it this far, then mark the logRecoveryProcess as done. | ||
| 545 | $self->{logRecoveryDone} = 1; | ||
| 546 | |||
| 547 | # Make a list of all log files | ||
| 548 | my @pastLogFiles = sort <$LogFile.[0-9]*>; | ||
| 549 | |||
| 550 | # Now read them in one-by-one in order of oldest to newest, ending with the | ||
| 551 | # current logfile itself. | ||
| 552 | foreach $file (@pastLogFiles, $LogFile) | ||
| 553 | { | ||
| 554 | &log("Reading logfile '$file'") if ($VERBOSE > 1); | ||
| 555 | |||
| 556 | if($file =~ /^$LogFile.([\d:-]+)$/) | ||
| 557 | { | ||
| 558 | $timestamp=$1; | ||
| 559 | $unixTimestamp = &unixdate($timestamp); | ||
| 560 | } | ||
| 561 | elsif($file eq $LogFile) | ||
| 562 | { | ||
| 563 | $unixTimestamp = time; | ||
| 564 | } | ||
| 565 | else | ||
| 566 | { | ||
| 567 | &error("Invalid logfile '$file'"); | ||
| 568 | next; | ||
| 569 | } | ||
| 570 | |||
| 571 | &log("Comparing timestamp '$unixTimestamp' with Rrd last timestamp " . | ||
| 572 | "$earliestTimestamp") if ($VERBOSE > 3); | ||
| 573 | |||
| 574 | # Now compare the file timestamp with the last entered timestamp | ||
| 575 | next if ($unixTimestamp < $earliestTimestamp); | ||
| 576 | |||
| 577 | # Read in all data from the file and process it, adding it to the | ||
| 578 | # graph data queue. | ||
| 579 | $fh = new FileHandle "< $file"; | ||
| 580 | |||
| 581 | # Make sure the open worked | ||
| 582 | unless($fh) | ||
| 583 | { | ||
| 584 | &error("Can't read from log file '$file'"); | ||
| 585 | next; | ||
| 586 | } | ||
| 587 | |||
| 588 | # Process each row in the log | ||
| 589 | &log("Reading data from '$file'") if ($VERBOSE > 2); | ||
| 590 | while (<$fh>) | ||
| 591 | { | ||
| 592 | foreach my $vbObj (@{$vbObjects}) | ||
| 593 | { | ||
| 594 | $vbObj->processGraphData($_); | ||
| 595 | } | ||
| 596 | } | ||
| 597 | $fh->close; | ||
| 598 | |||
| 599 | $self->setVBObjects($::SUCCESS,"Loading graph data from logs"); | ||
| 600 | } | ||
| 601 | |||
| 602 | # If we made it this far, and 'fork' is supported then we're the child | ||
| 603 | # process, so just exit. | ||
| 604 | exit 0 if ($FORK_SUPPORTED); | ||
| 605 | |||
| 606 | (0); | ||
| 607 | } | ||
| 608 | |||
| 609 | #------------------------------------------------------------------------------- | ||
| 610 | # Function: getEarliestLastGraphDbTimestamp | ||
| 611 | # Description: Request the last graphDb timestamp for each of the VBObjects | ||
| 612 | # associated with this parser object. Determine the earliest | ||
| 613 | # one and return it back. | ||
| 614 | # Input Parms: None | ||
| 615 | # Output Parms: Earliest Last Graph Db Timestamp | ||
| 616 | #------------------------------------------------------------------------------- | ||
| 617 | sub getEarliestLastGraphDbTimestamp | ||
| 618 | { | ||
| 619 | my $self = shift; | ||
| 620 | my $time = shift; | ||
| 621 | my $vbObjects = $self->{vbObjects}; | ||
| 622 | my ($vbObj,$lastTimestamp); | ||
| 623 | |||
| 624 | &log("Attempting to recover graphing data from past logs") if ($VERBOSE); | ||
| 625 | |||
| 626 | my $earliestTimestamp = time; | ||
| 627 | |||
| 628 | # Step through each vbObj requesting the last timestamp | ||
| 629 | foreach $vbObj (@{$vbObjects}) | ||
| 630 | { | ||
| 631 | $lastTimestamp = $vbObj->getLastGraphDbTimestamp($time); | ||
| 632 | |||
| 633 | $earliestTimestamp = $lastTimestamp | ||
| 634 | if(($lastTimestamp > 0)&&($lastTimestamp < $earliestTimestamp)); | ||
| 635 | } | ||
| 636 | |||
| 637 | &log("Earliest timestamp was $earliestTimestamp") if ($VERBOSE > 2); | ||
| 638 | ($earliestTimestamp); | ||
| 639 | } | ||
| 640 | |||
| 641 | #------------------------------------------------------------------------------- | ||
| 642 | # Function: getLastRows | ||
| 643 | # Description: Request the pointer to the 'lastRows' element which contains a | ||
| 644 | # two-dimensional matrix of the last rows processed by the parser. | ||
| 645 | # Input Parms: None | ||
| 646 | # Output Parms: Pointer to two-dimensional matrix of last rows | ||
| 647 | #------------------------------------------------------------------------------- | ||
| 648 | sub getLastRows | ||
| 649 | { | ||
| 650 | my $self = shift; | ||
| 651 | my $lastRows = $self->{lastRows}; | ||
| 652 | |||
| 653 | # See if there are any non-numeric keys | ||
| 654 | my @nonNumericKeys = grep(/[^\d]/, keys %{$lastRows}); | ||
| 655 | |||
| 656 | # Sort the key list based on whether it contains non-numeric keys | ||
| 657 | my @keyList = (@nonNumericKeys > 0) ? | ||
| 658 | sort keys %{$lastRows} : sort byNum keys %{$lastRows}; | ||
| 659 | |||
| 660 | # Now return the values in order | ||
| 661 | [ map { $lastRows->{$_} } @keyList ]; | ||
| 662 | } | ||
| 663 | sub byNum { $a <=> $b }; | ||
| 664 | |||
| 665 | |||
| 666 | #------------------------------------------------------------------------------- | ||
| 667 | # Function: setVBObjects | ||
| 668 | # Description: Step through all VBServer objects, sending their statuses | ||
| 669 | # and messages to the VBServer. Override with the passed master | ||
| 670 | # status and message if specified. | ||
| 671 | # Input Parms: Master status, Master message | ||
| 672 | # Output Parms: None | ||
| 673 | #------------------------------------------------------------------------------- | ||
| 674 | sub setVBObjects | ||
| 675 | { | ||
| 676 | my $self = shift; | ||
| 677 | my ($masterStatus,$masterMsg,$time) = @_; | ||
| 678 | my $vbObjects = $self->{vbObjects}; | ||
| 679 | my $VBHeader = $self->{VBHeader}; | ||
| 680 | my $errors = 0; | ||
| 681 | |||
| 682 | return 0 if (! $self->{logRecoveryDone}); | ||
| 683 | |||
| 684 | # Generate the header string | ||
| 685 | my $header = swrite($VBHeader,$time); | ||
| 686 | |||
| 687 | # Step through each unique vbobject name specified | ||
| 688 | foreach my $vbObj (@{$vbObjects}) | ||
| 689 | { | ||
| 690 | $vbObj->sendStatus($masterStatus,$masterMsg,$header) || $errors++; | ||
| 691 | } | ||
| 692 | |||
| 693 | ($errors); | ||
| 694 | } | ||
| 695 | |||
| 696 | |||
| 697 | #------------------------------------------------------------------------------- | ||
| 698 | # Function: swrite | ||
| 699 | # Description: Read an array of format lines and return the resulting string | ||
| 700 | # Input Parms: Pointer to array of format strings, Time value, Pointer to | ||
| 701 | # data aray, Pointer to delta array. | ||
| 702 | # Output Parms: None | ||
| 703 | #------------------------------------------------------------------------------- | ||
| 704 | sub swrite | ||
| 705 | { | ||
| 706 | my ($ptr,$time,$data,$dataPtr,$deltaPtr,$ratePtr) = @_; | ||
| 707 | my ($str,$line,$pos,$numlines,$cmd); | ||
| 708 | |||
| 709 | return undef if ($ptr eq ''); | ||
| 710 | |||
| 711 | # Copy the @data, @delta, and @rate arrays locally so that they can be used | ||
| 712 | # in the formline calls. | ||
| 713 | my @data = ($dataPtr) ? @{$dataPtr} : (); | ||
| 714 | my @delta = ($deltaPtr) ? @{$deltaPtr} : (); | ||
| 715 | my @rate = ($ratePtr) ? @{$ratePtr} : (); | ||
| 716 | |||
| 717 | $numlines = @{$ptr}; | ||
| 718 | |||
| 719 | # Step through each format string | ||
| 720 | for($pos = 0; $pos < $numlines; $pos++) | ||
| 721 | { | ||
| 722 | $line = $ptr->[$pos]; | ||
| 723 | |||
| 724 | # If the line contains an '@', then assume that it is a format | ||
| 725 | # line and that the following line is the variable line | ||
| 726 | if($line =~ /\@/) | ||
| 727 | { | ||
| 728 | $cmd = "formline '$line', $ptr->[$pos+1]"; | ||
| 729 | eval($cmd); | ||
| 730 | &fatal("swrite: Can't eval: $cmd: $@") if ($@ ne ''); | ||
| 731 | $str .= "$^A\n"; | ||
| 732 | $^A = ''; | ||
| 733 | $pos++; | ||
| 734 | } | ||
| 735 | # Otherwise, just interpret the line. | ||
| 736 | else | ||
| 737 | { | ||
| 738 | $cmd = "\$str .= \"$line\n\""; | ||
| 739 | eval($cmd); | ||
| 740 | &fatal("swrite: Can't eval: $cmd: $@") if ($@ ne ''); | ||
| 741 | } | ||
| 742 | } | ||
| 743 | ($str); | ||
| 744 | } | ||
| 745 | |||
| 746 | |||
| 747 | #------------------------------------------------------------------------------- | ||
| 748 | # Function: filterString | ||
| 749 | # Description: Process the passed string. If the string matches the ignore | ||
| 750 | # pattern, then return undef. If it matches the filter pattern, | ||
| 751 | # then if there are () in it, use them to construct an array of | ||
| 752 | # $1, $2, etc. If the split pattern is specified, then override | ||
| 753 | # any () and just split the text apart. Return the resulting | ||
| 754 | # array. | ||
| 755 | # Input Parms: String, Filter Pattern, Ignore Pattern, Split Pattern | ||
| 756 | # Output Parms: Array of resulting values | ||
| 757 | #------------------------------------------------------------------------------- | ||
| 758 | sub filterString | ||
| 759 | { | ||
| 760 | my ($string,$filterPattern,$ignorePattern,$splitPattern) = @_; | ||
| 761 | my (@data); | ||
| 762 | |||
| 763 | &log("Running filter on string - $string") if ($VERBOSE > 3); | ||
| 764 | |||
| 765 | # Look for rows to ignore, if an Ignore filter was specified | ||
| 766 | if(defined $ignorePattern) | ||
| 767 | { | ||
| 768 | if ($string =~ /$ignorePattern/) | ||
| 769 | { | ||
| 770 | &log("Ignoring row") if ($VERBOSE > 3); | ||
| 771 | return (); | ||
| 772 | } | ||
| 773 | } | ||
| 774 | |||
| 775 | if(defined $filterPattern) | ||
| 776 | { | ||
| 777 | # If there are any () in the filter, then create the data array | ||
| 778 | # using the values of $1, $2, $3, etc. Otherwise, just place the | ||
| 779 | # whole row in $data[0]; | ||
| 780 | |||
| 781 | &log("Checking against filter '$filterPattern'") if ($VERBOSE > 3); | ||
| 782 | @data = ($string =~ /$filterPattern/); | ||
| 783 | return () if ((@data == 0)||((@data == 1)&&($data[0] eq ''))); | ||
| 784 | |||
| 785 | &log("Row matched filter") if ($VERBOSE > 3); | ||
| 786 | $data[0] = $string if($1 eq ''); | ||
| 787 | } | ||
| 788 | else | ||
| 789 | { | ||
| 790 | $data[0] = $string; | ||
| 791 | $data[0] =~ s/\n$//; | ||
| 792 | } | ||
| 793 | |||
| 794 | # If a split pattern was specified, it overrides everything. If the split | ||
| 795 | # returns nothing (ie: a blank line), then throw an undef into the array so | ||
| 796 | # that it has at least one element. | ||
| 797 | if(defined $splitPattern) | ||
| 798 | { | ||
| 799 | @data = split(/$splitPattern/, $string); | ||
| 800 | @data = (undef) if (@data == 0); | ||
| 801 | } | ||
| 802 | |||
| 803 | (@data); | ||
| 804 | } | ||
| 805 | |||
| 806 | #------------------------------------------------------------------------------- | ||
| 807 | # Function: calcSleepTime | ||
| 808 | # Description: Calculate when the cycle should next be run, and how long to | ||
| 809 | # sleep before that time arrives. If the increment flag is passed, | ||
| 810 | # then increment the lastTime value by the Interval * the increment | ||
| 811 | # value. | ||
| 812 | # Input Parms: Increment value | ||
| 813 | # Output Parms: Sleep time | ||
| 814 | #------------------------------------------------------------------------------- | ||
| 815 | sub calcSleepTime | ||
| 816 | { | ||
| 817 | my $self = shift; | ||
| 818 | my $incBy = shift; | ||
| 819 | my $Interval = $self->{Interval}; | ||
| 820 | my $now = time; | ||
| 821 | |||
| 822 | if($incBy > 0) | ||
| 823 | { | ||
| 824 | # Increment 'lastTime' accordingly. If this is the first time it's being | ||
| 825 | # set, then add a random factor to the lastTime, so that the objects | ||
| 826 | # don't all report in at the same time. | ||
| 827 | if(! defined $self->{lastRunTime}) { | ||
| 828 | $self->{lastRunTime} = $now + int((rand() - 0.5) * ($Interval * $incBy)); | ||
| 829 | } else { | ||
| 830 | $self->{lastRunTime} += ($Interval * $incBy); | ||
| 831 | |||
| 832 | # If we get more than 3 Intervals behind, then start skipping to | ||
| 833 | # catch up. | ||
| 834 | while($self->{lastRunTime} < ($now - ($Interval * 3))) | ||
| 835 | { | ||
| 836 | $self->{lastRunTime} += $Interval; | ||
| 837 | } | ||
| 838 | } | ||
| 839 | } | ||
| 840 | |||
| 841 | # Calculate how many seconds until it needs to be run again. | ||
| 842 | my $sleepTime = ($self->{lastRunTime} + $Interval) - $now; | ||
| 843 | $sleepTime = 0 if ($sleepTime < 0); | ||
| 844 | |||
| 845 | ($sleepTime); | ||
| 846 | } | ||
| 847 | |||
| 848 | # Put in a stub for handleSignal | ||
| 849 | sub handleSignal { (0); } | ||
| 850 | |||
| 851 | 1; | ||
| 852 | __END__ |