From ecfa71b43c1124933df1b265e34a4d7942a55521 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 25 Aug 2023 14:15:32 +0200 Subject: [PATCH] ITSM pandora_enterprise#11471 --- .../godmode/agentes/agent_incidents.php | 56 +---------- .../godmode/agentes/configurar_agente.php | 15 +-- .../godmode/alerts/configure_alert_action.php | 5 +- pandora_console/include/functions_config.php | 18 ++-- pandora_console/include/lib/ITSM/ITSM.php | 99 +++++++++++++++++++ pandora_console/include/lib/ITSM/Manager.php | 38 +++++++ .../agentes/estado_generalagente.php | 42 ++++++++ .../operation/agentes/ver_agente.php | 32 ++++-- .../views/ITSM/ITSMTicketDetailView.php | 4 +- pandora_server/lib/PandoraFMS/Core.pm | 85 ++++++++-------- pandora_server/util/pandora_db.pl | 2 +- 11 files changed, 266 insertions(+), 130 deletions(-) diff --git a/pandora_console/godmode/agentes/agent_incidents.php b/pandora_console/godmode/agentes/agent_incidents.php index 29442bf227..6b6efee2ae 100644 --- a/pandora_console/godmode/agentes/agent_incidents.php +++ b/pandora_console/godmode/agentes/agent_incidents.php @@ -26,6 +26,8 @@ * ============================================================================ */ +use PandoraFMS\ITSM\ITSM; + global $config; check_login(); @@ -44,59 +46,9 @@ if (! check_acl($config['id_user'], $id_grupo, 'AW', $id_agente)) { return; } -\ui_require_css_file('pandoraitsm'); -\ui_require_javascript_file('ITSM'); - -$agent = db_get_row('tagente', 'id_agente', $id_agente, false, false); - try { - $columns = [ - 'idIncidence', - 'title', - 'groupCompany', - 'statusResolution', - 'priority', - 'updateDate', - 'startDate', - 'idCreator', - 'owner', - ]; - - $column_names = [ - __('ID'), - __('Title'), - __('Group').'/'.__('Company'), - __('Status').'/'.__('Resolution'), - __('Priority'), - __('Updated'), - __('Started'), - __('Creator'), - __('Owner'), - ]; - - ui_print_datatable( - [ - 'id' => 'itms_list_tickets', - 'class' => 'info_table', - 'style' => 'width: 99%', - 'columns' => $columns, - 'column_names' => $column_names, - 'ajax_url' => 'operation/ITSM/itsm', - 'ajax_data' => [ - 'method' => 'getListTickets', - 'externalIdLike' => $config['metaconsole_node_id'].'-'.$agent['id_agente'], - ], - 'no_sortable_columns' => [ - 2, - 3, - -1, - ], - 'order' => [ - 'field' => 'updateDate', - 'direction' => 'desc', - ], - ] - ); + $ITSM = new ITSM(); + echo $ITSM->getTableIncidencesForAgent($id_agente); } catch (Exception $e) { echo $e->getMessage(); } diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index 6348dd07b0..826aca12f4 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -39,6 +39,7 @@ ui_require_javascript_file('encode_decode_base64'); ui_require_css_file('agent_manager'); use PandoraFMS\Event; +use PandoraFMS\ITSM\ITSM; check_login(); @@ -605,20 +606,6 @@ if ($id_agente) { $agent_wizard['active'] = false; } - // Incident tab. - if ((bool) $config['ITSM_enabled'] === true) { - $incidenttab['text'] = html_print_menu_button( - [ - 'href' => 'index.php?sec=gagente&sec2=godmode/agentes/configurar_agente&tab=incident&id_agente='.$id_agente, - 'image' => 'images/logs@svg.svg', - 'title' => __('Incidents'), - ], - true - ); - - $incidenttab['active'] = ($tab === 'incident'); - } - if (check_acl_one_of_groups($config['id_user'], $all_groups, 'AW') === true) { if ($has_remote_conf !== false) { $agent_name = agents_get_name($id_agente); diff --git a/pandora_console/godmode/alerts/configure_alert_action.php b/pandora_console/godmode/alerts/configure_alert_action.php index 4f87289255..4883467c10 100644 --- a/pandora_console/godmode/alerts/configure_alert_action.php +++ b/pandora_console/godmode/alerts/configure_alert_action.php @@ -800,12 +800,13 @@ $(document).ready (function () { old_recovery_value = ''; } } else if (i === 7) { + var text = ''; if(!old_value) { - old_value = ''; + old_value = text; } if(!old_recovery_value) { - old_recovery_value = ''; + old_recovery_value = text; } } } diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index 2aec7dd6bd..6f2aca4b9e 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -1979,15 +1979,15 @@ function config_update_config() $text_incident_content = (string) get_parameter('incident_content', $config['incident_content']); if (empty($text_incident_content) === true) { $text_incident_content = sprintf( - 'Hello, - - A new ticket has been created due a problem in monitoring. - - Agent : _agent_ - Module: _module_ - - Regards, - Your %s server.', + 'Hello, %s %s A new ticket has been created due a problem in monitoring. %s %s Agent : _agent_ %s Module: _module_ %s %s Regards, %s Your %s server.', + PHP_EOL, + PHP_EOL, + PHP_EOL, + PHP_EOL, + PHP_EOL, + PHP_EOL, + PHP_EOL, + PHP_EOL, get_product_name() ); } diff --git a/pandora_console/include/lib/ITSM/ITSM.php b/pandora_console/include/lib/ITSM/ITSM.php index 52fee63dc4..c718a93268 100644 --- a/pandora_console/include/lib/ITSM/ITSM.php +++ b/pandora_console/include/lib/ITSM/ITSM.php @@ -257,6 +257,10 @@ class ITSM $path = '/customSearch/'.$id; break; + case 'inventory': + $path = '/inventory/'.$id; + break; + default: // Not posible. break; @@ -436,4 +440,99 @@ class ITSM } + /** + * List incidences. + * + * @param integer $idAgent Agent id. + * + * @return array list Incidences. + */ + public function listIncidenceAgents(int $idAgent): array + { + global $config; + $listIncidences = $this->callApi( + 'listTickets', + [ + 'page' => 0, + 'sizePage' => 0, + ], + ['externalIdLike' => $config['metaconsole_node_id'].'-'.$idAgent] + ); + + return $listIncidences['data']; + } + + + /** + * Get table incicidences for agent. + * + * @param integer $idAgent Id agent. + * @param boolean|null $mini visual mode mini. + * @param integer|null $blocked Blocked. + * + * @return string Html output. + */ + public function getTableIncidencesForAgent(int $idAgent, ?bool $mini=false, ?int $blocked=null) + { + \ui_require_css_file('pandoraitsm'); + \ui_require_javascript_file('ITSM'); + + global $config; + $columns = [ + 'idIncidence', + 'title', + 'groupCompany', + 'statusResolution', + 'priority', + 'updateDate', + 'startDate', + 'idCreator', + 'owner', + ]; + + $column_names = [ + __('ID'), + __('Title'), + __('Group').'/'.__('Company'), + __('Status').'/'.__('Resolution'), + __('Priority'), + __('Updated'), + __('Started'), + __('Creator'), + __('Owner'), + ]; + + $options = [ + 'id' => 'itms_list_tickets', + 'class' => 'info_table', + 'style' => 'width: 99%', + 'columns' => $columns, + 'column_names' => $column_names, + 'ajax_url' => 'operation/ITSM/itsm', + 'ajax_data' => [ + 'method' => 'getListTickets', + 'externalIdLike' => $config['metaconsole_node_id'].'-'.$idAgent, + 'blocked' => $blocked, + ], + 'no_sortable_columns' => [ + 2, + 3, + -1, + ], + 'order' => [ + 'field' => 'updateDate', + 'direction' => 'desc', + ], + 'return' => true, + ]; + + if ($mini === true) { + $options['csv'] = 0; + $options['dom_elements'] = 'frtip'; + } + + return ui_print_datatable($options); + } + + } diff --git a/pandora_console/include/lib/ITSM/Manager.php b/pandora_console/include/lib/ITSM/Manager.php index 97730665b5..9e08b05910 100644 --- a/pandora_console/include/lib/ITSM/Manager.php +++ b/pandora_console/include/lib/ITSM/Manager.php @@ -325,6 +325,7 @@ class Manager $users = null; $priorities = null; $priorityDiv = null; + $inventories = null; $ITSM = new ITSM(); try { if (empty($idIncidence) === false) { @@ -390,7 +391,16 @@ class Manager $users = $this->getUsers($ITSM, $usersInvolved); + $inventories = []; $priorityDiv = $this->priorityDiv($incidence['priority'], $priorities[$incidence['priority']]); + if (empty($incidence) === false + && isset($incidence['inventories']) === true + && empty($incidence['inventories']) === false + ) { + foreach ($incidence['inventories'] as $inventory) { + $inventories[] = $this->getInventory($ITSM, $inventory['idInventory']); + } + } } } catch (\Throwable $th) { $error = $th->getMessage(); @@ -414,6 +424,7 @@ class Manager 'priorities' => $priorities, 'priorityDiv' => $priorityDiv, 'headerTabs' => $headerTabs, + 'inventories' => $inventories, ] ); } @@ -855,6 +866,28 @@ class Manager } + /** + * Get Inventory. + * + * @param ITSM $ITSM Object for callApi. + * @param integer $idInventory Inventory ID. + * + * @return array Data inventory + */ + private function getInventory(ITSM $ITSM, int $idInventory): array + { + $result = $ITSM->callApi( + 'inventory', + [], + [], + $idInventory, + 'GET' + ); + + return $result; + } + + /** * Get Incidences group by for groups. * @@ -1113,6 +1146,11 @@ class Manager $filters['externalIdLike'] = $externalIdLike; } + $blocked = get_parameter('blocked', null); + if (isset($blocked) === true) { + $filters['blocked'] = $blocked; + } + if (isset($filters['status']) === true && empty($filters['status']) === true) { unset($filters['status']); } diff --git a/pandora_console/operation/agentes/estado_generalagente.php b/pandora_console/operation/agentes/estado_generalagente.php index c0c1fc8582..ab71c2b944 100755 --- a/pandora_console/operation/agentes/estado_generalagente.php +++ b/pandora_console/operation/agentes/estado_generalagente.php @@ -26,6 +26,8 @@ * ============================================================================ */ +use PandoraFMS\ITSM\ITSM; + // Begin. global $config; @@ -944,6 +946,46 @@ if (empty($agentAdditionalInfo) === false) { ); } +if ((bool) $config['ITSM_enabled'] === true) { + $show_tab_issue = false; + try { + $ITSM = new ITSM(); + $list = $ITSM->listIncidenceAgents($id_agente); + if (empty($list) === false) { + $show_tab_issue = true; + } + } catch (\Throwable $th) { + $show_tab_issue = false; + } + + if ($show_tab_issue === true) { + try { + $table_itsm = $ITSM->getTableIncidencesForAgent($id_agente, true, 0); + } catch (Exception $e) { + $table_itsm = $e->getMessage(); + } + + $itsmInfo = ui_toggle( + $table_itsm, + ''.__('Incidences').'', + 'status_monitor_agent', + false, + false, + true, + '', + 'white-box-content', + 'box-flat white_table_graph w100p' + ); + + html_print_div( + [ + 'class' => 'agent_details_line', + 'content' => $itsmInfo, + ] + ); + } +} + if (empty($agentIncidents) === false) { html_print_div( [ diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index 74000850f8..9bc9280b31 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -27,6 +27,7 @@ */ use PandoraFMS\Enterprise\Metaconsole\Node; +use PandoraFMS\ITSM\ITSM; global $config; @@ -1558,16 +1559,29 @@ if ((bool) $config['activate_gis'] === true) { // Incident tab. if ((bool) $config['ITSM_enabled'] === true) { - $incidenttab['text'] = html_print_menu_button( - [ - 'href' => 'index.php?sec=gagente&sec2=operation/agentes/ver_agente&tab=incident&id_agente='.$id_agente, - 'image' => 'images/logs@svg.svg', - 'title' => __('Incidents'), - ], - true - ); + $show_tab_issue = false; + try { + $ITSM = new ITSM(); + $list = $ITSM->listIncidenceAgents($id_agente); + if (empty($list) === false) { + $show_tab_issue = true; + } + } catch (\Throwable $th) { + $show_tab_issue = false; + } - $incidenttab['active'] = ($tab === 'incident'); + if ($show_tab_issue === true) { + $incidenttab['text'] = html_print_menu_button( + [ + 'href' => 'index.php?sec=gagente&sec2=operation/agentes/ver_agente&tab=incident&id_agente='.$id_agente, + 'image' => 'images/logs@svg.svg', + 'title' => __('Incidents'), + ], + true + ); + + $incidenttab['active'] = ($tab === 'incident'); + } } // Url address tab. diff --git a/pandora_console/views/ITSM/ITSMTicketDetailView.php b/pandora_console/views/ITSM/ITSMTicketDetailView.php index 9bebb94709..a6ed7d9b7e 100644 --- a/pandora_console/views/ITSM/ITSMTicketDetailView.php +++ b/pandora_console/views/ITSM/ITSMTicketDetailView.php @@ -52,6 +52,8 @@ ui_print_standard_header( ] ); +hd($inventories); + if (empty($error) === false) { ui_print_error_message($error); } @@ -190,7 +192,7 @@ if (empty($incidence) === true) { // Show description. $description_box = '
'; - $description_box .= $incidence['description']; + $description_box .= str_replace("\r\n", '
', $incidence['description']); $description_box .= '
'; ui_toggle($description_box, __('Description'), '', '', false); diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 2d286816c5..33b45b0ec1 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -1932,7 +1932,8 @@ sub pandora_execute_action ($$$$$$$$$;$$) { 'IP Address' => safe_output($agent->{'direccion'}), 'URL Address' => safe_output($agent->{'url_address'}), 'ID Agent' => $agent->{'id_agente'}, - 'Group' => safe_output(get_db_value($dbh, 'select nombre from tgrupo where id_grupo = ?', $agent->{'id_grupo'})) + 'Group' => safe_output(get_db_value($dbh, 'select nombre from tgrupo where id_grupo = ?', $agent->{'id_grupo'})), + 'OS Version' => $agent->{'os_version'} ); my %dataSend = ( @@ -1948,11 +1949,6 @@ sub pandora_execute_action ($$$$$$$$$;$$) { 'createWu' => $action->{'create_wu_integria'} ); - # TODO: change to logger. - use Data::Dumper; - $Data::Dumper::SortKeys = 1; - print Dumper(\%dataSend); - my $response = pandora_API_ITSM_call($pa_config, 'post', $ITSM_path . '/pandorafms/alert', $ITSM_token, \%dataSend); if (!defined($response)){ return; @@ -3878,6 +3874,32 @@ 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 ########################################################################## @@ -7019,8 +7041,8 @@ sub pandora_API_ITSM_call ($$$$$) { } } -sub pandora_sync_agents_integria ($) { - my ($dbh) = @_; +sub pandora_sync_agents_integria ($$) { + my ($pa_config, $dbh) = @_; my $config_ITSM_enabled = pandora_get_tconfig_token ($dbh, 'ITSM_enabled', ''); @@ -7028,44 +7050,23 @@ sub pandora_sync_agents_integria ($) { return; } - my $config_api_path = pandora_get_tconfig_token ($dbh, 'ITSM_hostname', ''); - my $config_integria_user_pass = pandora_get_tconfig_token ($dbh, 'ITSM_token', ''); + my $ITSM_path = pandora_get_tconfig_token ($dbh, 'ITSM_hostname', ''); + my $ITSM_token = pandora_get_tconfig_token ($dbh, 'ITSM_token', ''); - my $api_path = $config_api_path; - my @agents_string = ''; - my @agents = get_db_rows ($dbh, 'SELECT * FROM tagente'); - - my @agents_array = (); - my $agents_string = ''; + my %dataAgents = ( + 'agents' => pandora_prepare_info_object_inventory_itsm($dbh), + 'idNode' => pandora_get_tconfig_token($dbh, 'metaconsole_node_id', 0) + ); - foreach my $agent (@agents) { - push @agents_array, $agent->{'nombre'} . - "|;|" . - $agent->{'alias'} . - "|;|" . - $agent->{'id_os'} . - "|;|" . - $agent->{'direccion'} . - "|;|" . - $agent->{'id_grupo'}; - } + # TODO: change to logger. + #use Data::Dumper; + #$Data::Dumper::SortKeys = 1; + #print Dumper(\%dataAgents); - my $ua = LWP::UserAgent->new(); - my $response = $ua->post( $api_path, { - 'user_pass' => $config_integria_user_pass, - 'op' => 'sync_pandora_agents_inventory', - 'params[]' => [@agents_array], - 'token' => '|;|' - }); - - my $content = $response->decoded_content(); - - if (defined $content && is_numeric($content) && $content ne "-1") { - return $content; - } - else { - return 0; + my $response = pandora_API_ITSM_call($pa_config, 'post', $ITSM_path . '/pandorafms/agents', $ITSM_token, \%dataAgents); + if (!defined($response)){ + return; } } diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 09574fefb1..f3e40e9e9e 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -1397,7 +1397,7 @@ if (defined($history_dbh)) { } # Keep integrity between PandoraFMS agents and Pandora ITSM inventory objects. -pandora_sync_agents_integria($dbh); +#pandora_sync_agents_integria($dbh); # Cleanup and exit db_disconnect ($history_dbh) if defined ($history_dbh);