From fa39a975182eef8e60726240edf06c748b491c1b Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 27 Oct 2023 10:18:10 +0200 Subject: [PATCH] Module histogram only events pandora_enterprise#12209 --- pandora_console/include/functions_events.php | 52 +++ .../include/functions_reporting.php | 300 ++++++++---------- 2 files changed, 185 insertions(+), 167 deletions(-) diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index f709fe2b4c..78a5a65bd2 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -127,6 +127,58 @@ function events_translate_event_type($event_type) } +/** + * Module status event_type into descriptive text. + * + * @param integer $event_type Event type. + * + * @return string Module status. + */ +function events_status_module_event_type($event_type) +{ + $module_status = ''; + switch ($event_type) { + case 'alert_fired': + case 'alert_recovered': + case 'alert_ceased': + case 'alert_manual_validation': + $module_status = AGENT_MODULE_STATUS_CRITICAL_ALERT; + break; + + case 'going_down_normal': + case 'going_up_normal': + $module_status = AGENT_MODULE_STATUS_NORMAL; + break; + + case 'going_unknown': + case 'unknown': + $module_status = AGENT_MODULE_STATUS_UNKNOWN; + break; + + case 'going_up_warning': + case 'going_down_warning': + $module_status = AGENT_MODULE_STATUS_WARNING; + break; + + case 'going_up_critical': + case 'going_down_critical': + $module_status = AGENT_MODULE_STATUS_CRITICAL_BAD; + break; + + case 'recon_host_detected': + case 'system': + case 'error': + case 'new_agent': + case 'configuration_change': + default: + $module_status = AGENT_MODULE_STATUS_NOT_INIT; + break; + } + + return $module_status; +} + + /** * Translates a numeric value event_status into descriptive text. * diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 39f1642fa0..d7dfef4b58 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -15920,171 +15920,153 @@ function reporting_module_histogram_graph($report, $content, $pdf=0) return false; } - $module_interval = modules_get_interval( - $content['id_agent_module'] - ); - $slice = ($content['period'] / $module_interval); + $filter = [ + 'id_agentmodule' => $content['id_agent_module'], + 'group_rep' => EVENT_GROUP_REP_ALL, + 'date_to' => date('Y-m-d', ($report['datetime'] - $content['period'])), + 'time_to' => date('H:i:s', ($report['datetime'] - $content['period'])), + ]; - $result_sla = reporting_advanced_sla( - $content['id_agent_module'], - ($report['datetime'] - $content['period']), - $report['datetime'], - null, - null, + $previous_event = events_get_all( + ['te.event_type, te.timestamp, te.utimestamp'], + $filter, 0, - null, - null, - null, - $slice, 1, + 'desc', + 'timestamp', true ); - // Select Warning and critical values. - $agentmodule_info = modules_get_agentmodule($content['id_agent_module']); - $min_value_critical = ($agentmodule_info['min_critical'] == 0) ? null : $agentmodule_info['min_critical']; + $status = ''; + if ($previous_event !== false) { + $previous_event = end($previous_event); + $status = $previous_event['event_type']; + } - // Check if module type is string. - $modules_is_string = modules_is_string($agentmodule_info['id_agente_modulo']); + $date_start = ($report['datetime'] - $content['period']); + $filter = [ + 'id_agentmodule' => $content['id_agent_module'], + 'group_rep' => EVENT_GROUP_REP_ALL, + 'date_from' => date('Y-m-d', $date_start), + 'date_to' => date('Y-m-d', $report['datetime']), + 'time_from' => date('H:i:s', $date_start), + 'time_to' => date('H:i:s', $report['datetime']), + ]; - if ($modules_is_string === false) { - if ($agentmodule_info['max_critical'] == 0) { - $max_value_critical = null; - if ($agentmodule_info['min_critical'] == 0) { - if ((bool) $content['dinamic_proc'] === true) { - $max_value_critical = 0.01; - } + $events = events_get_all( + ['te.event_type, te.timestamp, te.utimestamp'], + $filter, + null, + null, + null, + null, + true + ); + + $not_init_data = []; + $previous_data = [ + 'event_type' => $status, + 'utimestamp' => $date_start, + ]; + if (empty($status) === true) { + // Si viene de no iniciado busco el rpimer dato del modulo y si es de histórico. + $first_utimestamp = false; + $search_historydb = false; + $extract_first_data = modules_get_first_date($content['id_agent_module'], 0); + if (empty($extract_first_data) === false) { + $first_utimestamp = $extract_first_data['first_utimestamp']; + $search_historydb = (isset($extract_first_data['search_historydb']) === true) ? $extract_first_data['search_historydb'] : false; + } + + // Si se encuentra algun dato. + // Si no hay eventos, la fecha del primer dato no sea mayor al fin del report (seria un bloque completo de no iniciado). + // Se comprueba que si existen eventos el dato no sea previo al evento. + if ($first_utimestamp !== false + && (($events === false && $first_utimestamp < $report['datetime']) || ($events !== false && $first_utimestamp < $events[0]['utimestamp'])) + ) { + // Tenemos en cuenta si el modulo es de tipo string. + $module = modules_get_agentmodule($content['id_agent_module']); + $module_type = $module['id_tipo_modulo']; + $module_type_str = modules_get_type_name($module_type); + $table = 'tagente_datos'; + if (strstr($module_type_str, 'string') !== false) { + $table = 'tagente_datos_string'; + } + + $query = sprintf( + 'SELECT datos,utimestamp + FROM %s + WHERE id_agente_modulo = %d + AND utimestamp = %d + ', + $table, + $content['id_agent_module'], + $first_utimestamp + ); + + $data = db_get_all_rows_sql($query, $search_historydb); + if ($data !== false) { + $not_init_data = [ + 'event_type' => $status, + 'utimestamp' => $date_start, + ]; + $previous_data = [ + 'event_type' => 'going_up_normal', + 'utimestamp' => $data[0]['utimestamp'], + ]; } - } else { - $max_value_critical = $agentmodule_info['max_critical']; - } - } else { - if ($agentmodule_info['str_critical'] == '') { - $max_value_critical = null; - } else { - $max_value_critical = $agentmodule_info['str_critical']; } } - $inverse_critical = $agentmodule_info['critical_inverse']; - - $min_value_warning = ($agentmodule_info['min_warning'] == 0) ? null : $agentmodule_info['min_warning']; - - if ($modules_is_string === false) { - if ($agentmodule_info['max_warning'] == 0) { - $max_value_warning = null; - } else { - $max_value_warning = $agentmodule_info['max_warning']; - } - } else { - if ($agentmodule_info['str_warning'] == '') { - $max_value_warning = null; - } else { - $max_value_warning = $agentmodule_info['str_warning']; - } + $array_result = []; + $time_total = $content['period']; + $check_total = 0; + $check_ok = 0; + $time_ok = 0; + if (empty($events) === true) { + $events = []; } - $inverse_warning = $agentmodule_info['warning_inverse']; + // Añadimos el dato previo. + array_unshift($events, $previous_data); + if (empty($not_init_data) === false) { + // Añadimos si viene de no iniciado el no iniciado. + array_unshift($events, $not_init_data); + } - $data = []; - $data['time_total'] = 0; - $data['time_ok'] = 0; - $data['time_error'] = 0; - $data['time_warning'] = 0; - $data['time_unknown'] = 0; - $data['time_not_init'] = 0; - $data['time_downtime'] = 0; - $data['checks_total'] = 0; - $data['checks_ok'] = 0; - $data['checks_error'] = 0; - $data['checks_warning'] = 0; - $data['checks_unknown'] = 0; - $data['checks_not_init'] = 0; - - $array_graph = []; + $current_time = time(); $i = 0; - foreach ($result_sla as $value_sla) { - $data['time_total'] += $value_sla['time_total']; - $data['time_ok'] += $value_sla['time_ok']; - $data['time_error'] += $value_sla['time_error']; - $data['time_warning'] += $value_sla['time_warning']; - $data['time_unknown'] += $value_sla['time_unknown']; - $data['time_downtime'] += $value_sla['time_downtime']; - $data['time_not_init'] += $value_sla['time_not_init']; - $data['checks_total'] += $value_sla['checks_total']; - $data['checks_ok'] += $value_sla['checks_ok']; - $data['checks_error'] += $value_sla['checks_error']; - $data['checks_warning'] += $value_sla['checks_warning']; - $data['checks_unknown'] += $value_sla['checks_unknown']; - $data['checks_not_init'] += $value_sla['checks_not_init']; - - // Generate raw data for graph. - if ($value_sla['time_total'] != 0) { - if ($value_sla['time_error'] > 0) { - // ERR. - $array_graph[$i]['data'] = 3; - } else if ($value_sla['time_unknown'] > 0) { - // UNKNOWN. - $array_graph[$i]['data'] = 4; - } else if ($value_sla['time_warning'] > 0) { - // Warning. - $array_graph[$i]['data'] = 2; - } else if ($value_sla['time_not_init'] == $value_sla['time_total']) { - // NOT INIT. - $array_graph[$i]['data'] = 6; - } else { - $array_graph[$i]['data'] = 1; - } + foreach ($events as $event) { + $array_result[$i]['data'] = events_status_module_event_type($event['event_type']); + if (isset($events[($i + 1)]) === true) { + $period = ($events[($i + 1)]['utimestamp'] - $event['utimestamp']); + $array_result[$i]['utimestamp'] = $period; } else { - $array_graph[$i]['data'] = 7; + $period = ($report['datetime'] - $event['utimestamp']); + // El ultimo evento solo lo arrastramos hasta la fecha actual. + // Si pedimos mas fecha sera desconocido. + if ($report['datetime'] > $current_time) { + $period = ($current_time - $event['utimestamp']); + } + + $array_result[$i]['utimestamp'] = $period; } - $array_graph[$i]['utimestamp'] = ($value_sla['date_to'] - $value_sla['date_from']); + if ($array_result[$i]['data'] === AGENT_MODULE_STATUS_NORMAL) { + $check_ok++; + $time_ok += $period; + } + + $check_total++; $i++; } - $data['sla_value'] = reporting_sla_get_compliance_from_array( - $data - ); - - $data['sla_fixed'] = sla_truncate( - $data['sla_value'], - $config['graph_precision'] - ); - - $data_init = -1; - $acum = 0; - $sum = 0; - $array_result = []; - $i = 0; - foreach ($array_graph as $value) { - if ($data_init == -1) { - $data_init = $value['data']; - $acum = $value['utimestamp']; - } else { - if ($data_init == $value['data']) { - $acum = ($acum + $value['utimestamp']); - } else { - $array_result[$i]['data'] = $data_init; - $array_result[$i]['utimestamp'] = $acum; - $array_result[$i]['real_data'] = $sum; - $i++; - $data_init = $value['data']; - $acum = $value['utimestamp']; - } - } + // Bloque por si se pide mas fecha. + if ($report['datetime'] > $current_time) { + $array_result[$i]['data'] = AGENT_MODULE_STATUS_UNKNOWN; + $array_result[$i]['utimestamp'] = ($report['datetime'] - $current_time); } - if (count($array_result) == 0) { - $array_result = $array_graph; - } else { - $array_result[$i]['data'] = $data_init; - $array_result[$i]['utimestamp'] = $acum; - $array_result[$i]['real_data'] = $sum; - } - - $time_total = $data['time_total']; - // Slice graphs calculation. $return['agent'] = modules_get_agentmodule_agent_alias( $content['id_agent_module'] ); @@ -16092,37 +16074,21 @@ function reporting_module_histogram_graph($report, $content, $pdf=0) $content['id_agent_module'] ); - $return['max_critical'] = $max_value_critical; - $return['min_critical'] = $min_value_critical; - $return['critical_inverse'] = $inverse_critical; - $return['max_warning'] = $max_value_warning; - $return['min_warning'] = $min_value_warning; - $return['warning_inverse'] = $inverse_warning; - $return['data_not_init'] = $data['checks_not_init']; - $return['data_unknown'] = $data['checks_unknown']; - $return['data_critical'] = $data['checks_error']; - $return['data_warning'] = $data['checks_warning']; - $return['data_ok'] = $data['checks_ok']; - $return['data_total'] = $data['checks_total']; - $return['time_not_init'] = $data['time_not_init']; - $return['time_unknown'] = $data['time_unknown']; - $return['time_critical'] = $data['time_error']; - $return['time_warning'] = $data['time_warning']; - $return['time_ok'] = $data['time_ok']; - if ($data['checks_total'] > 0) { - $return['percent_ok'] = (($data['checks_ok'] * 100) / $data['checks_total']); + $return['data_ok'] = $check_ok; + $return['data_total'] = $check_total; + if ($check_total > 0) { + $return['percent_ok'] = (($time_ok * 100) / $content['period']); } else { $return['percent_ok'] = 0; } $colors = [ - 1 => COL_NORMAL, - 2 => COL_WARNING, - 3 => COL_CRITICAL, - 4 => COL_UNKNOWN, - 5 => COL_DOWNTIME, - 6 => COL_NOTINIT, - 7 => COL_IGNORED, + AGENT_MODULE_STATUS_CRITICAL_ALERT => COL_ALERTFIRED, + AGENT_MODULE_STATUS_NORMAL => COL_NORMAL, + AGENT_MODULE_STATUS_UNKNOWN => COL_UNKNOWN, + AGENT_MODULE_STATUS_WARNING => COL_WARNING, + AGENT_MODULE_STATUS_CRITICAL_BAD => COL_CRITICAL, + AGENT_MODULE_STATUS_NOT_INIT => COL_NOTINIT, ]; $width_graph = 100;