/usr/share/cricket/lib/Monitor.pm is in cricket 1.0.5-20.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 | # Cricket: a configuration, polling and data display wrapper for RRD files
#
# Copyright (C) 1998 Javier Muniz and WebTV Networks, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
package Monitor;
use strict;
use snmpUtils;
use RRD::File;
use Common::Log;
use Common::Util;
# Registered monitors
$Common::global::gMonitorTable{'value'} = \&monValue;
$Common::global::gMonitorTable{'hunt'} = \&monHunt;
$Common::global::gMonitorTable{'relation'} = \&monRelation;
$Common::global::gMonitorTable{'exact'} = \&monExact;
# Support for aberrant behavior detection
$Common::global::gMonitorTable{'failures'} = \&monFailures;
$Common::global::gMonitorTable{'quotient'} = \&monQuotient;
# NaN Alarm processing
# Return value of nanErr = 1 is not an alarm or threshold pass
# Return value of nanErr = 0 is an alarm or threshold failure
my $nanErr = 1;
if ($Common::global::gEnableNoValueAlarms) {
$nanErr = ($Common::global::gEnableNoValueAlarms) ? 0 : 1;
}
sub new {
my($package) = @_;
my $self = { };
bless $self, $package;
return $self;
}
sub monValue {
my($self,$target,$ds,$type,$args) = @_;
my($min,$max,$minOK,$maxOK);
my(@Thresholds) = split(/\s*:\s*/, $args);
my($value) = $self->rrdFetch(
$target->{'rrd-datafile'},
$self->getDSNum($target, $ds), 0
);
if (!defined($value)) {
Warn("Monitor: Couldn't fetch last $ds value from " .
$target->{'rrd-datafile'}.".");
return 1;
}
return ($nanErr, 'NaN') if isNaN($value);
$min = shift(@Thresholds);
$min = 'n' if (! defined($min));
if (lc($min) eq 'n') {
$minOK = 1;
} else {
$minOK = ($value > $min) ? 1 : 0;
}
$max = shift(@Thresholds);
$max = 'n' if (! defined($max));
if (lc($max) eq 'n') {
$maxOK = 1;
} else {
$maxOK = ($value < $max) ? 1 : 0;
}
Debug ("Value is $value; min is $min; max is $max");
return ($maxOK && $minOK,$value);
}
sub monHunt {
my($self,$target,$ds,$type,$args) = @_;
my($roll, $cmp_name, $cmp_ds) = split(/\s*:\s*/, $args);
if (!defined($roll)) {
Warn("Monitor: Missing rollover value in hunt threshold for " .
$target->{'auto-target-name'} . " datasource $ds.");
return 1;
}
# Fetch the current value from target's rrd file
my($value) = $self->rrdFetch($target->{'rrd-datafile'},
$self->getDSNum($target, $ds), 0);
if (!defined($value)) {
Warn("Monitor: Couldn't fetch last $ds value from " .
"$target->{'rrd-datafile'}.");
return 1;
}
if ($value == 0) {
return 1; # no rollover, test succeeded.
}
my($cmp_target);
if (defined($cmp_name)) {
$cmp_name = join('/',$target->{'auto-target-path'},$cmp_name)
if (!($cmp_name =~ /^\//));
$cmp_target = $Common::global::gCT->configHash(lc($cmp_name), 'target');
if (defined($cmp_target)) {
ConfigTree::Cache::addAutoVariables(lc($cmp_name), $cmp_target,
$Common::global::gConfigRoot);
ConfigTree::Cache::expandHash($cmp_target, $cmp_target, \&Warn);
} else {
Warn("Monitor: No such target: $cmp_name");
return 1;
}
} else {
$cmp_target = $target;
}
$cmp_ds = $ds unless ($cmp_ds);
my($cmp_value) = $self->rrdFetch($cmp_target->{'rrd-datafile'},
$self->getDSNum($cmp_target, $cmp_ds), 0);
if (!defined($cmp_value)) {
Warn("Monitor: Couldn't fetch current value from $cmp_name");
return 1;
}
return ($cmp_value >= $roll);
}
# if X and Y are data sources (Y is possibly X shifted by some temporal offset)
# and Z is a threshold, then a relation monitor checks:
# abs(Y - X)/Y > Z for Z a percentage (marked pct)
# abs(Y - X) > Z for Z not a percentage
# Default > can be replaced with <
# return 1 if the check passes or error, 0 if fail
sub monRelation {
my($self, $target, $ds, $type, $args) = @_;
my($thresh, $cmp_name, $cmp_ds, $cmp_time) = split(/\s*:\s*/, $args);
my($pct) = ($thresh =~ s/\s*pct\s*//i);
$cmp_time = 0 unless(defined($cmp_time));
if (!defined($thresh) || !defined($cmp_time)) {
Warn("Monitor: Improperly formatted relation monitor for " .
"$target->{'auto-target-name'} datasource $ds.");
return 1;
}
my($gtlt);
if (substr($thresh,0,1) eq '<' || substr($thresh,0,1) eq '>') {
$gtlt = substr($thresh,0,1);
$thresh = substr($thresh,1);
} else {
$gtlt = '>';
}
# Fetch the current value from target's rrd file
my($value) = $self->rrdFetch($target->{'rrd-datafile'},
$self->getDSNum($target,$ds), 0);
if (!defined($value)) {
Warn("Monitor: Couldn't fetch last $ds value from " .
"$target->{'rrd-datafile'}.");
return 1;
}
return ($nanErr, 'NaN') if isNaN($value);
my ($cmp_value);
# Is this a straight comparison value or should we fetch it as a DS
if ($cmp_ds =~ /^[-+]?([0-9]*\.[0-9]+|[0-9]+)$/) {
$cmp_value = $cmp_ds;
# Fetch the value it must be a DS
} else {
($cmp_value) = $self -> FetchComparisonValue($target,$ds,$cmp_name,$cmp_ds,$cmp_time);
return ($nanErr,'NaN') if isNaN($cmp_value);
}
my($difference) = abs($cmp_value - $value);
$thresh = abs($thresh); # differences are always positive
if ($pct) {
# threshold is a percentage
if ($cmp_value == 0) {
#avoid division by 0
if ($difference == 0 && $gtlt eq '<') {
return (1,$value);
} else {
return (0,$value);
}
}
$difference = $difference / abs($cmp_value) * 100;
}
Debug("Values $value, $cmp_value; Difference is $difference; " .
"gtlt is $gtlt; thresh is $thresh.");
# see documentation: threshold fails if expression is false
return (0,$value) if ((eval "$difference $gtlt $thresh") == 0);
return (1,$value);
}
sub monExact {
my($self,$target,$ds,$type,$args) = @_;
my(@args) = split(/\s*:\s*/, $args);
if (@args != 1) {
Warn("Monitor: monitor type \"exact\" requires 1 argument for " .
"$target->{'auto-target-name'} $ds.");
return 1;
}
my $exact = shift @args;
my($value) = $self->rrdFetch(
$target->{'rrd-datafile'},
$self->getDSNum($target, $ds), 0
);
if (!defined($value)) {
Warn("Monitor: Couldn't fetch last $ds value from " .
"$target->{'rrd-datafile'}.");
return 1;
}
return ($nanErr,'NaN') if isNaN($value);
return $exact eq $value ? 0 : 1;
}
# check the FAILURES array for failure recorded by the aberrant behavior
# detection algorithm in RRD
# Return values: 1 = success
# 0 = failure
# Returns 1 on default or unexpected error
sub monFailures {
my($self, $target, $ds, $type, $args) = @_;
# only check value range if specified, as this requires fetching from the RRD
if ($args =~ /:/) {
# monValue returns 0 if out of range, 1 otherwise
my ($rc, $value) = $self->monValue($target,$ds,'value',$args);
Debug("value is $value, in range $rc");
# value is out of range, so ignore any failure alarm
if ($rc == 0) { return 1 };
}
my $datafile = $target->{'rrd-datafile'};
my $dsNum = $self->getDSNum($target, $ds);
return 1 if (!defined($dsNum));
return 1 if (!defined($datafile));
# open the file if not already open
if (!defined($self->{currentfile}) || $datafile ne $self->{currentfile}) {
my($rrd) = new RRD::File( -file => $datafile );
return 1 if (!$rrd->open() || !$rrd->loadHeader());
$self->{currentfile} = $datafile;
$self->{openrrd} = $rrd;
}
# check for a valid $dsNum
return 1 unless ($dsNum < $self->{openrrd}->ds_cnt());
# find the FAILURES RRA
my $rra;
my $rraNum;
for($rraNum = 0; $rraNum <= $#{$self->{openrrd}->{rra_def}}; $rraNum++) {
$rra = $self->{openrrd}->rra_def($rraNum);
last if ($rra -> {rraName} eq 'FAILURES');
}
return 1 if ($rra -> {rraName} ne 'FAILURES');
# retrieve the most recent value
my $ret;
$ret = $self->{openrrd}->getDSRowValue($rraNum,0,$dsNum);
if (!defined($ret)) {
Warn("Monitor: Couldn't fetch last $ds value from " .
"$target->{'rrd-datafile'} FAILURES RRA.");
return 1;
}
# FAILURES array stores a 1 for a failure (so should return 0)
return (1,'NaN') if isNaN($ret);
return !($ret);
}
# if X and Y are data sources (Y is possibly X shifted by some temporal offset)
# and Z is a threshold, then a quotient monitor checks:
# X/Y > Z for Z a percentage (marked pct)
# abs(Y - X) > Z for Z not a percentage (deprecated)
# Default > can be replaced with <
# return 1 if the check passes or error, 0 if fail
sub monQuotient {
my($self, $target, $ds, $type, $args) = @_;
my($thresh, $cmp_name, $cmp_ds, $cmp_time) = split(/\s*:\s*/, $args);
my($pct) = ($thresh =~ s/\s*pct\s*//i);
$cmp_time = 0 unless(defined($cmp_time));
Info("Monitor: Use of quotient monitors without percent threshold is " .
"deprecated for $target->{'auto-target-name'} datasource $ds.")
unless (defined($pct));
if (!defined($thresh) || !defined($cmp_time)) {
Warn("Monitor: Improperly formatted quotient monitor for " .
"$target->{'auto-target-name'} datasource $ds.");
return 1;
}
my($gtlt);
if (substr($thresh,0,1) eq '<' || substr($thresh,0,1) eq '>') {
$gtlt = substr($thresh,0,1);
$thresh = substr($thresh,1);
} else {
$gtlt = '>';
}
# Fetch the current value from target's rrd file
my($value) = $self->rrdFetch($target->{'rrd-datafile'},
$self->getDSNum($target,$ds), 0);
if (!defined($value)) {
Warn("Monitor: Couldn't fetch last $ds value from " .
"$target->{'rrd-datafile'}.");
return 1;
}
return ($nanErr,'NaN') if isNaN($value);
my ($cmp_value);
# Is this a straight comparison value or should we fetch it as a DS
if ($cmp_ds =~ /^[-+]?([0-9]*\.[0-9]+|[0-9]+)$/) {
$cmp_value = $cmp_ds;
# Fetch the value it must be a DS
} else {
($cmp_value) = $self -> FetchComparisonValue($target,$ds,$cmp_name,$cmp_ds,$cmp_time);
return ($nanErr,'NaN') if isNaN($cmp_value);
}
my($difference) = abs($cmp_value - $value);
$thresh = abs($thresh); # differences are always positive
if ($pct) {
# threshold is a percentage
if ($cmp_value == 0) {
# avoid division by 0
if ($difference == 0 && $gtlt eq '>') {
return 1;
} else {
return 0;
}
}
$difference = abs($value/$cmp_value) * 100;
}
Debug("Difference is $difference; gtlt is $gtlt; thresh is $thresh.");
return (0,$value) if (eval "$difference $gtlt $thresh");
return (1,$value);
}
# shared code used by monRelation and monQuotient
sub FetchComparisonValue {
my($self,$target,$ds,$cmp_name,$cmp_ds,$cmp_time) = @_;
my($cmp_target);
if (! $cmp_name) {
$cmp_target = $target;
} else {
$cmp_name = join('/',$target->{'auto-target-path'}, $cmp_name)
if (!($cmp_name =~ /^\//));
$cmp_target = $Common::global::gCT->configHash(lc($cmp_name), 'target');
if (defined($cmp_target)) {
ConfigTree::Cache::addAutoVariables(lc($cmp_name), $cmp_target,
$Common::global::gConfigRoot);
ConfigTree::Cache::expandHash($cmp_target, $cmp_target, \&Warn);
} else {
Warn("Monitor: No such target: $cmp_name");
return 'NaN';
}
}
$cmp_ds = $ds unless ($cmp_ds);
my($cmp_value) = $self->rrdFetch($cmp_target->{'rrd-datafile'},
$self->getDSNum($cmp_target,$cmp_ds),
$cmp_time);
if (!defined($cmp_value)) {
Warn("Monitor: Couldn't fetch value for $cmp_time ".
"seconds ago from $cmp_name.");
return 'NaN';
}
if (isNaN($cmp_value)) {
Info("Monitor: Data for $cmp_time seconds ago from ".
"$cmp_name is NaN");
return 'NaN';
}
return $cmp_value;
}
# fetches any data you might need from a rrd file
# in a RRA with consolidation function AVERAGE
# caching the open RRD::File object for later use
# if possible.
sub rrdFetch {
my($self,$datafile,$dsNum,$sec) = @_;
# check all of our arguments;
return if (!defined($dsNum));
return if (!defined($sec));
return if (!defined($datafile));
Debug("in rrdFetch: file is $datafile");
if (!defined($self->{currentfile}) || $datafile ne $self->{currentfile}) {
my($rrd) = new RRD::File( -file => $datafile );
return if (!$rrd->open() || !$rrd->loadHeader());
$self->{currentfile} = $datafile;
$self->{openrrd} = $rrd;
}
return unless ($dsNum < $self->{openrrd}->ds_cnt());
my($rra,$lastrecord,$rraNum);
for($rraNum = 0; $rraNum <= $#{$self->{openrrd}->{rra_def}}; $rraNum++) {
$rra = $self->{openrrd}->rra_def($rraNum);
# if the consolidation function is not AVERAGE, we can
# skip this RRA
Debug("in rrdFetch: skipping RRA");
next if ($rra->{rraName} ne "AVERAGE");
$lastrecord = $rra->{row_cnt} * $rra->{pdp_cnt} *
$self->{openrrd}->pdp_step();
last if ($lastrecord >= $sec);
}
if ($lastrecord < $sec) {
Warn("Monitor: $datafile does not have data going back " .
"$sec seconds.");
return;
}
if ($sec % ($lastrecord / $rra->{row_cnt})) {
Warn("Monitor: RRA required to find data $sec seconds ago " .
"is too granular.");
return;
}
my($rowNum) = $sec / ($self->{openrrd}->pdp_step() * $rra->{pdp_cnt});
Debug("in rrdFetch: rraNum is $rraNum rowNum is $rowNum dsNum is $dsNum");
my ($foo) = $self->{openrrd}->getDSRowValue($rraNum,$rowNum,$dsNum);
Debug("in rrdFetch: return is $foo");
return $self->{openrrd}->getDSRowValue($rraNum,$rowNum,$dsNum);
}
# Given a target reference and datasource name,
# returns the datasource number or undef if no
# datasource of that name can be found in target's
# target-type dictionary
sub getDSNum {
my($self, $target, $dsName) = @_;
my($ttRef) = $Common::global::gCT->configHash(
join('/',$target->{'auto-target-path'},
$target->{'auto-target-name'}),
'targettype',
lc($target->{'target-type'}),
$target);
my($Counter) = 0;
my(%dsMap) = map { $_ => $Counter++ } split(/\s*,\s*/,$ttRef->{'ds'});
return $dsMap{$dsName};
}
# Subroutines to handle alarms
sub dispatchAlarm {
# order of arguments: $self, $target, $name, $ds, $type, $threshold,
# $alarmType, $alarmArgs, $val
my ($args, $action) = @_;
my ($target, $ds, $val) = ($$args[1], $$args[3], $$args[8]);
my $alarmType = $$args[6];
my %dispatch = (
EXEC => \&alarmExec,
FILE => \&alarmFile,
FUNC => \&alarmFunc,
MAIL => \&alarmMail,
SNMP => \&alarmSnmp,
META => \&alarmMeta,
);
my $alarm = $dispatch{$alarmType};
unless (defined($alarm)) {
Warn("Monitor: unknown alarm $alarmType for target: ".
$target->{'auto-target-path'} . $target->{'auto-target-name'} .
" for datasource $ds");
return;
}
my $return = $alarm->($args, $action);
return;
};
# Process alarm action
sub Alarm {
# order of arguments: $self, $target, $name, $ds, $type, $threshold,
# $alarmType, $alarmArgs, $val
my $action = 'ADD';
my $return = \&dispatchAlarm(\@_, $action);
return;
};
# Action to clear an alarm
sub Clear {
# order of arguments: $self, $target, $name, $ds, $type, $threshold,
# $alarmType, $alarmArgs, $val
my $action = 'CLEAR';
my $return = \&dispatchAlarm(\@_, $action);
return;
};
sub alarmExec {
my ($args, $action) = @_;
my $alarmArgs = $$args[7];
system($alarmArgs->[0]);
if ($action eq 'ADD') {
Info("Monitor: Triggered event with system command '".
$alarmArgs->[0]."' .");
}
else {
Info("Monitor: Cleared event with shell command '".
$alarmArgs->[1]."' .");
}
return;
};
sub alarmFile {
my ($args, $action) = @_;
my ($self, $target) = ($$args[0], $$args[1]);
my ($name, $ds) = ($$args[2], $$args[3]);
my ($type, $threshold) = ($$args[4], $$args[5]);
my ($alarmArgs, $val) = ($$args[7], $$args[8]);
$self->LogToFile($alarmArgs, $action, $name, $ds, $val);
return;
};
sub alarmFunc {
my ($args, $action) = @_;
my $alarmArgs = $$args[7];
if (defined $main::gMonFuncEnabled) {
if ($action eq 'ADD') {
eval($alarmArgs->[0]);
Info("Monitor: Triggered event with FUNC '".$alarmArgs->[0]."' .");
}
elsif ($action eq 'CLEAR') {
eval($alarmArgs->[1]);
Info("Monitor: Cleared event with FUNC '".$alarmArgs->[1]."' .");
}
}
else {
Warn("Monitor: Exec triggered, but executable alarms are not enabled.");
}
return;
};
sub alarmMail {
my ($args, $action) = @_;
my ($self, $target) = ($$args[0], $$args[1]);
my ($name, $ds) = ($$args[2], $$args[3]);
my ($type, $threshold) = ($$args[4], $$args[5]);
my ($alarmArgs, $val) = ($$args[7], $$args[8]);
$self->sendEmail( $action, $alarmArgs, $type, $threshold,
$name, $ds, $val, $target->{'inst'} );
return;
};
sub alarmSnmp {
my ($args, $action) = @_;
my ($self, $target) = ($$args[0], $$args[1]);
my ($name, $ds) = ($$args[2], $$args[3]);
my ($type, $threshold) = ($$args[4], $$args[5]);
my ($val) = ($$args[8]);
my $Specific_Trap_Type = $action eq 'ADD' ? 4 : 5;
$self->sendMonitorTrap( $target, $Specific_Trap_Type, $type,
$threshold, $name, $ds, $val );
return;
};
sub alarmMeta {
my ($args, $action) = @_;
# No action is required.
# Alarm data is already stored in the Cricket meta files by HandleTarget.pm
# The meta data is stored in $CRICKET/cricket-data/ directories.
return;
};
# Attempt to send an alarm trap for a given target
sub sendMonitorTrap {
my($self,$target,$spec,$type,$threshold,$name,$ds,$val) = @_;
my $to = $target -> {'trap-address'};
if (!defined($to)) {
Warn("Monitor: No trap address defined for $target, couldn't send trap.");
Info("Monitor: Threshold Failed: $threshold for target $target");
return;
}
my($OID_Prefix) = '.1.3.6.1.4.1.2595.1.3'; # OID for Cricket Violations
my(@VarBinds);
push(@VarBinds, "${OID_Prefix}.1", 'string', $type);
push(@VarBinds, "${OID_Prefix}.2", 'string', $threshold);
# name is the fully qualified target name
push(@VarBinds, "${OID_Prefix}.3", 'string', $name);
push(@VarBinds, "${OID_Prefix}.4", 'string', $ds);
my($logName) = "cricket";
if (!Common::Util::isWin32() && defined($ENV{'LOGNAME'})) {
$logName = $ENV{'LOGNAME'};
}
push(@VarBinds, "${OID_Prefix}.5", 'string', $logName);
# Common::HandleTarget overloads this tag
# for scalar targets, it could be "" or 0
# otherwise, it is set the instance number
if ($target->{'inst'}) {
push(@VarBinds, "${OID_Prefix}.6", 'string', $target->{'inst'});
}
if (defined($target->{'inst-name'})) {
push(@VarBinds, "${OID_Prefix}.7", 'string', $target->{'inst-name'});
}
# send the html contact-name
my $htmlRef = $Common::global::gCT -> configHash($name,'html');
if (defined($htmlRef -> {'contact-name'})) {
# parse the mailto tag
my $tag = $htmlRef -> {'contact-name'};
if ($tag =~ /mailto\:([A-Z,a-z,.,@]+)/) {
push(@VarBinds, "${OID_Prefix}.8", 'string', $1);
}
}
push(@VarBinds, "${OID_Prefix}.9", 'string', $val) unless (!defined($val));
Info("Monitor: Trap Sent to $to:\n ". join(' -- ',@VarBinds));
snmpUtils::trap2($to,$spec,@VarBinds);
}
sub LogToFile {
my ($self, $alarmArgs, $action, $targetName, $dataSourceName) = @_;
my $filePath = $alarmArgs->[0];
my @lines = ();
my $targetLine;
return unless ($action eq 'ADD' || $action eq 'CLEAR');
# try to open for read first and hunt for duplicate lines
my $bFound = 0;
if (open(INFILE, "$filePath")) {
$targetLine = $targetName . " " . $dataSourceName;
while (<INFILE>) {
chomp;
if ($_ eq $targetLine) {
$bFound = 1;
# nothing to add
last if ($action eq 'ADD');
} else {
push (@lines, $_) if ($action eq 'CLEAR');
}
}
}
unless (open(INFILE, "+>>$filePath")) {
Info("Monitor: Failed to open file $filePath: $!");
return;
}
# append the new line to the end of the file
if ($action eq 'ADD' && $bFound == 0) {
Info("Monitor: Appending line $targetLine to $filePath");
print INFILE $targetLine . "\n";
close(INFILE);
return;
}
# don't need INFILE anymore
close(INFILE);
if ($action eq 'ADD' && $bFound == 1) {
Info("Monitor: $targetName datasource $dataSourceName already in ".
"file $filePath");
return;
}
if ($action eq 'CLEAR' && $bFound == 0) {
Info("Monitor: $targetName datasource $dataSourceName already " .
"deleted from file $filePath");
return;
}
# need to print out @lines, which excludes the $targetLine
# overwrite old file
unless (open(OUTFILE, ">$filePath")) {
Info("Monitor: Failed to open file $filePath: $!");
return;
}
Info("Monitor: Deleting $targetLine from $filePath");
print OUTFILE join("\n", @lines);
print OUTFILE "\n" unless (scalar(@lines) == 0);
close(OUTFILE);
}
sub sendEmail {
my($self, $spec, $alarmArgs, $type, $threshold, $target, $ds, $val, $inst) = @_;
my $to = $alarmArgs -> [1];
if (!defined($to)) {
Warn("Monitor: No destination address defined for $target, " .
"couldn't send email.");
return;
}
my @Message;
push @Message, "type:\t\t$type";
push @Message, "threshold:\t$threshold";
push @Message, "target:\t$target";
push @Message, "ds:\t\t$ds";
if (defined($val)) {
push @Message, "val:\t\t$val";
}
# for scalar targets, inst is either "" or 0
if ($inst) {
push @Message, "inst:\t\t$inst";
}
my $mail_program = $alarmArgs -> [0];
if (!defined($mail_program)) {
Warn("Monitor: No email-program defined. Not sending email");
return;
} elsif (!open(MAIL, "|$mail_program -s 'Cricket $spec: $target' $to\n")) {
Warn("Monitor: Failed to open pipe to mail program: $!");
return;
}
Debug("|$mail_program -s 'Cricket $spec: $target' $to\n");
print (MAIL join ("\n", @Message));
Info("Monitor: Email sent to: $to\n" . join(' -- ', @Message));
close(MAIL);
}
1;
# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# tab-width: 4
# perl-indent-level: 4
# End:
|