From 69a07a0ae1f6371b3c10e5d3bd4ebb28b28d7671 Mon Sep 17 00:00:00 2001 From: m-lopez-f Date: Thu, 31 Dec 2015 13:37:56 +0100 Subject: [PATCH] Fixed problems with monitor report. Tiquet: #2848 --- .../include/functions_reporting.php | 147 +++++++++++++++++- .../include/functions_reporting_html.php | 146 ++++++++++++++++- .../operation/reporting/reporting_xml.php | 5 +- 3 files changed, 295 insertions(+), 3 deletions(-) diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 729608203f..6cf894f531 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -1140,6 +1140,151 @@ function reporting_get_agentmodule_sla_array ($id_agent_module, $period = 0, $mi return $data_colors; } + + +/** + * Get Monitor report of a module. + * if (($min_value < $data['datos'])OR ($max_value > 0 AND + ($data['datos'] > $min_value AND $data['datos'] < $max_value))) + * @param int Agent module to calculate monitor report + * @param int Period to check the SLA compliance. + * @param int Minimum data value the module in the right interval + * @param int Maximum data value the module in the right interval. False will + * ignore max value + * @param int Beginning date of the report in UNIX time (current date by default). + * + * @return float Monitor percentage of the requested module. False if no data were + * found + */ +function reporting_get_agentmodule_monitor ($id_agent_module, $period = 0, $min_value = 1, $max_value = false, $date = 0) { + global $config; + + + + if (empty($id_agent_module)) + return false; + + // Set initial conditions + $bad_period = 0; + // Limit date to start searching data + $datelimit = $date - $period; + $search_in_history_db = db_search_in_history_db($datelimit); + + // Initialize variables + if (empty ($date)) { + $date = get_system_time (); + } + if ($daysWeek === null) { + $daysWeek = array(); + } + + + + + // Calculate the SLA for large time without hours + + // Get interval data + $sql = sprintf ('SELECT * + FROM tagente_datos + WHERE id_agente_modulo = %d + AND utimestamp > %d AND utimestamp <= %d', + $id_agent_module, $datelimit, $date); + + $sql .= ' ORDER BY utimestamp ASC'; + $interval_data = db_get_all_rows_sql ($sql, $search_in_history_db); + + if ($interval_data === false) { + $interval_data = array (); + } + + // Calculate planned downtime dates + $downtime_dates = reporting_get_planned_downtimes_intervals( + $id_agent_module, $datelimit, $date); + + // Get previous data + $previous_data = modules_get_previous_data ($id_agent_module, $datelimit); + + if ($previous_data !== false) { + $previous_data['utimestamp'] = $datelimit; + array_unshift ($interval_data, $previous_data); + } + + // Get next data + $next_data = modules_get_next_data ($id_agent_module, $date); + + if ($next_data !== false) { + $next_data['utimestamp'] = $date; + array_push ($interval_data, $next_data); + } + else if (count ($interval_data) > 0) { + // Propagate the last known data to the end of the interval + $next_data = array_pop ($interval_data); + array_push ($interval_data, $next_data); + $next_data['utimestamp'] = $date; + array_push ($interval_data, $next_data); + } + + if (count ($interval_data) < 2) { + return false; + } + + + $first_data = array_shift ($interval_data); + + // Do not count the empty start of an interval as 0 + if ($first_data['utimestamp'] != $datelimit) { + $period = $date - $first_data['utimestamp']; + } + + $previous_utimestamp = $first_data['utimestamp']; + if (($max_value > 0) AND ($data['datos'] > $min_value + AND $data['datos'] < $max_value)) { + + $previous_status = 1; + foreach ($downtime_dates as $date_dt) { + + if (($date_dt['date_from'] <= $previous_utimestamp) AND + ($date_dt['date_to'] >= $previous_utimestamp)) { + + $previous_status = 0; + } + } + } + else { + $previous_status = 0; + } + + foreach ($interval_data as $data) { + // Previous status was critical + if ($previous_status == 1) { + $bad_period += $data['utimestamp'] - $previous_utimestamp; + } + + if (array_key_exists('datos', $data)) { + // Re-calculate previous status for the next data + if (($max_value > 0) AND ($data['datos'] > $min_value + AND $data['datos'] < $max_value)) { + + $previous_status = 1; + foreach ($downtime_dates as $date_dt) { + if (($date_dt['date_from'] <= $data['utimestamp']) AND ($date_dt['date_to'] >= $data['utimestamp'])) { + $previous_status = 0; + } + } + } + else { + $previous_status = 0; + } + } + + $previous_utimestamp = $data['utimestamp']; + } + + // Return the percentage of SLA compliance + return (float) (100 - ($bad_period / $period) * 100); +} + + /** * Get the time intervals where an agentmodule is affected by the planned downtimes. * @@ -4623,7 +4768,7 @@ function reporting_render_report_html_item ($content, $table, $report, $mini = f } $module = modules_get_agentmodule ($content['id_agent_module']); $data = array (); - $monitor_value = reporting_get_agentmodule_sla ($content['id_agent_module'], $content['period'], + $monitor_value = reporting_get_agentmodule_monitor ($content['id_agent_module'], $content['period'], $module['min_critical'],$module['max_critical'], $report["datetime"]); if ($monitor_value === false) { $monitor_value = __('Unknown'); diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index 4fa1d921ce..656fa7aa99 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -889,6 +889,148 @@ function reporting_get_agentmodule_sla_array ($id_agent_module, $period = 0, $mi return $data_colors; } +/** + * Get Monitor report of a module. + * if (($min_value < $data['datos'])OR ($max_value > 0 AND + ($data['datos'] > $min_value AND $data['datos'] < $max_value))) + * @param int Agent module to calculate monitor report + * @param int Period to check the SLA compliance. + * @param int Minimum data value the module in the right interval + * @param int Maximum data value the module in the right interval. False will + * ignore max value + * @param int Beginning date of the report in UNIX time (current date by default). + * + * @return float Monitor percentage of the requested module. False if no data were + * found + */ +function reporting_get_agentmodule_monitor ($id_agent_module, $period = 0, $min_value = 1, $max_value = false, $date = 0) { + global $config; + + + + if (empty($id_agent_module)) + return false; + + // Set initial conditions + $bad_period = 0; + // Limit date to start searching data + $datelimit = $date - $period; + $search_in_history_db = db_search_in_history_db($datelimit); + + // Initialize variables + if (empty ($date)) { + $date = get_system_time (); + } + if ($daysWeek === null) { + $daysWeek = array(); + } + + + + + // Calculate the SLA for large time without hours + + // Get interval data + $sql = sprintf ('SELECT * + FROM tagente_datos + WHERE id_agente_modulo = %d + AND utimestamp > %d AND utimestamp <= %d', + $id_agent_module, $datelimit, $date); + + $sql .= ' ORDER BY utimestamp ASC'; + $interval_data = db_get_all_rows_sql ($sql, $search_in_history_db); + + if ($interval_data === false) { + $interval_data = array (); + } + + // Calculate planned downtime dates + $downtime_dates = reporting_get_planned_downtimes_intervals( + $id_agent_module, $datelimit, $date); + + // Get previous data + $previous_data = modules_get_previous_data ($id_agent_module, $datelimit); + + if ($previous_data !== false) { + $previous_data['utimestamp'] = $datelimit; + array_unshift ($interval_data, $previous_data); + } + + // Get next data + $next_data = modules_get_next_data ($id_agent_module, $date); + + if ($next_data !== false) { + $next_data['utimestamp'] = $date; + array_push ($interval_data, $next_data); + } + else if (count ($interval_data) > 0) { + // Propagate the last known data to the end of the interval + $next_data = array_pop ($interval_data); + array_push ($interval_data, $next_data); + $next_data['utimestamp'] = $date; + array_push ($interval_data, $next_data); + } + + if (count ($interval_data) < 2) { + return false; + } + + + $first_data = array_shift ($interval_data); + + // Do not count the empty start of an interval as 0 + if ($first_data['utimestamp'] != $datelimit) { + $period = $date - $first_data['utimestamp']; + } + + $previous_utimestamp = $first_data['utimestamp']; + if (($max_value > 0) AND ($data['datos'] > $min_value + AND $data['datos'] < $max_value)) { + + $previous_status = 1; + foreach ($downtime_dates as $date_dt) { + + if (($date_dt['date_from'] <= $previous_utimestamp) AND + ($date_dt['date_to'] >= $previous_utimestamp)) { + + $previous_status = 0; + } + } + } + else { + $previous_status = 0; + } + + foreach ($interval_data as $data) { + // Previous status was critical + if ($previous_status == 1) { + $bad_period += $data['utimestamp'] - $previous_utimestamp; + } + + if (array_key_exists('datos', $data)) { + // Re-calculate previous status for the next data + if (($max_value > 0) AND ($data['datos'] > $min_value + AND $data['datos'] < $max_value)) { + + $previous_status = 1; + foreach ($downtime_dates as $date_dt) { + if (($date_dt['date_from'] <= $data['utimestamp']) AND ($date_dt['date_to'] >= $data['utimestamp'])) { + $previous_status = 0; + } + } + } + else { + $previous_status = 0; + } + } + + $previous_utimestamp = $data['utimestamp']; + } + + // Return the percentage of SLA compliance + return (float) (100 - ($bad_period / $period) * 100); +} + /** * Get the time intervals where an agentmodule is affected by the planned downtimes. * @@ -4349,7 +4491,9 @@ function reporting_render_report_html_item ($content, $table, $report, $mini = f } $data = array (); - $monitor_value = reporting_get_agentmodule_sla ($content['id_agent_module'], $content['period'], 1, false, $report["datetime"]); + $module = modules_get_agentmodule ($content['id_agent_module']); + $monitor_value = reporting_get_agentmodule_monitor ($content['id_agent_module'], $content['period'], + $module['min_critical'],$module['max_critical'], $report["datetime"]); if ($monitor_value === false) { $monitor_value = __('Unknown'); } diff --git a/pandora_console/operation/reporting/reporting_xml.php b/pandora_console/operation/reporting/reporting_xml.php index bcfdaf1742..6bfcc6389b 100644 --- a/pandora_console/operation/reporting/reporting_xml.php +++ b/pandora_console/operation/reporting/reporting_xml.php @@ -517,7 +517,10 @@ foreach ($contents as $content) { case 6: case 'monitor_report': $data["title"] = __('Monitor report'); - $monitor_value = reporting_get_agentmodule_sla ($content['id_agent_module'], $content['period'], 1, false, $datetime); + + $module = modules_get_agentmodule ($content['id_agent_module']); + $monitor_value = reporting_get_agentmodule_monitor ($content['id_agent_module'], $content['period'], + $module['min_critical'],$module['max_critical'], $datetime); if ($monitor_value === false) { $monitor_value = __('Unknown'); }