From 26c08d93097ad0c540f762acfc19735310f9b117 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 20 Sep 2018 15:51:36 +0200 Subject: [PATCH] refactoriced view module_groups --- pandora_console/extensions/module_groups.php | 485 +++++++++--------- .../include/class/TreeGroup.class.php | 20 +- pandora_console/include/styles/pandora.css | 20 + 3 files changed, 266 insertions(+), 259 deletions(-) diff --git a/pandora_console/extensions/module_groups.php b/pandora_console/extensions/module_groups.php index 9a4f66935c..c617d6425e 100644 --- a/pandora_console/extensions/module_groups.php +++ b/pandora_console/extensions/module_groups.php @@ -17,103 +17,21 @@ global $config; -if (is_ajax ()) { - check_login (); - - require_once('include/functions_agents.php'); - - $get_info_alert_module_group = (bool)get_parameter('get_info_alert_module_group'); - $module_group = (int)get_parameter('module_group'); - $id_agent_group = (int)get_parameter('id_agent_group'); - - $data = false; - if ($get_info_alert_module_group) { - $agents = agents_get_group_agents($id_agent_group); - if (!empty($agents)) { - $alerts = agents_get_alerts_simple(array_keys($agents)); - foreach ($alerts as $alert) { - $module = db_get_row_filter('tagente_modulo', array('id_agente_modulo' => $alert['id_agent_module'])); - if ($module_group == $module['id_module_group']) { - if ($alert["times_fired"] > 0) { - $data = true; - echo '' . __('Number fired of alerts').': ' . $alert["times_fired"] . '
'; - $agent = db_get_row('tagente', 'id_agente', $module['id_agente']); - echo '' . __('Agent').': '; - echo io_safe_output($agent['nombre']) . '
'; - echo '' . __('Module') . ': '; - echo io_safe_output($module['nombre']) . '
'; - $template = db_get_row('talert_templates', 'id' , $alert['id_alert_template']); - echo '' . __('Alert template') . ': '; - echo io_safe_output($template['name']) . '
'; - - // This prevent from templates without predefined actions - if (empty($template['id_alert_action'])) - $template_id_alert_action = "''"; - else - $template_id_alert_action = $template['id_alert_action']; - - // True if the alert only has the default template action - $default_action = false; - // Try to get actions for the current alert - $sql = 'SELECT t2.name - FROM talert_template_module_actions t1 - INNER JOIN talert_actions t2 - INNER JOIN talert_template_modules t3 - ON t3.id = t1.id_alert_template_module - AND t1.id_alert_action = t2.id - WHERE (t3.id_alert_template = ' . $template['id'] . ' AND - t3.id_agent_module = ' . $module['id_agente_modulo'] . ');'; - - $actions = db_get_all_rows_sql($sql); - - // If this alert doesn't have actions try to get default action from template - if ($actions === false) { - $sql = 'SELECT name - FROM talert_actions - WHERE (id = ' . $template_id_alert_action . ');'; - - $default_action = true; - - $actions = db_get_all_rows_sql($sql); - } - - if ($actions === false) { - $actions = array(); - } - - echo '' . __('Actions') . ': ' . '
'; - echo ''; - if ($alert != end($alerts)) { - echo '
'; - } - } - } - } - if (!$data) { - echo 'These module/s have no alerts or alert/s are not fired'; - } - } - else { - echo 'No available data'; - } - } - else { - echo 'No available data'; - } -} +check_login (); -/** - * Translate the array texts using gettext - */ -function translate(&$item, $key) { - $item = __($item); +if (is_ajax ()) { + $get_info_alert_module_group = (bool)get_parameter('get_info_alert_module_group'); + + if ($get_info_alert_module_group) { + $send_tooltip = json_decode(io_safe_output(get_parameter('send_tooltip')), true); + echo ""; + } } /** @@ -121,169 +39,229 @@ function translate(&$item, $key) { * execute the code. */ function mainModuleGroups() { - global $config; //the useful global var of Pandora Console, it has many data can you use - - require_once ('include/functions_reporting.php'); - require_once($config['homedir'] . "/include/functions_agents.php"); - require_once($config['homedir'] . "/include/functions_users.php"); - - //The big query - $sql = "SELECT COUNT(id_agente) AS count, case utimestamp when 0 then 5 else estado end as estado - FROM tagente_estado - WHERE id_agente IN - (SELECT id_agente - FROM tagente ta LEFT JOIN tagent_secondary_group tasg - ON ta.id_agente = tasg.id_agent - WHERE (ta.id_grupo = %d OR tasg.id_group = %d) AND disabled IS FALSE - ) - AND id_agente_modulo IN - (SELECT id_agente_modulo - FROM tagente_modulo - WHERE id_module_group = %d AND disabled IS FALSE AND delete_pending IS FALSE) - GROUP BY estado"; + global $config; + + require_once($config['homedir'] . "/include/class/TreeGroup.class.php"); + require_once($config['homedir'] . "/include/functions_groupview.php"); + + $tree_group = new TreeGroup("group", "group"); + $tree_group->setPropagateCounters(false); + $tree_group->setDisplayAllGroups(true); + $tree_group->setFilter( array( + 'searchAgent' => '', + 'statusAgent' => AGENT_STATUS_ALL, + 'searchModule' => '', + 'statusModule' => -1, + 'groupID' => 0, + 'tagID' => 0, + 'show_not_init_agents' => 1, + 'show_not_init_modules' => 1 + )); + $info = $tree_group->getArray(); + $info = groupview_plain_groups($info); + $counter = count($info); + $offset = get_parameter('offset', 0); + $groups_view = $is_not_paginated + ? $info + : array_slice($info, $offset, $config['block_size']); + $agents_counters = array_reduce($groups_view, function($carry, $item){ + $carry[$item['id']] = $item; + return $carry; + }, array()); + + $ids_array = array_keys($agents_counters); + + $ids_group = implode(',', $ids_array); + + $condition_critical = modules_get_state_condition(AGENT_MODULE_STATUS_CRITICAL_ALERT); + $condition_warning = modules_get_state_condition(AGENT_MODULE_STATUS_WARNING_ALERT); + $condition_unknown = modules_get_state_condition(AGENT_MODULE_STATUS_UNKNOWN); + $condition_not_init = modules_get_state_condition(AGENT_MODULE_STATUS_NO_DATA); + $condition_normal = modules_get_state_condition(AGENT_MODULE_STATUS_NORMAL); + + $array_for_defect = array(); + $array_module_group = array(); + $array_data = array(); + + $sql = "SELECT id_mg, `name` FROM tmodule_group"; + $array_mod= db_get_all_rows_sql($sql); + + foreach ($array_mod as $key => $value) { + $array_module_group[$value['id_mg']] = $value['name']; + } + $array_module_group[0] = 'Nothing'; + foreach ($agents_counters as $key => $value) { + $array_for_defect[$key]['gm'] = $array_module_group; + $array_for_defect[$key]['data']['name'] = $value['name']; + $array_for_defect[$key]['data']['parent'] = $value['parent']; + $array_for_defect[$key]['data']['icon'] = $value['icon']; + } + +$sql = + "SELECT SUM(IF(tae.alert_fired <> 0, 1, 0)) AS alerts_module_count, + SUM(IF($condition_warning, 1, 0)) AS warning_module_count, + SUM(IF($condition_unknown, 1, 0)) AS unknown_module_count, + SUM(IF($condition_not_init, 1, 0)) AS notInit_module_count, + SUM(IF($condition_critical, 1, 0)) AS critical_module_count, + SUM(IF($condition_normal, 1, 0)) AS normal_module_count, + COUNT(tae.id_agente_modulo) AS total_count, + tmg.id_mg, + tmg.name as n, + tg.id_grupo + FROM ( + SELECT tam.id_agente_modulo, + tam.id_module_group, + ta.id_grupo AS g, + tae.estado, + SUM(IF(tatm.last_fired <> 0, 1, 0)) AS alert_fired + FROM tagente_modulo tam + LEFT JOIN talert_template_modules tatm + ON tatm.id_agent_module = tam.id_agente_modulo + LEFT JOIN tagente_estado tae + ON tae.id_agente_modulo = tam.id_agente_modulo + INNER JOIN tagente ta + ON ta.id_agente = tam.id_agente + WHERE ta.disabled = 0 + AND tam.disabled = 0 + AND tam.delete_pending = 0 + AND ta.id_grupo IN ($ids_group) + GROUP BY tam.id_agente_modulo + UNION ALL + SELECT tam.id_agente_modulo, + tam.id_module_group, + tasg.id_group AS g, + tae.estado, + SUM(IF(tatm.last_fired <> 0, 1, 0)) AS alert_fired + FROM tagente_modulo tam + LEFT JOIN talert_template_modules tatm + ON tatm.id_agent_module = tam.id_agente_modulo + LEFT JOIN tagente_estado tae + ON tae.id_agente_modulo = tam.id_agente_modulo + INNER JOIN tagente ta + ON ta.id_agente = tam.id_agente + INNER JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE ta.disabled = 0 + AND tam.disabled = 0 + AND tam.delete_pending = 0 + AND tasg.id_group IN ($ids_group) + GROUP BY tam.id_agente_modulo, tasg.id_group + ) AS tae + RIGHT JOIN tgrupo tg + ON tg.id_grupo = tae.g + INNER JOIN ( + SELECT * FROM tmodule_group + UNION ALL + SELECT 0 AS 'id_mg', 'Nothing' AS 'name' + ) AS tmg + ON tae.id_module_group = tmg.id_mg + GROUP BY tae.g, tmg.id_mg"; + + $array_data_prev = db_get_all_rows_sql($sql); + + foreach ($array_data_prev as $key => $value) { + $array_data[$value['id_grupo']][$value['id_mg']] = $value; + } ui_print_page_header (__("Combined table of agent group and module group"), "images/module_group.png", false, "", false, ''); - - ui_print_info_message ( array('no_close'=>true, 'message'=> - __("This table shows in columns the modules group and in rows agents group. The cell shows all modules") ) - ); - - - $agentGroups = users_get_groups ($config['id_user'], "AR", false); - $modelGroups = users_get_all_model_groups(); - - if (!empty($agentGroups) && !empty($modelGroups)) { - array_walk($modelGroups, 'translate'); //Translate all head titles to language is set - - $table = null; - $table->headstyle[] = "width: 20%"; - foreach ($modelGroups as $i => $n) { - $table->headstyle[] = "min-width: 60px;max-width: 5%;text-align:center;"; - $modelGroups[$i] = ui_print_truncate_text($n, GENERIC_SIZE_TEXT, true, true, true, '…', 'color:#FFF'); - } - - $head = $modelGroups; - array_unshift($head, ' '); - - //Metaobject use in html_print_table - $table->align[0] = 'left'; //Align to left the first column. - $table->style[0] = 'color: #ffffff; '. - 'background-color: #373737; font-weight: bolder; padding-right: 10px; width:20%; '; - $table->head = $head; - $table->width = '100%'; - - //The content of table - $tableData = array(); - - //Create rows and cells - foreach ($agentGroups as $idAgentGroup => $name) { - $fired = false; - $row = array(); - - array_push($row, ui_print_truncate_text($name, GENERIC_SIZE_TEXT, true, true, true, '…', 'color:#FFF')); - - foreach ($modelGroups as $idModelGroup => $modelGroup) { - $fired = false; - $query = sprintf($sql, $idAgentGroup, $idAgentGroup, $idModelGroup); - - $rowsDB = db_get_all_rows_sql ($query); - - $agents = agents_get_group_agents($idAgentGroup); - - if (!empty($agents)) { - $alerts = agents_get_alerts_simple(array_keys($agents)); - - foreach ($alerts as $alert) { - $module = db_get_row_filter('tagente_modulo', array('id_agente_modulo' => $alert['id_agent_module'])); - - if ($idModelGroup == $module['id_module_group']) { - if ($alert["times_fired"] > 0) { - $fired = true; - } - } - } - } - - $states = array(); - if ($rowsDB !== false) { - foreach ($rowsDB as $rowDB) { - $states[$rowDB['estado']] = $rowDB['count']; - } - } - - $count = 0; - foreach ($states as $idState => $state) { - $count += $state; - } - - $color = 'transparent'; //Defaut color for cell - $font_color = '#000000'; //Default font color for cell - if ($count == 0) { - $color = '#eeeeee'; //Soft grey when the cell for this model group and agent group hasn't modules. - $alinkStart = ''; - $alinkEnd = ''; - } - else { - - if ($fired) { - $color = '#FFA631'; //Orange when the cell for this model group and agent has at least one alert fired. - } - else if (array_key_exists(1, $states)) { - $color = '#FC4444'; //Red when the cell for this model group and agent has at least one module in critical state and the rest in any state. - $font_color = '#ffffff'; - } - elseif (array_key_exists(2, $states)) { - $color = '#FAD403'; //Yellow when the cell for this model group and agent has at least one in warning state and the rest in green state. - } - elseif (array_key_exists(3, $states)) { - $color = '#B2B2B2 '; //Grey when the cell for this model group and agent has at least one module in unknown state and the rest in any state. - } - elseif (array_key_exists(0, $states)) { - $color = '#80BA27'; //Green when the cell for this model group and agent has OK state all modules. - } - elseif (array_key_exists(5, $states)) { - $color = '#5BB6E5'; // Blue when the cell for this module group and all modules have not init value. - } - - - $alinkStart = ''; - $alinkEnd = ''; - } - - array_push($row, - '
- ' . $alinkStart . $count . $alinkEnd . '
'); - } - array_push($tableData,$row); - } - $table->data = $tableData; - echo "
"; - html_print_table($table); - echo "
"; - - echo "
"; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo "
" . __('Legend') . "
" . __("Orange cell when the module group and agent have at least one alarm fired.") . "
" . __("Red cell when the module group and agent have at least one module in critical status and the others in any status") . "
" . __("Yellow cell when the module group and agent have at least one in warning status and the others in grey or green status") . "
" . __("Green cell when the module group and agent have all modules in OK status") . "
" . __("Grey cell when the module group and agent have at least one in unknown status and the others in green status") . "
" .__("Blue cell when the module group and agent have all modules in not init status.") . "
"; + if(count($array_for_defect) > 0){ + $table = new StdClass(); + $table->style[0] = 'color: #ffffff; background-color: #373737; font-weight: bolder; padding-right: 10px; min-width: 230px;'; + $table->width = '100%'; + + $head[0] = __('Groups'); + $headstyle[0] = "width: 20%; font-weight: bolder;"; + foreach ($array_module_group as $key => $value) { + $headstyle[] = "min-width: 60px;max-width: 5%;text-align:center; color: #ffffff; background-color: #373737; font-weight: bolder;"; + $head[] = ui_print_truncate_text($value, GENERIC_SIZE_TEXT, true, true, true, '…', 'color:#FFF'); + } + + $i = 0; + foreach ($array_for_defect as $key => $value) { + $deep = groups_get_group_deep($key); + $data[$i][0] = $deep . ui_print_truncate_text($value['data']['name'], GENERIC_SIZE_TEXT, true, true, true, '…', 'color:#FFF'); + $j = 1; + if(isset($array_data[$key])){ + foreach ($value['gm'] as $k => $v) { + if(isset($array_data[$key][$k])){ + $send_tooltip = json_encode($array_data[$key][$k]); + $rel = "ajax.php?page=extensions/module_groups&get_info_alert_module_group=1&send_tooltip=" . $send_tooltip; + $url = "index.php?sec=estado&sec2=operation/agentes/status_monitor&status=-1&ag_group=" . $key . "&modulegroup=" . $k; + + if ($array_data[$key][$k]['alerts_module_count'] != 0) { + $color = '#FFA631'; //Orange when the cell for this model group and agent has at least one alert fired. + } + else if ($array_data[$key][$k]['critical_module_count'] != 0) { + $color = '#FC4444'; //Red when the cell for this model group and agent has at least one module in critical state and the rest in any state. + } + elseif ($array_data[$key][$k]['warning_module_count'] != 0) { + $color = '#FAD403'; //Yellow when the cell for this model group and agent has at least one in warning state and the rest in green state. + } + elseif ($array_data[$key][$k]['unknown_module_count'] != 0) { + $color = '#B2B2B2 '; //Grey when the cell for this model group and agent has at least one module in unknown state and the rest in any state. + } + elseif ($array_data[$key][$k]['normal_module_count'] != 0) { + $color = '#80BA27'; //Green when the cell for this model group and agent has OK state all modules. + } + elseif ($array_data[$key][$k]['notInit_module_count'] != 0) { + $color = '#5BB6E5'; // Blue when the cell for this module group and all modules have not init value. + } + + $data[$i][$j] ="
"; + $data[$i][$j] .= ""; + $data[$i][$j] .= $array_data[$key][$k]['total_count']; + $data[$i][$j] .= "
"; + } + else{ + $data[$i][$j] ="
"; + $data[$i][$j] .= 0; + $data[$i][$j] .= "
"; + } + $j++; + } + } + else{ + foreach ($value['gm'] as $k => $v) { + $data[$i][$j] ="
"; + $data[$i][$j] .= 0; + $data[$i][$j] .= "
"; + $j++; + } + } + $i++; + } + + $table->head = $head; + $table->headstyle = $headstyle; + $table->data = $data; + + ui_pagination($counter); + + echo "
"; + html_print_table($table); echo "
"; + + ui_pagination($counter); + + echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo "
" . __('Legend') . "
" . __("Orange cell when the module group and agent have at least one alarm fired.") . "
" . __("Red cell when the module group and agent have at least one module in critical status and the others in any status") . "
" . __("Yellow cell when the module group and agent have at least one in warning status and the others in grey or green status") . "
" . __("Grey cell when the module group and agent have at least one in unknown status and the others in green status") . "
" . __("Green cell when the module group and agent have all modules in OK status") . "
" .__("Blue cell when the module group and agent have all modules in not init status.") . "
"; + echo "
"; + } else { - echo "
".__('There are no defined groups or module groups')."
"; + ui_print_info_message ( array('no_close'=>true, 'message'=> __("This table shows in columns the modules group and in rows agents group. The cell shows all modules") )); + ui_print_info_message ( array('no_close'=>true, 'message'=> __('There are no defined groups or module groups') )); } - + ui_require_css_file('cluetip'); ui_require_jquery_file('cluetip'); ?> @@ -292,7 +270,8 @@ function mainModuleGroups() { $("a.info_cell").cluetip ({ arrows: true, attribute: 'rel', - cluetipClass: 'default' + cluetipClass: 'default', + width: '200px' }); }); diff --git a/pandora_console/include/class/TreeGroup.class.php b/pandora_console/include/class/TreeGroup.class.php index af5542e396..ced6b8d442 100644 --- a/pandora_console/include/class/TreeGroup.class.php +++ b/pandora_console/include/class/TreeGroup.class.php @@ -20,6 +20,7 @@ require_once($config['homedir']."/include/class/Tree.class.php"); class TreeGroup extends Tree { protected $propagateCounters = true; + protected $displayAllGroups = false; public function __construct($type, $rootType = '', $id = -1, $rootID = -1, $serverID = false, $childrenMethod = "on_demand", $access = 'AR') { @@ -44,6 +45,10 @@ class TreeGroup extends Tree { $this->propagateCounters = (bool)$value; } + public function setDisplayAllGroups($value) { + $this->displayAllGroups = (bool)$value; + } + protected function getData() { if ($this->id == -1) { $this->getFirstLevel(); @@ -132,12 +137,13 @@ class TreeGroup extends Tree { return !$group['have_parent']; }); // Propagate child counters to her parents + if ($this->propagateCounters) { TreeGroup::processCounters($groups); // Filter groups and eliminates the reference to empty groups - $groups = TreeGroup::deleteEmptyGroups($groups); + $groups = $this->deleteEmptyGroups($groups); } else { - $groups = TreeGroup::deleteEmptyGroupsNotPropagate($groups); + $groups = $this->deleteEmptyGroupsNotPropagate($groups); } usort($groups, array("Tree", "cmpSortNames")); @@ -345,7 +351,8 @@ class TreeGroup extends Tree { * * @return new_groups A new groups structure without empty groups */ - protected static function deleteEmptyGroups ($groups) { + protected function deleteEmptyGroups ($groups) { + if($this->displayAllGroups) return $groups; $new_groups = array(); foreach ($groups as $group) { // If a group is empty, do not add to new_groups. @@ -354,7 +361,7 @@ class TreeGroup extends Tree { } // Tray to remove the children groups if (!empty($group['children'])) { - $children = TreeGroup::deleteEmptyGroups ($group['children']); + $children = $this->deleteEmptyGroups ($group['children']); if (empty($children)) unset($group['children']); else $group['children'] = $children; } @@ -363,12 +370,13 @@ class TreeGroup extends Tree { return $new_groups; } - protected static function deleteEmptyGroupsNotPropagate ($groups) { + protected function deleteEmptyGroupsNotPropagate ($groups) { + if($this->displayAllGroups) return $groups; $new_groups = array(); foreach ($groups as $group) { // Tray to remove the children groups if (!empty($group['children'])) { - $children = TreeGroup::deleteEmptyGroupsNotPropagate ($group['children']); + $children = $this->deleteEmptyGroupsNotPropagate ($group['children']); if (empty($children)) { unset($group['children']); // If a group is empty, do not add to new_groups. diff --git a/pandora_console/include/styles/pandora.css b/pandora_console/include/styles/pandora.css index 8cdfa3a9e7..2184bd4a7a 100644 --- a/pandora_console/include/styles/pandora.css +++ b/pandora_console/include/styles/pandora.css @@ -4726,3 +4726,23 @@ a.up_arrow { tr:last-child > td > a.down_arrow, tr:first-child > td > a.up_arrow { visibility: hidden; } + +.tooltip_counters h3{ + font-size: 12pt; + padding-bottom: 10px !important; + text-align: center; +} + +.tooltip_counters li{ + font-size: 8pt; + margin: 2px; + margin-left: 5px; + +} +.tooltip_counters li div{ + width: 12px; + height: 12px; + border-radius: 3px; + float: left; + margin-right: 5px; +} \ No newline at end of file