From 3d5a5c856b0fa3a53d150e309c6ceaaad945be71 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 8 Sep 2023 13:09:03 +0200 Subject: [PATCH] ITSM pandora_enterprise#11471 --- pandora_console/godmode/setup/setup_ITSM.php | 132 +++++++++++++++---- pandora_console/include/functions_api.php | 114 ++++++++++++++++ pandora_console/include/functions_config.php | 20 ++- pandora_console/include/lib/ITSM/ITSM.php | 19 ++- pandora_server/lib/PandoraFMS/Core.pm | 62 --------- pandora_server/util/pandora_db.pl | 3 - 6 files changed, 254 insertions(+), 96 deletions(-) diff --git a/pandora_console/godmode/setup/setup_ITSM.php b/pandora_console/godmode/setup/setup_ITSM.php index cbdb34b572..d71b3b984c 100644 --- a/pandora_console/godmode/setup/setup_ITSM.php +++ b/pandora_console/godmode/setup/setup_ITSM.php @@ -28,7 +28,7 @@ use PandoraFMS\ITSM\ITSM; - // Load globals. +// Load globals. global $config; check_login(); @@ -54,6 +54,17 @@ try { $priority_values = $ITSM->getPriorities(); $status_values = $ITSM->getStatus(); $object_types_values = $ITSM->getObjectypes(); + if ((bool) get_parameter('update_config', 0) === true) { + $set_config_inventories = $ITSM->createNode( + [ + 'serverAuth' => $config['server_unique_identifier'], + 'agentsForExecution' => $config['ITSM_agents_sync'], + 'path' => $config['ITSM_public_url'], + 'label' => array_keys(servers_get_names())[0], + 'nodeId' => $config['metaconsole_node_id'], + ] + ); + } } catch (\Throwable $th) { $error = $th->getMessage(); $has_connection = false; @@ -103,7 +114,10 @@ $table_remote->data['ITSM_user_level_conf'] = $row; // ITSM hostname. $row = []; $row['hostname'] = html_print_label_input_block( - __('URL to Pandora ITSM setup').ui_print_help_tip(__('Full URL to your Pandora ITSM setup (e.g., http://192.168.1.20/integria/api/v1).'), true), + __('URL to Pandora ITSM setup').ui_print_help_tip( + __('Full URL to your Pandora ITSM setup (e.g., http://192.168.1.20/integria/api/v1).'), + true + ), html_print_input_text( 'ITSM_hostname', $config['ITSM_hostname'], @@ -131,8 +145,47 @@ $row['password'] = html_print_label_input_block( $table_remote->data['ITSM_token'] = $row; $row = []; -$row['control'] = __('Inventory'); -$row['control'] .= html_print_button( +$itsm_public_url = $config['ITSM_public_url']; +if (empty($itsm_public_url) === true) { + $itsm_public_url = $config['homeurl']; + if (isset($config['public_url']) === true && empty($config['public_url']) === false) { + $itsm_public_url = $config['public_url']; + } +} + +$row['publicUrl'] = html_print_label_input_block( + __('URL conect to API %s', get_product_name()).ui_print_help_tip( + __('Full URL to your Pandora (e.g., http://192.168.1.20).'), + true + ), + html_print_input_text( + 'ITSM_public_url', + $itsm_public_url, + '', + 30, + 100, + true + ) +); + +$row['agentsSync'] = html_print_label_input_block( + __('Number Agents to synchronize').ui_print_help_tip( + __('Number of agents that will synchronize at the same time, minimum 10 max 1000'), + true + ), + html_print_input_number( + [ + 'name' => 'ITSM_agents_sync', + 'min' => 10, + 'max' => 1000, + 'value' => ($config['ITSM_agents_sync'] ?? 20), + ] + ) +); + +/* + $row['control'] = __('Inventory'); + $row['control'] .= html_print_button( __('Sync inventory'), 'sync-inventory', false, @@ -142,12 +195,53 @@ $row['control'] .= html_print_button( 'mode' => 'secondary mini', ], true -); -$row['control'] .= ''; -$row['control'] .= ''; -$row['control'] .= ''; + ); + $row['control'] .= ''; + $row['control'] .= ''; + $row['control'] .= ''; +*/ + $table_remote->data['ITSM_sync_inventory'] = $row; +// Test. +$row = []; +$button_test = html_print_button( + __('Test'), + 'ITSM', + false, + '', + [ + 'icon' => 'cog', + 'mode' => 'secondary mini', + ], + true +); +$button_test .= ''; +$button_test .= ''; +$button_test .= ''; +$button_test .= ' '; + +$row['control'] = html_print_label_input_block( + __('Test connection'), + $button_test +); +$table_remote->data['ITSM_test'] = $row; + // Alert settings. $table_alert_settings = new StdClass(); $table_alert_settings->data = []; @@ -402,26 +496,6 @@ $table_cr_settings->data[4][0] = html_print_label_input_block( ) ); -// Test. -$row = []; -$row['control'] = __('Test connection'); -$row['control'] .= html_print_button( - __('Test'), - 'ITSM', - false, - '', - [ - 'icon' => 'cog', - 'mode' => 'secondary mini', - ], - true -); -$row['control'] .= ''; -$row['control'] .= ''; -$row['control'] .= ''; -$row['control'] .= ' '; -$table_remote->data['ITSM_test'] = $row; - // Print. echo '
'; echo ''; @@ -458,7 +532,7 @@ html_print_table($table_remote); echo ''; echo '
'; -if ($has_connection != false) { +if ($has_connection !== false) { // Form alert default settings. echo '
'; echo '
'; diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 7e22befbee..9dfe1c0801 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -17783,3 +17783,117 @@ function api_token_check(string $token) return db_get_value('id_user', 'tusuario', 'api_token', $token); } } + + +/** + * Extract info Agents for inventories ITSM. + * + * @return string Json output. + */ +function api_get_itsm_agents($thrash1, $thrash2, $other) +{ + $custom_fields = db_get_all_fields_in_table('tagent_custom_fields'); + if ($custom_fields === false) { + $custom_fields = []; + } + + $count_custom_fields = count($custom_fields); + $custom_field_sql = ''; + $index_name_custom_fields = []; + foreach ($custom_fields as $key => $field) { + $index_name_custom_fields[$field['name']] = $field; + if ($key !== $count_custom_fields) { + $custom_field_sql .= ', '; + } + + $custom_field_sql .= sprintf( + 'MAX(CASE WHEN tagent_custom_fields.name = "%s" THEN tagent_custom_data.description END) AS "%s"', + $field['name'], + $field['name'] + ); + } + + $where = '1=1'; + $limit = ''; + if (empty($other['data']) === false) { + // Current idAgent. + if (isset($other['data'][0]) === true && empty($other['data'][0]) === false) { + $where = sprintf(' tagente.id_agente > %d', $other['data'][0]); + } + + // Offset + if (isset($other['data'][1]) === true && empty($other['data'][1]) === false) { + $limit = sprintf(' LIMIT %d OFFSET %d', $other['data'][1], 0); + } + } + + $sql = sprintf( + 'SELECT tagente.alias, + tagente.id_agente AS "ID Agent", + tagente.os_version AS "OS Version", + tagente.direccion AS "IP Address", + tagente.url_address AS "URL Address", + tgrupo.nombre AS "Group", + tconfig_os.name AS "OS" + %s + FROM tagente + LEFT JOIN tagent_custom_data + ON tagent_custom_data.id_agent = tagente.id_agente + LEFT JOIN tagent_custom_fields + ON tagent_custom_data.id_field = tagent_custom_fields.id_field + INNER JOIN tgrupo + ON tgrupo.id_grupo = tagente.id_grupo + INNER JOIN tconfig_os + ON tconfig_os.id_os = tagente.id_os + WHERE %s + GROUP BY tagente.id_agente + ORDER BY tagente.id_agente + %s', + $custom_field_sql, + $where, + $limit + ); + + $data = db_get_all_rows_sql($sql); + if ($data === false) { + $data = []; + } + + $result = []; + foreach ($data as $key => $agent_fields) { + foreach ($agent_fields as $name_field => $value_field) { + $type = 'text'; + if (isset($index_name_custom_fields[$name_field]) === true) { + if ($index_name_custom_fields[$name_field]['is_password_type']) { + $type = 'password'; + } else if ($index_name_custom_fields[$name_field]['is_link_enabled']) { + $type = 'link'; + } + } + + $result[$agent_fields['ID Agent']][$name_field] = [ + 'data' => $value_field, + 'type' => $type, + ]; + } + } + + returnData('json', $result); +} + + +/** + * Extract info Agents for inventories ITSM. + * + * @return string Json output. + */ +function api_get_itsm_count_agents() +{ + $sql = 'SELECT COUNT(tagente.id_agente) FROM tagente'; + $result = db_get_value_sql($sql); + if ($result === false) { + $result = 0; + } + + returnData('json', (int) $result); +} diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index a33b3e50ae..de796b30ca 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -1905,7 +1905,17 @@ function config_update_config() } if (config_update_value('ITSM_hostname', $ITSM_hostname, true) === false) { - $error_update[] = __('Pandora iTSM API hostname'); + $error_update[] = __('Pandora ITSM API hostname'); + } + + $ITSM_public_url = (string) get_parameter('ITSM_public_url', $config['ITSM_public_url']); + if (config_update_value('ITSM_public_url', $ITSM_public_url, true) === false) { + $error_update[] = __('Pandora ITSM API public url'); + } + + $ITSM_agents_sync = (int) get_parameter('ITSM_agents_sync', $config['ITSM_agents_sync']); + if (config_update_value('ITSM_agents_sync', $ITSM_agents_sync, true) === false) { + $error_update[] = __('Pandora ITSM API agents sync'); } $incident_default_group = (int) get_parameter('default_group', $config['default_group']); @@ -3818,6 +3828,14 @@ function config_process_config() config_update_value('ITSM_hostname', ''); } + if (!isset($config['ITSM_public_url'])) { + config_update_value('ITSM_public_url', ''); + } + + if (!isset($config['ITSM_agents_sync'])) { + config_update_value('ITSM_agents_sync', 20); + } + // Module Library. if (!isset($config['module_library_user'])) { config_update_value('module_library_user', ''); diff --git a/pandora_console/include/lib/ITSM/ITSM.php b/pandora_console/include/lib/ITSM/ITSM.php index c718a93268..50f0c621cd 100644 --- a/pandora_console/include/lib/ITSM/ITSM.php +++ b/pandora_console/include/lib/ITSM/ITSM.php @@ -261,6 +261,10 @@ class ITSM $path = '/inventory/'.$id; break; + case 'createNode': + $path = '/pandorafms/nodes'; + break; + default: // Not posible. break; @@ -467,7 +471,7 @@ class ITSM * Get table incicidences for agent. * * @param integer $idAgent Id agent. - * @param boolean|null $mini visual mode mini. + * @param boolean|null $mini Visual mode mini. * @param integer|null $blocked Blocked. * * @return string Html output. @@ -535,4 +539,17 @@ class ITSM } + /** + * Create Node in pandora ITSM. + * + * @param array $data Info connect to node from ITSM. + * + * @return boolean + */ + public function createNode(array $data): array + { + return $this->callApi('createNode', null, $data); + } + + } diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 199b84482e..b182914d81 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -265,7 +265,6 @@ our @EXPORT = qw( pandora_self_monitoring pandora_thread_monitoring pandora_process_policy_queue - pandora_sync_agents_integria subst_alert_macros subst_column_macros locate_agent @@ -1979,12 +1978,6 @@ sub pandora_execute_action ($$$$$$$$$;$$) { 'createWu' => $action->{'create_wu_integria'} ); - my $test = pandora_prepare_info_object_inventory_itsm($dbh); - # TODO: change to logger. - use Data::Dumper; - $Data::Dumper::SortKeys = 1; - print Dumper($test); - my $response = pandora_API_ITSM_call($pa_config, 'post', $ITSM_path . '/pandorafms/alert', $ITSM_token, \%dataSend); if (!defined($response)){ return; @@ -3930,32 +3923,6 @@ sub pandora_get_custom_field_for_itsm ($$) { return \%result; } -sub pandora_prepare_info_object_inventory_itsm ($) { - my ($dbh) = @_; - my $custom_fields = pandora_get_custom_fields($dbh); - - my $sql = 'SELECT tagente.alias, tagente.id_agente AS "ID Agent", tagente.os_version AS "OS Version", tagente.direccion AS "IP Address",'; - $sql .= ' tagente.url_address AS "URL Address", tgrupo.nombre AS "Group", tconfig_os.name AS "OS",'; - - foreach my $field (@{$custom_fields}) { - $sql .= ' case when tagent_custom_fields.name = "'. $field->{'name'} . '" then tagent_custom_data.description end AS "'. $field->{'name'} . '",'; - } - - $sql = substr($sql, 0, -1); - - $sql .= ' FROM tagent_custom_fields'; - $sql .= ' INNER JOIN tagent_custom_data ON tagent_custom_data.id_field = tagent_custom_fields.id_field'; - $sql .= ' RIGHT JOIN tagente ON tagent_custom_data.id_agent = tagente.id_agente'; - $sql .= ' INNER JOIN tgrupo ON tgrupo.id_grupo = tagente.id_grupo'; - $sql .= ' INNER JOIN tconfig_os ON tconfig_os.id_os = tagente.id_os'; - $sql .= ' GROUP BY tagente.id_agente'; - $sql .= ' ORDER BY tagente.id_agente'; - - my @result = get_db_rows($dbh, $sql); - - return \@result; -} - ########################################################################## ## Update a custom field from agent of tagent_custom_data ########################################################################## @@ -7097,35 +7064,6 @@ sub pandora_API_ITSM_call ($$$$$) { } } -sub pandora_sync_agents_integria ($$) { - my ($pa_config, $dbh) = @_; - - my $config_ITSM_enabled = pandora_get_tconfig_token ($dbh, 'ITSM_enabled', ''); - - if (!$config_ITSM_enabled) { - return; - } - - my $ITSM_path = pandora_get_tconfig_token ($dbh, 'ITSM_hostname', ''); - my $ITSM_token = pandora_get_tconfig_token ($dbh, 'ITSM_token', ''); - - - my %dataAgents = ( - 'agents' => pandora_prepare_info_object_inventory_itsm($dbh), - 'idNode' => pandora_get_tconfig_token($dbh, 'metaconsole_node_id', 0) - ); - - # TODO: change to logger. - #use Data::Dumper; - #$Data::Dumper::SortKeys = 1; - #print Dumper(\%dataAgents); - - my $response = pandora_API_ITSM_call($pa_config, 'post', $ITSM_path . '/pandorafms/agents', $ITSM_token, \%dataAgents); - if (!defined($response)){ - return; - } -} - ########################################################################## =head2 C<< pandora_input_password (I<$pa_config>, I<$password>) >> diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 213588c074..c883ee1f23 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -1396,9 +1396,6 @@ if (defined($history_dbh)) { } -# Keep integrity between PandoraFMS agents and Pandora ITSM inventory objects. -#pandora_sync_agents_integria($dbh); - # Cleanup and exit db_disconnect ($history_dbh) if defined ($history_dbh); db_disconnect ($dbh);