From 4c82ade184d04e80893223e1e64e3c5553a2fc8a Mon Sep 17 00:00:00 2001 From: marcos Date: Fri, 9 Jul 2021 13:58:01 +0200 Subject: [PATCH 1/6] add agent_update_custom_fields on cli --- pandora_server/lib/PandoraFMS/Core.pm | 13 +++++ pandora_server/util/pandora_manage.pl | 69 +++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index b24d6696a1..e704ae410e 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -241,6 +241,7 @@ our @EXPORT = qw( pandora_update_agent_alert_count pandora_update_agent_module_count pandora_update_config_token + pandora_update_agent_custom_field pandora_update_gis_data pandora_update_module_on_error pandora_update_module_from_hash @@ -3406,6 +3407,18 @@ sub pandora_update_config_token ($$$) { return $result; } + +########################################################################## +## 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 = ?', $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 6eaad30bf4..1c7abef93c 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, departament, aditional_id, ehorus_id, wmware_vcenter_ip, wmware_datacenter, wmware_user, wmware_pass, wmware_type, wmware_parent."); + 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'); @@ -3043,6 +3045,70 @@ 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,$field,$new_value) = @ARGV[2..4]; + + my $agent_name = get_agent_name($dbh, $id_agent); + + my $id_field; + + if($agent_name eq '') { + print_log "[ERROR] Agent '$id_agent' doesnt exist\n\n"; + exit; + } + + # serial_number, departament, aditional_id, ehorus_id, wmware_vcenter_ip, wmware_datacenter, wmware_user, wmware_pass, wmware_type, wmware_parent. + + if($field eq 'serial_number') { + $id_field = 1; + + } elsif ($field eq 'departament') { + $id_field = 2; + + } elsif ($field eq 'additional_id') { + $id_field = 3; + + } elsif ($field eq 'ehorus_id') { + $id_field = 4; + + } elsif ($field eq 'wmware_vcenter_ip') { + $id_field = 5; + + } elsif ($field eq 'wmware_datacenter') { + $id_field = 6; + + } elsif ($field eq 'wmware_user') { + $id_field = 7; + + } elsif ($field eq 'wmware_pass') { + $id_field = 8; + + } elsif ($field eq 'wmware_type') { + $id_field = 9; + + } elsif ($field eq 'wmware_parent') { + $id_field = 10; + + } + else { + print_log "[ERROR] Field '$field' doesnt exist\n\n"; + exit; + + } + + print_log "[INFO] Updating field '$field' in agent with ID '$id_agent'\n\n"; + + pandora_update_agent_custom_field ($dbh, $new_value, $id_field, $id_agent); + exit; + +} + ############################################################################## # Update an agent field. # Related option: --update_agent @@ -7883,6 +7949,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, 3, 4); + cli_agent_update_custom_fields(); } else { print_log "[ERROR] Invalid option '$param'.\n\n"; From a7e97f04939e01c4e8f99b37d0042dafcae10c2a Mon Sep 17 00:00:00 2001 From: marcos Date: Mon, 27 Sep 2021 17:56:41 +0200 Subject: [PATCH 2/6] refactor update agent custom fields --- pandora_server/util/pandora_manage.pl | 44 +++++---------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 384b87361e..97058e741d 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, departament, aditional_id, ehorus_id, wmware_vcenter_ip, wmware_datacenter, wmware_user, wmware_pass, wmware_type, wmware_parent."); + help_screen_line('--update_agent_custom_fields', ' ', "Update an agent custom field. The fields can be \n\t the following: Serial number, Department ..."); 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'); @@ -3062,48 +3062,18 @@ sub cli_agent_update_custom_fields() { exit; } - # serial_number, departament, aditional_id, ehorus_id, wmware_vcenter_ip, wmware_datacenter, wmware_user, wmware_pass, wmware_type, wmware_parent. - - if($field eq 'serial_number') { - $id_field = 1; - - } elsif ($field eq 'departament') { - $id_field = 2; - - } elsif ($field eq 'additional_id') { - $id_field = 3; - - } elsif ($field eq 'ehorus_id') { - $id_field = 4; - - } elsif ($field eq 'wmware_vcenter_ip') { - $id_field = 5; - - } elsif ($field eq 'wmware_datacenter') { - $id_field = 6; - - } elsif ($field eq 'wmware_user') { - $id_field = 7; - - } elsif ($field eq 'wmware_pass') { - $id_field = 8; - - } elsif ($field eq 'wmware_type') { - $id_field = 9; - - } elsif ($field eq 'wmware_parent') { - $id_field = 10; + # Department, Serial number ... + my $custom_field = get_db_single_row ($dbh, 'SELECT id_field FROM tagent_custom_fields WHERE name = ? ', safe_input($field)); + if($custom_field eq '') { + print_log "[ERROR] Field '$field' doesnt exist\n\n"; + exit; } - else { - print_log "[ERROR] Field '$field' doesnt exist\n\n"; - exit; - } + pandora_update_agent_custom_field ($dbh, $new_value, $id_field, $id_agent); print_log "[INFO] Updating field '$field' in agent with ID '$id_agent'\n\n"; - pandora_update_agent_custom_field ($dbh, $new_value, $id_field, $id_agent); exit; } From a8a528965267d59458738d79f99d000f4c14eaf0 Mon Sep 17 00:00:00 2001 From: marcos Date: Tue, 28 Sep 2021 09:31:06 +0200 Subject: [PATCH 3/6] update agent custom fields --- pandora_server/lib/PandoraFMS/Core.pm | 12 ++++++++++++ pandora_server/util/pandora_manage.pl | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index e1ee218876..c494682f08 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -242,6 +242,7 @@ our @EXPORT = qw( pandora_update_agent_module_count pandora_update_config_token pandora_update_agent_custom_field + pandora_select_id_custom_field pandora_update_gis_data pandora_update_module_on_error pandora_update_module_from_hash @@ -3407,6 +3408,17 @@ 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; +} ########################################################################## ## 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 97058e741d..f58316efdd 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -3063,7 +3063,7 @@ sub cli_agent_update_custom_fields() { } # Department, Serial number ... - my $custom_field = get_db_single_row ($dbh, 'SELECT id_field FROM tagent_custom_fields WHERE name = ? ', safe_input($field)); + my $custom_field = pandora_select_id_custom_field ($dbh, $field); if($custom_field eq '') { print_log "[ERROR] Field '$field' doesnt exist\n\n"; From 4e254428130668f48445bf4e335bac64c348ed84 Mon Sep 17 00:00:00 2001 From: marcos Date: Tue, 28 Sep 2021 11:20:26 +0200 Subject: [PATCH 4/6] update agent custom fields --- pandora_server/lib/PandoraFMS/Core.pm | 2 +- pandora_server/util/pandora_manage.pl | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index c494682f08..95c30314d3 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -3417,7 +3417,7 @@ sub pandora_select_id_custom_field ($$) { $result = get_db_single_row ($dbh, 'SELECT id_field FROM tagent_custom_fields WHERE name = ? ', safe_input($field)); - return $result; + return $result->{'id_field'}; } ########################################################################## diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index f58316efdd..1edc2698b3 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -3070,10 +3070,16 @@ sub cli_agent_update_custom_fields() { exit; } - pandora_update_agent_custom_field ($dbh, $new_value, $id_field, $id_agent); - print_log "[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"; + } else { + print_log "[INFO] Field '$field' updated succesfully!\n\n"; + } + exit; } From d54af52baf54f4d14b88014b8b64c566d240b377 Mon Sep 17 00:00:00 2001 From: marcos Date: Thu, 30 Sep 2021 12:50:59 +0200 Subject: [PATCH 5/6] 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 { From d0f08be953c5dd96004eba6abccb12bab2bdb67e Mon Sep 17 00:00:00 2001 From: marcos Date: Thu, 30 Sep 2021 14:34:58 +0200 Subject: [PATCH 6/6] update agent custom fields --- pandora_server/lib/PandoraFMS/Core.pm | 2 +- pandora_server/util/pandora_manage.pl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 8e84a0c834..20f41873e4 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -3439,7 +3439,7 @@ sub pandora_select_combo_custom_field ($$) { 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 = ?', $token, $field, $id_agent); + $result = db_update ($dbh, 'UPDATE tagent_custom_data SET description = ? WHERE id_field = ? AND id_agent = ?', safe_input($token), $field, $id_agent); return $result; } diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index eff172553a..46c558c472 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -3078,7 +3078,7 @@ sub cli_agent_update_custom_fields() { my @fields = split(',',$exist_option); foreach my $combo (@fields) { - if($combo eq $new_value) { + if($combo eq safe_input($new_value)) { $found = 1; } } @@ -3090,7 +3090,7 @@ sub cli_agent_update_custom_fields() { 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); + 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";