From 29cecf2e29bc350e2a75aee0719d368637bb63d3 Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Tue, 29 Aug 2017 13:54:44 +0200 Subject: [PATCH] Remove nested queries to prevent deadlocks with the Data Server. Ref pandora_enterprise#1248. --- pandora_server/lib/PandoraFMS/Core.pm | 29 ++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 5bd389a46b..59f737c3ff 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -5051,15 +5051,26 @@ sub pandora_set_event_storm_protection ($) { ########################################################################## sub pandora_update_agent_module_count ($$$) { my ($pa_config, $dbh, $agent_id) = @_; - - db_do ($dbh, 'UPDATE tagente SET update_module_count=0, - normal_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=0), - critical_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=1), - warning_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=2), - unknown_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=3), - notinit_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . ' AND estado=4), - total_count=(SELECT COUNT(*) FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=' . $agent_id . - ') WHERE id_agente = ' . $agent_id); + my $total = 0; + my $counts = { + '0' => 0, + '1' => 0, + '2' => 0, + '3' => 0, + '4' => 0, + }; # Module counts by status. + + # Retrieve and hash module status counts. + my @rows = get_db_rows ($dbh, 'SELECT estado, COUNT(*) AS total FROM tagente_modulo, tagente_estado WHERE tagente_modulo.disabled=0 AND tagente_modulo.id_agente_modulo=tagente_estado.id_agente_modulo AND tagente_modulo.id_agente=?GROUP BY estado', $agent_id); + foreach my $row (@rows) { + $counts->{$row->{'estado'}} = $row->{'total'}; + $total += $row->{'total'}; + } + + # Update the agent. + db_do ($dbh, 'UPDATE tagente + SET update_module_count=0, normal_count=?, critical_count=?, warning_count=?, unknown_count=?, notinit_count=?, total_count=? + WHERE id_agente = ?', $counts->{'0'}, $counts->{'1'}, $counts->{'2'}, $counts->{'3'}, $counts->{'4'}, $total, $agent_id); # Sync the agent cache every time the module count is updated. enterprise_hook('update_agent_cache', [$pa_config, $dbh, $agent_id]) if ($pa_config->{'node_metaconsole'} == 1);