From: cvs Date: Tue, 7 Dec 1999 07:23:27 +0000 (+0000) Subject: Added host_groups and exclude_host_groups to rules matching. Integrated X-Git-Tag: spong-2_6-beta8~11 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=957b6ca7110720710e1231bc0c8ce5d47e617039;p=spong.git Added host_groups and exclude_host_groups to rules matching. Integrated host_groups into host matching. They are now either or... --- diff --git a/src/spong-message.pl b/src/spong-message.pl index 023dcb1..7d59788 100755 --- a/src/spong-message.pl +++ b/src/spong-message.pl @@ -14,6 +14,8 @@ use lib "@@LIBDIR@@"; +use Data::Dumper; + use Spong::AckList; use Sys::Hostname; @@ -25,6 +27,7 @@ if( $ARGV[0] eq "--test" ) { $test = 1; shift @ARGV; } $conf_file = "@@ETCDIR@@/spong.conf"; $hosts_file = "@@ETCDIR@@/spong.hosts"; +$groups_file = "@@ETCDIR@@/spong.groups"; $message_file = "@@ETCDIR@@/spong.message"; $msgfunc_path = "@@LIBDIR@@/Spong/Message/plugins"; ($HOST) = gethostbyname(&Sys::Hostname::hostname()); @@ -59,9 +62,10 @@ require $hosts_file || die "Can't load $hosts_file: $!"; require $message_file || die "Can't load $message_file: $!"; &debug( "spong.message file loaded" ); -# Load the messaging functions -&load_msg_funcs(); -&debug("Messaging functions loaded"); +# Read in the spong.hosts file + +require $groups_file || die "Can't load $groups_file: $!"; +&debug( "spong.groups file loaded" ); if (! defined $RULES_MATCH) { $RULES_MATCH = "OLD" }; @@ -279,7 +283,7 @@ RULE: &debug("Processing rule # $c"); - # Check for matches in excluded hosts and services + # Check for matches in excluded hosts and services and host_groups if ( defined $rule->{'exclude_hosts'} ) { my($e); foreach $e ( @{$rule->{'exclude_hosts'}} ) { @@ -300,8 +304,18 @@ RULE: } } - # Match on hosts - if ( ! defined $rule->{'hosts'} ) { + if (defined $rule->{'exclude_host_groups'} ) { + foreach $g ( @{$rule->{'exclude_host_groups'}} ) { + if ( &in_group($host,$g) ) { + &debug("Match on exclude host group $g on rule $c, going to next rule"); + next RULE; + } + } + } + + # Match on hosts and host_groups + if ( ! defined $rule->{'hosts'} && + ! defined $rule->{'host_groups'} ) { &debug("Host match for $host on rule $c"); } else { $m = 0; @@ -310,8 +324,22 @@ RULE: if ( $host =~ /$e/ ) { $m = 1; last; } } if (! $m) { - &debug("No match for host $host on rule $c"); - next RULE; + &debug("No match for host $host on rule $c, checking host groups"); + if ( defined $rule->{'host_groups'} ) { + $m = 0; + foreach $g ( @{$rule->{'host_groups'}} ) { + if ( &in_group($host,$g) ) { $m = 1; last; } + } + if (! $m) { + &debug("No match for host_groups on rule $c"); + next RULE; + } else { + &debug("host group match for $g on rule $c"); + } + } else { + &debug("No match for host_groups on rule $c"); + next RULE; + } } else { &debug("Host match for $host on rule $c"); } @@ -334,6 +362,7 @@ RULE: } } + # Match on times if ( ! defined $rule->{'times'} ) { &debug("time default match on rule $c"); } else { @@ -404,16 +433,21 @@ RULE: foreach $contact ( @{$rule->{contacts}} ) { push @con,$contact; } } + # Load the messaging functions + &load_msg_funcs(); + &debug("Messaging functions loaded"); + my(@recipients) = (); + # Starting sending out the notifications foreach $contact ( @con ) { if ($test) { &debug("Test mode: Skipping page to $contact"); next;} -# if ( $ccnt{$contact} >= $MESSAGES_PER_HOUR ) { -# debug( $ccnt{$contact} . " pages sent to $contact in last " . -# "hour - no message sent." ); -# next; -# } + if ( $ccnt{$contact} >= $MESSAGES_PER_HOUR ) { + debug( $ccnt{$contact} . " pages sent to $contact in last " . + "hour - no message sent." ); + next; + } # If if ( ref($contact) eq 'HASH') { @@ -473,7 +507,9 @@ RULE: # Remove the old npfile and fall thru to send messages unlink $np_file; # If last page time + repeat time still in the future - } elsif ( $pagetime + $repeat > time() ) { next; } # next contact + # or if no delay specified + } elsif ( $pagetime + $repeat > time() || $delay == 0 ) + { next; } # next contact else { # Update np-file and fall thru to send message &save_data( ">", $np_file, time() ); @@ -531,6 +567,23 @@ RULE: return; } +# -------------------------------------------------------------------------- +# Check to see if a host is in the specifed hosts group +# -------------------------------------------------------------------------- + +sub in_group { + my ($host,$group) = @_; + + foreach my $h (@{$GROUPS{$group}->{'members'}}) { + if ($host eq $h) { + return 1; + } + } + + return 0; +} + + # -------------------------------------------------------------------------- # Send a normal notification message to $recipient via e-mail # --------------------------------------------------------------------------