From eb1d17d3b08b930c705d4df7adc38f47aaab4415 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Mon, 19 Dec 2022 17:05:49 +0100 Subject: [PATCH] fix --- pandora_server/lib/PandoraFMS/Core.pm | 172 ++++++++++++++++++ .../lib/PandoraFMS/PredictionServer.pm | 6 +- 2 files changed, 175 insertions(+), 3 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index e4bf6eeaae..7428314069 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -280,6 +280,9 @@ our @EXPORT = qw( notification_set_targets notification_get_users notification_get_groups + exec_cluster_aa_module + exec_cluster_ap_module + exec_cluster_status_module ); # Some global variables @@ -7275,6 +7278,175 @@ sub pandora_snmptrapd_still_working ($$) { } } + +################################################################################ +# Execute a cluster status module. +################################################################################ +sub exec_cluster_status_module ($$$$) { + my ($pa_config, $module, $server_id, $dbh) = @_; + + # Execute cluster modules. + my @modules = get_db_rows($dbh, + 'SELECT * + FROM tagente_modulo + WHERE tagente_modulo.id_agente = ? + AND tagente_modulo.disabled != 1 + AND tagente_modulo.tcp_port = 1', + $module->{'id_agente'}); + foreach my $agent_module (@modules) { + # Cluster active-active module. + if ($agent_module->{'prediction_module'} == 6) { + logger ($pa_config, "Executing cluster active-active critical module " . $agent_module->{'nombre'}, 10); + exec_cluster_aa_module($pa_config, $agent_module, $server_id, $dbh); + } + # Cluster active-passive module. + elsif ($agent_module->{'prediction_module'} == 7) { + logger ($pa_config, "Executing cluster active-passive critical module " . $agent_module->{'nombre'}, 10); + exec_cluster_ap_module($pa_config, $agent_module, $server_id, $dbh); + } + } + + # Get the status of cluster modules. + my $data = -1; + @modules = get_db_rows($dbh, + 'SELECT tagente_modulo.id_agente_modulo, tagente_estado.estado + FROM tagente_estado, tagente_modulo + WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo + AND tagente_modulo.disabled != 1 + AND tagente_modulo.tcp_port = 1 + AND tagente_modulo.id_agente = ?', + $module->{'id_agente'}); + foreach my $cluster_module (@modules) { + next if ($cluster_module->{'id_agente_modulo'} == $module->{'id_agente_modulo'}); # Skip the current module. + + # Critical. + if ($cluster_module->{'estado'} == MODULE_NORMAL && $data < 0) { + $data = 0; + } elsif ($cluster_module->{'estado'} == MODULE_WARNING && $data < 1) { + $data = 1; + } elsif (($cluster_module->{'estado'} == MODULE_CRITICAL || $cluster_module->{'estado'} == MODULE_UNKNOWN) && $data < 2) { + $data = 2; + } + } + + # No data. + if ($data < 0) { + pandora_update_module_on_error ($pa_config, $module, $dbh); + return; + } + + # Get the current timestamp. + my $utimestamp = time (); + my $timestamp = strftime("%Y-%m-%d %H:%M:%S", localtime($utimestamp)); + + # Update the module. + pandora_process_module($pa_config, {'data' => $data}, '', $module, '', $timestamp, $utimestamp, $server_id, $dbh); + + pandora_update_agent($pa_config, $timestamp, $module->{'id_agente'}, undef, undef, -1, $dbh); +} + +################################################################################ +# Execute a cluster active-active module. +################################################################################ +sub exec_cluster_aa_module ($$$$) { + my ($pa_config, $module, $server_id, $dbh) = @_; + + # Get the cluster item. + my $item = get_db_single_row($dbh, 'SELECT * FROM tcluster_item WHERE id=?', $module->{'custom_integer_2'}); + if (!defined($item)) { + pandora_update_module_on_error ($pa_config, $module, $dbh); + return; + } + + # Get cluster agents and compute the item status. + my ($not_normal, $total) = (0, 0); + my @agents = get_db_rows($dbh, 'SELECT id_agent FROM tcluster_agent WHERE id_cluster = ?', $module->{'custom_integer_1'}); + foreach my $agent_id (@agents) { + my $item_status = get_db_value($dbh, + 'SELECT estado + FROM tagente_estado, tagente_modulo + WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo + AND tagente_modulo.id_agente = ? + AND tagente_modulo.nombre = ?', + $agent_id->{'id_agent'}, $item->{'name'}); + + # Count modules in a status other than normal. + if (!defined($item_status) || $item_status != MODULE_NORMAL) { + $not_normal += 1; + } + + $total += 1; + } + + # No data. + if ($total < 1) { + pandora_update_module_on_error ($pa_config, $module, $dbh); + return; + } + + # Convert $not_normal to a percentage. + $not_normal = 100 * $not_normal / $total; + + # Get the current timestamp. + my $utimestamp = time (); + my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($utimestamp)); + + # Update module + pandora_process_module ($pa_config, {'data' => $not_normal}, '', $module, '', $timestamp, $utimestamp, $server_id, $dbh); + + pandora_update_agent ($pa_config, $timestamp, $module->{'id_agente'}, undef, undef, -1, $dbh); +} + +################################################################################ +# Execute a cluster active-pasive module. +################################################################################ +sub exec_cluster_ap_module ($$$$) { + my ($pa_config, $module, $server_id, $dbh) = @_; + + # Get the cluster item. + my $item = get_db_single_row($dbh, 'SELECT * FROM tcluster_item WHERE id=?', $module->{'custom_integer_2'}); + if (!defined($item)) { + pandora_update_module_on_error ($pa_config, $module, $dbh); + return; + } + + # Get cluster agents and compute the item status. + my $data = undef; + my $utimestamp = 0; + my @agents = get_db_rows($dbh, 'SELECT id_agent FROM tcluster_agent WHERE id_cluster = ?', $module->{'custom_integer_1'}); + foreach my $agent_id (@agents) { + my $status = get_db_single_row($dbh, + 'SELECT datos, estado, utimestamp + FROM tagente_estado, tagente_modulo + WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo + AND tagente_modulo.id_agente = ? + AND tagente_modulo.nombre = ?', + $agent_id->{'id_agent'}, $item->{'name'}); + + # Get the most recent data. + if (defined($status) && $status->{'estado'} != MODULE_UNKNOWN && $status->{'utimestamp'} > $utimestamp) { + $utimestamp = $status->{'utimestamp'}; + $data = $status->{'datos'}; + } + } + + # No data. + if ($utimestamp == 0) { + pandora_update_module_on_error ($pa_config, $module, $dbh); + return; + } + + # Get the current timestamp. + $utimestamp = time (); + my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime($utimestamp)); + + # Update the module. + pandora_process_module ($pa_config, {'data' => $data }, '', $module, '', $timestamp, $utimestamp, $server_id, $dbh); + + # Update the agent. + pandora_update_agent ($pa_config, $timestamp, $module->{'id_agente'}, undef, undef, -1, $dbh); +} + # End of function declaration # End of defined Code diff --git a/pandora_server/lib/PandoraFMS/PredictionServer.pm b/pandora_server/lib/PandoraFMS/PredictionServer.pm index 1fd05730cd..1225ab5b19 100644 --- a/pandora_server/lib/PandoraFMS/PredictionServer.pm +++ b/pandora_server/lib/PandoraFMS/PredictionServer.pm @@ -207,21 +207,21 @@ sub exec_prediction_module ($$$$) { # Cluster status module. if ($agent_module->{'prediction_module'} == 5) { logger ($pa_config, "Executing cluster status module " . $agent_module->{'nombre'}, 10); - enterprise_hook ('exec_cluster_status_module', [$pa_config, $agent_module, $server_id, $dbh]); + exec_cluster_status_module($pa_config, $agent_module, $server_id, $dbh); return; } # Cluster active-active module. if ($agent_module->{'prediction_module'} == 6) { logger ($pa_config, "Executing cluster active-active module " . $agent_module->{'nombre'}, 10); - enterprise_hook ('exec_cluster_aa_module', [$pa_config, $agent_module, $server_id, $dbh]); + exec_cluster_aa_module($pa_config, $agent_module, $server_id, $dbh); return; } # Cluster active-passive module. if ($agent_module->{'prediction_module'} == 7) { logger ($pa_config, "Executing cluster active-passive module " . $agent_module->{'nombre'}, 10); - enterprise_hook ('exec_cluster_ap_module', [$pa_config, $agent_module, $server_id, $dbh]); + exec_cluster_ap_module($pa_config, $agent_module, $server_id, $dbh); return; }