use Spong::HistoryList;
use Spong::History;
use Spong::Daemon;
+use Spong::Log;
use Sys::Hostname;
use File::Path;
$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; }
$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 };
$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{'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' );
$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;
};
&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
}
}
- &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-$_"; }
# 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 );
# 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
# 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;
}
# 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 );
}
}
# 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
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..." );
if ( $@ ) { &error("Could not load messaging function $base: $@"); }
}
- &debug("Done loading data plugins");
}