diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 1b98c20783..18fbf391a7 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -241,6 +241,9 @@ our @EXPORT = qw( pandora_update_agent_alert_count pandora_update_agent_module_count 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 @@ -3413,6 +3416,41 @@ sub pandora_update_config_token ($$$) { return $result; } +########################################################################## +## Select custom field id by name tagent_custom_field +########################################################################## +sub pandora_select_id_custom_field ($$) { + my ($dbh, $field) = @_; + my $result = undef; + + $result = get_db_single_row ($dbh, 'SELECT id_field FROM tagent_custom_fields WHERE name = ? ', safe_input($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 +########################################################################## +sub pandora_update_agent_custom_field ($$$$) { + my ($dbh, $token, $field, $id_agent) = @_; + my $result = undef; + $result = db_update ($dbh, 'UPDATE tagent_custom_data SET description = ? WHERE id_field = ? AND id_agent = ?', safe_input($token), $field, $id_agent); + + return $result; +} + ########################################################################## ## Get value of a token of tconfig table ########################################################################## diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index be50d71bdb..7bc4fc9eb1 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -142,6 +142,8 @@ 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 ... 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'); help_screen_line('--create_web_module', " [ \n\t \n\t \n\t \n\t \n\t \n\t ].\n\t The valid data types are web_data, web_proc, web_content_data or web_content_string", 'Add web server module to agent'); @@ -3042,6 +3044,64 @@ sub cli_user_update() { pandora_update_user_from_hash ($update, 'id_user', safe_input($user_id), $dbh); } + +############################################################################## +# Update an agent customs field. +# Related option: --update_agent_custom_fields +############################################################################## + +sub cli_agent_update_custom_fields() { + 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; + } + + # 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; + } + + if($type == 1) { + my $exist_option = pandora_select_combo_custom_field ($dbh, $custom_field); + + my @fields = split(',',$exist_option); + foreach my $combo (@fields) { + if($combo eq safe_input($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\n"; + } else { + print_log "[INFO] Field '$field' updated succesfully!\n\n"; + } + + exit; + +} + ############################################################################## # Update an agent field. # Related option: --update_agent @@ -7866,6 +7926,9 @@ sub pandora_manage_main ($$$) { }elsif ($param eq '--event_in_progress') { param_check($ltotal, 1, 0); cli_event_in_progress(); + } elsif ($param eq '--agent_update_custom_fields') { + param_check($ltotal, 4, 5); + cli_agent_update_custom_fields(); } else { print_log "[ERROR] Invalid option '$param'.\n\n";