From 85dddf0b56f2816451f690481308717b2568b3e0 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 3 Jan 2017 19:31:35 +0100 Subject: [PATCH] fixed errors in reports alerts --- pandora_console/include/functions_alerts.php | 79 +++ pandora_console/include/functions_events.php | 2 +- .../include/functions_reporting.php | 464 ++++++++++-------- .../include/functions_reporting_html.php | 159 +++--- 4 files changed, 394 insertions(+), 310 deletions(-) diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index a206311483..4f121cc8c0 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -1273,6 +1273,85 @@ function alerts_get_alert_agent_module_actions ($id_alert_agent_module, $fields return $retval; } +/** + * Returns the actions applied to an alert assigned to a module in a hash. + * @param unsigned int id_agent_module + * + * @return hash with the actions + * + * hash[template1][action1] <- fired + * hash[template1][action2] <- fired + * hash[template1][action3] <- fired + * hash[template2][action1] <- fired + */ +function alerts_get_effective_alert_actions($id_agent_module) { + if (empty ($id_agent_module)) + return false; + + $default_sql = 'select tm.id, t.name as template, a.name as action, tm.last_fired as last_execution from talert_templates t, talert_actions a, talert_template_modules tm where tm.id_alert_template=t.id and t.id_alert_action=a.id and tm.id_agent_module=' . $id_agent_module; + $actions = db_get_all_rows_sql ($default_sql); + + $custom_sql = 'select tm.id, t.name as template, a.name as action, tma.last_execution from talert_actions a, talert_template_module_actions tma, talert_template_modules tm, talert_templates t where tma.id_alert_template_module=tm.id and tma.id_alert_action=a.id and tm.id_alert_template = t.id and tm.id_agent_module=' . $id_agent_module; + $custom_actions = db_get_all_rows_sql($custom_sql); + + $no_actions_sql = 'select tm.id, t.name as template from talert_templates t, talert_template_modules tm where tm.id_alert_template=t.id and tm.id_agent_module=' . $id_agent_module; + $no_actions = db_get_all_rows_sql ($no_actions_sql); + + $nactions = 0; + $return = array(); + + if ($actions !== false) { + foreach ($actions as $a) { + if (!isset($return[$a["template"]]["id"])){ + $return[$a["template"]]["id"] = $a["id"]; + } + if (!isset($return[$a["template"]]["default"])){ + $return[$a["template"]]["default"] = array(); + + } + $return[$a["template"]]["default"][$nactions]["fired"] = $a["last_execution"]; + $return[$a["template"]]["default"][$nactions]["name"] = $a["action"]; + $nactions++; + } + + } + + if ($custom_actions !== false) { + foreach ($custom_actions as $a) { + if (!isset($return[$a["template"]]["id"])){ + $return[$a["template"]]["id"] = $a["id"]; + } + if (!isset($return[$a["template"]]["custom"])){ + $return[$a["template"]]["custom"] = array(); + } + $return[$a["template"]]["custom"][$nactions]["fired"] = $a["last_execution"]; + $return[$a["template"]]["custom"][$nactions]["name"] = $a["action"]; + $nactions++; + } + } + + if ($no_actions !== false){ + foreach ($no_actions as $a) { + if (!isset($return[$a["template"]]["id"])){ + $return[$a["template"]]["id"] = $a["id"]; + } + if (!isset($return[$a["template"]]["unavailable"])){ + $return[$a["template"]]["unavailable"] = array(); + } + $return[$a["template"]]["unavailable"][$nactions]["fired"] = 0; + $return[$a["template"]]["unavailable"][$nactions]["name"] = __("No actions defined"); + $nactions++; + } + } + + + if ($nactions == 0) { + return false; + } + + return $return; +} + /** * Validates an alert id or an array of alert id's. * diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 33ee2c446d..ba5b446bae 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -221,7 +221,7 @@ function events_get_events_grouped($sql_post, $offset = 0, // Override the column 'user_comment' with the column 'user_comments' when oracle if (!empty($events) && $config["dbtype"] == "oracle") { array_walk($events, function(&$value, $key) { - set_if_defined($value['user_comments'], $value['user_comments']); + set_if_defined($value['user_comment'], $value['user_comments']); }); } diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index ceba437b79..b1d0c81faa 100644 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -1287,7 +1287,7 @@ function reporting_event_report_group($report, $content, $return['failed'] = __('No events'); } else { - $return['data'] = $data; + $return['data'] = array_reverse($data); } @@ -1451,7 +1451,7 @@ function reporting_event_report_module($report, $content) { $return['failed'] = __('No events'); } else { - $return['data'] = $data; + $return['data'] = array_reverse($data); } if ($config['metaconsole']) { @@ -2654,6 +2654,32 @@ function reporting_network_interfaces_report($report, $content, return reporting_check_structure_content($return); } +/** + * reporting alert get fired + */ +function reporting_alert_get_fired($id_agent_module, $id_alert_template_module, $period, $datetime) { + $fired = array(); + $firedTimes = get_module_alert_fired( + $id_agent_module, + $id_alert_template_module, + $period, + $datetime); + + if (empty($firedTimes)) { + $firedTimes = array(); + $firedTimes[0]['timestamp'] = '----------------------------'; + } + + foreach ($firedTimes as $fireTime) { + $fired[] = $fireTime['timestamp']; + } + + return $fired; +} + +/** + * Reporting alert report group + */ function reporting_alert_report_group($report, $content) { global $config; @@ -2680,8 +2706,8 @@ function reporting_alert_report_group($report, $content) { $return["date"] = reporting_get_date_text($report, $content); if ($content['id_group'] == 0) { - $alerts = db_get_all_rows_sql(' - SELECT * + $agent_modules = db_get_all_rows_sql(' + SELECT distinct(id_agent_module) FROM talert_template_modules WHERE disabled = 0 AND id_agent_module IN ( @@ -2689,8 +2715,8 @@ function reporting_alert_report_group($report, $content) { FROM tagente_modulo)'); } else { - $alerts = db_get_all_rows_sql(' - SELECT * + $agent_modules = db_get_all_rows_sql(' + SELECT distinct(id_agent_module) FROM talert_template_modules WHERE disabled = 0 AND id_agent_module IN ( @@ -2708,71 +2734,95 @@ function reporting_alert_report_group($report, $content) { $data = array(); - foreach ($alerts as $alert) { + foreach ($agent_modules as $agent_module) { $data_row = array(); - $data_row['disabled'] = $alert['disabled']; - + $data_row['agent'] = io_safe_output(agents_get_name( - agents_get_agent_id_by_module_id($alert['id_agent_module']))); + agents_get_agent_id_by_module_id($agent_module['id_agent_module']))); $data_row['module'] = db_get_value_filter('nombre', 'tagente_modulo', - array('id_agente_modulo' => $alert['id_agent_module'])); - $data_row['template'] = db_get_value_filter('name', 'talert_templates', - array('id' => $alert['id_alert_template'])); - - $actions = alerts_get_alert_agent_module_actions ($alert['id']); - - if (!empty($actions)) { - $row = db_get_row_sql('SELECT id_alert_action - FROM talert_templates - WHERE id IN (SELECT id_alert_template - FROM talert_template_modules - WHERE id = ' . $alert['id'] . ')'); - - $id_action = 0; - if (!empty($row)) - $id_action = $row['id_alert_action']; - - // Prevent from void action - if (empty($id_action)) - $id_action = 0; + array('id_agente_modulo' => $agent_module['id_agent_module'])); + + // Alerts over $id_agent_module + $alerts = alerts_get_effective_alert_actions($agent_module['id_agent_module']); + + if ($alerts === false){ + continue; } - else { - $actions = db_get_all_rows_sql('SELECT name - FROM talert_actions - WHERE id = ' . $id_action); + + $ntemplates = 0; + + foreach ($alerts as $template => $actions) { + + $data_action = array(); + $data_action['actions'] = array(); + + $naction = 0; + if (isset($actions["custom"])) { + foreach ($actions["custom"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; + } + } + elseif (isset($actions["default"])) { + foreach ($actions["default"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; + } + } + elseif(isset($actions["unavailable"])) { + foreach ($actions["unavailable"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; + } + } + + $module_actions = array(); + + $module_actions["template"] = $template; + $module_actions["template_fired"] = reporting_alert_get_fired( + $agent_module['id_agent_module'], + $actions["id"], + (int) $content["period"], + (int) $report["datetime"]); + $module_actions["actions"] = $data_action; + + $data_row['alerts'][$ntemplates] = $module_actions; + $ntemplates++; } - $data_row['action'] = array(); - foreach ($actions as $action) { - $data_row['action'][] = $action['name']; + if ($ntemplates > 0) { + $data[] = $data_row; } - - $data_row['fired'] = array(); - $firedTimes = get_module_alert_fired( - $alert['id_agent_module'], - $alert['id'], - (int) $content['period'], - (int) $report["datetime"]); - - if (empty($firedTimes)) { - $firedTimes = array(); - $firedTimes[0]['timestamp'] = '----------------------------'; - } - - foreach ($firedTimes as $fireTime) { - $data_row['fired'][] = $fireTime['timestamp']; - } - - $data[] = $data_row; } - - $return['data'] = $data; - + + $return["data"] = $data; + if ($config['metaconsole']) { metaconsole_restore_db(); } - + return reporting_check_structure_content($return); } @@ -2800,83 +2850,92 @@ function reporting_alert_report_agent($report, $content) { $return["description"] = $content["description"]; $return["date"] = reporting_get_date_text($report, $content); $return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : ''; - - $alerts = agents_get_alerts($content['id_agent']); - - if (isset($alerts['simple'])) { - $alerts = $alerts['simple']; - } - else { - $alerts = array(); - } - + + $module_list = agents_get_modules($content['id_agent']); + $data = array(); - - if (is_array($alerts) || is_object($alerts)) { - foreach ($alerts as $alert) { - $data_row = array(); - - $data_row['disabled'] = $alert['disabled']; - - $data_row['module'] = db_get_value_filter('nombre', 'tagente_modulo', - array('id_agente_modulo' => $alert['id_agent_module'])); - $data_row['template'] = db_get_value_filter('name', 'talert_templates', - array('id' => $alert['id_alert_template'])); - - $actions = alerts_get_alert_agent_module_actions ($alert['id']); + foreach ($module_list as $id => $module_name) { - if (!empty($actions)) { - $row = db_get_row_sql('SELECT id_alert_action - FROM talert_templates - WHERE id IN (SELECT id_alert_template - FROM talert_template_modules - WHERE id = ' . $alert['id_alert_template'] . ')'); - - $id_action = 0; - if (!empty($row)) - $id_action = $row['id_alert_action']; - - // Prevent from void action - if (empty($id_action)) - $id_action = 0; - } - else { - $actions = db_get_all_rows_sql('SELECT name - FROM talert_actions - WHERE id = ' . $id_action); - } + $data_row = array(); + $data_row['agent'] = $agent_name; + $data_row['module'] = $module_name; + + // Alerts over $id_agent_module + $alerts = alerts_get_effective_alert_actions($id); + + if ($alerts === false){ + continue; + } + + $ntemplates = 0; + + foreach ($alerts as $template => $actions) { + + $data_action = array(); + $data_action['actions'] = array(); - if (empty($actions)) { - $actions = array(); + $naction = 0; + if (isset($actions["custom"])) { + foreach ($actions["custom"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; + } + } + elseif (isset($actions["default"])) { + foreach ($actions["default"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; + } + } + elseif(isset($actions["unavailable"])) { + foreach ($actions["unavailable"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; + } } - $data_row['action'] = array(); - foreach ($actions as $action) { - $data_row['action'][] = $action['name']; - } + $module_actions = array(); - $data_row['fired'] = array(); - $firedTimes = get_module_alert_fired( - $alert['id_agent_module'], - $alert['id'], - (int) $content['period'], - (int) $report["datetime"]); - - if (empty($firedTimes)) { - $firedTimes = array(); - $firedTimes[0]['timestamp'] = '----------------------------'; - } + $module_actions["template"] = $template; + $module_actions["template_fired"] = reporting_alert_get_fired( + $id, + $actions["id"], + (int) $content["period"], + (int) $report["datetime"]); + $module_actions["actions"] = $data_action; - foreach ($firedTimes as $fireTime) { - $data_row['fired'][] = $fireTime['timestamp']; - } - + $data_row['alerts'][$ntemplates] = $module_actions; + $ntemplates++; + } + + if ($ntemplates > 0) { $data[] = $data_row; } } - $return['data'] = $data; - + $return["data"] = $data; + if ($config['metaconsole']) { metaconsole_restore_db(); } @@ -2913,101 +2972,92 @@ function reporting_alert_report_module($report, $content) { $return["date"] = reporting_get_date_text($report, $content); $return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : ''; - switch ($config["dbtype"]) { - case "mysql": - case "postgresql": - $alerts = db_get_all_rows_sql(' - SELECT *, t1.id as id_alert_template_module - FROM talert_template_modules t1 - INNER JOIN talert_templates t2 - ON t1.id_alert_template = t2.id - WHERE id_agent_module = ' . $content['id_agent_module']); - break; - case "oracle": - $alerts = db_get_all_rows_sql(' - SELECT t1.*, t2.*, t1.id as id_alert_template_module - FROM talert_template_modules t1 - INNER JOIN talert_templates t2 - ON t1.id_alert_template = t2.id - WHERE id_agent_module = ' . $content['id_agent_module']); - break; + + $data_row = array(); + + + $data_row['agent'] = io_safe_output(agents_get_name( + agents_get_agent_id_by_module_id($content['id_agent_module']))); + $data_row['module'] = db_get_value_filter('nombre', 'tagente_modulo', + array('id_agente_modulo' => $content['id_agent_module'])); + + // Alerts over $id_agent_module + $alerts = alerts_get_effective_alert_actions($content['id_agent_module']); + + if ($alerts === false){ + continue; } + + $ntemplates = 0; - - if ($alerts === false) { - $alerts = array(); - } - - $data = array(); - $actions = array(); - foreach ($alerts as $alert) { + foreach ($alerts as $template => $actions) { + + $data_action = array(); + $data_action['actions'] = array(); - $data_row = array(); - - $data_row['disabled'] = $alert['disabled']; - - $data_row['template'] = db_get_value_filter('name', - 'talert_templates', array('id' => $alert['id_alert_template'])); - - $actions = alerts_get_alert_agent_module_actions ($alert['id_alert_template_module']); - - if (!empty($actions)) { - $row = db_get_row_sql('SELECT id_alert_action - FROM talert_templates - WHERE id IN (SELECT id_alert_template - FROM talert_template_modules - WHERE id = ' . $alert['id_alert_template_module'] . ')'); - - $id_action = 0; - - if (!empty($row)) - $id_action = $row['id_alert_action']; - - // Prevent from void action - if (empty($id_action)) - $id_action = 0; - } - else { - if ($id_action != null) { - $actions = db_get_all_rows_sql('SELECT name - FROM talert_actions - WHERE id = ' . $id_action); + $naction = 0; + if (isset($actions["custom"])) { + foreach ($actions["custom"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; } } - - if (empty($actions)) { - $data_row['action'][] = __('No defined actions'); + elseif (isset($actions["default"])) { + foreach ($actions["default"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; + } } - else { - $data_row['action'] = array(); - foreach ($actions as $action) { - $data_row['action'][] = $action['name']; + elseif(isset($actions["unavailable"])) { + foreach ($actions["unavailable"] as $action) { + $data_action[$naction]["name"] = $action["name"]; + $fired = $action["fired"]; + if ($fired == 0){ + $data_action[$naction]['fired'] = '----------------------------'; + } + else { + $data_action[$naction]['fired'] = $fired; + } + $naction++; } } - $data_row['fired'] = array(); - $firedTimes = get_module_alert_fired( - $content['id_agent_module'], - $alert['id_alert_template_module'], - (int) $content['period'], - (int) $report["datetime"]); + $module_actions = array(); - if (empty($firedTimes)) { - $firedTimes = array(); - $firedTimes[0]['timestamp'] = '----------------------------'; - } + $module_actions["template"] = $template; + $module_actions["template_fired"] = reporting_alert_get_fired( + $content['id_agent_module'], + $actions["id"], + (int) $content["period"], + (int) $report["datetime"]); + $module_actions["actions"] = $data_action; - foreach ($firedTimes as $fireTime) { - $data_row['fired'][] = $fireTime['timestamp']; - } + $data_row['alerts'][$ntemplates] = $module_actions; + $ntemplates++; + } + if ($ntemplates > 0) { $data[] = $data_row; } - - $return['data'] = $data; - + + $return["data"] = $data; + if ($config['metaconsole']) { - metaconsole_restore_db(); + metaconsole_restore_db(); } return reporting_check_structure_content($return); @@ -5237,7 +5287,7 @@ function reporting_availability_graph($report, $content, $date=false, $time=fals $width_graph, $height_graph, $urlImage, - 5, + 1, $raw_graph, false); @@ -6127,7 +6177,7 @@ function reporting_get_group_detailed_event ($id_group, $period = 0, $filter_event_validated, $filter_event_critical, $filter_event_warning, $filter_event_no_validated, $filter_event_filter_search, false, $history, $filter_event_type); - + if ($return_type === 'hash') { return $events; } diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index db603b4b75..b39f2ce2ca 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -234,14 +234,10 @@ function reporting_html_print_report($report, $mini = false) { case 'sql_graph_pie': reporting_html_sql_graph($table, $item); break; - case 'alert_report_module': - reporting_html_alert_report_module($table, $item); - break; - case 'alert_report_agent': - reporting_html_alert_report_agent($table, $item); - break; case 'alert_report_group': - reporting_html_alert_report_group($table, $item); + case 'alert_report_module': + case 'alert_report_agent': + reporting_html_alert_report($table, $item); break; case 'network_interfaces_report': reporting_html_network_interfaces_report($table, $item); @@ -1611,113 +1607,72 @@ function reporting_html_network_interfaces_report($table, $item) { } } -function reporting_html_alert_report_group($table, $item) { +/** + * Unified alert report HTML + */ +function reporting_html_alert_report($table, $item) { $table->colspan['alerts']['cell'] = 3; $table->cellstyle['alerts']['cell'] = 'text-align: left;'; - $table1->width = '99%'; - $table1->head = array (); - $table1->head['agent'] = __('Agent'); - $table1->head['module'] = __('Module'); + $table1->width = '99%'; + $table1->head = array (); + $table1->data = array (); + $table1->rowspan = array(); + $table1->valign = array(); + $table1->head['agent'] = __('Agent'); + $table1->head['module'] = __('Module'); $table1->head['template'] = __('Template'); - $table1->head['actions'] = __('Actions'); - $table1->head['fired'] = __('Fired'); - $table1->data = array (); - foreach ($item['data'] as $alert) { - $row = array(); - - $row['agent'] = $alert['agent']; - $row['module'] = $alert['module']; - $row['template'] = $alert['template']; - $row['actions'] = $alert['template']; - - $row['actions'] = ''; - - $row['fired'] = ''; - - $table1->data[] = $row; - } - - $table->data['alerts']['cell'] = html_print_table($table1, true); -} + $table1->head['actions'] = __('Actions'); + $table1->head['fired'] = __('Action') . " " . __('Fired'); + $table1->head['tfired'] = __('Template') . " " . __('Fired'); + $table1->valign["agent"] = "top"; + $table1->valign["module"] = "top"; + $table1->valign["template"] = "top"; + $table1->valign["actions"] = "top"; + $table1->valign["fired"] = "top"; + $table1->valign["tfired"] = "top"; -function reporting_html_alert_report_agent($table, $item) { - $table->colspan['alerts']['cell'] = 3; - $table->cellstyle['alerts']['cell'] = 'text-align: left;'; - - $table1 = new stdClass(); - $table1->width = '99%'; - $table1->head = array (); - $table1->head['module'] = __('Module'); - $table1->head['template'] = __('Template'); - $table1->head['actions'] = __('Actions'); - $table1->head['fired'] = __('Fired'); - $table1->data = array (); - foreach ($item['data'] as $alert) { + $td = 0; + foreach ($item['data'] as $information) { $row = array(); - $row['module'] = $alert['module']; - $row['template'] = $alert['template']; - $row['actions'] = $alert['template']; + $td = count($information["alerts"]); - $row['actions'] = ''; - - $row['fired'] = ''; - - $table1->data[] = $row; - } + $row['agent'] = $information['agent']; + $row['module'] = $information['module']; - $table->data['alerts']['cell'] = html_print_table($table1, true); -} + foreach ($information["alerts"] as $alert) { + $row['template'] = $alert["template"]; + $row['actions'] = ''; -function reporting_html_alert_report_module($table, $item) { - $table->colspan['alerts']['cell'] = 3; - $table->cellstyle['alerts']['cell'] = 'text-align: left;'; - - $table1 = new stdClass(); - $table1->width = '99%'; - $table1->head = array (); - $table1->head['template'] = __('Template'); - $table1->head['actions'] = __('Actions'); - $table1->head['fired'] = __('Fired'); - $table1->data = array (); - foreach ($item['data'] as $alert) { - $row = array(); - - $row['template'] = $alert['template']; - $row['actions'] = $alert['template']; - - $row['actions'] = ''; - - $row['fired'] = ''; - - $table1->data[] = $row; } - $table->data['alerts']['cell'] = html_print_table($table1, true); }