Working in the trees

This commit is contained in:
Alejandro Gallardo Escobar 2015-01-14 17:56:42 +01:00
parent 84b6f70df4
commit fc65b60ead
2 changed files with 587 additions and 239 deletions

View File

@ -24,6 +24,9 @@ class Tree {
protected $userGroups;
protected $strictACL = false;
protected $acltags = false;
public function __construct($type, $root = null,
$childrenMethod = "on_demand",
$countModuleStatusMethod = "on_demand",
@ -64,8 +67,6 @@ class Tree {
$module['server_type'] = (int) $module['id_modulo'];
// $module['icon'] = modules_get_type_icon($module['id_tipo_modulo']);
if (!isset($module['status']))
$module['status'] = modules_get_status($module['id']);
if (!isset($module['value']))
$module['value'] = modules_get_last_value($module['id']);
@ -186,6 +187,7 @@ class Tree {
$agent['name'] = $agent['nombre'];
// Counters
if (empty($agent['counters'])) {
$agent['counters'] = array();
if (isset($agent['unknown_count']))
@ -222,6 +224,7 @@ class Tree {
$agent['counters']['alerts'] = $agent['fired_count'];
else
$agent['counters']['alerts'] = agents_get_alerts_fired($agent['id']);
}
// Status image
$agent['statusImageHTML'] = agents_tree_view_status_img_ball(
@ -258,6 +261,7 @@ class Tree {
}
// Children
if (empty($agent['children'])) {
$agent['children'] = array();
if ($agent['counters']['total'] > 0) {
switch ($this->childrenMethod) {
@ -283,6 +287,7 @@ class Tree {
}
}
}
}
protected function processAgents (&$agents, $modulesFilter = array()) {
if (!empty($agents)) {
@ -293,6 +298,18 @@ class Tree {
}
protected function getAgents ($parent = 0, $parentType = '') {
// Agent name filter
$agent_search = "";
if (!empty($this->filter['searchAgent'])) {
$agent_search = " AND ta.nombre LIKE '%".$this->filter['searchAgent']."%' ";
}
// Module name filter
$module_search = "";
if (!empty($this->filter['searchModule'])) {
$module_search = " AND tam.nombre LIKE '%".$this->filter['searchModule']."%' ";
}
switch ($parentType) {
case 'group':
// ACL Groups
@ -303,24 +320,139 @@ class Tree {
if (!isset($this->userGroups[$parent]))
return array();
}
$filter = array();
$filter['id_grupo'] = $parent;
if (isset($this->filter['status']) && $this->filter['status'] != -1)
$filter['status'] = $this->filter['status'];
if (!empty($this->filter['searchAgent']))
$filter['nombre'] = "%" . $this->filter['searchAgent'] . "%";
// TODO: Check ACL
$agents = agents_get_agents($filter, array('id_agente', 'nombre'));
if (empty($agents)) {
$agents = array();
// Get the agents. The modules are optional (LEFT JOIN), like their status
$sql = "SELECT ta.id_agente, ta.nombre AS agent_name, ta.fired_count,
ta.normal_count, ta.warning_count, ta.critical_count,
ta.unknown_count, ta.notinit_count, ta.total_count,
tam.id_agente_modulo, tam.nombre AS module_name,
tam.id_tipo_modulo, tam.id_modulo, tae.estado, tae.datos
FROM tagente AS ta
LEFT JOIN tagente_modulo AS tam
LEFT JOIN tagente_estado AS tae
ON tam.id_agente_modulo IS NOT NULL
AND tam.id_agente_modulo = tae.id_agente_modulo
ON tam.disabled = 0
AND ta.id_agente = tam.id_agente
$module_search
WHERE ta.id_grupo = $parent
AND ta.disabled = 0
$agent_search
ORDER BY ta.nombre ASC, ta.id_agente ASC, tam.nombre ASC, tam.id_agente_modulo ASC";
$data = db_process_sql($sql);
break;
case 'tag':
$groups_clause = "";
if (!empty($this->acltags)) {
$i = 0;
$groups = array();
foreach ($this->acltags as $group_id => $tags) {
if (!empty($tags)) {
$tags_arr = explode(',', $tags);
if (in_array($id_tag, $tags_arr))
$groups[] = $group_id;
}
}
if (!empty($groups)) {
$groups_str = implode(",", $groups);
$groups_clause = " AND ta.id_grupo IN ($groups_str)";
}
}
// Get the agents. The modules are required (INNER JOIN), although their status
$sql = "SELECT ta.id_agente, ta.nombre AS agent_name, ta.fired_count,
ta.normal_count, ta.warning_count, ta.critical_count,
ta.unknown_count, ta.notinit_count, ta.total_count,
tam.id_agente_modulo, tam.nombre AS module_name,
tam.id_tipo_modulo, tam.id_modulo, tae.estado, tae.datos
FROM tagente AS ta
INNER JOIN tagente_modulo AS tam
ON tam.disabled = 0
AND ta.id_agente = tam.id_agente
$module_search
INNER JOIN ttag_module AS ttm
ON ttm.id_tag = $parent
AND tam.id_agente_modulo = ttm.id_agente_modulo
LEFT JOIN tagente_estado AS tae
ON tam.id_agente_modulo = tae.id_agente_modulo
WHERE ta.disabled = 0
$groups_clause
$agent_search
ORDER BY ta.nombre ASC, ta.id_agente ASC, tam.nombre ASC, tam.id_agente_modulo ASC";
$data = db_process_sql($sql);
break;
default:
return array();
break;
}
$this->processAgents($agents);
if (empty($data))
return array();
$agents = array();
$actual_agent = array();
foreach ($data as $key => $value) {
if (empty($actual_agent) || $actual_agent['id_agente'] != (int)$value['id_agente']) {
if (!empty($actual_agent)) {
$this->processAgent(&$actual_agent, array(), false);
$agents[] = $actual_agent;
}
$actual_agent = array();
$actual_agent['id_agente'] = (int) $value['id_agente'];
$actual_agent['nombre'] = $value['agent_name'];
$actual_agent['children'] = array();
// Initialize counters
$actual_agent['counters'] = array();
$actual_agent['counters']['total'] = 0;
$actual_agent['counters']['alerts'] = 0;
$actual_agent['counters']['critical'] = 0;
$actual_agent['counters']['warning'] = 0;
$actual_agent['counters']['unknown'] = 0;
$actual_agent['counters']['not_init'] = 0;
$actual_agent['counters']['ok'] = 0;
// $actual_agent['counters'] = array();
// $actual_agent['counters']['total'] = (int) $value['total_count'];
// $actual_agent['counters']['alerts'] = (int) $value['fired_count_count'];
// $actual_agent['counters']['critical'] = (int) $value['critical_count'];
// $actual_agent['counters']['warning'] = (int) $value['warning_count'];
// $actual_agent['counters']['unknown'] = (int) $value['unknown_count'];
// $actual_agent['counters']['not_init'] = (int) $value['notinit_count'];
// $actual_agent['counters']['ok'] = (int) $value['normal_count'];
}
if (empty($value['id_agente_modulo']))
continue;
$module = array();
$module['id_agente_modulo'] = (int) $value['id_agente_modulo'];
$module['nombre'] = $value['module_name'];
$module['id_tipo_modulo'] = (int) $value['id_tipo_modulo'];
$module['server_type'] = (int) $value['id_modulo'];
$module['status'] = (int) $value['estado'];
$module['value'] = $value['data'];
$this->processModule($module);
$actual_agent['children'][] = $module;
$actual_agent['counters']['total']++;
if (isset($actual_agent['counters'][$module['statusText']]))
$actual_agent['counters'][$module['statusText']]++;
if ($module['alert'])
$actual_agent['counters']['alerts']++;
}
if (!empty($actual_agent)) {
$this->processAgent(&$actual_agent, array(), false);
$agents[] = $actual_agent;
}
return $agents;
}
@ -473,19 +605,168 @@ class Tree {
}
private function getDataGroup() {
global $config;
// Get the parent
if (empty($this->root))
$parent = 0;
else
$parent = $this->root;
$groups = $this->getGroupsRecursive($parent);
// Get all groups
if (empty($parent)) {
require_once($config['homedir']."/include/functions_groups.php");
// Return all the children groups
function __searchChildren(&$groups, $id) {
$children = array();
foreach ($groups as $key => $group) {
if (isset($group['_parent_id_']) && $group['_parent_id_'] == $id) {
$processed_group = array();
$processed_group['id'] = $group['_id_'];
$processed_group['parentID'] = $group['_parent_id_'];
$processed_group['name'] = $group['_name_'];
$processed_group['iconHTML'] = $group['_iconImg_'];
$processed_group['type'] = 'group';
$processed_group['searchChildren'] = 1;
$counters = array();
if (isset($group['_agents_unknown_']))
$counters['unknown'] = $group['_agents_unknown_'];
if (isset($group['_agents_critical_']))
$counters['critical'] = $group['_agents_critical_'];
if (isset($group['_agents_warning_']))
$counters['warning'] = $group['_agents_warning_'];
if (isset($group['_agents_not_init_']))
$counters['not_init'] = $group['_agents_not_init_'];
if (isset($group['_agents_ok_']))
$counters['ok'] = $group['_agents_ok_'];
if (isset($group['_total_agents_']))
$counters['total'] = $group['_total_agents_'];
if (isset($group['_monitors_alerts_fired_']))
$counters['alerts'] = $group['_monitors_alerts_fired_'];
$children = __searchChildren($groups, $group['_id_']);html_debug_print($children, true);
if (!empty($children)) {
$processed_group['children'] = $children;
foreach ($children as $key => $child) {
if (isset($child['counters'])) {
foreach ($child['counters'] as $type => $value) {
if (isset($counters[$type]))
$counters[$type] += $value;
}
}
}
}
if (!empty($counters))
$processed_group['counters'] = $counters;
$children[] = $processed_group;
unset($groups[$key]);
}
}
return $children;
}
if (! defined ('METACONSOLE')) {
$groups = group_get_data($config['id_user'], $this->strictACL, $this->acltags, false, 'tree');
} else {
}
//$groups = group_get_groups_list($config['id_user'], true, 'AR', true, false, 'tree');
// Build the group hierarchy
foreach ($groups as $key => $group) {
if (empty($group['_is_tag_']))
$children = __searchChildren($groups, $group['_id_']);
if (!empty($children))
$groups[$key]['children'] = $children;
}
// Process the groups for the tree
$processed_groups = array();
foreach ($groups as $key => $group) {
$processed_group = array();
$processed_group['id'] = $group['_id_'];
$processed_group['name'] = $group['_name_'];
$processed_group['searchChildren'] = 1;
if (!empty($group['_iconImg_']))
$processed_group['iconHTML'] = $group['_iconImg_'];
if (!empty($group['_parent_id_']))
$processed_group['parentID'] = $group['_parent_id_'];
if (empty($group['_is_tag_']))
$processed_group['type'] = 'group';
else
$processed_group['type'] = 'tag';
$counters = array();
if (isset($group['_agents_unknown_']))
$counters['unknown'] = $group['_agents_unknown_'];
if (isset($group['_agents_critical_']))
$counters['critical'] = $group['_agents_critical_'];
if (isset($group['_agents_warning_']))
$counters['warning'] = $group['_agents_warning_'];
if (isset($group['_agents_not_init_']))
$counters['not_init'] = $group['_agents_not_init_'];
if (isset($group['_agents_ok_']))
$counters['ok'] = $group['_agents_ok_'];
if (isset($group['_total_agents_']))
$counters['total'] = $group['_total_agents_'];
if (isset($group['_monitors_alerts_fired_']))
$counters['alerts'] = $group['_monitors_alerts_fired_'];
if (!empty($group['children'])) {
$processed_group['children'] = $group['children'];
if ($processed_group['type'] == 'group') {
foreach ($processed_group['children'] as $key => $child) {
if (isset($child['counters'])) {
foreach ($child['counters'] as $type => $value) {
if (isset($counters[$type]))
$counters[$type] += $value;
}
}
}
}
}
if (!empty($counters))
$processed_group['counters'] = $counters;
$processed_groups[] = $processed_group;
}
$groups = $processed_groups;
// $groups = $this->getGroupsRecursive($parent);
if (empty($groups))
$groups = array();
$this->tree = $groups;
}
// Get the group agents
else {
$this->tree = $this->getAgents($parent, $this->type);
}
}
private function getDataModules() {
// ACL Group
@ -520,12 +801,12 @@ class Tree {
INNER JOIN tagente AS ta
ON ta.id_agente = tam.id_agente
AND ta.disabled = 0
$agent_search
$group_acl
INNER JOIN tagente_estado AS tae
ON tae.id_agente_modulo = tam.id_agente_modulo
WHERE tam.disabled = 0
$agent_search
$module_search
$group_acl
ORDER BY tam.nombre ASC, ta.nombre ASC";
$data = db_process_sql($sql);
@ -544,6 +825,15 @@ class Tree {
$agent['id_agente'] = (int) $value['id_agente'];
$agent['nombre'] = $value['agent_name'];
$agent['counters'] = array();
$agent['counters']['total'] = (int) $value['total_count'];
$agent['counters']['alerts'] = (int) $value['fired_count_count'];
$agent['counters']['critical'] = (int) $value['critical_count'];
$agent['counters']['warning'] = (int) $value['warning_count'];
$agent['counters']['unknown'] = (int) $value['unknown_count'];
$agent['counters']['not_init'] = (int) $value['notinit_count'];
$agent['counters']['ok'] = (int) $value['normal_count'];
$this->processAgent(&$agent, array(), false);
$module = array();
@ -633,14 +923,14 @@ class Tree {
INNER JOIN tagente AS ta
ON ta.id_agente = tam.id_agente
AND ta.disabled = 0
$agent_search
$group_acl
INNER JOIN tagente_estado AS tae
ON tae.id_agente_modulo = tam.id_agente_modulo
LEFT JOIN tmodule_group AS tmg
ON tmg.id_mg = tam.id_module_group
WHERE tam.disabled = 0
$agent_search
$module_search
$group_acl
ORDER BY tmg.name ASC, tmg.id_mg ASC, ta.nombre ASC, tam.nombre ASC";
$data = db_process_sql($sql);
@ -684,6 +974,15 @@ class Tree {
$actual_agent['nombre'] = $value['agent_name'];
$actual_agent['children'] = array();
$actual_agent['counters'] = array();
$actual_agent['counters']['total'] = (int) $value['total_count'];
$actual_agent['counters']['alerts'] = (int) $value['fired_count_count'];
$actual_agent['counters']['critical'] = (int) $value['critical_count'];
$actual_agent['counters']['warning'] = (int) $value['warning_count'];
$actual_agent['counters']['unknown'] = (int) $value['unknown_count'];
$actual_agent['counters']['not_init'] = (int) $value['notinit_count'];
$actual_agent['counters']['ok'] = (int) $value['normal_count'];
$this->processAgent(&$actual_agent, array(), false);
// Add the module to the agent
@ -714,6 +1013,15 @@ class Tree {
$actual_agent['nombre'] = $value['agent_name'];
$actual_agent['children'] = array();
$actual_agent['counters'] = array();
$actual_agent['counters']['total'] = (int) $value['total_count'];
$actual_agent['counters']['alerts'] = (int) $value['fired_count_count'];
$actual_agent['counters']['critical'] = (int) $value['critical_count'];
$actual_agent['counters']['warning'] = (int) $value['warning_count'];
$actual_agent['counters']['unknown'] = (int) $value['unknown_count'];
$actual_agent['counters']['not_init'] = (int) $value['notinit_count'];
$actual_agent['counters']['ok'] = (int) $value['normal_count'];
$this->processAgent(&$actual_agent, array(), false);
// Add the module to the agent
@ -793,14 +1101,14 @@ class Tree {
INNER JOIN tagente AS ta
ON ta.id_agente = tam.id_agente
AND ta.disabled = 0
$agent_search
$group_acl
INNER JOIN tagente_estado AS tae
ON tae.id_agente_modulo = tam.id_agente_modulo
LEFT JOIN tconfig_os AS tos
ON tos.id_os = ta.id_os
WHERE tam.disabled = 0
$agent_search
$module_search
$group_acl
ORDER BY tos.icon_name ASC, tos.id_os ASC, ta.nombre ASC, tam.nombre";
$data = db_process_sql($sql);
@ -844,6 +1152,15 @@ class Tree {
$actual_agent['nombre'] = $value['agent_name'];
$actual_agent['children'] = array();
$actual_agent['counters'] = array();
$actual_agent['counters']['total'] = (int) $value['total_count'];
$actual_agent['counters']['alerts'] = (int) $value['fired_count_count'];
$actual_agent['counters']['critical'] = (int) $value['critical_count'];
$actual_agent['counters']['warning'] = (int) $value['warning_count'];
$actual_agent['counters']['unknown'] = (int) $value['unknown_count'];
$actual_agent['counters']['not_init'] = (int) $value['notinit_count'];
$actual_agent['counters']['ok'] = (int) $value['normal_count'];
$this->processAgent(&$actual_agent, array(), false);
// Add the module to the agent
@ -889,6 +1206,15 @@ class Tree {
$actual_agent['nombre'] = $value['agent_name'];
$actual_agent['children'] = array();
$actual_agent['counters'] = array();
$actual_agent['counters']['total'] = (int) $value['total_count'];
$actual_agent['counters']['alerts'] = (int) $value['fired_count_count'];
$actual_agent['counters']['critical'] = (int) $value['critical_count'];
$actual_agent['counters']['warning'] = (int) $value['warning_count'];
$actual_agent['counters']['unknown'] = (int) $value['unknown_count'];
$actual_agent['counters']['not_init'] = (int) $value['notinit_count'];
$actual_agent['counters']['ok'] = (int) $value['normal_count'];
$this->processAgent(&$actual_agent, array(), false);
// Add the module to the agent
@ -923,6 +1249,15 @@ class Tree {
}
private function getDataTag() {
// Get the parent
if (empty($this->root))
$parent = 0;
else
$parent = $this->root;
// Get all groups
if (empty($parent)) {
// ACL Group
if (isset($this->userGroups) && $this->userGroups === false)
return array();
@ -1076,6 +1411,10 @@ class Tree {
$this->tree = $nodes;
}
else {
$this->tree = $this->getAgents($parent, $this->type);
}
}
public function getJSON() {
$this->getData();

View File

@ -49,7 +49,6 @@ TreeController = {
}
// Normal group
else {
rootGroup = false;
$group
.addClass("tree-group")
.hide();
@ -57,12 +56,8 @@ TreeController = {
container.append($group);
var lastNode;
var firstNode;
elements.forEach(function(element, index) {
lastNode = index == elements.length - 1 ? true : false;
firstNode = rootGroup && index == 0 ? true : false;
element.jqObject = _processNode($group, element, lastNode, firstNode);
element.jqObject = _processNode($group, element);
}, $group);
return $group;
@ -287,7 +282,7 @@ TreeController = {
}
// Load leaf
function _processNode (container, element, lastNode, firstNode) {
function _processNode (container, element) {
var $node = $("<li></li>");
var $leafIcon = $("<div></div>");
var $content = $("<div></div>");
@ -303,6 +298,9 @@ TreeController = {
$content.append('<img src="'+(controller.baseURL.length > 0 ? controller.baseURL : '')
+'images/groups_small/'+element.icon+'" /> ');
}
else if (typeof element.iconHTML != 'undefined' && element.iconHTML.length > 0) {
$content.append(element.iconHTML);
}
$content.append(element.name);
break;
case 'agent':
@ -426,22 +424,20 @@ TreeController = {
.append($leafIcon)
.append($content);
if (typeof lastNode != 'undefinded' && lastNode == true) {
$node.addClass("tree-last");
}
if (typeof firstNode != 'undefinded' && firstNode == true) {
$node.addClass("tree-first");
}
container.append($node);
$node.addClass("leaf-empty");
if (typeof element.children != 'undefined' && element.children.length > 0) {
$node.addClass("leaf-closed");
$node
.removeClass("leaf-empty")
.addClass("leaf-closed");
// Add children
var $children = _processGroup($node, element.children);
$node.data('children', $children);
if (typeof element.searchChildren == 'undefined' || !element.searchChildren) {
$leafIcon.click(function (e) {
e.preventDefault();
@ -461,8 +457,11 @@ TreeController = {
}
});
}
else if (typeof element.searchChildren != 'undefined' && element.searchChildren) {
$node.addClass("leaf-closed");
}
if (typeof element.searchChildren != 'undefined' && element.searchChildren) {
$node
.removeClass("leaf-empty")
.addClass("leaf-closed");
$leafIcon.click(function (e) {
e.preventDefault();
@ -492,17 +491,30 @@ TreeController = {
$node.addClass("children-loaded");
},
success: function(data, textStatus, xhr) {
data = $.parseJSON(data);
if (data.success) {
if (typeof data.tree != 'undefined' && data.tree.length > 0) {
$node.addClass("leaf-open");
var $children = _processGroup($node, data.tree);
$children.slideDown();
var $group = $node.find("ul.tree-group");
$node.data('children', $children);
if ($group.length <= 0) {
$group = $("<ul></ul>");
$group
.addClass("tree-group")
.hide();
$node.append($group);
}
data.tree.forEach(function(element, index) {
element.jqObject = _processNode($group, element);
}, $group);
$group.slideDown();
$node.data('children', $group);
// Add again the hover event to the 'force_callback' elements
forced_title_callback();
}
else {
$node.addClass("leaf-empty");
@ -535,9 +547,6 @@ TreeController = {
}
});
}
else {
$node.addClass("leaf-empty");
}
return $node;
}