| 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/ClientObject.pm,v $ | ||
| 7 | # $Revision: 1.11 $ | ||
| 8 | # $Date: 2002/03/04 20:53:06 $ | ||
| 9 | # $Author: bhenry $ | ||
| 10 | # $Locker: $ | ||
| 11 | # $State: Exp $ | ||
| 12 | # | ||
| 13 | # Purpose: A common perl library used to define the client side | ||
| 14 | # of a VB object. | ||
| 15 | # | ||
| 16 | # Copyright (C) 1996 - 2002 Brent Henry | ||
| 17 | # | ||
| 18 | # This program is free software; you can redistribute it and/or | ||
| 19 | # modify it under the terms of version 2 of the GNU General Public | ||
| 20 | # License as published by the Free Software Foundation available at: | ||
| 21 | # http://www.gnu.org/copyleft/gpl.html | ||
| 22 | # | ||
| 23 | # This program is distributed in the hope that it will be useful, | ||
| 24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 26 | # GNU General Public License for more details. | ||
| 27 | # | ||
| 28 | ############################################################################# | ||
| 29 | # | ||
| 30 | # | ||
| 31 | # REVISION HISTORY: | ||
| 32 | # | ||
| 33 | # $Log: ClientObject.pm,v $ | ||
| 34 | # Revision 1.11 2002/03/04 20:53:06 bhenry | ||
| 35 | # *** empty log message *** | ||
| 36 | # | ||
| 37 | # Revision 1.10 2002/03/04 16:49:09 bhenry | ||
| 38 | # Changed requirement back to perl 5.6.0 | ||
| 39 | # | ||
| 40 | # Revision 1.9 2002/03/02 00:53:54 bhenry | ||
| 41 | # Documentation updates | ||
| 42 | # | ||
| 43 | # Revision 1.8 2002/02/19 19:05:33 bhenry | ||
| 44 | # Changed to pass baseline with first status submission if not already set | ||
| 45 | # | ||
| 46 | # Revision 1.7 2002/02/13 07:41:35 bhenry | ||
| 47 | # Disabled RrdLogRecovery and removed use of @log | ||
| 48 | # | ||
| 49 | # | ||
| 50 | |||
| 51 | package VBTK::ClientObject; | ||
| 52 | |||
| 53 | use 5.6.0; | ||
| 54 | use strict; | ||
| 55 | use warnings; | ||
| 56 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 57 | no warnings qw(uninitialized); | ||
| 58 | |||
| 59 | use VBTK::Common; | ||
| 60 | use VBTK::Parser; | ||
| 61 | use VBTK::Client; | ||
| 62 | use Date::Manip; | ||
| 63 | use VBTK::Objects::UpgradeRules; | ||
| 64 | use VBTK::Objects::ChangeActions; | ||
| 65 | use VBTK::Objects::Graph; | ||
| 66 | use VBTK::Objects::Rrd; | ||
| 67 | use FileHandle; | ||
| 68 | use File::Basename; | ||
| 69 | use Algorithm::Diff qw(diff); | ||
| 70 | use Storable qw(freeze thaw); | ||
| 71 | |||
| 72 | our $VERBOSE = $ENV{VERBOSE}; | ||
| 73 | |||
| 74 | #------------------------------------------------------------------------------- | ||
| 75 | # Function: new | ||
| 76 | # Description: Object constructor. Allocates memory for all class members | ||
| 77 | # Input Parms: | ||
| 78 | # Output Parms: Pointer to class | ||
| 79 | #------------------------------------------------------------------------------- | ||
| 80 | sub new | ||
| 81 | { | ||
| 82 | my $type = shift; | ||
| 83 | my $self = {}; | ||
| 84 | bless $self, $type; | ||
| 85 | |||
| 86 | $self->set(@_); | ||
| 87 | |||
| 88 | # Setup a hash of rules to be returned | ||
| 89 | my $defaultParms = { | ||
| 90 | VBClientObj => $::REQUIRED, | ||
| 91 | Interval => $::REQUIRED, | ||
| 92 | VBObjName => $::REQUIRED, | ||
| 93 | Filter => undef, | ||
| 94 | TextHistoryLimit => undef, | ||
| 95 | ReverseText => undef, | ||
| 96 | Rules => { }, | ||
| 97 | Requirements => { }, | ||
| 98 | StatusHistoryLimit => 20, | ||
| 99 | StatusChangeActions => undef, | ||
| 100 | StatusUpgradeRules => undef, | ||
| 101 | ExpireAfter => undef, | ||
| 102 | Description => undef, | ||
| 103 | BaselineDiffStatus => undef, | ||
| 104 | LogFile => undef, | ||
| 105 | RrdTimeCol => '$time', | ||
| 106 | RrdColumns => undef, | ||
| 107 | RrdFilter => undef, | ||
| 108 | RrdMin => undef, | ||
| 109 | RrdMax => undef, | ||
| 110 | RrdXFF => undef, | ||
| 111 | RrdCF => undef, | ||
| 112 | RrdDST => undef, | ||
| 113 | }; | ||
| 114 | |||
| 115 | # Run the validation | ||
| 116 | $self->validateParms($defaultParms) || &fatal("Exiting"); | ||
| 117 | |||
| 118 | # Retrieve the VBServer object name | ||
| 119 | my $VBObjName = $self->{VBObjName}; | ||
| 120 | |||
| 121 | # Check for proper usage | ||
| 122 | if (($self->{ReverseText}||$self->{TextHistoryLimit}) && | ||
| 123 | ($self->{BaselineDiffStatus} ne '')) | ||
| 124 | { | ||
| 125 | &fatal("Can't use 'BaselineDiffStatus' option with the 'TextHistoryLimit' or " . | ||
| 126 | "'ReverseText' options in the same rule - $VBObjName"); | ||
| 127 | } | ||
| 128 | |||
| 129 | if(($self->{RrdLogRecovery})&&(! defined $self->{RrdTimeCol})) | ||
| 130 | { | ||
| 131 | &fatal("VBTK::ClientObject::new: Can't use 'RrdLogRecovery' without ". | ||
| 132 | "specifying 'RrdTimeCol'."); | ||
| 133 | } | ||
| 134 | |||
| 135 | # Check for required name/value pairs | ||
| 136 | &log("Creating client object for $VBObjName") if ($VERBOSE); | ||
| 137 | |||
| 138 | # Initialize member values | ||
| 139 | $self->{status} = $::SUCCESS; | ||
| 140 | $self->{reqFound} = {}; | ||
| 141 | $self->{graphDataQueue} = []; | ||
| 142 | $self->{graphGroupList} = {}; | ||
| 143 | |||
| 144 | my ($ruleReqPtr,$clause,$cmd,$newStatus,$inputStatus,$dummy); | ||
| 145 | my (@data,$data,@delta); | ||
| 146 | |||
| 147 | # Now step through each rule and requirement | ||
| 148 | foreach $ruleReqPtr ($self->{Rules},$self->{Requirements}) | ||
| 149 | { | ||
| 150 | foreach $clause (keys %{$ruleReqPtr}) | ||
| 151 | { | ||
| 152 | $inputStatus = $ruleReqPtr->{$clause}; | ||
| 153 | |||
| 154 | &log("Checking '$clause' => '$inputStatus'") if ($VERBOSE > 1); | ||
| 155 | |||
| 156 | # Mark the DeltaUsed flag if the clause contains the word 'delta' | ||
| 157 | $self->{DeltaUsed} = 1 if ($clause =~ /delta/); | ||
| 158 | |||
| 159 | # Make sure the status specified is a valid status | ||
| 160 | $newStatus = $ruleReqPtr->{$clause} = map_status($inputStatus); | ||
| 161 | &fatal("VBTK::ClientObject::new: Invalid status '$inputStatus' specified") | ||
| 162 | if (($newStatus eq '')&&($inputStatus !~ /^Ignore/i)); | ||
| 163 | |||
| 164 | # Generate a dummy eval command to test the clause | ||
| 165 | $cmd = '$dummy = 1 if ' . $clause; | ||
| 166 | eval($cmd); | ||
| 167 | fatal("VBTK::ClientObject::new: Error executing '$cmd': '$@'") if($@ ne ''); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | my ($upgradeStr,$upgradeRuleObj,$status,$action,$changeActionObj,$test); | ||
| 172 | |||
| 173 | # Check the upgrade rules | ||
| 174 | if(defined $self->{StatusUpgradeRules}) | ||
| 175 | { | ||
| 176 | # If the upgrade rule is just a string, then make it an array | ||
| 177 | $self->{StatusUpgradeRules} = [ $self->{StatusUpgradeRules} ] | ||
| 178 | unless(ref($self->{StatusUpgradeRules})); | ||
| 179 | |||
| 180 | # Step through each status upgrade rule | ||
| 181 | foreach $upgradeStr (@{$self->{StatusUpgradeRules}}) | ||
| 182 | { | ||
| 183 | $upgradeRuleObj = new VBTK::Objects::UpgradeRules( | ||
| 184 | RuleText => $upgradeStr); | ||
| 185 | |||
| 186 | &fatal("Exiting") if (! defined $upgradeRuleObj); | ||
| 187 | |||
| 188 | # Allocate an array if there isn't already one. | ||
| 189 | $self->{upgradeRuleObjList} = [] | ||
| 190 | if (! defined $self->{upgradeRuleObjList}); | ||
| 191 | |||
| 192 | push(@{$self->{upgradeRuleObjList}},$upgradeRuleObj); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | # Check the status change actions | ||
| 197 | if(defined $self->{StatusChangeActions}) | ||
| 198 | { | ||
| 199 | &fatal("StatusChangeActions parm must be a hash") | ||
| 200 | unless (ref($self->{StatusChangeActions}) eq 'HASH'); | ||
| 201 | |||
| 202 | # Step through each status change action | ||
| 203 | while(($status,$action) = each %{$self->{StatusChangeActions}}) | ||
| 204 | { | ||
| 205 | $changeActionObj = new VBTK::Objects::ChangeActions( | ||
| 206 | TestStatus => $status, | ||
| 207 | ActionList => $action); | ||
| 208 | |||
| 209 | &fatal("Exiting") if (! defined $changeActionObj); | ||
| 210 | |||
| 211 | # Allocate an array if there isn't already one. | ||
| 212 | $self->{changeActionObjList} = [] | ||
| 213 | if (! defined $self->{changeActionObjList}); | ||
| 214 | |||
| 215 | push(@{$self->{changeActionObjList}},$changeActionObj); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | # Check the 'ExpireAfter' key for a valid date string | ||
| 220 | if(defined $self->{ExpireAfter}) | ||
| 221 | { | ||
| 222 | $test = &DateCalc("today",$self->{ExpireAfter}); | ||
| 223 | &fatal("Invalid ExpireAfter string '$self->{ExpireAfter}' specified " . | ||
| 224 | "for object '$self->{VBObjName}'") if ($test eq ''); | ||
| 225 | } | ||
| 226 | |||
| 227 | # Check the 'LimitHistoryTo' key for a valid date string | ||
| 228 | if((defined $self->{LimitHistoryTo})&&($self->{LimitHistoryTo} !~ /^\d+$/)) | ||
| 229 | { | ||
| 230 | $test = &DateCalc("today",$self->{LimitHistoryTo}); | ||
| 231 | &fatal("Invalid LimitHistoryTo string '$self->{LimitHistoryTo}' specified " . | ||
| 232 | "for object '$self->{VBObjName}'") if ($test eq ''); | ||
| 233 | } | ||
| 234 | |||
| 235 | ($self); | ||
| 236 | } | ||
| 237 | |||
| 238 | #------------------------------------------------------------------------------- | ||
| 239 | # Function: addGraphGroup | ||
| 240 | # Description: Add a graph grouping to this object | ||
| 241 | # Input Parms: Parms | ||
| 242 | # Output Parms: None | ||
| 243 | #------------------------------------------------------------------------------- | ||
| 244 | sub addGraphGroup | ||
| 245 | { | ||
| 246 | my $self = shift; | ||
| 247 | my $VBObjName = $self->{VBObjName}; | ||
| 248 | my $graphGroupList = $self->{graphGroupList}; | ||
| 249 | my %args = (@_); | ||
| 250 | |||
| 251 | # Setup some default parms | ||
| 252 | my $defaultParms = { | ||
| 253 | GroupNumber => 1, | ||
| 254 | DataSourceList => undef, | ||
| 255 | Labels => $::REQUIRED, | ||
| 256 | LineWidth => undef, | ||
| 257 | Colors => undef, | ||
| 258 | VLabel => undef, | ||
| 259 | Title => undef, | ||
| 260 | TimeWindowList => '1day,1week,1month,1year', | ||
| 261 | CF => undef, | ||
| 262 | XSize => undef, | ||
| 263 | YSize => undef, | ||
| 264 | Target => { | ||
| 265 | XSize => 800, | ||
| 266 | YSize => 300 } | ||
| 267 | }; | ||
| 268 | |||
| 269 | # Run a validation, using the defaults | ||
| 270 | &validateParms(\%args,$defaultParms) || fatal("Exiting"); | ||
| 271 | |||
| 272 | my @timeWindowList = split(/,/,$args{TimeWindowList}); | ||
| 273 | delete $args{TimeWindowList}; | ||
| 274 | |||
| 275 | my $groupNumber = $args{GroupNumber}; | ||
| 276 | delete $args{GroupNumber}; | ||
| 277 | |||
| 278 | my ($graphObj,$graphGroup,@retList); | ||
| 279 | |||
| 280 | # Iterate through each time window entry, creating a graph object | ||
| 281 | foreach my $timeWindow (@timeWindowList) | ||
| 282 | { | ||
| 283 | $graphObj = new VBTK::Objects::Graph ( | ||
| 284 | TimeWindow => $timeWindow, | ||
| 285 | %args ); | ||
| 286 | |||
| 287 | push(@retList,$graphObj) if (defined $graphObj); | ||
| 288 | } | ||
| 289 | |||
| 290 | # Retrieve the array for this graph group | ||
| 291 | $graphGroup = $graphGroupList->{$groupNumber}; | ||
| 292 | |||
| 293 | # If the array doesn't exist, then allocate one | ||
| 294 | $graphGroup = $graphGroupList->{$groupNumber} = [] | ||
| 295 | if (! defined $graphGroup); | ||
| 296 | |||
| 297 | # Now add the current graph definition into the specified graph group array | ||
| 298 | push(@{$graphGroup},@retList); | ||
| 299 | |||
| 300 | (@retList); | ||
| 301 | } | ||
| 302 | |||
| 303 | #------------------------------------------------------------------------------- | ||
| 304 | # Function: processText | ||
| 305 | # Description: Process one row of data from the parser | ||
| 306 | # Input Parms: | ||
| 307 | # Output Parms: Return value | ||
| 308 | #------------------------------------------------------------------------------- | ||
| 309 | sub processText | ||
| 310 | { | ||
| 311 | my $self = shift; | ||
| 312 | my $VBObjName = $self->{VBObjName}; | ||
| 313 | |||
| 314 | my ($time,$data,$dataPtr,$deltaPtr,$ratePtr,$vbMessage) = @_; | ||
| 315 | |||
| 316 | # Setup variables to be used in the eval clause below | ||
| 317 | my @data = @{$dataPtr}; | ||
| 318 | my @delta = @{$deltaPtr}; | ||
| 319 | my @rate = @{$ratePtr}; | ||
| 320 | |||
| 321 | my $rulePtr = $self->{Rules}; | ||
| 322 | my $reqPtr = $self->{Requirements}; | ||
| 323 | my $reqfoundPtr = $self->{reqFound}; | ||
| 324 | my $Filter = $self->{Filter}; | ||
| 325 | my $rowStatus = $::SUCCESS; | ||
| 326 | my $tmpMessage = undef; | ||
| 327 | my $reqfound = 0; | ||
| 328 | |||
| 329 | my ($ruleReqPtr,$clause,$flag,$cmd,$newStatus,$msg); | ||
| 330 | |||
| 331 | # Test to see if line should have an object-specific grep applied. | ||
| 332 | if($Filter ne '') | ||
| 333 | { | ||
| 334 | $flag = 1; | ||
| 335 | $cmd = '$flag = 0 if ' . $Filter; | ||
| 336 | eval($cmd); | ||
| 337 | return 1 if $flag; | ||
| 338 | } | ||
| 339 | |||
| 340 | # Step through each clause for all rules and requirements | ||
| 341 | foreach $ruleReqPtr ($rulePtr,$reqPtr) | ||
| 342 | { | ||
| 343 | foreach $clause (keys %{$ruleReqPtr}) | ||
| 344 | { | ||
| 345 | $flag = 0; | ||
| 346 | |||
| 347 | &log("Checking clause '$clause'") if ($VERBOSE > 2); | ||
| 348 | |||
| 349 | # Generate the eval command to test the clause | ||
| 350 | $cmd = '$flag = 1 if ' . $clause; | ||
| 351 | eval($cmd); | ||
| 352 | fatal("Error executing '$clause': '$@'") if($@ ne ''); | ||
| 353 | |||
| 354 | # If clause tests true and this is a rule, then change to the status | ||
| 355 | # specified by the rule | ||
| 356 | if(($flag) and ($ruleReqPtr eq $rulePtr)) | ||
| 357 | { | ||
| 358 | $newStatus = $rulePtr->{$clause}; | ||
| 359 | my $msg = "Setting '$VBObjName' to '$newStatus' because $clause"; | ||
| 360 | &log($msg); | ||
| 361 | $rowStatus = find_higher_status($rowStatus,$newStatus); | ||
| 362 | |||
| 363 | if ($newStatus eq $::FAILED) | ||
| 364 | { $self->{statusLine} .= red("$msg\n"); } | ||
| 365 | elsif($newStatus eq $::WARNING) | ||
| 366 | { $self->{statusLine} .= yellow("$msg\n"); } | ||
| 367 | else | ||
| 368 | { $self->{statusLine} .= "$msg\n"; } | ||
| 369 | } | ||
| 370 | # If clause tests true and this is a requirements, then just mark the | ||
| 371 | # requirement as being fulfilled. | ||
| 372 | elsif(($flag) and ($ruleReqPtr eq $reqPtr)) | ||
| 373 | { | ||
| 374 | $reqfoundPtr->{$clause} = 1; | ||
| 375 | $reqfound = 1; | ||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | # Color the message using the HTML font tag based on the status | ||
| 381 | if ($rowStatus eq $::FAILED) | ||
| 382 | { $vbMessage = red($vbMessage); } | ||
| 383 | elsif($rowStatus eq $::WARNING) | ||
| 384 | { $vbMessage = yellow($vbMessage); } | ||
| 385 | # If a requirement was fulfilled, then mark the line green | ||
| 386 | elsif($reqfound) | ||
| 387 | { $vbMessage = green($vbMessage); } | ||
| 388 | |||
| 389 | # Add the new row at the top if the 'ReverseText' parm was set | ||
| 390 | # otherwise, add it to the bottom. | ||
| 391 | if ($self->{ReverseText}) | ||
| 392 | { $self->{Message} = $vbMessage . $self->{Message}; } | ||
| 393 | else | ||
| 394 | { $self->{Message} .= $vbMessage; } | ||
| 395 | |||
| 396 | # Most Severe status always takes precedence | ||
| 397 | $self->{status} = find_higher_status($self->{status},$rowStatus); | ||
| 398 | |||
| 399 | (0); | ||
| 400 | } | ||
| 401 | |||
| 402 | #------------------------------------------------------------------------------- | ||
| 403 | # Function: processGraphData | ||
| 404 | # Description: Read through the passed logfile string and process data to be | ||
| 405 | # passed to the vbServer process for graphing. | ||
| 406 | # Input Parms: | ||
| 407 | # Output Parms: | ||
| 408 | #------------------------------------------------------------------------------- | ||
| 409 | sub processGraphData | ||
| 410 | { | ||
| 411 | my $self = shift; | ||
| 412 | my ($time,$data,$dataPtr,$deltaPtr,$ratePtr) = @_; | ||
| 413 | |||
| 414 | # Setup variables to be used in the eval clause below | ||
| 415 | my @data = @{$dataPtr}; | ||
| 416 | my @delta = @{$deltaPtr}; | ||
| 417 | my @rate = @{$ratePtr}; | ||
| 418 | |||
| 419 | my $VBObjName = $self->{VBObjName}; | ||
| 420 | my $RrdTimeCol = $self->{RrdTimeCol}; | ||
| 421 | my $RrdColumns = $self->{RrdColumns}; | ||
| 422 | my $RrdFilter = $self->{RrdFilter}; | ||
| 423 | my $graphDataQueue = $self->{graphDataQueue}; | ||
| 424 | my $lastGraphDbTimestamp = $self->{lastGraphDbTimestamp}; | ||
| 425 | |||
| 426 | # Just return if there's nothing in @data, or if RrdColumns or RrdTimeCol | ||
| 427 | # isn't setup right. | ||
| 428 | return 0 if (! defined $data[0] || ! defined $RrdTimeCol || | ||
| 429 | ! defined $RrdColumns || @{$RrdColumns} < 1); | ||
| 430 | |||
| 431 | my ($expr,$val,@exprVals,@vals,$timeValUnix,$timeValStr,$newEntry); | ||
| 432 | my ($flag,$cmd); | ||
| 433 | |||
| 434 | &log("Processing graph data for '$VBObjName'") if ($VERBOSE > 3); | ||
| 435 | |||
| 436 | # Test to see if line should have an object-specific grep applied. | ||
| 437 | if($RrdFilter ne '') | ||
| 438 | { | ||
| 439 | $flag = 1; | ||
| 440 | $cmd = '$flag = 0 if ' . $RrdFilter; | ||
| 441 | eval($cmd); | ||
| 442 | return 1 if $flag; | ||
| 443 | } | ||
| 444 | |||
| 445 | |||
| 446 | # Step through each of the column expressions, evaluating them and storing | ||
| 447 | # their resulting values. | ||
| 448 | foreach $expr ($RrdTimeCol,@{$RrdColumns}) | ||
| 449 | { | ||
| 450 | if (! defined $expr) | ||
| 451 | { | ||
| 452 | &error("Empty expression passed in RrdColumns for '$VBObjName', no update will be made"); | ||
| 453 | return 0; | ||
| 454 | } | ||
| 455 | $cmd = "\$val = $expr"; | ||
| 456 | eval ($cmd); | ||
| 457 | fatal("VBTK::ClientObject::processGraphData: Error evaluating '$expr': '$@'") | ||
| 458 | if($@ ne ''); | ||
| 459 | $val = 'U' if (! defined $val); | ||
| 460 | push(@exprVals, $val); | ||
| 461 | } | ||
| 462 | |||
| 463 | ($timeValStr,@vals) = @exprVals; | ||
| 464 | |||
| 465 | # Make sure the time string is valid | ||
| 466 | if($timeValStr =~ /[^\d:\s-]/) | ||
| 467 | { | ||
| 468 | &log("Skipping log entry, can't understand timestamp '$timeValStr'") | ||
| 469 | if ($VERBOSE > 1); | ||
| 470 | return 0; | ||
| 471 | } | ||
| 472 | |||
| 473 | # Make sure the values are all numeric! | ||
| 474 | if(grep(/[^\d\.]/ && ! /^U$/,@vals)) | ||
| 475 | { | ||
| 476 | &log("Skipping log entry, data is not numeric: @vals") if ($VERBOSE > 1); | ||
| 477 | return 0; | ||
| 478 | } | ||
| 479 | |||
| 480 | # Translate the timeValStr to unix time | ||
| 481 | $timeValUnix = &unixdate($timeValStr); | ||
| 482 | |||
| 483 | if ($timeValUnix <= $lastGraphDbTimestamp) | ||
| 484 | { | ||
| 485 | &log("Skipping log entry, timestamp '$timeValUnix' is <= last graph " . | ||
| 486 | "db timestamp of '$lastGraphDbTimestamp'") if ($VERBOSE > 3); | ||
| 487 | return 0; | ||
| 488 | } | ||
| 489 | |||
| 490 | # Store the last timestamp for later use. | ||
| 491 | $self->{lastGraphDbTimestamp} = $timeValUnix; | ||
| 492 | |||
| 493 | # Store the values into a queue for later processing | ||
| 494 | $newEntry = join(':', $timeValUnix,@vals); | ||
| 495 | &log("Adding '$newEntry' to graphDataQueue") if ($VERBOSE > 3); | ||
| 496 | push(@{$graphDataQueue}, $newEntry); | ||
| 497 | |||
| 498 | (0); | ||
| 499 | } | ||
| 500 | |||
| 501 | #------------------------------------------------------------------------------- | ||
| 502 | # Function: getEarliestLastGraphDbTimestamp | ||
| 503 | # Description: Request the last graphDb timestamp for this object from the VBServer. | ||
| 504 | # Input Parms: None | ||
| 505 | # Output Parms: Earliest Last Graph Db Timestamp | ||
| 506 | #------------------------------------------------------------------------------- | ||
| 507 | sub getLastGraphDbTimestamp | ||
| 508 | { | ||
| 509 | my $self = shift; | ||
| 510 | my $time = shift; | ||
| 511 | my $VBObjName = $self->{VBObjName}; | ||
| 512 | my $VBClientObj = $self->{VBClientObj}; | ||
| 513 | my $RrdLogRecovery = $self->{RrdLogRecovery}; | ||
| 514 | |||
| 515 | my ($lastTimestamp); | ||
| 516 | |||
| 517 | if($RrdLogRecovery) | ||
| 518 | { | ||
| 519 | # Get the most recent timestamp from the Rrd database on the main vbserver | ||
| 520 | &log("Requesting last GraphDB timestamp for '$VBObjName' from VB Server") | ||
| 521 | if ($VERBOSE > 1); | ||
| 522 | $lastTimestamp = $VBClientObj->getGraphDbLastTimestamp($VBObjName); | ||
| 523 | |||
| 524 | # If there's an error, then just return undef | ||
| 525 | if(! defined $lastTimestamp) | ||
| 526 | { | ||
| 527 | &error("Can't retrieve lastTimestamp for '$VBObjName'"); | ||
| 528 | } | ||
| 529 | else | ||
| 530 | { | ||
| 531 | &log("Last timestamp was $lastTimestamp") if ($VERBOSE > 2); | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | # If we can't determine a valid last timestamp, then just set it to one | ||
| 536 | # second before the current loop's timestamp. | ||
| 537 | $lastTimestamp = ($time - 1) if (! defined $lastTimestamp); | ||
| 538 | |||
| 539 | # Store the last timestamp value in the object | ||
| 540 | $self->{lastGraphDbTimestamp} = $lastTimestamp; | ||
| 541 | |||
| 542 | ($lastTimestamp); | ||
| 543 | } | ||
| 544 | |||
| 545 | #------------------------------------------------------------------------------- | ||
| 546 | # Function: sendStatus | ||
| 547 | # Description: Send the status and messages of this VB object to the VB server. | ||
| 548 | # Override with the passed master status and message if specified. | ||
| 549 | # Input Parms: Master status, Master message | ||
| 550 | # Output Parms: None | ||
| 551 | #------------------------------------------------------------------------------- | ||
| 552 | sub sendStatus | ||
| 553 | { | ||
| 554 | my $self = shift; | ||
| 555 | my ($masterStatus,$masterMsg,$header) = @_; | ||
| 556 | |||
| 557 | my $VBClientObj = $self->{VBClientObj}; | ||
| 558 | my $baseline = $self->{baseline}; | ||
| 559 | |||
| 560 | # Skip if the VBClient object wasn't initialized | ||
| 561 | return 1 if ($VBClientObj eq ''); | ||
| 562 | |||
| 563 | my ($sel,$vbPtr,$VBObjName,$vbmsg,$status,$statusLine); | ||
| 564 | my ($newStatus,@messageLines,$message,$numRows,$graphStruct,$retval,@oneTimeParms); | ||
| 565 | |||
| 566 | $status = $self->{status}; | ||
| 567 | $VBObjName = $self->{VBObjName}; | ||
| 568 | |||
| 569 | $status = find_higher_status($status,$masterStatus) | ||
| 570 | if ($masterStatus ne ''); | ||
| 571 | |||
| 572 | if ($masterStatus eq $::RUNNING) | ||
| 573 | { | ||
| 574 | $status = $masterStatus; | ||
| 575 | } | ||
| 576 | else | ||
| 577 | { | ||
| 578 | # Check for requirement rules | ||
| 579 | $newStatus = $self->checkRequirements; | ||
| 580 | $status = &find_higher_status($newStatus,$status); | ||
| 581 | |||
| 582 | # Check the baseline if specified | ||
| 583 | if($self->checkBaseline) | ||
| 584 | { | ||
| 585 | $newStatus = $self->{BaselineDiffStatus}; | ||
| 586 | $newStatus = $::FAILED if ($newStatus eq ''); | ||
| 587 | $status = &find_higher_status($newStatus,$status); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | # Check the statusLine variable, and add a carriage return if | ||
| 592 | # it's not empty | ||
| 593 | $statusLine = $self->{statusLine}; | ||
| 594 | $statusLine .= "\n" if ($statusLine ne ''); | ||
| 595 | $masterMsg .= "\n" if ($masterMsg ne ''); | ||
| 596 | |||
| 597 | # If the TextHistoryLimit option is specified, then split the message | ||
| 598 | # apart and then re-assemble just the first $TextHistoryLimit lines. | ||
| 599 | if($self->{TextHistoryLimit}) | ||
| 600 | { | ||
| 601 | @messageLines = split(/\n/,$self->{Message}); | ||
| 602 | $numRows = @messageLines; | ||
| 603 | $numRows = $self->{TextHistoryLimit} if ($self->{TextHistoryLimit} < $numRows); | ||
| 604 | $self->{Message} = join("\n",@messageLines[0..$numRows]); | ||
| 605 | } | ||
| 606 | |||
| 607 | $message = $self->{Message}; | ||
| 608 | $message = "No new data reported\n" if($message eq ''); | ||
| 609 | |||
| 610 | # If this the first time we're submitting a status to the VB server, then | ||
| 611 | # pass along these additional values. | ||
| 612 | @oneTimeParms = (); | ||
| 613 | $graphStruct = undef; | ||
| 614 | |||
| 615 | # Setup values to be passed only the first time we connect | ||
| 616 | if ($self->{sendCount} < 1) | ||
| 617 | { | ||
| 618 | @oneTimeParms = ( | ||
| 619 | StatusHistoryLimit => $self->{StatusHistoryLimit}, | ||
| 620 | ChangeActionObjList => $self->{changeActionObjList}, | ||
| 621 | UpgradeRuleObjList => $self->{upgradeRuleObjList}, | ||
| 622 | ExpireAfter => $self->{ExpireAfter}, | ||
| 623 | Description => $self->{Description}, | ||
| 624 | GraphGroupList => $self->{graphGroupList}, | ||
| 625 | BaselineDiffStatus => $self->{BaselineDiffStatus}, | ||
| 626 | LogFile => $self->{LogFile}, | ||
| 627 | Interval => $self->{Interval}, | ||
| 628 | RunningFrom => $::HOST, | ||
| 629 | ScriptName => &basename($0) | ||
| 630 | ); | ||
| 631 | |||
| 632 | $graphStruct = { | ||
| 633 | Interval => $self->{Interval}, | ||
| 634 | Min => $self->{RrdMin}, | ||
| 635 | Max => $self->{RrdMax}, | ||
| 636 | XFF => $self->{RrdXFF}, | ||
| 637 | CF => $self->{RrdCF}, | ||
| 638 | DST => $self->{RrdDST}, | ||
| 639 | DataQueue => $self->{graphDataQueue} | ||
| 640 | }; | ||
| 641 | } | ||
| 642 | # Otherwise, if there's data to be passed, then just include the dataqueue | ||
| 643 | elsif(@{$self->{graphDataQueue}} > 0) | ||
| 644 | { | ||
| 645 | $graphStruct = { | ||
| 646 | DataQueue => $self->{graphDataQueue} | ||
| 647 | }; | ||
| 648 | } | ||
| 649 | |||
| 650 | # Build the final obj structure to be passed to the VB server. | ||
| 651 | my $objStruct = { | ||
| 652 | Name => $VBObjName, | ||
| 653 | Status => $status, | ||
| 654 | Text => $message, | ||
| 655 | HeaderMsg => $masterMsg . $statusLine . $header, | ||
| 656 | FooterMsg => undef, | ||
| 657 | GraphStruct => $graphStruct, | ||
| 658 | @oneTimeParms | ||
| 659 | }; | ||
| 660 | |||
| 661 | # If the 'setBaselineFlag' was set, then pass it along to the server and | ||
| 662 | # then reset it. | ||
| 663 | if($self->{setBaselineFlag}) | ||
| 664 | { | ||
| 665 | $objStruct->{SetBaselineFlag} = 1; | ||
| 666 | $self->{setBaselineFlag} = undef; | ||
| 667 | } | ||
| 668 | |||
| 669 | &log("Setting status of '$VBObjName' to '$status'") if ($VERBOSE > 1); | ||
| 670 | $retval = $VBClientObj->setStatus(objStruct => &freeze($objStruct)); | ||
| 671 | |||
| 672 | # Only clear out the graph data queue if the transmit was successful. | ||
| 673 | if($retval eq 0) | ||
| 674 | { | ||
| 675 | $self->{graphDataQueue} = []; | ||
| 676 | $self->{sendCount}++; | ||
| 677 | } | ||
| 678 | |||
| 679 | # Reset status to Success and clear out messages for the next round | ||
| 680 | $self->{status} = $::SUCCESS; | ||
| 681 | $self->{statusLine} = ''; | ||
| 682 | |||
| 683 | # Clear out the 'Requirements Found' indicator for the next round. | ||
| 684 | $self->{reqFound} = {}; | ||
| 685 | |||
| 686 | # Clear out the Message unless the TextHistoryLimit option is specified | ||
| 687 | $self->{Message} = '' unless ($self->{TextHistoryLimit}); | ||
| 688 | |||
| 689 | return undef if ($retval ne 0); | ||
| 690 | |||
| 691 | (1); | ||
| 692 | } | ||
| 693 | |||
| 694 | #------------------------------------------------------------------------------- | ||
| 695 | # Function: checkRequirements | ||
| 696 | # Description: Check to make sure all of the requirements rules were satisfied. | ||
| 697 | # Input Parms: None | ||
| 698 | # Output Parms: None | ||
| 699 | #------------------------------------------------------------------------------- | ||
| 700 | sub checkRequirements | ||
| 701 | { | ||
| 702 | my $self = shift; | ||
| 703 | my $status = $::SUCCESS; | ||
| 704 | |||
| 705 | my $reqPtr = $self->{Requirements}; | ||
| 706 | my $reqFoundPtr = $self->{reqFound}; | ||
| 707 | my ($newStatus,$row_status,$msg,$clause); | ||
| 708 | |||
| 709 | # Just return if there aren't any requirements | ||
| 710 | return $status if ($reqPtr eq ''); | ||
| 711 | |||
| 712 | foreach $clause (keys %{$reqPtr}) | ||
| 713 | { | ||
| 714 | unless($reqFoundPtr->{$clause}) | ||
| 715 | { | ||
| 716 | $newStatus = $reqPtr->{$clause}; | ||
| 717 | $msg = "Required clause '$clause' not true, setting to '$newStatus'"; | ||
| 718 | &log($msg); | ||
| 719 | if($newStatus eq $::FAILED) { $msg = red($msg); } | ||
| 720 | elsif($newStatus eq $::WARNING) { $msg = yellow($msg); } | ||
| 721 | $self->{statusLine} .= "$msg\n"; | ||
| 722 | $status = find_higher_status($status,$newStatus); | ||
| 723 | } | ||
| 724 | } | ||
| 725 | ($status); | ||
| 726 | } | ||
| 727 | |||
| 728 | #------------------------------------------------------------------------------- | ||
| 729 | # Function: checkBaseline | ||
| 730 | # Description: Compare the output from the command to a pre-specified baseline | ||
| 731 | # file. | ||
| 732 | # Input Parms: None | ||
| 733 | # Output Parms: None | ||
| 734 | #------------------------------------------------------------------------------- | ||
| 735 | sub checkBaseline | ||
| 736 | { | ||
| 737 | my $self = shift; | ||
| 738 | |||
| 739 | return 0 if (! defined $self->{BaselineDiffStatus}); | ||
| 740 | |||
| 741 | my $VBClientObj = $self->{VBClientObj}; | ||
| 742 | my $VBObjName = $self->{VBObjName}; | ||
| 743 | my $baselineText = $VBClientObj->getBaseline($VBObjName); | ||
| 744 | my $messageText = $self->{Message}; | ||
| 745 | |||
| 746 | # If we don't get anything back from the baseline request, then try to set | ||
| 747 | # it once. If we can't set it, or if we've already tried this before, then | ||
| 748 | # just report the error. | ||
| 749 | if (! $baselineText) | ||
| 750 | { | ||
| 751 | if(! $self->{baselineSetOnce}) | ||
| 752 | { | ||
| 753 | &log("Marking flag to set baseline for $VBObjName"); | ||
| 754 | $self->{setBaselineFlag} = 1; | ||
| 755 | $self->{baselineSetOnce} = 1; | ||
| 756 | return 0; | ||
| 757 | } | ||
| 758 | |||
| 759 | $self->{statusLine} .= "Error: Cannot compare baseline because it cannot " . | ||
| 760 | "be retrieved or has not been set\n"; | ||
| 761 | return -1; | ||
| 762 | } | ||
| 763 | |||
| 764 | my ($chunk,$line,$diffText,$sign,$lineno,$text); | ||
| 765 | |||
| 766 | my @messageLines = split(/\n/,$messageText); | ||
| 767 | my @baselineLines = split(/\n/,$baselineText); | ||
| 768 | |||
| 769 | my $diffList = diff(\@baselineLines, \@messageLines); | ||
| 770 | |||
| 771 | foreach $chunk (@$diffList) | ||
| 772 | { | ||
| 773 | foreach $line (@$chunk) | ||
| 774 | { | ||
| 775 | ($sign, $lineno, $text) = @$line; | ||
| 776 | $diffText .= sprintf "%4d$sign %s\n", $lineno+1, $text; | ||
| 777 | } | ||
| 778 | $diffText .= "--------\n"; | ||
| 779 | } | ||
| 780 | |||
| 781 | if(defined $diffText) | ||
| 782 | { | ||
| 783 | $self->{statusLine} .= "Error: Differences found with baseline\n$diffText"; | ||
| 784 | return -1; | ||
| 785 | } | ||
| 786 | |||
| 787 | (0); | ||
| 788 | } | ||
| 789 | |||
| 790 | 1; | ||
| 791 | __END__ |