diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 9a443398d2..50fb98fc9f 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,8 @@ +2014-06-11 Ramon Novoa + + * lib/PandoraFMS/Core.pm: Prevent race conditions between keep_alive and + keep_alive_nd. + 2014-06-08 Junichi Satoh * FreeBSD/pandora_server.conf, NetBSD/pandora_server.conf: Updated diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 70f5deead9..128f3e3540 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -108,6 +108,7 @@ use Time::Local; use POSIX qw(strftime); use threads; use threads::shared; +use Thread::Semaphore; use JSON qw(decode_json encode_json); use MIME::Base64; @@ -224,6 +225,9 @@ our @AlertStatus = ('Execute the alert', 'Do not execute the alert', 'Do not exe # Event storm protection (no alerts or events) our $EventStormProtection :shared = 0; +# Semaphore for keep alive modules +my $KeepAliveSem :shared = Thread::Semaphore->new (1); + ########################################################################## # Return the agent given the IP address. ########################################################################## @@ -2220,13 +2224,21 @@ sub pandora_module_keep_alive ($$$$$) { my ($pa_config, $id_agent, $agent_name, $server_id, $dbh) = @_; logger($pa_config, "Updating keep_alive module for agent '" . safe_output($agent_name) . "'.", 10); + $KeepAliveSem->down_force(); # Update keepalive module my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND delete_pending = 0 AND id_tipo_modulo = 100', $id_agent); - return unless defined ($module); - - my %data = ('data' => 1); - pandora_process_module ($pa_config, \%data, '', $module, 'keep_alive', '', time(), $server_id, $dbh); + if (defined ($module)) { + my %data = ('data' => 1); + eval { + pandora_process_module ($pa_config, \%data, '', $module, 'keep_alive', '', time(), $server_id, $dbh); + }; + if ($@) { + $KeepAliveSem->up(); + die($@); + } + } + $KeepAliveSem->up(); } ########################################################################## @@ -2818,7 +2830,15 @@ sub pandora_module_keep_alive_nd { my %data = ('data' => 0); foreach my $module (@modules) { logger($pa_config, "Updating keep_alive module for module '" . $module->{'nombre'} . "' agent ID " . $module->{'id_agente'} . " (agent without data).", 10); - pandora_process_module ($pa_config, \%data, '', $module, 'keep_alive', '', time (), 0, $dbh); + $KeepAliveSem->down(); + eval { + pandora_process_module ($pa_config, \%data, '', $module, 'keep_alive', '', time (), 0, $dbh); + }; + if ($@) { + $KeepAliveSem->up(); + die($@); + } + $KeepAliveSem->up(); } }