diff --git a/pandora_console/godmode/reporting/reporting_builder.php b/pandora_console/godmode/reporting/reporting_builder.php index e93f87a672..edf4df6a40 100755 --- a/pandora_console/godmode/reporting/reporting_builder.php +++ b/pandora_console/godmode/reporting/reporting_builder.php @@ -2223,15 +2223,7 @@ switch ($action) { 'historical_db_check' ); $values['top_n_value'] = get_parameter('max_items'); - - if ($values['type'] === 'sql_graph_hbar' - || ($values['type'] === 'sql_graph_vbar') - || ($values['type'] === 'sql_graph_pie') - ) { - $values['server_name'] = get_parameter('combo_server_sql'); - } else { - $values['server_name'] = get_parameter('combo_server'); - } + $values['server_name'] = get_parameter('combo_server_sql'); if ($sql !== '') { if ($values['server_name'] === 'all') { @@ -3000,15 +2992,8 @@ switch ($action) { 'historical_db_check' ); $values['top_n_value'] = get_parameter('max_items'); + $values['server_name'] = get_parameter('combo_server_sql'); - if ($values['type'] === 'sql_graph_hbar' - || ($values['type'] === 'sql_graph_vbar') - || ($values['type'] === 'sql_graph_pie') - ) { - $values['server_name'] = get_parameter('combo_server_sql'); - } else { - $values['server_name'] = get_parameter('combo_server'); - } if ($sql !== '') { if ($values['server_name'] === 'all') { diff --git a/pandora_console/godmode/setup/performance.php b/pandora_console/godmode/setup/performance.php index c4ea68c974..0cd6c53d2d 100644 --- a/pandora_console/godmode/setup/performance.php +++ b/pandora_console/godmode/setup/performance.php @@ -821,6 +821,23 @@ $table_other->data[8][0] = html_print_label_input_block( ) ); +$limit_sql_pdf_tip = ui_print_help_tip( + __('Before increasing this value, be aware that a large number can affect performance in PDF generation. Set to 0 to disregard this limit.'), + true +); + +$table_other->data[8][1] = html_print_label_input_block( + __('Rows limit for SQL report item PDF').$limit_sql_pdf_tip, + html_print_input_text( + 'limit_sql_pdf', + $config['limit_sql_pdf'], + '', + false, + 15, + true + ) +); + // Agent Wizard defaults. $defaultAgentWizardOptions = json_decode(io_safe_output($config['agent_wizard_defaults'])); $tableSnmpWizard = new stdClass(); diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index d9196d5967..18ddd0ffb0 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -952,6 +952,10 @@ function config_update_config() $error_update[] = __('Max execution event response'); } + if (config_update_value('limit_sql_pdf', get_parameter('limit_sql_pdf'), true) === false) { + $error_update[] = __('Rows limit for SQL report item PDF'); + } + if (config_update_value('row_limit_csv', get_parameter('row_limit_csv'), true) === false) { $error_update[] = __('Row limit in csv log'); } @@ -2209,6 +2213,10 @@ function config_process_config() config_update_value('max_execution_event_response', 10); } + if (!isset($config['limit_sql_pdf'])) { + config_update_value('limit_sql_pdf', 5000); + } + if (!isset($config['max_number_of_events_per_node'])) { config_update_value('max_number_of_events_per_node', 100000); } diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index aba777f8fb..9d8c7bcaa0 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -452,7 +452,8 @@ function reporting_make_reporting_data( case 'sql': $report['contents'][] = reporting_sql( $report, - $content + $content, + $pdf ); break; @@ -7541,7 +7542,7 @@ function reporting_text($report, $content) * * @return array */ -function reporting_sql($report, $content) +function reporting_sql($report, $content, $pdf=false) { global $config; @@ -7570,10 +7571,10 @@ function reporting_sql($report, $content) if (is_metaconsole() === true && $content['server_name'] === 'all') { $sync = new Synchronizer(); $results = $sync->apply( - function ($node) use ($report, $content) { + function ($node) use ($report, $content, $pdf) { try { $node->connect(); - $rs = reporting_sql_auxiliary($report, $content); + $rs = reporting_sql_auxiliary($report, $content, $pdf); $node->disconnect(); } catch (Exception $e) { return [ @@ -7623,7 +7624,7 @@ function reporting_sql($report, $content) $node->connect(); } - $query_result = reporting_sql_auxiliary($report, $content); + $query_result = reporting_sql_auxiliary($report, $content, $pdf); $return = array_merge($return, $query_result); if (is_metaconsole() === true && $id_server > 0) { @@ -7648,8 +7649,10 @@ function reporting_sql($report, $content) * * @return array */ -function reporting_sql_auxiliary($report, $content) +function reporting_sql_auxiliary($report, $content, $pdf=false) { + global $config; + if ($content['treport_custom_sql_id'] != 0) { $sql = io_safe_output( db_get_value_filter( @@ -7662,6 +7665,46 @@ function reporting_sql_auxiliary($report, $content) $sql = $content['external_source']; } + if ($pdf === true && isset($config['limit_sql_pdf']) === true && $config['limit_sql_pdf'] > 0) { + $pattern_limit_offset = '/LIMIT\s+(\d+)(?:\s*,\s*(\d+))?/i'; + + if (preg_match($pattern_limit_offset, $sql, $matches_limit_offset)) { + // Item query contains a LIMIT clause. + $limit1 = (int) $matches_limit_offset[1]; + + if (isset($matches_limit_offset[2]) === true && $matches_limit_offset[2] !== '') { + // The LIMIT clause has a second limit value in the form of LIMIT X, Y. + $limit2 = (int) $matches_limit_offset[2]; + + if ($config['limit_sql_pdf'] < $limit2) { + // Overwrite the second limit value only if $config['limit_sql_pdf'] is less than the original limit. + $new_limit2 = $config['limit_sql_pdf']; + $sql = preg_replace($pattern_limit_offset, " LIMIT $limit1, $new_limit2", $sql); + } + } else { + // The LIMIT clause is a simple LIMIT in the form of LIMIT X. + if ($config['limit_sql_pdf'] < $limit1) { + // Overwrite the limit value only if $config['limit_sql_pdf'] is less than the original limit. + $new_limit1 = $config['limit_sql_pdf']; + $sql = preg_replace($pattern_limit_offset, " LIMIT $new_limit1", $sql); + } + } + } else { + $limit_str = ' LIMIT '.$config['limit_sql_pdf']; + + // Check if SQL ends with semicolon or "\G". + if (substr(trim($sql), -1) === ';') { + $sql = rtrim($sql, ';'); + $sql .= $limit_str.';'; + } else if (substr(trim($sql), -2) === '\\G') { + $sql = rtrim($sql, '\G'); + $sql .= $limit_str.'\G'; + } else { + $sql .= $limit_str; + } + } + } + // Check if SQL macro exists. $sql = reporting_sql_macro($report, $sql); @@ -7692,6 +7735,7 @@ function reporting_sql_auxiliary($report, $content) } $result = db_get_all_rows_sql($sql, $historical_db); + if ($result !== false) { foreach ($result as $row) { $data_row = []; @@ -7714,7 +7758,7 @@ function reporting_sql_auxiliary($report, $content) } } else { $return['correct'] = 0; - $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.'); + $return['error'] = __('Illegal query: Due to security restrictions, there are some tokens or words you cannot use: *, delete, drop, alter, modify, password, pass, insert or update.'); } return $return;