diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index d97e59319d..fa702cb5e0 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,13 @@ +2012-01-24 Ramon Novoa + + * lib/PandoraFMS/Config.pm, + lib/PandoraFMS/PredictionServer.pm, + lib/PandoraFMS/Core.pm, + bin/pandora_server: Added support for enterprise netflow modules. + + * lib/PandoraFMS/PluginServer.pm: Run plugins inside eval to avoid + server crashes. + 2012-01-23 Hirofumi Kosaka * util/pandora_manage.pl: Fixed mismatch arguments call of diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index 716dbea2f7..8ab11eae5b 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -54,6 +54,9 @@ sub pandora_shutdown () { $server->stop (); } + # Stop the netflow daemon + pandora_stop_netflow_daemon (); + print_message (\%Config, ' [*] Shutting down ' . $Config{'servername'} . "(received signal)...\n", 1); db_disconnect ($DBH); if ($Config{'PID'} ne "") { @@ -92,6 +95,9 @@ sub pandora_startup () { enterprise_hook('load_enterprise_servers', [\@Servers, \%Config, $DBH]); + # Start the netflow daemon if necessary + pandora_start_netflow_daemon (); + # Remove disabled servers @Servers = grep { defined ($_) } @Servers; @@ -172,6 +178,62 @@ sub pandora_crash () { } +######################################################################################## +# Start the netflow daemon if necessary. +######################################################################################## +sub pandora_start_netflow_daemon () { + my $pid_file = '/var/run/pandora_nfcapd.pid'; + + # Check if netflow is enabled + if ($Config{'activate_netflow'} != 1) { + logger (\%Config, "Netflow daemon disabled.", 1); + print_message (\%Config, "Netflow daemon disabled.", 1); + return; + } + + # Stop nfcapd if it's already running + my $pid = pandora_stop_netflow_daemon (); + if (pandora_stop_netflow_daemon () != 0) { + logger (\%Config, "nfcapd (pid $pid) is already running, attempting to kill it...", 1); + print_message (\%Config, "nfcapd (pid $pid) is already running, attempting to kill it...", 1); + } + + # Start nfcapd + my $command = $Config{'netflow_daemon'} . ' -D -T all -w -t ' . $Config{'netflow_interval'} . ' -P ' . $pid_file . ' -l ' . $Config{'netflow_path'}; + if (system ("$command >/dev/null 2>&1") != 0) { + logger (\%Config, " [E] Could not start nfcapd: $command", 1); + print_message (\%Config, " [E] Could not start nfcapd: $command", 1); + return; + } + + logger (\%Config, "Netflow daemon started.", 1); + print_message (\%Config, "Netflow daemon started.", 1); +} + +######################################################################################## +# Stop the netflow daemon if it's running. +######################################################################################## +sub pandora_stop_netflow_daemon () { + + my $pid_file = '/var/run/pandora_nfcapd.pid'; + + # Open the pid file + if ( ! (-e $pid_file && open (PIDFILE, $pid_file))) { + return 0; + } + + my $pid = ; + close PIDFILE; + + # Check if nfcapd is running + if (kill (0, $pid) > 0) { + kill (9, $pid); + return $pid; + } + + return 0; +} + $SIG{'TERM'} = 'pandora_shutdown'; $SIG{'INT'} = 'pandora_shutdown'; diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index 2ca390101e..1b2a88d5bc 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -38,6 +38,7 @@ our @EXPORT = qw( pandora_load_config pandora_start_log pandora_get_sharedconfig + pandora_get_tconfig_token ); # version: Defines actual version of Pandora Server for this module only @@ -128,36 +129,25 @@ sub pandora_init { ########################################################################## # Read some config tokens from database set by the console ########################################################################## - sub pandora_get_sharedconfig ($$) { - my $pa_config = $_[0]; - my $dbh = $_[1]; - - my $temp; + my ($pa_config, $dbh) = @_; # Agentaccess option - - $temp = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'agentaccess'"); - if (defined($temp)) { - $pa_config->{"agentaccess"} = $temp; - } + $pa_config->{"agentaccess"} = pandora_get_tconfig_token ($dbh, 'agentaccess', 1); # Realtimestats 0 disabled, 1 enabled. # Master servers will generate all the information (global tactical stats). # and each server will generate it's own server stats (lag, etc). - - $temp = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'realtimestats'"); - if (defined($temp)) { - $pa_config->{"realtimestats"} = $temp; - } + $pa_config->{"realtimestats"} = pandora_get_tconfig_token ($dbh, 'realtimestats', 0); # Stats_interval option + $pa_config->{"stats_interval"} = pandora_get_tconfig_token ($dbh, 'stats_interval', 300); - $temp = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = 'stats_interval'"); - if (defined($temp)) { - $pa_config->{"stats_interval"} = $temp; - } - + # Netflow configuration options + $pa_config->{"activate_netflow"} = pandora_get_tconfig_token ($dbh, 'activate_netflow', 0); + $pa_config->{"netflow_path"} = pandora_get_tconfig_token ($dbh, 'netflow_path', '/var/spool/pandora/data_in/netflow'); + $pa_config->{"netflow_interval"} = pandora_get_tconfig_token ($dbh, 'netflow_interval', 300); + $pa_config->{"netflow_daemon"} = pandora_get_tconfig_token ($dbh, 'netflow_daemon', '/usr/bin/nfcapd'); } ########################################################################## @@ -296,12 +286,6 @@ sub pandora_load_config { $pa_config->{"agentaccess"} = 1; # ------------------------------------------------------------------------- - # Netflow server (4.1) - $pa_config->{'netflowserver'} = 0; - $pa_config->{'netflow_daemon'} = '/usr/bin/nfcapd'; - $pa_config->{'netflow_interval'} = 300; - $pa_config->{'netflow_basedir'} = '/var/spool/pandora/data_in/netflow'; - # Check for UID0 if ($pa_config->{"quiet"} != 0){ if ($> == 0){ @@ -620,18 +604,6 @@ sub pandora_load_config { elsif ($parametro =~ m/^block_size\s+([0-9]*)/i) { $pa_config->{'block_size'}= clean_blank($1); } - elsif ($parametro =~ m/^netflowserver\s+([0-9]*)/i) { - $pa_config->{'netflowserver'}= clean_blank($1); - } - elsif ($parametro =~ m/^netflow_daemon\s+(.*)/i) { - $pa_config->{'netflow_daemon'}= clean_blank($1); - } - elsif ($parametro =~ m/^netflow_interval\s+([0-9]*)/i) { - $pa_config->{'netflow_interval'}= clean_blank($1); - } - elsif ($parametro =~ m/^netflow_basedir\s+(.*)/i) { - $pa_config->{'netflow_basedir'}= clean_blank($1); - } } # end of loop for parameter # if (($pa_config->{"verbosity"} > 4) && ($pa_config->{"quiet"} == 0)){ @@ -666,6 +638,9 @@ sub pandora_load_config { } +########################################################################## +# Open the log file and start logging. +########################################################################## sub pandora_start_log ($){ my $pa_config = shift; @@ -674,6 +649,20 @@ sub pandora_start_log ($){ print STDERR strftime ("%Y-%m-%d %H:%M:%S", localtime()) . ' - ' . $pa_config->{'servername'} . $pa_config->{'servermode'} . " Starting Pandora FMS Server. Error logging activated.\n"; } +########################################################################## +# Read the given token from the tconfig table. +########################################################################## +sub pandora_get_tconfig_token ($$$) { + my ($dbh, $token, $default_value) = @_; + + my $token_value = get_db_value ($dbh, "SELECT value FROM tconfig WHERE token = ?", $token); + if (defined ($token_value)) { + return safe_output ($token_value); + } + + return $default_value; +} + # End of function declaration # End of defined Code diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 704debe240..76daa3926b 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -178,7 +178,7 @@ our @EXPORT = qw( # Some global variables our @DayNames = qw(sunday monday tuesday wednesday thursday friday saturday); -our @ServerTypes = qw (dataserver networkserver snmpconsole reconserver pluginserver predictionserver wmiserver exportserver inventoryserver webserver eventserver icmpserver snmpserver netflowserver); +our @ServerTypes = qw (dataserver networkserver snmpconsole reconserver pluginserver predictionserver wmiserver exportserver inventoryserver webserver eventserver icmpserver snmpserver); our @AlertStatus = ('Execute the alert', 'Do not execute the alert', 'Do not execute the alert, but increment its internal counter', 'Cease the alert', 'Recover the alert', 'Reset internal counter'); diff --git a/pandora_server/lib/PandoraFMS/PluginServer.pm b/pandora_server/lib/PandoraFMS/PluginServer.pm index d4267bd035..ca3ae808a8 100644 --- a/pandora_server/lib/PandoraFMS/PluginServer.pm +++ b/pandora_server/lib/PandoraFMS/PluginServer.pm @@ -170,7 +170,10 @@ sub data_consumer ($$) { # Execute command $command = $pa_config->{'plugin_exec'} . ' ' . $timeout . ' ' . quotemeta ($command); - my $module_data = `$command`; + my $module_data; + eval { + $module_data = `$command`; + }; my $ReturnCode = ($? >> 8) & 0xff; diff --git a/pandora_server/lib/PandoraFMS/PredictionServer.pm b/pandora_server/lib/PandoraFMS/PredictionServer.pm index e21978ea24..cc4bf37783 100644 --- a/pandora_server/lib/PandoraFMS/PredictionServer.pm +++ b/pandora_server/lib/PandoraFMS/PredictionServer.pm @@ -155,6 +155,13 @@ sub exec_prediction_module ($$$$) { return; } + # Netflow modules + if ($agent_module->{'prediction_module'} == 4) { + logger ($pa_config, "Executing netflow module " . $agent_module->{'nombre'}, 10); + enterprise_hook ('exec_netflow_module', [$pa_config, $agent_module, $server_id, $dbh]); + return; + } + # Get a full hash for target agent_module record reference ($target_module) my $target_module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente_modulo = ?', $agent_module->{'prediction_module'}); return unless defined $target_module;