From d54af52baf54f4d14b88014b8b64c566d240b377 Mon Sep 17 00:00:00 2001 From: marcos Date: Thu, 30 Sep 2021 12:50:59 +0200 Subject: [PATCH] update agent custom fields --- pandora_server/lib/PandoraFMS/Core.pm | 13 ++++++++++++ pandora_server/util/pandora_manage.pl | 30 +++++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 95c30314d3..8e84a0c834 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -243,6 +243,7 @@ our @EXPORT = qw( pandora_update_config_token pandora_update_agent_custom_field pandora_select_id_custom_field + pandora_select_combo_custom_field pandora_update_gis_data pandora_update_module_on_error pandora_update_module_from_hash @@ -3420,6 +3421,18 @@ sub pandora_select_id_custom_field ($$) { return $result->{'id_field'}; } +########################################################################## +## Select custom field id by name tagent_custom_field +########################################################################## +sub pandora_select_combo_custom_field ($$) { + my ($dbh, $field) = @_; + my $result = undef; + + $result = get_db_single_row ($dbh, 'SELECT combo_values FROM tagent_custom_fields WHERE id_field = ? ', $field); + + return $result->{'combo_values'}; +} + ########################################################################## ## Update a custom field from agent of tagent_custom_data ########################################################################## diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 1edc2698b3..eff172553a 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -142,7 +142,7 @@ sub help_screen{ help_screen_line('--get_cluster_status', '', 'Getting cluster status'); help_screen_line('--set_disabled_and_standby', ' ', 'Overwrite and disable and standby status'); help_screen_line('--reset_agent_counts', '', 'Resets module counts and alert counts in the agents'); - help_screen_line('--update_agent_custom_fields', ' ', "Update an agent custom field. The fields can be \n\t the following: Serial number, Department ..."); + help_screen_line('--update_agent_custom_fields', ' ', "Update an agent custom field. The fields can be \n\t the following: Serial number, Department ... and types can be 0 text and 1 combo "); print "\nMODULES:\n\n" unless $param ne ''; help_screen_line('--create_data_module', " [ \n\t \n\t \n\t \n\t \n\t ]", 'Add data server module to agent'); @@ -3051,12 +3051,14 @@ sub cli_user_update() { ############################################################################## sub cli_agent_update_custom_fields() { - my ($id_agent,$field,$new_value) = @ARGV[2..4]; + my ($id_agent,$type,$field,$new_value) = @ARGV[2..5]; my $agent_name = get_agent_name($dbh, $id_agent); my $id_field; + my $found = 0; + if($agent_name eq '') { print_log "[ERROR] Agent '$id_agent' doesnt exist\n\n"; exit; @@ -3065,17 +3067,33 @@ sub cli_agent_update_custom_fields() { # Department, Serial number ... my $custom_field = pandora_select_id_custom_field ($dbh, $field); + if($custom_field eq '') { print_log "[ERROR] Field '$field' doesnt exist\n\n"; exit; } - print_log "[INFO] Updating field '$field' in agent with ID '$id_agent'\n\n"; + if($type == 1) { + my $exist_option = pandora_select_combo_custom_field ($dbh, $custom_field); - my $result = pandora_update_agent_custom_field ($dbh, $new_value, $custom_field, $id_agent); + my @fields = split(',',$exist_option); + foreach my $combo (@fields) { + if($combo eq $new_value) { + $found = 1; + } + } + if($found == 0) { + print_log "\n[ERROR] Field '$new_value' doesn't match with combo option values\n\n"; + exit + } + } + + print_log "\n[INFO] Updating field '$field' in agent with ID '$id_agent'\n\n"; + + my $result = pandora_update_agent_custom_field ($dbh, $new_value, $custom_field, $id_agent); if($result == "0E0"){ - print_log "[ERROR] Error updating field '$field'\n"; + print_log "[ERROR] Error updating field '$field'\n\n"; } else { print_log "[INFO] Field '$field' updated succesfully!\n\n"; } @@ -7909,7 +7927,7 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 1, 0); cli_event_in_progress(); } elsif ($param eq '--agent_update_custom_fields') { - param_check($ltotal, 3, 4); + param_check($ltotal, 4, 5); cli_agent_update_custom_fields(); } else {