From e5cb798571460e7e0acbad0718cb6f4ef74c63e0 Mon Sep 17 00:00:00 2001 From: Stephen L Johnson Date: Mon, 24 Jan 2000 07:09:43 +0000 Subject: [PATCH] Switch to using Getopt::Long for parameter parsing. Changed logging and debugging to use Spong::Log module. --- src/spong-server.pl | 70 ++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/spong-server.pl b/src/spong-server.pl index 8e0e871..3bf583f 100755 --- a/src/spong-server.pl +++ b/src/spong-server.pl @@ -22,6 +22,7 @@ use Spong::Service; use Spong::HistoryList; use Spong::History; use Spong::Daemon; +use Spong::Log; use Sys::Hostname; use File::Path; @@ -32,15 +33,12 @@ use Getopt::Long; $debug = $restart = $kill = 0; -if (! GetOptions("debug:i" => \$debug_level, "restart" => \$restart, +if (! GetOptions("debug:i" => \$debuglevel, "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; } @@ -57,17 +55,18 @@ $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 $shutdown = 0; &load_config_files(); # Loads the user specified configuration information +&init_logging(); # Initialize logging contexts 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 }; @@ -153,7 +152,7 @@ sub listen_for_updates { $SIG{'PIPE'} = 'IGNORE'; $SIG{'TERM'} = $SIG{'HUP'} = $SIG{'QUIT'} = sub { - &debug('spong updates caught QUIT signal, exiting'); + &debug('spong updates caught QUIT signal, exiting',3); close SERVER; exit; }; @@ -207,10 +206,10 @@ sub listen_for_bb_updates { $SIG{'PIPE'} = 'IGNORE'; $SIG{'TERM'} = $SIG{'HUP'} = $SIG{'QUIT'} = sub { - &debug('spong updates caught QUIT signal, exiting'); + &debug('spong updates caught QUIT signal, exiting',3); close SERVER; exit; }; - $SIG{'HUP'} = sub {&debug('bb updates caught HUP signal, exiting'); + $SIG{'HUP'} = sub {&debug('bb updates caught HUP signal, exiting',3); close SERVER; exit;}; my $proto = getprotobyname( 'tcp' ); @@ -264,7 +263,7 @@ sub listen_for_queries { $SIG{'PIPE'} = 'IGNORE'; $SIG{'TERM'} = $SIG{'HUP'} = $SIG{'QUIT'} = sub { - &debug('spong updates caught QUIT signal, exiting'); + &debug('spong updates caught QUIT signal, exiting',3); close SERVER; exit; }; @@ -351,7 +350,10 @@ sub save_status { &error( "save_status: invalid header [$header]" ); return; } - return if $main::HOSTS{$host} eq ""; + if ($main::HOSTS{$host} eq "") { + &error("save_status: undefined host [$host]"); + return; + } $start = $time; # Default start time to event time @@ -389,7 +391,7 @@ sub save_status { } } - &debug( "[$$] updating status for $host/$service/$color", 2 ); + &debug( "[$$] updating status for $host/$service/$color", 3 ); $path = "$SPONGDB/$host/services"; foreach( "red", "yellow", "green", "purple" ) { unlink "$path/$service-$_"; } @@ -398,7 +400,7 @@ sub save_status { # Call of the data function modules in the registry foreach my $df (keys %DATAFUNCS) { - &debug("[$$] Running data function $df",3); + &debug("[$$] Running data function $df",4); &{$DATAFUNCS{$df}}($host, $service, $color, $start, $time, $sum, $message ); @@ -683,6 +685,27 @@ sub show_hist_service { # Utility functions, and signal handlers... # =========================================================================== +# This function initializes the debug and error logging contexts in the +# Log module. + +sub init_logging { + if (defined $debuglevel) { + $debug = ($debuglevel == 0) ? 1 : $debuglevel + } + + Spong::Log::set_debug_context( "debuglevel" => $debug ); + + my $filename = ($SPONG_LOG_FILE) ? "$SPONGTMP/spong-server.log" : ""; + my $syslog = ($SPONG_LOG_SYSLOG) ? 1 : 0; + + Spong::Log::set_error_context( syslog => $syslog, + ident => 'spong-server', + logopt => 'pid cons', + priority => 'ERR', + filename => $filename, + ); + +} # 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 @@ -709,7 +732,7 @@ sub message_user { # If the color is 'red' and duration is non-zero, call spong-message for # escalation notifications if ($color eq 'red' && $duration != 0) { - &debug("color is red, calling smessage for escalations"); + &debug("color is red, calling smessage for escalations",2); &send_message( $host, $service, $color, $time, $sum, $duration ); return; } @@ -718,20 +741,20 @@ sub message_user { # If status has changed to red, call spong-mesage if ( ! -f "$SPONGDB/$host/services/$service-red" and $color eq "red" ) { - &debug("change in state to red, messaging a human"); + &debug("change in state to red, messaging a human",2); &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" ) and ! -f "$SPONGDB/$host/services/$service-$color" ) { - &debug("change in state to/from red, messaging a human"); + &debug("change in state to/from red, messaging a human",2); &send_message( $host, $service, $color, $time, $sum, $duration ); } } elsif ( $SEND_MESSAGE eq "CHANGE" ) { # If status has changed if ( ! -f "$SPONGDB/$host/services/$service.$color" ) { - &debug("change in state, messaging a human"); + &debug("change in state, messaging a human",2); &send_message( $host, $service, $color, $time, $sum, $duration ); } } @@ -902,14 +925,8 @@ sub save_data { # Output functions, one for debugging information, the other for errors. -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"; } +sub debug { Spong::Log::debug($_[0],$_[1]); } +sub error { Spong::Log::error($_[0]); } # Formatting function for query error reports @@ -958,7 +975,7 @@ sub chld_handler { my($pid) = wait(); $SIG{'CHLD'} = \&chld_handler; # In case of SYS V libraries if ($shutdown) { - &debug( 'Shutting down, not restarting children'); + &debug( 'Shutting down, not restarting children',2); return; } &debug( "caught CHLD signal, restarting child..." ); @@ -999,6 +1016,5 @@ sub load_data_funcs { if ( $@ ) { &error("Could not load messaging function $base: $@"); } } - &debug("Done loading data plugins"); } -- 2.30.2