From 55347713516a40fdf30bd36ce97acce723b4f657 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Tue, 14 Feb 2023 10:24:08 +0100 Subject: [PATCH 1/2] added all nodes option in sql query report item in meta --- .../reporting_builder.item_editor.php | 26 ++++- .../include/functions_reporting.php | 109 ++++++++++++++++-- 2 files changed, 123 insertions(+), 12 deletions(-) diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php index 80e72e65cf..8c66b583ee 100755 --- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php +++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php @@ -1233,6 +1233,29 @@ $class = 'databox filters'; } ?> + 'All nodes'], $servers); + if ($meta) { + ?> + + + + + + + + @@ -6334,6 +6357,7 @@ function chooseType() { $("#row_alert_templates").hide(); $("#row_alert_actions").hide(); $("#row_servers").hide(); + $("#row_servers_all_opt").hide(); $("#row_multiple_servers").hide(); $("#row_sort").hide(); $("#row_date").hide(); @@ -6648,7 +6672,7 @@ function chooseType() { $("#row_header").show(); $("#row_custom").show(); $("#row_custom_example").show(); - $("#row_servers").show(); + $("#row_servers_all_opt").show(); $("#row_historical_db_check").show(); break; diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 637ae59752..c17745da8b 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -68,6 +68,7 @@ define('REPORT_STATUS_IGNORED', 5); // Clases. use PandoraFMS\Enterprise\Metaconsole\Node; +use PandoraFMS\Enterprise\Metaconsole\Synchronizer; use PandoraFMS\Event; use PandoraFMS\Module; @@ -7421,6 +7422,14 @@ function reporting_text($report, $content) } +/** + * Build SQL report item. + * + * @param array $report Report info. + * @param array $content Content info. + * + * @return array + */ function reporting_sql($report, $content) { global $config; @@ -7438,15 +7447,97 @@ function reporting_sql($report, $content) $return['description'] = $content['description']; $return['date'] = reporting_get_date_text(); - if ($config['metaconsole'] && !empty($content['server_name'])) { - $id_meta = metaconsole_get_id_server( + if (is_metaconsole() === true + && empty($content['server_name']) === false + && $content['server_name'] !== 'all' + ) { + $id_server = metaconsole_get_id_server( $content['server_name'] ); - - $server = metaconsole_get_connection_by_id($id_meta); - metaconsole_connect($server); } + if (is_metaconsole() === true && $content['server_name'] === 'all') { + $sync = new Synchronizer(); + $results = $sync->apply( + function ($node) use ($report, $content) { + try { + $node->connect(); + $rs = reporting_sql_auxiliary($report, $content); + $node->disconnect(); + } catch (Exception $e) { + return [ + 'error' => __( + 'Failed to connect to node %s', + $node->server_name() + ), + ]; + } + + if ($rs === false) { + return ['result' => []]; + } + + return ['result' => $rs]; + }, + false + ); + + $data = []; + $return['correct'] = 1; + $return['error'] = ''; + + foreach ($results as $id_node => $items) { + foreach ($items['result']['data'] as $key => $item) { + $items['result']['data'][$key] = ['node_id' => $id_node] + $items['result']['data'][$key]; + } + + if ((int) $items['result']['correct'] !== 1) { + $return['correct'] = 0; + } + + if ($items['result']['error'] !== '') { + $return['error'] = $items['result']['error']; + } + + $return['sql'] = $items['result']['sql']; + + $data = array_merge($data, $items['result']['data']); + } + + $return['data'] = $data; + } else { + try { + if (is_metaconsole() === true && $id_server > 0) { + $node = new Node($id_server); + $node->connect(); + } + + $query_result = reporting_sql_auxiliary($report, $content); + $return = array_merge($return, $query_result); + + if (is_metaconsole() === true && $id_server > 0) { + $node->disconnect(); + } + } catch (\Exception $e) { + if (is_metaconsole() === true && $id_server > 0) { + $node->disconnect(); + } + } + } + + return reporting_check_structure_content($return); +} + + +/** + * Auxiliary function for reporting_sql. + * + * @param array $report Report info. + * @param array $content Content info. + * + * @return array + */ +function reporting_sql_auxiliary($report, $content) { if ($content['treport_custom_sql_id'] != 0) { $sql = io_safe_output( db_get_value_filter( @@ -7459,7 +7550,7 @@ function reporting_sql($report, $content) $sql = $content['external_source']; } - // Check if exist sql macro. + // Check if SQL macro exists. $sql = reporting_sql_macro($report, $sql); // Do a security check on SQL coming from the user. @@ -7514,11 +7605,7 @@ function reporting_sql($report, $content) $return['error'] = __('Illegal query: Due security restrictions, there are some tokens or words you cannot use: *, delete, drop, alter, modify, password, pass, insert or update.'); } - if ($config['metaconsole'] && !empty($content['server_name'])) { - metaconsole_restore_db(); - } - - return reporting_check_structure_content($return); + return $return; } From 25bfc490555f38de1cdada1d66d46d592d638056 Mon Sep 17 00:00:00 2001 From: "alejandro.campos@artica.es" Date: Thu, 23 Feb 2023 18:04:04 +0100 Subject: [PATCH 2/2] minor fix --- .../godmode/reporting/reporting_builder.item_editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php index 8c66b583ee..c6650d08fe 100755 --- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php +++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php @@ -281,7 +281,7 @@ switch ($action) { $server_name = $item['server_name']; // Metaconsole db connection. - if ($meta && empty($server_name) === false) { + if ($meta && empty($server_name) === false && $server_name !== 'all') { $connection = metaconsole_get_connection($server_name); $server_id = $connection['id']; if (metaconsole_load_external_db($connection) != NOERR) {