From 044c50fbc4c0524ffd9c118b3617e380085853c4 Mon Sep 17 00:00:00 2001 From: Stephen L Johnson Date: Thu, 6 Jan 2000 22:15:53 +0000 Subject: [PATCH] Changed &debug() function to allow the level of debugging output to be selected via the --debug parameter. Debug levels have been added individual &debug calls. &load_data_funcs call moved to after signal handling to prvent unnecessary loads. --- src/spong-server.pl | 133 +++++++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 45 deletions(-) diff --git a/src/spong-server.pl b/src/spong-server.pl index 1c043cd..8e0e871 100755 --- a/src/spong-server.pl +++ b/src/spong-server.pl @@ -23,16 +23,29 @@ use Spong::HistoryList; use Spong::History; use Spong::Daemon; -use Time::Local; - use Sys::Hostname; use File::Path; use Socket; use Config; +use Time::Local; +use Getopt::Long; + +$debug = $restart = $kill = 0; + +if (! GetOptions("debug:i" => \$debug_level, "restart" => \$restart, + "kill" => \$kill ) ) { + &usage(); + exit 1; +} + +if ( defined $debug_level) { + $debug = ($debug_level == 0) ? 1 : $debug_level; +} + -if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; } -if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; } -if( $ARGV[0] eq "--kill" ) { $kill = 1; shift; } +#if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; } +#if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; } +#if( $ARGV[0] eq "--kill" ) { $kill = 1; shift; } $me = "@@BINDIR@@/spong-server"; $smessage = "@@BINDIR@@/spong-message"; @@ -44,8 +57,6 @@ $datafunc_path = "@@LIBDIR@@/Spong/plugins"; $HOST =~ tr/A-Z/a-z/; %DATAFUNCS = (); -&load_data_funcs(); -&debug("Done loading data plugins"); %PROCS = (); # All of the procs that we will be monitoring in the main process @@ -55,6 +66,8 @@ $shutdown = 0; Spong::Daemon::Daemonize() unless ($debug || $restart || $kill) ; &handle_signals(); # Set up handlers, and signal the current server if asked +&load_data_funcs(); # Load any data handling function that needed + # Determine all of the update processes that will need to be running $PROCS{'query'} = { func => \&listen_for_queries, pid => undef }; @@ -109,6 +122,26 @@ while ( 1 ) { } +# Print out the command parameter usase +sub usage { + + print qq +(Usage: + $0 [--debug [n]] [--restart|--kill] [config_file] + + --debug + Run in the foreground and print debugging output. + --restart + Signal a running spong-server to restart. + --kill + Signal a running spong-server to terminated. + config_file + Use the named file as configuration file. +); + +} + + # This procedure listens for connections at port 1970 (the update port), and # each time a connection is made, it grabs the info that is being sent to us, # and hands it off to the function that saves that info to the database. Then @@ -206,7 +239,7 @@ sub listen_for_bb_updates { } close( CLIENT ); -&debug($message); + &debug($message,8); # # Now depending on what kind of message it is, pass it off to a routine # # that can process the message. Currently valid messages are "status", @@ -356,7 +389,7 @@ sub save_status { } } - &debug( "[$$] updating status for $host/$service/$color" ); + &debug( "[$$] updating status for $host/$service/$color", 2 ); $path = "$SPONGDB/$host/services"; foreach( "red", "yellow", "green", "purple" ) { unlink "$path/$service-$_"; } @@ -365,7 +398,7 @@ sub save_status { # Call of the data function modules in the registry foreach my $df (keys %DATAFUNCS) { - &debug("[$$] Running data function $df"); + &debug("[$$] Running data function $df",3); &{$DATAFUNCS{$df}}($host, $service, $color, $start, $time, $sum, $message ); @@ -381,14 +414,14 @@ sub save_bb_status { my( $header, $message ) = @_; my( $cmd, $host, $service, $color, $bbtime, $time, $sum ); -&debug("The old header is = $header"); + &debug("The old header is = $header",9); # if ( $header =~ m/^(\w+) (\w+)\.(\w+) (\w+) (\w{3} \w{3}\s+\d+ \d{2}:\d{2}:\d{2} \w+ \d{4})\s+(.*)/ ) { if ( $header =~ m/^(\w+) ([\w,-_]+)\.(\w+) (\w+) (\w{3} \w{3}\s+\d+ \d{2}:\d{2}:\d{2}[ A-Z]+\d{4})\s+(.*)$/ ) { ($cmd, $host, $service, $color, $bbtime, $sum) = ($1, $2, $3, $4, $5, $6); &debug("cmd = '$cmd' : host = '$host' : service = '$service' : " . - "color = '$color' : time = '$bbtime' : summary = '$sum' "); + "color = '$color' : time = '$bbtime' : summary = '$sum' ", 9); # Convert the system date format to unix time format if ($bbtime =~ /\w+ (\w{3}) +(\d+) (\d{2}):(\d{2}):(\d{2})[ A-Z]+(\d{4})/) { @@ -409,7 +442,6 @@ sub save_bb_status { if ($sum =~ m/\[([A-Za-z0-9._\-]+)\]\w*(.*)/ ) { $sum = $2; $host = $1; -&debug("FQDN found in \$sum: $host '$sum'"); } # Convert the commas in the BB machine back to periods. @@ -417,7 +449,7 @@ sub save_bb_status { $header = "$cmd $host $service $color $time $sum"; -&debug("The new header is = $header"); + &debug("The new header is = $header", 9); save_status($header,$message); } @@ -651,6 +683,7 @@ sub show_hist_service { # Utility functions, and signal handlers... # =========================================================================== + # This function will collect information from various sources, run it through # a set of rules defined by the user and optionally call the spong-message # program which will either email or page a human with a message providing @@ -673,48 +706,33 @@ sub message_user { &remove_np_files($host,$service); } + # If the color is 'red' and duration is non-zero, call spong-message for + # escalation notifications if ($color eq 'red' && $duration != 0) { - if ( -f $smessage ) { - &debug("color is red, calling smessage for escalations"); - system $smessage, $color, $host, $service, $time, $sum, $duration; - } else { - &error( "could not send message, $smessage not found" ); - } + &debug("color is red, calling smessage for escalations"); + &send_message( $host, $service, $color, $time, $sum, $duration ); return; - } + } if ( $SEND_MESSAGE eq "RED" ) { # If status has changed to red, call spong-mesage if ( ! -f "$SPONGDB/$host/services/$service-red" and $color eq "red" ) { - if ( -f $smessage ) { - &debug("change in state to red, messaging a human"); - system $smessage, $color, $host, $service, $time, $sum, $duration; - } else { - &error( "could not send message, $smessage not found" ); - } - } } elsif ( $SEND_MESSAGE eq "RED-CHANGE" ) { + &debug("change in state to red, messaging a human"); + &send_message( $host, $service, $color, $time, $sum, $duration ); + } + } elsif ( $SEND_MESSAGE eq "RED-CHANGE" ) { # If status has changed and either color is red, call spong-message - if ( ( -f "$SPONGDB/$host/services/$service-red" - or $color eq "red" ) + if ( ( -f "$SPONGDB/$host/services/$service-red" or $color eq "red" ) and ! -f "$SPONGDB/$host/services/$service-$color" ) { - if ( -f $smessage ) { - &debug("change in state to/from red, messaging a human"); - system $smessage, $color, $host, $service, $time, $sum, $duration; - } else { - &error( "could not send message, $smessage not found" ); - } + &debug("change in state to/from red, messaging a human"); + &send_message( $host, $service, $color, $time, $sum, $duration ); } } elsif ( $SEND_MESSAGE eq "CHANGE" ) { # If status has changed if ( ! -f "$SPONGDB/$host/services/$service.$color" ) { - if ( -f $smessage ) { - &debug("change in state, messaging a human"); - system $smessage, $color, $host, $service, $time, $sum, $duration; - - } else { - &error( "could not send message, $smessage not found" ); - } + &debug("change in state, messaging a human"); + &send_message( $host, $service, $color, $time, $sum, $duration ); } } } @@ -722,6 +740,24 @@ sub message_user { $SIG{'CHLD'} = \&chld_handler; } +# This func takes care of sending out the notification message by calling +# spong-message. + +sub send_message { + my ( $host, $service, $color, $time, $sum, $duration ) = @_; + + if ( -f $smessage ) { + if ( $debug < 3 ) { + system $smessage, $color, $host, $service, $time, $sum, $duration; + } else { + system $smessage, '--debug', $color, $host, $service, $time, $sum, + $duration; + } + } else { + &error( "could not send message, $smessage not found" ); + } +} + # This function removes old np_files which are status information for # notification delay and repeat notification processing @@ -866,7 +902,13 @@ sub save_data { # Output functions, one for debugging information, the other for errors. -sub debug { print STDOUT scalar localtime, " ", $_[0], "\n" if $main::debug; } +sub debug { + my( $str, $level ) = @_; + $level = 1 if ! defined $level; + + print STDOUT scalar localtime, " ", $str, "\n" if $main::debug >= $level; +} + sub error { warn scalar localtime(), " Error: ", $_[0], "\n"; } @@ -952,10 +994,11 @@ sub load_data_funcs { foreach $file (@files) { $file =~ /data_(.*)$/; $base = $1; - &debug("Loading data function $base"); + &debug("Loading data function $base", 3 ); eval { require "$datafunc_path/$file"; }; if ( $@ ) { &error("Could not load messaging function $base: $@"); } } + &debug("Done loading data plugins"); } -- 2.30.2