use lib "@@LIBDIR@@";
+use Data::Dumper;
+
use Spong::AckList;
use Sys::Hostname;
$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());
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" };
&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'}} ) {
}
}
- # 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;
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");
}
}
}
+ # Match on times
if ( ! defined $rule->{'times'} ) {
&debug("time default match on rule $c");
} else {
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') {
# 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() );
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
# --------------------------------------------------------------------------