From: Stephen L Johnson Date: Thu, 7 Nov 2002 21:50:23 +0000 (+0000) Subject: added global include/exclude filering on status/color X-Git-Tag: spong-2_8_0-beta1~57 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=30807c234c54496f800f5e5ba5282254574b42e3;p=spong.git added global include/exclude filering on status/color --- diff --git a/src/spong-message.pl b/src/spong-message.pl index 2b05e8a..0b73540 100755 --- a/src/spong-message.pl +++ b/src/spong-message.pl @@ -12,7 +12,7 @@ # (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@@"; @@ -295,28 +295,27 @@ sub scan_rules { 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." @@ -324,6 +323,38 @@ sub scan_rules { 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) { @@ -337,7 +368,12 @@ RULE: &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'}} ) { @@ -347,6 +383,7 @@ RULE: } } } + # Excluded services if ( defined $rule->{'exclude_services'} ) { foreach $e ( @{$rule->{'exclude_services'}} ) { if ( $service =~ /^$e/ ) { @@ -356,6 +393,7 @@ RULE: } } + # Excluded host groups if (defined $rule->{'exclude_host_groups'} ) { foreach $g ( @{$rule->{'exclude_host_groups'}} ) { if ( &in_group($host,$g) ) { @@ -365,6 +403,7 @@ RULE: } } + # Excluded colors if( defined $rule->{'exclude_colors'} or defined $rule->{'exclude_status'} ) { foreach my $clr ( @{$rule->{'exclude_colors'}}, @@ -377,11 +416,16 @@ RULE: } } + # # 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; } @@ -390,6 +434,7 @@ RULE: &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; } } @@ -408,10 +453,15 @@ RULE: } } - # 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; } @@ -424,13 +474,18 @@ RULE: } } + # # 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 @@ -450,11 +505,13 @@ RULE: 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)/ ) { @@ -470,11 +527,13 @@ RULE: 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;