Merge branch 'ent-9962-14776-Mergear-resultados-de-ITEM-SQL-Query-de-informes-en-META' into 'develop'

added all nodes option in sql query report item in meta

See merge request artica/pandorafms!5535
This commit is contained in:
Daniel Rodriguez 2023-03-01 12:29:54 +00:00
commit 23f91fa0f3
2 changed files with 124 additions and 13 deletions

View File

@ -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) {
@ -1233,6 +1233,29 @@ $class = 'databox filters';
}
?>
<?php
$servers_all_opt = array_merge(['all' => 'All nodes'], $servers);
if ($meta) {
?>
<tr id="row_servers_all_opt" class="datos">
<td class="bolder"><?php echo __('Server'); ?></td>
<td >
<?php
html_print_select(
$servers_all_opt,
'combo_server',
$server_name,
'',
$nothing,
$nothing_value
);
?>
</td>
</tr>
<?php
}
?>
<?php
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;

View File

@ -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;
}