# (2) Added rules based paging (Stephen Johnson Nov 14, 1998)
# (3) Added checks against Acks and downtime (Stephen Johnson Mar 17, 1999)
#
-# $Id: spong-message.pl,v 1.36 2002/05/10 14:43:09 sljohnson Exp $
+# $Id: spong-message.pl,v 1.37 2002/11/07 21:50:23 sljohnson Exp $
use lib "@@LIBDIR@@";
my($rule,$match,$m,$c);
- # Do the following, in this little loop. 1) Check to see if we have
- # already sent this page in the last X minutes. 2) Check to make
- # sure we have sent less then X pages to a single contact in the
- # last X minutes. 3) Clean up the message history database
- # by removing any items that are over 1 day old.
-
+ # Read in the notification history file
$mcnt = 0; @okhistory = (); %ccnt = ();
foreach $item ( @history ) {
my( $itime, $iok, $icolor, $ihost, $iservice, $icontact ) =
( $item =~ /^(\d+)\s+([-+])\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/ );
+ # Omit anyting older then 24 hours
if( $itime > (time() - 24*60*60) ) { push( @okhistory, $item ); }
+ # Count number of times this message has been sent in the past hour
if( $icolor eq $color && $ihost eq $host && $iservice eq $service ) {
if( $itime > (time() - 60*60) && $iok eq "+" ) { $mcnt++; }
}
+ # Count the number of messages sent to each contact in the past hour
foreach $contact (split(/ /,$icontact)) {
if( $itime > (time() - 60*60) ) { $ccnt{"$contact"}++; }
}
}
+ # Don't send this messages is the # / hour exceeded
if( $mcnt >= $IDENT_MESSAGES_PER_HOUR ) {
&debug(
"$mcnt identical pages sent in last hour - no message sent."
exit(0);
}
+ #
+ # Check default status/color filters
+ #
+
+ # Check excluded colors
+ if ( defined($MESSAGE_DEFAULTS{'exclude_colors'} ) {
+ foreach my $mcolor ( $MESSAGE_DEFAULTS{'exclude_colors'} ) {
+ if ( $color eq $mcolor ) {
+ main::debug("Hit on exclude_colors in default filters";
+ return;
+ }
+ }
+ }
+
+ if ( defined( $MESSAGE_DEFAULTS{'include_colors'} ) {
+ my $hit = 0;
+ foreach my $mcolor ( $MESSAGE_DEFAULTS{'include_colors'} ) {
+ if $color eq $mcolor || $mcolor eq 'all' {
+ $hit = 1; last;
+ }
+ }
+ }
+ if (! $hit ) {
+ main::debug("No Hit on include_colors in default filters";
+ return;
+ }
+ }
+
+ #
+ # Start of messaging rules engine
+ #
+
$match = 0; $c = -1;
RULE:
foreach $rule (@$MESSAGING_RULES) {
&debug("Processing rule # $c $rule->{'name'}");
+ #
# Check for matches in excluded hosts and services and host_groups
+ # and colors
+ #
+
+ # excluded hosts
if ( defined $rule->{'exclude_hosts'} ) {
my($e);
foreach $e ( @{$rule->{'exclude_hosts'}} ) {
}
}
}
+ # Excluded services
if ( defined $rule->{'exclude_services'} ) {
foreach $e ( @{$rule->{'exclude_services'}} ) {
if ( $service =~ /^$e/ ) {
}
}
+ # Excluded host groups
if (defined $rule->{'exclude_host_groups'} ) {
foreach $g ( @{$rule->{'exclude_host_groups'}} ) {
if ( &in_group($host,$g) ) {
}
}
+ # Excluded colors
if( defined $rule->{'exclude_colors'} or
defined $rule->{'exclude_status'} ) {
foreach my $clr ( @{$rule->{'exclude_colors'}},
}
}
+ #
# Match on hosts and host_groups
+ #
+
+ # If no matech elements defined, match automatically
if ( ! defined $rule->{'hosts'} &&
! defined $rule->{'host_groups'} ) {
&debug("Host match for $host on rule $c $rule->{'name'}");
} else {
+ # Scan the list of hosts
$m = 0;
foreach $e ( @{$rule->{'hosts'}} ) {
if ( $host =~ /^$e/ ) { $m = 1; last; }
&debug("No match for host $host on rule $c, checking host groups");
if ( defined $rule->{'host_groups'} ) {
$m = 0;
+ # Scan the list of host groups
foreach $g ( @{$rule->{'host_groups'}} ) {
if ( &in_group($host,$g) ) { $m = 1; last; }
}
}
}
- # Match on service
+ #
+ # Match on services
+ #
+
+ # If not match term defined, match automatically
if ( ! defined $rule->{'services'} ) {
&debug("service match for $service on rule $c $rule->{'name'}");
} else {
+ # Scan the list of services
$m = 0;
foreach $e ( @{$rule->{'services'}} ) {
if ( $service =~ /^$e/ ) { $m = 1; last; }
}
}
+ #
# Match on times
+ #
+
+ # IF no time term given, match automatically
if ( ! defined $rule->{'times'} ) {
&debug("time default match on rule $c $rule->{'name'}");
} else {
my($day,$time,$beg,$end,$timehit,$dayhit);
- my( $nday, $nhour, $nmin ) = (localtime())[6,2,1];
+ my( $nday, $nhour, $nmin ) = (localtime())[6,2,1]; # Current date/time
$m = 0;
+ # Scan eatch time term
foreach $e ( @{$rule->{'times'}} ) {
$dayhit = 0; $timehit = 0;
# Check days clause
last;
}
}
+ # If no days element, match automatically
} else {
$dayhit = 1;
&debug("Match on days sub-clause of rule $c $rule->{'name'}");
}
+ # Scan the times elements
if (defined $e->{'times'}) {
foreach $time ( @{$e->{'times'}} ) {
if ($time =~ m/(\d{1,2}):(\d\d)-(\d{1,2}):(\d\d)/ ) {
last;
}
}
+ # If not times element, match automatically
} else {
$timehit = 1;
&debug("Match on times sub-clause of rule $c $rule->{'name'}");
}
+ # If times and day elements match, the date term matches
if ($timehit and $dayhit) {
&debug("Match on times clause of rule $c $rule->{'name'}");
$m=1;