diff --git a/pandora_console/include/functions_forecast.php b/pandora_console/include/functions_forecast.php index f58e3592e7..fb09624081 100644 --- a/pandora_console/include/functions_forecast.php +++ b/pandora_console/include/functions_forecast.php @@ -20,31 +20,29 @@ */ /** - * Create a prediction based on module data with least square method (linear regression) + * Create a prediction based on module data with least square method (linear regression) * * @param int Module id. * @param int Period of the module data. - * @param int Period of the prediction or false to use it in prediction_date function (see below). + * @param int Period of the prediction or false to use it in prediction_date function (see below). * @param int Maximun value using this function for prediction_date. * @param int Minimun value using this function for prediction_date. * @param bool Result data for CSV file exportation. - * + * * @return array Void array or prediction of the module data. */ function forecast_projection_graph($module_id, $period = SECONDS_2MONTHS, $prediction_period, $max_value = false, $min_value = false, $csv = false) { - + global $config; - - $max_exec_time = ini_get('max_execution_time'); - + + $max_exec_time = ini_get('max_execution_time'); + if ($max_exec_time !== false) { - - $max_exec_time = (int)$max_exec_time; - + $max_exec_time = (int)$max_exec_time; } - + $begin_time = time(); $params =array( @@ -54,16 +52,16 @@ function forecast_projection_graph($module_id, 'projection' => true ); - $module_data = grafico_modulo_sparse ($params); - + $module_data = grafico_modulo_sparse($params); + if (empty($module_data)) { - return array(); + return array(); } - // Prevents bad behaviour over image error + // Prevents bad behaviour over image error else if (!is_array($module_data) and preg_match('/^= -1.0 and $linear_coef <= -0.8999 // Function variables have an inverse linear relathionship! - // else - // Function variables don't have an inverse linear relathionship! - + // else + // Function variables don't have an inverse linear relathionship! // Could be a direct correlation coefficient - // else + // else // if ($linear_coef >= 0.8999 and $linear_coef <= 1.0) { // Function variables have a direct linear relathionship! - // else + // else // Function variables don't have a direct linear relathionship! - // 2. Calculation of linear regresion... - + $b_num = (($cont * $sum_xi_yi) - ($sum_xi * $sum_yi)); $b_den = (($cont * $sum_xi2) - ($sum_xi * $sum_xi)); if ($b_den == 0) return; $b = $b_num / $b_den; - + $a_num = ($sum_yi) - ($b * $sum_xi); - + if ($cont != 0) { $a = $a_num / $cont; } else { $a = 0; } - + // Data inicialization $output_data = array(); if ($prediction_period != false) { @@ -188,11 +165,11 @@ function forecast_projection_graph($module_id, $current_ts = $last_timestamp; $in_range = true; $time_format_2 = ''; - + $temp_range = $period; if ($period < $prediction_period) $temp_range = $prediction_period; - + if ($temp_range <= SECONDS_6HOURS) { $time_format = 'H:i:s'; } @@ -206,15 +183,15 @@ function forecast_projection_graph($module_id, elseif ($temp_range <= SECONDS_1MONTH) { $time_format = 'M d'; $time_format_2 = 'H\h'; - } + } else { $time_format = 'M d'; } - - // Aplying linear regression to module data in order to do the prediction - $output_data = array(); + + // Aplying linear regression to module data in order to do the prediction $idx = 0; // Create data in graph format like + while ($in_range) { $now = time(); @@ -242,9 +219,10 @@ function forecast_projection_graph($module_id, if ($current_ts - $last_timestamp >= 94608000) { return false; } - + // Found it - if ($max_value >= $output_data[$idx][0] and $min_value <= $output_data[$idx][0]) { + if (($max_value >= $output_data[$idx][0]) && + ($min_value <= $output_data[$idx][0]) ) { return $current_ts; } } @@ -254,7 +232,6 @@ function forecast_projection_graph($module_id, $current_ts = $current_ts + $agent_interval; $idx++; } - return $output_data; } @@ -264,8 +241,8 @@ function forecast_projection_graph($module_id, * @param int Module id. * @param int Given data period to make the prediction * @param int Max value in the interval. - * @param int Min value in the interval. - * + * @param int Min value in the interval. + * * @return mixed timestamp with the prediction date or false */ function forecast_prediction_date ($module_id, @@ -274,6 +251,5 @@ function forecast_prediction_date ($module_id, if ($min_value > $max_value) { return false; } - return forecast_projection_graph($module_id, $period, false, $max_value, $min_value); } diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php index 2b3c149b99..ad6a9857ba 100644 --- a/pandora_console/include/functions_graph.php +++ b/pandora_console/include/functions_graph.php @@ -264,9 +264,7 @@ function grafico_modulo_sparse_data_chart ( $data_module_graph['id_module_type'] == 18 || $data_module_graph['id_module_type'] == 9 || $data_module_graph['id_module_type'] == 31 || - $data_module_graph['id_module_type'] == 100 || - $params['baseline'] || $params['projection'] - ){ + $data_module_graph['id_module_type'] == 100 ){ $data = db_get_all_rows_filter ( 'tagente_datos', @@ -421,8 +419,7 @@ function grafico_modulo_sparse_data( $data_module_graph['id_module_type'] == 18 || $data_module_graph['id_module_type'] == 9 || $data_module_graph['id_module_type'] == 31 || - $data_module_graph['id_module_type'] == 100 || - $params['projection'] ){ + $data_module_graph['id_module_type'] == 100 ){ $array_data = grafico_modulo_sparse_data_chart ( $agent_module_id, $date_array, @@ -924,7 +921,6 @@ function grafico_modulo_sparse ($params) { $legend = array(); $array_events_alerts = array(); - $date_array = array(); $date_array["period"] = $params['period']; $date_array["final_date"] = $params['date']; @@ -966,53 +962,58 @@ function grafico_modulo_sparse ($params) { } if(!$params['array_data_create']){ - if ($params['compare'] !== false) { - $series_suffix = 2; + if($params['baseline']){ + $array_data = get_baseline_data($agent_module_id, $date_array, $data_module_graph, $params); + } + else{ + if ($params['compare'] !== false) { + $series_suffix = 2; - $date_array_prev['final_date'] = $date_array['start_date']; - $date_array_prev['start_date'] = $date_array['start_date'] - $date_array['period']; - $date_array_prev['period'] = $date_array['period']; + $date_array_prev['final_date'] = $date_array['start_date']; + $date_array_prev['start_date'] = $date_array['start_date'] - $date_array['period']; + $date_array_prev['period'] = $date_array['period']; - if ($params['compare'] === 'overlapped') { - $params['flag_overlapped'] = 1; - } - else{ - $params['flag_overlapped'] = 0; + if ($params['compare'] === 'overlapped') { + $params['flag_overlapped'] = 1; + } + else{ + $params['flag_overlapped'] = 0; + } + + $array_data = grafico_modulo_sparse_data( + $agent_module_id, + $date_array_prev, + $data_module_graph, + $params, + $series_suffix + ); + + switch ($params['compare']) { + case 'separated': + case 'overlapped': + // Store the chart calculated + $array_data_prev = $array_data; + $legend_prev = $legend; + break; + } } + $series_suffix = 1; + $params['flag_overlapped'] = 0; + $array_data = grafico_modulo_sparse_data( $agent_module_id, - $date_array_prev, + $date_array, $data_module_graph, $params, $series_suffix ); - switch ($params['compare']) { - case 'separated': - case 'overlapped': - // Store the chart calculated - $array_data_prev = $array_data; - $legend_prev = $legend; - break; - } - } - - $series_suffix = 1; - $params['flag_overlapped'] = 0; - - $array_data = grafico_modulo_sparse_data( - $agent_module_id, - $date_array, - $data_module_graph, - $params, - $series_suffix - ); - - if($params['compare']){ - if ($params['compare'] === 'overlapped') { - $array_data = array_merge($array_data, $array_data_prev); - $legend = array_merge($legend, $legend_prev); + if($params['compare']){ + if ($params['compare'] === 'overlapped') { + $array_data = array_merge($array_data, $array_data_prev); + $legend = array_merge($legend, $legend_prev); + } } } } @@ -1221,7 +1222,7 @@ function graphic_combined_module ( } else{ $params['stacked'] = 'area'; - $params['projection'] = $params_combined['projection']; + $params['projection'] = true; } if(!isset($params_combined['labels'])){ @@ -1514,6 +1515,14 @@ function graphic_combined_module ( $date_array["final_date"] = $params['date']; $date_array["start_date"] = $params['date'] - $params['period']; + if($params_combined['projection']){ + $output_projection = forecast_projection_graph( + $module_list[0], + $params['period'], + $params_combined['projection'] + ); + } + $i=0; $array_data = array(); foreach ($module_list as $key => $agent_module_id) { @@ -1580,10 +1589,13 @@ function graphic_combined_module ( $i++; } - if($params_combined['projection'] && is_array($params_combined['projection'])){ - $date_array_projection = max($params_combined['projection']); - $date_array['final_date'] = $date_array_projection[0] / 1000; - $array_data['projection']['data']= $params_combined['projection']; + if($params_combined['projection']){ + // If projection doesn't have data then don't draw graph + if ($output_projection != NULL) { + $date_array_projection = max($output_projection); + $date_array['final_date'] = $date_array_projection[0] / 1000; + $array_data['projection']['data']= $output_projection; + } } //summatory and average series @@ -5098,4 +5110,53 @@ function graph_monitor_wheel ($width = 550, $height = 600, $filter = false) { return d3_sunburst_graph ($graph_data, $width, $height, true); } +function get_baseline_data($agent_module_id, $date_array, $data_module_graph, $params){ + $period = $date_array["period"]; + $date = $date_array["final_date"]; + $array_data = array(); + for ($i = 0; $i < 4; $i++) { + $date_array = array(); + $date_array["period"] = $period; + $date_array["final_date"] = $date - $period * $i; + $date_array["start_date"] = $date - $period * ($i + 1); + + $data = grafico_modulo_sparse_data( + $agent_module_id, + $date_array, + $data_module_graph, + $params, + $i + ); + + $array_data[] = $data; + + } + $result = array(); + $array_data[1] = array_reverse($array_data[1]['sum1']['slice_data']); + $array_data[2] = array_reverse($array_data[2]['sum2']['slice_data']); + $array_data[3] = array_reverse($array_data[3]['sum3']['slice_data']); + foreach ($array_data[0]['sum0']['slice_data'] as $key => $value) { + $data1 = array_pop($array_data[1]); + $data2 = array_pop($array_data[2]); + $data3 = array_pop($array_data[3]); + + $result['slice_data'][$key]['min'] = ($data1['min'] + $data2['min'] + $data3['min'] + $value['min']) / 4; + $result['slice_data'][$key]['avg'] = ($data1['avg'] + $data2['avg'] + $data3['avg'] + $value['avg']) / 4; + $result['slice_data'][$key]['max'] = ($data1['max'] + $data2['max'] + $data3['max'] + $value['max']) / 4; + + $result['data'][] = array($key, $result['slice_data'][$key]['avg']); + } + + $result['avg'] = ($array_data[0]['sum0']['avg'] + $array_data[1]['sum1']['avg'] + $array_data[2]['sum2']['avg'] +$array_data[3]['sum3']['avg'])/4; + $result['max'] = max($array_data[0]['sum0']['max'], $array_data[1]['sum1']['max'], $array_data[2]['sum2']['max'], $array_data[3]['sum3']['max']); + $result['min'] = min($array_data[0]['sum0']['min'], $array_data[1]['sum1']['min'], $array_data[2]['sum2']['min'], $array_data[3]['sum3']['min']); + + $result['agent_module_id'] = $array_data[0]['sum0']['agent_module_id']; + $result['id_module_type'] = $array_data[0]['sum0']['id_module_type']; + $result['agent_name'] = $array_data[0]['sum0']['agent_name']; + $result['module_name'] = $array_data[0]['sum0']['module_name']; + $result['agent_alias'] = $array_data[0]['sum0']['agent_alias']; + return array('sum0' => $result); +} + ?> diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index b406dbf1dc..4cc8315b0b 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -395,7 +395,8 @@ function reporting_make_reporting_data($report = null, $id_report, $content, $type, $force_width_chart, - $force_height_chart); + $force_height_chart + ); break; case 'netflow_area': $report['contents'][] = reporting_netflow( @@ -3481,80 +3482,6 @@ function reporting_netflow($report, $content, $type, return reporting_check_structure_content($return); } -function reporting_simple_baseline_graph($report, $content, - $type = 'dinamic', $force_width_chart = null, - $force_height_chart = null) { - - global $config; - - if ($config['metaconsole']) { - $id_meta = metaconsole_get_id_server($content["server_name"]); - $server = metaconsole_get_connection_by_id ($id_meta); - metaconsole_connect($server); - } - - $return['type'] = 'simple_baseline_graph'; - - if (empty($content['name'])) { - $content['name'] = __('Simple baseline graph'); - } - - $module_name = io_safe_output( - modules_get_agentmodule_name($content['id_agent_module'])); - $agent_name = io_safe_output( - modules_get_agentmodule_agent_alias ($content['id_agent_module'])); - - $return['title'] = $content['name']; - $return['subtitle'] = $agent_name . " - " . $module_name; - $return["description"] = $content["description"]; - $return["date"] = reporting_get_date_text($report, $content); - $return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : ''; - - // Get chart - reporting_set_conf_charts($width, $height, $only_image, $type, - $content, $ttl); - - $baseline_data = enterprise_hook( - 'reporting_enterprise_get_baseline', - array ( - $content['id_agent_module'], - $content['period'], - $report["datetime"] - ) - ); - - if ($baseline_data === ENTERPRISE_NOT_HOOK) { - $baseline_data = array (); - } - - switch ($type) { - case 'dinamic': - case 'static': - $params =array( - 'agent_module_id' => $content['id_agent_module'], - 'period' => $content['period'], - 'date' => $report["datetime"], - 'only_image' => $only_image, - 'homeurl' => ui_get_full_url(false, false, false, false), - 'ttl' => $ttl, - 'array_data_create' => $baseline_data, - 'server_id' => $id_meta, - 'height' => $config['graph_image_height'] - ); - - $return['chart'] = grafico_modulo_sparse ($params); - break; - case 'data': - break; - } - - if ($config['metaconsole']) { - metaconsole_restore_db(); - } - - return reporting_check_structure_content($return); -} - function reporting_prediction_date($report, $content) { global $config; @@ -3630,17 +3557,6 @@ function reporting_projection_graph($report, $content, switch ($type) { case 'dinamic': case 'static': - $output_projection = forecast_projection_graph( - $content['id_agent_module'], - $content['period'], - $content['top_n_value'] - ); - - // If projection doesn't have data then don't draw graph - if ($output_projection == NULL) { - $output_projection = false; - } - $params =array( 'period' => $content['period'], 'width' => $width, @@ -3654,7 +3570,7 @@ function reporting_projection_graph($report, $content, ); $params_combined = array( - 'projection' => $output_projection + 'projection' => $content['top_n_value'], ); $return['chart'] = graphic_combined_module(