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";
$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
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 };
}
+# 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
}
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",
}
}
- &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-$_"; }
# 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 );
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})/) {
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.
$header = "$cmd $host $service $color $time $sum";
-&debug("The new header is = $header");
+ &debug("The new header is = $header", 9);
save_status($header,$message);
}
# 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
&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 );
}
}
}
$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
# 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"; }
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");
}