From: Stephen L Johnson Date: Fri, 11 Jan 2002 19:28:44 +0000 (+0000) Subject: Added pre and post plugin support; pre post and data plugins module sare now X-Git-Tag: spong-2_7_7~25 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54fa62d0a215020ef2f7f12f978f1678971b970b;p=spong.git Added pre and post plugin support; pre post and data plugins module sare now called in sorted key order. --- diff --git a/src/spong-server.pl b/src/spong-server.pl index 11d78f1..2ec5658 100755 --- a/src/spong-server.pl +++ b/src/spong-server.pl @@ -6,7 +6,7 @@ # There are one or more update processes that listen for status updates # from client programs. -# $Id: spong-server.pl,v 1.47 2001/11/14 17:05:24 sljohnson Exp $ +# $Id: spong-server.pl,v 1.48 2002/01/11 19:28:44 sljohnson Exp $ use lib "@@LIBDIR@@"; @@ -69,7 +69,11 @@ $HOST =~ tr/A-Z/a-z/; %DATAFUNCS = (); +%PREDATAFUNCS = (); +%POSTDATAFUNCS = (); &load_data_funcs(); +&load_predata_funcs(); +&load_postdata_funcs(); &debug("Done loading data plugins"); %PROCS = (); # All of the procs that we will be monitoring in the main process @@ -613,16 +617,34 @@ sub save_status { if ($header =~ /^timestamp (\d+) (\d+)/) { $start = $1; } close FILE; } + $duration = $time - $start; - # Before we change the state of the database, call message_user()... + # Build message hash for data plugin calls + my $msg = { 'header' => $header, + 'message' => $message, + 'cmd' => $cmd, + 'host' => $host, + 'service' => $service, + 'color' => $color, + 'time' => $time, + 'duration' => $duration, + }; - $duration = $time - $start; - &message_user( "status", $host, $service, $color, $time, $sum, $duration, - $message ); + # Call of the data function modules in the registry + foreach my $df (sort(keys %PREDATAFUNCS)) { + &debug("[$$] Running pre data function $df",4); + &{$PREDATAFUNCS{$df}}( $msg ); + } - # Make sure that this service is registered in our master service list. + # If a Predata plugin says to drop the message, drop it + if ( $msg->{'drop_msg'} ) { + debug( "[$$] signal from pre data func module, dropping message",5); + return; + } - $main::SERVICES{$service} = 1; + # Before we change the state of the database, call message_user()... + &message_user( "status", $host, $service, $color, $time, $sum, $duration, + $message ); # Save the status information to the database, if there is a change in # color, then update the history table and state file as well. @@ -652,7 +674,13 @@ sub save_status { $expire ); # Call of the data function modules in the registry - foreach my $df (keys %DATAFUNCS) { + foreach my $df (sort(keys %POSTFUNCS)) { + &debug("[$$] Running post data function $df",4); + &{$POSTFUNCS{$df}}( $msg ); + } + + # Call of the data function modules in the registry + foreach my $df (sort(keys %DATAFUNCS)) { &debug("[$$] Running data function $df",4); &{$DATAFUNCS{$df}}($host, $service, $color, $start, $time, $sum, @@ -845,11 +873,33 @@ sub save_ack { &error( "save_ack: invalid header [$header]" ); return; } - return if $main::HOSTS{$host} eq ""; + if ($main::HOSTS{$host} eq "") { + &error("save_ack: undefined host [$host]"); + return; + } + + # Build message hash for the ack + my $msg = { 'header' => $header, + 'message' => $message, + 'cmd' => $cmd, + 'host' => $host, + 'service' => $service, + 'start_time' => $now, + 'end_time' => $time, + 'user' => $user, + }; - # Before we change the state of the database, call message_user()... + # Call of the data function modules in the registry + foreach my $df (sort(keys %PREDATAFUNCS)) { + &debug("[$$] Running pre data function $df",4); + &{$PREDATAFUNCS{$df}}( $msg ); + } -# &message_user( "ack", $host, $service, $color, $summ); + # If a Predata plugin says to drop the message, drop it + if ( $msg->{'drop_msg'} ) { + debug( "[$$] signal from pre data func module, dropping message",5); + return; + } # Clean up expired acknowledgments. @@ -875,10 +925,16 @@ sub save_ack { # Save the status update information $untiltime = POSIX::strftime( "%H:%M, %D", localtime($time) ); $data = "timestamp $now $time\ncolor blue\n$now $user ack $service Until $untiltime \nack: $service by $user \n$message\n"; - &save_data( ">>", "$SpongConf::SPONGDB/$host/history/status/$now-$service", $data); + &save_data( ">>", "$SPONGDB/$host/history/status/$now-$service", $data); } + # Call of the data function modules in the registry + foreach my $df (sort(keys %POSTDATAFUNCS)) { + &debug("[$$] Running post data function $df",4); + &{$POSTDATAFUNCS{$df}}( $msg ); + } + } # Take the incoming acknowledgment message, run it through some error checking @@ -901,6 +957,27 @@ sub del_ack { if( $main::HOSTS{$host} eq "" ) { &error( "del_ack: invalid host [$host]" ); return; } + # Build a hash of the del_ack message for data plugin runs + my $msg = { 'header' => $header, + 'message' => $message, + 'cmd' => $cmd, + 'host' => $host, + 'service' => $service, + 'end_time' => $end, + }; + + # Call of the data function modules in the registry + foreach my $df (keys %PREDATAFUNCS) { + &debug("[$$] Running pre data function $df",4); + &{$PREDATAFUNCS{$df}}( $msg ); + } + + # If a Predata plugin says to drop the message, drop it + if ( $msg->{'drop_msg'} ) { + debug( "[$$] signal from pre data func module, dropping message",5); + return; + } + # Remove the acknowledgement that the user specified. opendir( DIR, "$main::SPONGDB/$host/acks" ); @@ -913,6 +990,13 @@ sub del_ack { # I don't updated the history when an ack is deleted, I suppose I should # but it makes things a little messing (since an update is just a delete # and a re-add). + + # Call of the data function modules in the registry + foreach my $df (sort(keys %POSTDATAFUNCS)) { + &debug("[$$] Running post data function $df",4); + &{$POSTDATAFUNCS{$df}}( $msg ); + } + } # =========================================================================== @@ -1485,7 +1569,49 @@ sub load_data_funcs { $file =~ /data_(.*)$/; $base = $1; &debug("Loading data function $base", 3 ); eval { require "$datafunc_path/$file"; }; - if ( $@ ) { &error("Could not load messaging function $base: $@"); } + if ( $@ ) { &error("Could not load data function $base: $@"); } + } + +} + +# --------------------------------------------------------------------------- +# Load all of the data functions into the POSTDATAFUNCS registry +# --------------------------------------------------------------------------- + +sub load_postdata_funcs { + my($file,@files); + + if ( ! -d $datafunc_path) { return; } + opendir(MSG,$datafunc_path) or die "Could not opendir $datafunc_path: $!"; + @files = grep { /^post_/ } readdir(MSG); + closedir(MSG); + + foreach $file (@files) { + $file =~ /post_(.*)$/; $base = $1; + &debug("Loading post data function $base", 3 ); + eval { require "$datafunc_path/$file"; }; + if ( $@ ) { &error("Could not load post data function $base: $@"); } + } + +} + +# --------------------------------------------------------------------------- +# Load all of the predata functions into the PREDATAFUNCS registry +# --------------------------------------------------------------------------- + +sub load_predata_funcs { + my($file,@files); + + if ( ! -d $datafunc_path) { return; } + opendir(MSG,$datafunc_path) or die "Could not opendir $datafunc_path: $!"; + @files = grep { /^pre_/ } readdir(MSG); + closedir(MSG); + + foreach $file (@files) { + $file =~ /pre_(.*)$/; $base = $1; + &debug("Loading pre data function $base", 3 ); + eval { require "$datafunc_path/$file"; }; + if ( $@ ) { &error("Could not load pre data function $base: $@"); } } }