From 3a2c2f1c38b0a9fa5c9e2a6b5b65d6e432bde9af Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 17 Jun 2020 13:14:22 +0200 Subject: [PATCH] Forced thresholds ENT --- pandora_server/lib/PandoraFMS/DB.pm | 60 ++++++++++++++++++--- pandora_server/lib/PandoraFMS/DataServer.pm | 32 +++++++++-- 2 files changed, 79 insertions(+), 13 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index e5c63615e0..daeae4b864 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -52,8 +52,10 @@ our @EXPORT = qw( db_string db_text db_update + db_update_hash db_update_get_values set_update_agent + set_update_agentmodule get_action_id get_addr_id get_agent_addr_id @@ -870,17 +872,21 @@ sub get_db_rows_limit ($$$;@) { } ########################################################################## -## Updates agent fields using field => value -## Be careful, no filter is done. +## Updates using hashed data. +## $dbh database connector (active) +## $tablename table name +## $id hashref as { 'primary_key_id' => "value" } +## $data hashref as { 'field1' => "value", 'field2' => "value"} ########################################################################## -sub set_update_agent { - my ($dbh, $agent_id, $data) = @_; +sub db_update_hash { + my ($dbh, $tablename, $id, $data) = @_; - return undef unless (defined($agent_id) && $agent_id > 0); + return undef unless (defined($tablename) && $tablename ne ""); + return undef unless (ref($data) eq "HASH"); # Build update query - my $query = 'UPDATE tagente SET '; + my $query = 'UPDATE `'.$tablename.'` SET '; my @values; foreach my $field (keys %{$data}) { @@ -891,12 +897,50 @@ sub set_update_agent { chop($query); - $query .= ' WHERE id_agente = ? '; - push @values, $agent_id; + my @keys = keys %{$id}; + my $k = shift @keys; + + $query .= ' WHERE '.$k.' = ? '; + push @values, $id->{$k}; return db_update($dbh, $query, @values); } +########################################################################## +## Updates agent fields using field => value +## Be careful, no filter is done. +########################################################################## +sub set_update_agent { + my ($dbh, $agent_id, $data) = @_; + + return undef unless (defined($agent_id) && $agent_id > 0); + return undef unless (ref($data) eq "HASH"); + + return db_update_hash( + $dbh, + 'tagente', + { 'id_agente' => $agent_id }, + $data + ); +} + +########################################################################## +## Updates agent fields using field => value +## Be careful, no filter is done. +########################################################################## +sub set_update_agentmodule { + my ($dbh, $agentmodule_id, $data) = @_; + + return undef unless (defined($agentmodule_id) && $agentmodule_id > 0); + return undef unless (ref($data) eq "HASH"); + + return db_update_hash( + $dbh, + 'tagente_modulo', + { 'id_agente_modulo' => $agentmodule_id }, + $data + ); +} ########################################################################## ## SQL delete with a LIMIT clause. diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm index 69efd9f45a..e7c2ac7a2d 100644 --- a/pandora_server/lib/PandoraFMS/DataServer.pm +++ b/pandora_server/lib/PandoraFMS/DataServer.pm @@ -632,6 +632,9 @@ sub process_module_data ($$$$$$$$$$) { # Get module parameters, matching column names in tagente_modulo my $module_conf; + + # Extra usable fields but not supported at DB level. + my $extra = {}; # Supported tags my $tags = {'name' => 0, 'data' => 0, 'type' => 0, 'description' => 0, 'max' => 0, @@ -642,7 +645,9 @@ sub process_module_data ($$$$$$$$$$) { 'unknown_instructions' => '', 'tags' => '', 'critical_inverse' => 0, 'warning_inverse' => 0, 'quiet' => 0, 'module_ff_interval' => 0, 'alert_template' => '', 'crontab' => '', 'min_ff_event_normal' => 0, 'min_ff_event_warning' => 0, 'min_ff_event_critical' => 0, 'ff_timeout' => 0, 'each_ff' => 0, 'module_parent' => 0, - 'module_parent_unlink' => 0, 'cron_interval' => 0, 'ff_type' => 0}; + 'module_parent_unlink' => 0, 'cron_interval' => 0, 'ff_type' => 0, 'min_warning_forced' => 0, 'max_warning_forced' => 0, + 'min_critical_forced' => 0, 'max_critical_forced' => 0, 'str_warning_forced' => 0, 'str_critical_forced' => 0 + }; # Other tags will be saved here $module_conf->{'extended_info'} = ''; @@ -692,7 +697,15 @@ sub process_module_data ($$$$$$$$$$) { $module_conf->{'unknown_instructions'} = '' unless defined ($module_conf->{'unknown_instructions'}); $module_conf->{'disabled_types_event'} = '' unless defined ($module_conf->{'disabled_types_event'}); $module_conf->{'module_macros'} = '' unless defined ($module_conf->{'module_macros'}); - + + # Extract extra fields. + foreach my $pk (keys %{$module_conf}) { + if ($pk =~ /_forced$/) { + $extra->{$pk} = $module_conf->{$pk}; + delete $module_conf->{$pk}; + } + } + # Get module data or create it if it does not exist my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND ' . db_text ('nombre') . ' = ?', $agent->{'id_agente'}, safe_input($module_name)); if (! defined ($module)) { @@ -825,7 +838,13 @@ sub process_module_data ($$$$$$$$$$) { # Update module configuration if in learning mode and not a policy module if ((($agent->{'modo'} eq '1') || ($agent->{'modo'} eq '2')) && $policy_linked == 0) { - update_module_configuration ($pa_config, $dbh, $module, $module_conf); + update_module_configuration( + $pa_config, + $dbh, + $module, + $module_conf, + $extra + ); } # Module disabled! @@ -898,8 +917,8 @@ sub get_macros_for_data($$){ ########################################################################## # Update module configuration in tagente_modulo if necessary. ########################################################################## -sub update_module_configuration ($$$$) { - my ($pa_config, $dbh, $module, $module_conf) = @_; +sub update_module_configuration ($$$$$) { + my ($pa_config, $dbh, $module, $module_conf, $extra) = @_; # Update if at least one of the configuration tokens has changed foreach my $conf_token ('descripcion', 'extended_info', 'module_interval') { @@ -917,6 +936,9 @@ sub update_module_configuration ($$$$) { $module->{'extended_info'} = $module_conf->{'extended_info'} if (defined($module_conf->{'extended_info'})) ; $module->{'descripcion'} = ($module_conf->{'descripcion'} eq '') ? $module->{'descripcion'} : $module_conf->{'descripcion'}; $module->{'module_interval'} = ($module_conf->{'module_interval'} eq '') ? $module->{'module_interval'} : $module_conf->{'module_interval'}; + + # Enterprise updates. + enterprise_hook('update_module_fields', [$dbh, $pa_config, $module, $extra]); } ###############################################################################