Adapted the code to improve the performance of the tree view on the metaconsole with the 'tmetaconsole_agent'

This commit is contained in:
Alejandro Gallardo Escobar 2015-04-17 16:12:51 +02:00
parent e0165a4ab3
commit 8826f0c80c
2 changed files with 166 additions and 179 deletions

View File

@ -352,66 +352,109 @@ class Tree {
if ($this->strictACL) if ($this->strictACL)
return false; return false;
$columns = 'tg.id_grupo AS id, tg.nombre AS name, tg.parent, tg.icon, COUNT(DISTINCT(ta.id_agente)) AS total_count'; $columns = 'tg.id_grupo AS id, tg.nombre AS name, tg.parent, tg.icon';
$order_fields = 'tg.nombre ASC, tg.id_grupo ASC'; $order_fields = 'tg.nombre ASC, tg.id_grupo ASC';
// Add the agent counters to the columns if (! defined('METACONSOLE')) {
$agent_table = "SELECT COUNT(DISTINCT(ta.id_agente)) // Add the agent counters to the columns
FROM tagente AS ta $agent_table = "SELECT COUNT(DISTINCT(ta.id_agente))
LEFT JOIN tagente_modulo AS tam FROM tagente AS ta
ON tam.disabled = 0 LEFT JOIN tagente_modulo AS tam
AND ta.id_agente = tam.id_agente ON tam.disabled = 0
$module_search_filter AND ta.id_agente = tam.id_agente
$module_status_join $module_search_filter
WHERE ta.disabled = 0 $module_status_join
AND ta.id_grupo = tg.id_grupo WHERE ta.disabled = 0
AND ta.id_grupo = tg.id_grupo
$group_acl
$agent_search_filter
$agent_status_filter";
$counter_columns = $this->getAgentCounterColumnsSql($agent_table);
if (!empty($counter_columns))
$columns .= ", $counter_columns";
$sql = "SELECT $columns
FROM tgrupo AS tg
LEFT JOIN tagente AS ta
LEFT JOIN tagente_modulo AS tam
ON tam.disabled = 0
AND ta.id_agente = tam.id_agente
$module_search_filter
$module_status_join
ON ta.disabled = 0
AND tg.id_grupo = ta.id_grupo
$group_acl $group_acl
$agent_search_filter $agent_search_filter
$agent_status_filter $agent_status_filter
$module_search_filter"; GROUP BY tg.id_grupo
$counter_columns = $this->getAgentCounterColumnsSql($agent_table); ORDER BY $order_fields";
if (!empty($counter_columns)) }
$columns .= ", $counter_columns"; // Metaconsole
else {
// Add the agent counters to the columns
$agent_table = "SELECT COUNT(DISTINCT(ta.id_agente))
FROM tmetaconsole_agent AS ta
WHERE ta.disabled = 0
AND ta.id_grupo = tg.id_grupo
$group_acl
$agent_search_filter
$agent_status_filter";
$counter_columns = $this->getAgentCounterColumnsSql($agent_table);
if (!empty($counter_columns))
$columns .= ", $counter_columns";
$sql = "SELECT $columns $sql = "SELECT $columns
FROM tgrupo AS tg FROM tgrupo AS tg
LEFT JOIN tagente AS ta LEFT JOIN tagente AS ta
LEFT JOIN tagente_modulo AS tam ON ta.disabled = 0
ON tam.disabled = 0 AND tg.id_grupo = ta.id_grupo
AND ta.id_agente = tam.id_agente $group_acl
$module_search_filter $agent_search_filter
$module_status_join $agent_status_filter
ON ta.disabled = 0 GROUP BY tg.id_grupo
AND tg.id_grupo = ta.id_grupo ORDER BY $order_fields";
}
}
else {
if (! defined('METACONSOLE') || $this->strictACL) {
$columns = 'ta.id_agente AS id, ta.nombre AS name,
ta.fired_count, ta.normal_count, ta.warning_count,
ta.critical_count, ta.unknown_count, ta.notinit_count,
ta.total_count, ta.quiet';
$order_fields = 'ta.nombre ASC, ta.id_agente ASC';
$sql = "SELECT $columns
FROM tagente AS ta
LEFT JOIN tagente_modulo AS tam
ON tam.disabled = 0
AND ta.id_agente = tam.id_agente
$module_search_filter
$module_status_join
WHERE ta.disabled = 0
AND ta.id_grupo = $rootID
$group_acl $group_acl
$agent_search_filter $agent_search_filter
$agent_status_filter $agent_status_filter
$module_search_filter GROUP BY ta.id_agente
GROUP BY tg.id_grupo ORDER BY $order_fields";
ORDER BY $order_fields"; }
} else {
else { $columns = 'ta.id_tagente AS id, ta.nombre AS name,
$columns = 'ta.id_agente AS id, ta.nombre AS name, ta.fired_count, ta.normal_count, ta.warning_count,
ta.fired_count, ta.normal_count, ta.warning_count, ta.critical_count, ta.unknown_count, ta.notinit_count,
ta.critical_count, ta.unknown_count, ta.notinit_count, ta.total_count, ta.quiet, id_tmetaconsole_setup AS server_id';
ta.total_count, ta.quiet'; $order_fields = 'ta.nombre ASC, ta.id_tagente ASC';
$order_fields = 'ta.nombre ASC, ta.id_agente ASC';
$sql = "SELECT $columns $sql = "SELECT $columns
FROM tagente AS ta FROM tmetaconsole_agent AS ta
LEFT JOIN tagente_modulo AS tam WHERE ta.disabled = 0
ON tam.disabled = 0 AND ta.id_grupo = $rootID
AND ta.id_agente = tam.id_agente $group_acl
$module_search_filter $agent_search_filter
$module_status_join $agent_status_filter
WHERE ta.disabled = 0 GROUP BY ta.id_tagente
AND ta.id_grupo = $rootID ORDER BY $order_fields";
$group_acl }
$agent_search_filter
$agent_status_filter
$module_search_filter
GROUP BY ta.id_agente
ORDER BY $order_fields";
} }
break; break;
// Get the modules of an agent // Get the modules of an agent
@ -1387,8 +1430,12 @@ class Tree {
$agent['rootID'] = $this->rootID; $agent['rootID'] = $this->rootID;
$agent['rootType'] = $this->rootType; $agent['rootType'] = $this->rootType;
if (defined("METACONSOLE") && !empty($server)) if (defined("METACONSOLE")) {
$agent['serverID'] = $server['id']; if (isset($agent['server_id']))
$agent['serverID'] = $agent['server_id'];
else if (!empty($server))
$agent['serverID'] = $server['id'];
}
// Realtime counters for Strict ACL // Realtime counters for Strict ACL
if ($this->strictACL) { if ($this->strictACL) {
@ -1535,37 +1582,37 @@ class Tree {
if (isset($agent['unknown_count'])) if (isset($agent['unknown_count']))
$agent['counters']['unknown'] = $agent['unknown_count']; $agent['counters']['unknown'] = $agent['unknown_count'];
else else
$agent['counters']['unknown'] = agents_monitor_unknown($agent['id']); $agent['counters']['unknown'] = (int) agents_monitor_unknown($agent['id']);
if (isset($agent['critical_count'])) if (isset($agent['critical_count']))
$agent['counters']['critical'] = $agent['critical_count']; $agent['counters']['critical'] = $agent['critical_count'];
else else
$agent['counters']['critical'] = agents_monitor_critical($agent['id']); $agent['counters']['critical'] = (int) agents_monitor_critical($agent['id']);
if (isset($agent['warning_count'])) if (isset($agent['warning_count']))
$agent['counters']['warning'] = $agent['warning_count']; $agent['counters']['warning'] = $agent['warning_count'];
else else
$agent['counters']['warning'] = agents_monitor_warning($agent['id']); $agent['counters']['warning'] = (int) agents_monitor_warning($agent['id']);
if (isset($agent['notinit_count'])) if (isset($agent['notinit_count']))
$agent['counters']['not_init'] = $agent['notinit_count']; $agent['counters']['not_init'] = $agent['notinit_count'];
else else
$agent['counters']['not_init'] = agents_monitor_notinit($agent['id']); $agent['counters']['not_init'] = (int) agents_monitor_notinit($agent['id']);
if (isset($agent['normal_count'])) if (isset($agent['normal_count']))
$agent['counters']['ok'] = $agent['normal_count']; $agent['counters']['ok'] = $agent['normal_count'];
else else
$agent['counters']['ok'] = agents_monitor_ok($agent['id']); $agent['counters']['ok'] = (int) agents_monitor_ok($agent['id']);
if (isset($agent['total_count'])) if (isset($agent['total_count']))
$agent['counters']['total'] = $agent['total_count']; $agent['counters']['total'] = $agent['total_count'];
else else
$agent['counters']['total'] = agents_monitor_total($agent['id']); $agent['counters']['total'] = (int) agents_monitor_total($agent['id']);
if (isset($agent['fired_count'])) if (isset($agent['fired_count']))
$agent['counters']['alerts'] = $agent['fired_count']; $agent['counters']['alerts'] = $agent['fired_count'];
else else
$agent['counters']['alerts'] = agents_get_alerts_fired($agent['id']); $agent['counters']['alerts'] = (int) agents_get_alerts_fired($agent['id']);
} }
// Status image // Status image
@ -1643,25 +1690,25 @@ class Tree {
} }
} }
private static function extractItemWithID ($items, $item_id, $item_type = "group") { private static function extractItemWithID ($items, $item_id, $item_type = "group", $strictACL = false) {
foreach ($items as $item) { foreach ($items as $item) {
if ($item["type"] != $item_type) if ($item["type"] != $item_type)
continue; continue;
// Item found // Item found
if (! defined("METACONSOLE")) { if ($strictACL && defined("METACONSOLE")) {
if ($item["id"] == $item_id)
return $item;
}
else {
foreach ($item["id"] as $server_id => $id) { foreach ($item["id"] as $server_id => $id) {
if ($id == $item_id) if ($id == $item_id)
return $item; return $item;
} }
} }
else {
if ($item["id"] == $item_id)
return $item;
}
if ($item["type"] == "group" && !empty($item["children"])) { if ($item["type"] == "group" && !empty($item["children"])) {
$result = self::extractItemWithID($item["children"], $item_id, $item_type); $result = self::extractItemWithID($item["children"], $item_id, $item_type, $strictACL);
// Item found on children // Item found on children
if ($result !== false) if ($result !== false)
@ -1675,7 +1722,7 @@ class Tree {
public function getData() { public function getData() {
if (! $this->strictACL) { if (! defined('METACONSOLE')) {
switch ($this->type) { switch ($this->type) {
case 'os': case 'os':
$this->getDataOS(); $this->getDataOS();
@ -1699,7 +1746,7 @@ class Tree {
$this->getDataExtended(); $this->getDataExtended();
} }
} }
else { else if ($this->strictACL) {
switch ($this->type) { switch ($this->type) {
case 'group': case 'group':
case 'tag': case 'tag':
@ -1710,6 +1757,14 @@ class Tree {
break; break;
} }
} }
else {
if ($this->type == 'agent') {
$this->getDataAgent();
}
else {
$this->getDataGroup();
}
}
} }
protected function getDataExtended () { protected function getDataExtended () {
@ -1737,19 +1792,13 @@ class Tree {
$server = metaconsole_get_servers($this->serverID); $server = metaconsole_get_servers($this->serverID);
if (metaconsole_connect($server) == NOERR) { if (metaconsole_connect($server) == NOERR) {
db_clean_cache(); $items = $this->getItems();
$this->processModules($items, $server);
$newItems = $this->getItems();
$this->processModules($newItems, $server);
$items = array_merge($items, $newItems);
metaconsole_restore_db(); metaconsole_restore_db();
} }
} }
if (!empty($items))
usort($items, array("Tree", "cmpSortNames"));
$processed_items = $items; $processed_items = $items;
} }
} }
@ -1897,56 +1946,27 @@ class Tree {
// Groups // Groups
if ($this->id == -1) { if ($this->id == -1) {
if (! defined ('METACONSOLE')) {
$items = $this->getItems();
// Build the group hierarchy $items = $this->getItems();
foreach ($items as $key => $item) {
if (empty($item['parent'])) {
unset($items[$key]); // Build the group hierarchy
$items_tmp = array(); foreach ($items as $key => $item) {
$processed_item = $this->getProcessedItem($item, false, $items, $items_tmp, true); if (empty($item['parent'])) {
if (!empty($processed_item) unset($items[$key]);
&& isset($processed_item['counters']) $items_tmp = array();
&& isset($processed_item['counters']['total']) $processed_item = $this->getProcessedItem($item, false, $items, $items_tmp, true);
&& !empty($processed_item['counters']['total']))
$processed_items[] = $processed_item; if (!empty($processed_item)
} && isset($processed_item['counters'])
&& isset($processed_item['counters']['total'])
&& !empty($processed_item['counters']['total']))
$processed_items[] = $processed_item;
} }
} }
else {
$servers = metaconsole_get_servers();
$item_list = array();
foreach ($servers as $server) {
if (metaconsole_connect($server) != NOERR)
continue;
db_clean_cache();
$items = $this->getItems();
// Build the group hierarchy
$processed_items = array();
foreach ($items as $key => $item) {
if (empty($item['parent'])) {
unset($items[$key]);
$processed_items[] = $this->getProcessedItem($item, $server, $items);
}
}
$item_list = array_merge($item_list, $processed_items);
metaconsole_restore_db();
}
$processed_items = $this->getMergedItems($item_list);
}
// groupID filter. To access the view from tactical views f.e. // groupID filter. To access the view from tactical views f.e.
if (!empty($processed_items) && !empty($this->filter['groupID'])) { if (!empty($processed_items) && !empty($this->filter['groupID'])) {
$result = self::extractItemWithID($processed_items, $this->filter['groupID'], "group"); $result = self::extractItemWithID($processed_items, $this->filter['groupID'], "group", $this->strictACL);
if ($result === false) if ($result === false)
$processed_items = array(); $processed_items = array();
@ -1956,35 +1976,9 @@ class Tree {
} }
// Agents // Agents
else { else {
if (! defined ('METACONSOLE')) { $items = $this->getItems();
$items = $this->getItems(); $this->processAgents($items);
$this->processAgents($items); $processed_items = $items;
$processed_items = $items;
}
else {
$rootIDs = $this->rootID;
$items = array();
foreach ($rootIDs as $serverID => $rootID) {
$server = metaconsole_get_servers($serverID);
if (metaconsole_connect($server) != NOERR)
continue;
db_clean_cache();
$this->rootID = $rootID;
$newItems = $this->getItems();
$this->processAgents($newItems, $server);
$items = array_merge($items, $newItems);
metaconsole_restore_db();
}
$this->rootID = $rootIDs;
if (!empty($items))
usort($items, array("Tree", "cmpSortNames"));
$processed_items = $items;
}
} }
$this->tree = $processed_items; $this->tree = $processed_items;

View File

@ -108,13 +108,7 @@ switch ($tab) {
break; break;
} }
if (defined('METACONSOLE')) { if (!defined('METACONSOLE')) {
if ($strict_acl)
$header_sub_title = '';
ui_meta_print_header($header_title, $header_sub_title, $tabs);
}
else{
if (!$strict_acl) if (!$strict_acl)
$header_title = $header_title ." - ". $header_sub_title; $header_title = $header_title ." - ". $header_sub_title;
@ -153,25 +147,24 @@ $table->rowspan[][count($row)-1] = 2;
$table->data[] = $row; $table->data[] = $row;
// Module filter if (!defined('METACONSOLE')) {
$module_status_arr = array(); // Module filter
$module_status_arr[-1] = __('All'); //default $module_status_arr = array();
$module_status_arr[AGENT_MODULE_STATUS_NORMAL] = __('Normal'); $module_status_arr[-1] = __('All'); //default
$module_status_arr[AGENT_MODULE_STATUS_WARNING] = __('Warning'); $module_status_arr[AGENT_MODULE_STATUS_NORMAL] = __('Normal');
$module_status_arr[AGENT_MODULE_STATUS_CRITICAL_BAD] = __('Critical'); $module_status_arr[AGENT_MODULE_STATUS_WARNING] = __('Warning');
$module_status_arr[AGENT_MODULE_STATUS_UNKNOWN] = __('Unknown'); $module_status_arr[AGENT_MODULE_STATUS_CRITICAL_BAD] = __('Critical');
$module_status_arr[AGENT_MODULE_STATUS_NOT_INIT] = __('Not init'); $module_status_arr[AGENT_MODULE_STATUS_UNKNOWN] = __('Unknown');
$module_status_arr[AGENT_MODULE_STATUS_NOT_INIT] = __('Not init');
$row = array(); $row = array();
$row[] = __('Module status'); $row[] = __('Module status');
$row[] = html_print_select($module_status_arr, "status_module", $status_module, '', '', 0, true); $row[] = html_print_select($module_status_arr, "status_module", $status_module, '', '', 0, true);
$row[] = __('Search module'); $row[] = __('Search module');
if (defined('METACONSOLE'))
$row[] = html_print_input_text("search_module", $search_module, '', 70, 30, true);
else
$row[] = html_print_input_text("search_module", $search_module, '', 40, 30, true); $row[] = html_print_input_text("search_module", $search_module, '', 40, 30, true);
$table->data[] = $row; $table->data[] = $row;
}
if (defined('METACONSOLE')) { if (defined('METACONSOLE')) {
$table->width = "96%"; $table->width = "96%";