From 867fccf6045ca19f9ab7a1b18d88ffd297a63798 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 17 Sep 2020 14:38:01 +0200 Subject: [PATCH 1/6] Custom id improvements --- .../agentes/module_manager_editor_common.php | 4 +- .../godmode/setup/setup_general.php | 14 +++- pandora_console/include/functions_api.php | 67 +++++++++++++++++++ pandora_console/include/functions_config.php | 8 +++ pandora_server/lib/PandoraFMS/DB.pm | 31 +++++++++ pandora_server/util/pandora_manage.pl | 43 ++++++++++++ 6 files changed, 163 insertions(+), 4 deletions(-) diff --git a/pandora_console/godmode/agentes/module_manager_editor_common.php b/pandora_console/godmode/agentes/module_manager_editor_common.php index b812e5b8b0..4641fcf28c 100644 --- a/pandora_console/godmode/agentes/module_manager_editor_common.php +++ b/pandora_console/godmode/agentes/module_manager_editor_common.php @@ -528,10 +528,10 @@ $table_advanced->data[0][1] = html_print_input_text( 20, 65, true, - $disabledBecauseInPolicy, + (($config['module_custom_id_ro']) ? true : $disabledBecauseInPolicy), false, '', - $classdisabledBecauseInPolicy + (($config['module_custom_id_ro']) ? 'readonly' : $classdisabledBecauseInPolicy) ); $table_advanced->data[0][3] = __('Unit'); diff --git a/pandora_console/godmode/setup/setup_general.php b/pandora_console/godmode/setup/setup_general.php index 0d8ce68e8e..779b9c3d31 100644 --- a/pandora_console/godmode/setup/setup_general.php +++ b/pandora_console/godmode/setup/setup_general.php @@ -344,6 +344,9 @@ $table->data[$i++][1] = html_print_checkbox_switch('console_log_enabled', 1, $co $table->data[$i][0] = __('Enable audit log').ui_print_help_tip(__('Log location').': pandora_console/log/audit.log', true); $table->data[$i++][1] = html_print_checkbox_switch('audit_log_enabled', 1, $config['audit_log_enabled'], true); +$table->data[$i][0] = __('Module custom ID readonly').ui_print_help_tip(__('Useful for integrations'), true); +$table->data[$i++][1] = html_print_checkbox_switch('module_custom_id_ro', 1, $config['module_custom_id_ro'], true); + echo '
'; echo '
'; @@ -387,7 +390,7 @@ $table_mail_conf->data[6][1] = html_print_input_password('email_password', io_ou $uniqid = uniqid(); -$table_mail_conf->data[7][0] = html_print_button(__('Email test'), 'email_test_dialog', false, "show_email_test('$uniqid');", 'class="sub next"', true); +$table_mail_conf->data[7][0] = html_print_button(__('Email test'), 'email_test_dialog', false, "show_email_test('".$uniqid."');", 'class="sub next"', true); print_email_test_modal_window($uniqid); @@ -404,7 +407,14 @@ html_print_submit_button(__('Update'), 'update_button', false, 'class="sub upd"' echo ''; echo ''; -// Print the modal window for the summary of each alerts group + +/** + * Print the modal window for the summary of each alerts group + * + * @param string $id Id. + * + * @return void + */ function print_email_test_modal_window($id) { // Email config table. diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index e4f2bee4e2..e43e6e2561 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -35,6 +35,9 @@ enterprise_include_once('include/functions_modules.php'); enterprise_include_once('include/functions_clusters.php'); enterprise_include_once('include/functions_alerts.php'); +// Clases. +use PandoraFMS\Module; + /** * Parse the "other" parameter. @@ -2515,6 +2518,70 @@ function api_get_module_id($id, $thrash1, $name, $thrash3) } +/** + * Retrieves custom_id from given module_id. + * + * @param integer $id Module id. + * + * @return void + */ +function api_get_module_custom_id($id) +{ + if (is_metaconsole()) { + return; + } + + try { + $module = new Module($id); + if (!util_api_check_agent_and_print_error( + $module->id_agente(), + 'json' + ) + ) { + return; + } + } catch (Exception $e) { + returnError('id_not_found', 'json'); + } + + returnData('json', $module->custom_id()); +} + + +/** + * Retrieves custom_id from given module_id. + * + * @param integer $id Module id. + * + * @return void + */ +function api_set_module_custom_id($id, $value) +{ + if (is_metaconsole()) { + return; + } + + try { + $module = new Module($id); + if (!util_api_check_agent_and_print_error( + $module->id_agente(), + 'json', + 'AW' + ) + ) { + return; + } + + $module->custom_id($value); + $module->save(); + } catch (Exception $e) { + returnError('id_not_found', 'json'); + } + + returnData('json', ['type' => 'string', 'data' => $module->custom_id()]); +} + + /** * Get modules for an agent, and print all the result like a csv. * diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index 3554f091ed..cea1dd0f81 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -331,6 +331,10 @@ function config_update_config() $error_update[] = __('Audit log enabled'); } + if (!config_update_value('module_custom_id_ro', get_parameter('module_custom_id_ro'))) { + $error_update[] = __('Module Custom ID read only'); + } + if (!config_update_value('unique_ip', get_parameter('unique_ip'))) { $error_update[] = __('unique_ip'); } @@ -1946,6 +1950,10 @@ function config_process_config() config_update_value('audit_log_enabled', 0); } + if (!isset($config['module_custom_id_ro'])) { + config_update_value('module_custom_id_ro', 0); + } + if (!isset($config['elasticsearch_ip'])) { config_update_value('elasticsearch_ip', ''); } diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index ae07c49c96..88c51b2f03 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -99,6 +99,8 @@ our @EXPORT = qw( get_user_exists get_user_profile_id get_group_children + get_agentmodule_custom_id + set_agentmodule_custom_id is_agent_address is_group_disabled get_agent_status @@ -446,6 +448,35 @@ sub get_agentmodule_data ($$$$$) { return @rows; } +########################################################################## +## Return module custom ID given the module id. +########################################################################## +sub get_agentmodule_custom_id ($$) { + my ($dbh, $id_agent_module) = @_; + + my $rc = get_db_value( + $dbh, + "SELECT custom_id FROM tagente_modulo WHERE id_agente_modulo = ?", + safe_input($id_agent_module) + ); + return defined($rc) ? $rc : undef; +} + +########################################################################## +## Updates module custom ID given the module id and custom Id. +########################################################################## +sub set_agentmodule_custom_id ($$$) { + my ($dbh, $id_agent_module, $custom_id) = @_; + + my $rc = db_update( + $dbh, + "UPDATE tagente_modulo SET custom_id = ? WHERE id_agente_modulo = ?", + safe_input($custom_id), + safe_input($id_agent_module) + ); + return defined($rc) ? ($rc eq '0E0' ? 0 : $rc) : -1; +} + ######################################################################## ## SUB get_agentmodule_status (agent_module_id) ## Return agent module status. given "agent_module_id" diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 0a97efe1a8..bdfb0f007c 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -118,6 +118,8 @@ sub help_screen{ help_screen_line('--get_planned_downtimes_items', ' [ ]', 'Get all items of planned downtimes'); help_screen_line('--set_planned_downtimes_deleted', ' ', 'Deleted a planned downtime'); help_screen_line('--get_module_id', ' ', 'Get the id of an module'); + help_screen_line('--get_module_custom_id', '', 'Get the custom_id of given module'); + help_screen_line('--set_module_custom_id', ' []', 'Set (or erase if empty) the custom_id of given module'); help_screen_line('--get_agent_group', ' []', 'Get the group name of an agent'); help_screen_line('--get_agent_group_id', ' []', 'Get the group ID of an agent'); help_screen_line('--get_agent_modules', ' []', 'Get the modules of an agent'); @@ -4616,6 +4618,39 @@ sub cli_get_module_id() { } +############################################################################## +# Retrieves the module custom_id given id_agente_modulo. +# Related option: --get_module_custom_id +# perl pandora_manage.pl /etc/pandora/pandora_server.conf --get_module_custom_id 4 +############################################################################## + +sub cli_get_module_custom_id { + my $module_id = $ARGV[2]; + + my $custom_id = get_agentmodule_custom_id($dbh, $module_id); + + if (defined($custom_id)) { + print $custom_id; + } +} + +############################################################################## +# Update sor erases the module custom_id given id_agente_modulo. +# Related option: --get_module_custom_id +# perl pandora_manage.pl /etc/pandora/pandora_server.conf --get_module_custom_id 4 test +############################################################################## + +sub cli_set_module_custom_id { + my ($module_id, $custom_id) = @ARGV[2..3]; + + my $rs = set_agentmodule_custom_id($dbh, $module_id, $custom_id); + + if ($rs > 0) { + print $custom_id; + } else { + print "[ERROR] No changes."; + } +} ############################################################################## # Show the group name where a given agent is @@ -7491,6 +7526,14 @@ sub pandora_manage_main ($$$) { param_check($ltotal, 2); cli_get_module_id(); } + elsif ($param eq '--get_module_custom_id') { + param_check($ltotal, 1); + cli_get_module_custom_id(); + } + elsif ($param eq '--set_module_custom_id') { + param_check($ltotal, 2); + cli_set_module_custom_id(); + } elsif ($param eq '--get_agent_group') { param_check($ltotal, 2, 1); cli_get_agent_group(); From bc166dfba2f4b8683d4ec2ebae1cd3246d571d9a Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 17 Sep 2020 16:51:23 +0200 Subject: [PATCH 2/6] module custom id in event list + minor fixes --- pandora_console/godmode/events/custom_events.php | 1 + pandora_console/include/functions_events.php | 4 ++++ pandora_console/operation/events/events.php | 16 +++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pandora_console/godmode/events/custom_events.php b/pandora_console/godmode/events/custom_events.php index fb4b6f9ab1..124b0711c5 100644 --- a/pandora_console/godmode/events/custom_events.php +++ b/pandora_console/godmode/events/custom_events.php @@ -114,6 +114,7 @@ $fields_available['server_name'] = __('Server Name'); $fields_available['data'] = __('Data'); $fields_available['module_status'] = __('Module Status'); $fields_available['mini_severity'] = __('Severity mini'); +$fields_available['module_custom_id'] = __('Module custom ID'); // Remove fields already selected. diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 9f8cfdbf08..a6f0f83752 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -207,6 +207,7 @@ function events_get_all_fields() $columns['server_name'] = __('Server name'); $columns['data'] = __('Data'); $columns['module_status'] = __('Module status'); + $columns['module_custom_id'] = __('Module custom id'); return $columns; } @@ -291,6 +292,9 @@ function events_get_column_name($field, $table_alias=false) case 'module_status': return __('Module Status'); + case 'module_custom_id': + return __('Module custom ID'); + case 'options': return __('Options'); diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index f9a727f6c2..b0e7cc3002 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -292,6 +292,7 @@ if (is_ajax()) { if (!is_metaconsole()) { $fields[] = 'am.nombre as module_name'; $fields[] = 'am.id_agente_modulo as id_agentmodule'; + $fields[] = 'am.custom_id as module_custom_id'; $fields[] = 'ta.server_name as server_name'; } else { $fields[] = 'ts.server_name as server_name'; @@ -470,9 +471,13 @@ $tags_select_with = []; $tags_select_without = []; $tag_with_temp = []; $tag_without_temp = []; -$tag_with = json_decode(base64_decode($tag_with), true); -$tag_without = json_decode(base64_decode($tag_without), true); +if (is_array($tag_with) === false) { + $tag_with = json_decode(base64_decode($tag_with), true); +} +if (is_array($tag_without) === false) { + $tag_without = json_decode(base64_decode($tag_without), true); +} foreach ($tags as $id_tag => $tag) { if (is_array($tag_with) === true @@ -1315,10 +1320,12 @@ try { // 'timestamp_rep', // 'timestamp_rep_min', // 'module_name', + // 'custom_id', [ 'text' => 'options', 'class' => 'action_buttons w120px', - ],[ + ], + [ 'text' => 'm', 'extra' => $checkbox_all, 'class' => 'mw120px', @@ -2004,6 +2011,9 @@ function process_datatables_item(item) { /* Module name */ item.id_agentmodule = item.module_name; + + // Module custom_id. + item.custom_id = item.module_custom_id; } /* Datatables auxiliary functions ends */ From 92da78496be3dff8fb5fec98bf7198077e1aeada Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 17 Sep 2020 16:54:40 +0200 Subject: [PATCH 3/6] Added module custom id to general tab in event details --- pandora_console/include/functions_events.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index a6f0f83752..b398df5553 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -4682,6 +4682,16 @@ function events_page_general($event) $table_general->data[] = $data; + $data = []; + $data[0] = __('Module custom ID'); + if ($event['module_custom_id'] != '') { + $data[1] = $event['module_custom_id']; + } else { + $data[1] = ''.__('N/A').''; + } + + $table_general->data[] = $data; + $table_data = $table_general->data; if (is_array($table_data)) { $table_data_total = count($table_data); From 4fb69f343a58e55efee9592d4b354bf402201ef8 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 17 Sep 2020 19:32:28 +0200 Subject: [PATCH 4/6] minor fix --- .../godmode/agentes/module_manager_editor_common.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandora_console/godmode/agentes/module_manager_editor_common.php b/pandora_console/godmode/agentes/module_manager_editor_common.php index 4641fcf28c..50bcf4a824 100644 --- a/pandora_console/godmode/agentes/module_manager_editor_common.php +++ b/pandora_console/godmode/agentes/module_manager_editor_common.php @@ -528,10 +528,10 @@ $table_advanced->data[0][1] = html_print_input_text( 20, 65, true, - (($config['module_custom_id_ro']) ? true : $disabledBecauseInPolicy), + (($config['module_custom_id_ro'] && $__code_from != 'policies') ? true : $disabledBecauseInPolicy), false, '', - (($config['module_custom_id_ro']) ? 'readonly' : $classdisabledBecauseInPolicy) + (($config['module_custom_id_ro'] && $__code_from != 'policies') ? 'readonly' : $classdisabledBecauseInPolicy) ); $table_advanced->data[0][3] = __('Unit'); From 4f80eec179e808298d0d1f7f97b9084d97ae47c1 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 1 Oct 2020 13:42:41 +0200 Subject: [PATCH 5/6] recovered changes after merge request --- pandora_console/include/functions_api.php | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 19560ab774..c1c539e8d3 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -2519,6 +2519,70 @@ function api_get_module_id($id, $thrash1, $name, $thrash3) } +/** + * Retrieves custom_id from given module_id. + * + * @param integer $id Module id. + * + * @return void + */ +function api_get_module_custom_id($id) +{ + if (is_metaconsole()) { + return; + } + + try { + $module = new Module($id); + if (!util_api_check_agent_and_print_error( + $module->id_agente(), + 'json' + ) + ) { + return; + } + } catch (Exception $e) { + returnError('id_not_found', 'json'); + } + + returnData('json', $module->custom_id()); +} + + +/** + * Retrieves custom_id from given module_id. + * + * @param integer $id Module id. + * + * @return void + */ +function api_set_module_custom_id($id, $value) +{ + if (is_metaconsole()) { + return; + } + + try { + $module = new Module($id); + if (!util_api_check_agent_and_print_error( + $module->id_agente(), + 'json', + 'AW' + ) + ) { + return; + } + + $module->custom_id($value); + $module->save(); + } catch (Exception $e) { + returnError('id_not_found', 'json'); + } + + returnData('json', ['type' => 'string', 'data' => $module->custom_id()]); +} + + /** * Get modules for an agent, and print all the result like a csv. * From 308598ba8d9ab4340e9a45bead211107823f2bb0 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 1 Oct 2020 16:23:00 +0200 Subject: [PATCH 6/6] minor fix --- pandora_server/util/pandora_manage.pl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 24b206d076..4ed049eaf6 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -4631,6 +4631,8 @@ sub cli_get_module_custom_id { if (defined($custom_id)) { print $custom_id; + } else { + print "\n"; } }