fixed module tree view

This commit is contained in:
daniel 2018-08-30 12:23:35 +02:00
parent b346e4dca9
commit 6a124487a4
1 changed files with 163 additions and 45 deletions

View File

@ -1039,6 +1039,7 @@ class Tree {
break;
case 'module':
// ACL Group
//FIXME
$group_acl = "";
if (!empty($this->userGroups)) {
$user_groups_str = implode(",", array_keys($this->userGroups));
@ -1098,12 +1099,6 @@ class Tree {
ta.fired_count, ta.normal_count, ta.warning_count,
ta.critical_count, ta.unknown_count, ta.notinit_count,
ta.total_count, ta.quiet';
$group_by_fields = 'ta.id_agente, ta.nombre, ta.alias,
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.alias ASC, ta.id_agente ASC';
$symbols = ' !"#$%&\'()*+,./:;<=>?@[\\]^{|}~';
$name = $rootID;
for ($i = 0; $i < strlen($symbols); $i++) {
@ -1111,23 +1106,46 @@ class Tree {
ord(substr($symbols, $i, 1)) .'_pandoraartica_',
substr($symbols, $i, 1), $name);
}
$name = io_safe_input($name);
$this->filter['searchModule'] = $name = io_safe_input($name);
$filter_counters_where = "";
if(!empty($this->filter['searchModule'])){
$columns .=", (";
$columns .= sprintf("SELECT GROUP_CONCAT(tae.estado SEPARATOR ' ')
FROM tagente_modulo tam
INNER JOIN tagente_estado tae
ON tae.id_agente_modulo = tam.id_agente_modulo
WHERE tam.nombre LIKE '%%%s%%'
AND tam.disabled = 0
AND tam.id_agente = ta.id_agente
%s
GROUP BY tam.id_agente) AS filter_counters",
$this->filter['searchModule'],
$module_status_filter
);
$filter_counters_where = "WHERE filter_counters IS NOT NULL AND filter_counters <> ''";
}
$order_fields = 'ta.alias ASC, ta.id_agente ASC';
$inner_or_left = $this->filter['show_not_init_agents']
? "LEFT"
: "INNER";
$sql = "SELECT $columns
$sql = "SELECT * FROM (SELECT $columns
FROM tagente ta
INNER JOIN tagente_modulo tam
LEFT JOIN tagent_secondary_group tasg
ON tasg.id_agent = ta.id_agente
$inner_or_left JOIN tagente_modulo tam
ON tam.disabled = 0
AND ta.id_agente = tam.id_agente
AND tam.nombre = '$name'
$module_group_filter
$module_search_filter
$module_status_join
WHERE ta.disabled = 0
$group_acl
AND tam.nombre = '$name'
$module_status_from_agent
$group_filter
$agent_search_filter
$agent_status_filter
GROUP BY $group_by_fields
ORDER BY $order_fields";
$module_search_filter
GROUP BY ta.id_agente
ORDER BY $order_fields) as tx
$filter_counters_where";
}
break;
// Get the modules of an agent
@ -1159,28 +1177,21 @@ class Tree {
$agents_join .= " $group_acl";
}
// Set for the common ACL only. The strict ACL case is different (groups and tags divided).
// The modules only have visibility in two cases:
// 1. The user has access to the group of its agent and this group hasn't tags.
// 2. The user has access to the group of its agent, this group has tags and the module
// has any of this tags.
$tag_join = '';
if (!$this->strictACL) {
// $parent is the agent id
$group_id = (int) db_get_value('id_grupo', 'tagente', 'id_agente', $parent);
if (empty($group_id)) {
// ACL error, this will restrict (fuck) the module search
$tag_join = 'INNER JOIN ttag_module tta
ON 1=0';
}
else if (!empty($this->acltags) && isset($this->acltags[$group_id])) {
$tags_str = $this->acltags[$group_id];
// $parent is the agent id
$group_id = (int) db_get_value('id_grupo', 'tagente', 'id_agente', $parent);
if (empty($group_id)) {
// ACL error, this will restrict (fuck) the module search
$tag_join = 'INNER JOIN ttag_module tta
ON 1=0';
}
else if (!empty($this->acltags) && isset($this->acltags[$group_id])) {
$tags_str = $this->acltags[$group_id];
if (!empty($tags_str)) {
$tag_join = sprintf('INNER JOIN ttag_module ttm
ON tam.id_agente_modulo = ttm.id_agente_modulo
AND ttm.id_tag IN (%s)', $tags_str);
}
if (!empty($tags_str)) {
$tag_join = sprintf('INNER JOIN ttag_module ttm
ON tam.id_agente_modulo = ttm.id_agente_modulo
AND ttm.id_tag IN (%s)', $tags_str);
}
}
@ -1921,6 +1932,7 @@ class Tree {
// Groups
if ($this->id == -1) {
//$items = $this->getItems();
$processed_items = $this->getProcessedGroups();
if (!empty($processed_items)) {
@ -2056,17 +2068,123 @@ class Tree {
// Module names
if ($this->id == -1) {
if (! is_metaconsole()) {
$items = $this->getItems();
//FIXME REFACTOR ME, PLEASE
$fields = array (
"g AS nombre",
"SUM(x_critical) AS total_critical_count",
"SUM(x_warning) AS total_warning_count",
"SUM(x_normal) AS total_normal_count",
"SUM(x_unknown) AS total_unknown_count",
"SUM(x_not_init) AS total_notinit_count",
"SUM(x_alerts) AS total_alerts_count",
"SUM(x_total) AS total_count"
);
$fields = implode(", ", $fields);
$array_array = array(
'warning' => array(
'header' => "0 AS x_critical, SUM(total) AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g",
'condition' => "AND ta.warning_count > 0 AND ta.critical_count = 0"
),
'critical' => array(
'header' => "SUM(total) AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g",
'condition' => "AND ta.critical_count > 0"
),
'normal' => array(
'header' => "0 AS x_critical, 0 AS x_warning, SUM(total) AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g",
'condition' => "AND ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count = 0 AND ta.normal_count > 0"
),
'unknown' => array(
'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, SUM(total) AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g",
'condition' => "AND ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count > 0"
),
'not_init' => array(
'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, SUM(total) AS x_not_init, 0 AS x_alerts, 0 AS x_total, g",
'condition' => $this->filter['show_not_init_agents'] ? "AND ta.total_count = ta.notinit_count" : " AND 1=0"
),
'alerts' => array(
'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, SUM(total) AS x_alerts, 0 AS x_total, g",
'condition' => "AND ta.fired_count > 0"
),
'total' => array(
'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, SUM(total) AS x_total, g",
'condition' => $this->filter['show_not_init_agents'] ? "" : "AND ta.total_count <> ta.notinit_count"
)
);
$filters = array(
'agent_alias' => '',
'agent_status' => '',
'module_status' => '',
'module_search_condition' => '',
'module_status_inner' => '',
'group_search_condition' => '',
'group_search_inner' => ''
);
if (!empty($this->filter['searchAgent'])) {
$filters['agent_alias'] = "AND LOWER(ta.alias) LIKE LOWER('%".$this->filter['searchAgent']."%')";
}
if ($this->filter['statusAgent'] >= 0) {
$filters['agent_status'] = $this->getAgentStatusFilter();
}
if ($this->filter['statusModule'] >= 0) {
$filters['module_status_inner'] = "
INNER JOIN tagente_estado tae
ON tae.id_agente_modulo = tam.id_agente_modulo";
$filters['module_status'] = $this->getModuleStatusFilterFromTestado();
}
if (!empty($this->filter['searchGroup'])) {
$filters['group_search_inner'] = "
INNER JOIN tgrupo tg
ON ta.id_grupo = tg.id_grupo
OR tasg.id_group = tg.id_grupo";
$filters['group_search_condition'] = "AND tg.nombre LIKE '%" . $this->filter['searchGroup'] . "%'";
}
if (!empty($this->filter['searchModule'])) {
$filters['module_search_condition'] = " AND tam.nombre LIKE '%" . $this->filter['searchModule'] . "%' ";
}
$group_acl = "";
if (!users_can_manage_group_all("AR")) {
$user_groups_str = implode(",", $this->userGroupsArray);
$group_acl = " AND (ta.id_grupo IN ($user_groups_str) OR tasg.id_group IN ($user_groups_str))";
}
$sql_model = "SELECT %s FROM
(
SELECT COUNT(DISTINCT(ta.id_agente)) AS total, tam.nombre AS g
FROM tagente ta
LEFT JOIN tagent_secondary_group tasg
ON ta.id_agente = tasg.id_agent
INNER JOIN tagente_modulo tam
ON ta.id_agente = tam.id_agente
%s %s
WHERE ta.disabled = 0
AND tam.disabled = 0
%s %s %s
%s %s
%s %s
GROUP BY tam.nombre
) x GROUP BY g";
$sql_array = array();
foreach ($array_array as $s_array) {
$sql_array[] = sprintf(
$sql_model,
$s_array['header'],
$filters['module_status_inner'], $filters['group_search_inner'],
$s_array['condition'], $filters['agent_alias'], $filters['agent_status'],
$filters['module_status'], $filters['module_search_condition'],
$filters['group_search_condition'], $group_acl
);
}
$sql = "SELECT $fields FROM (" . implode(" UNION ALL ", $sql_array) . ") x2 GROUP BY g";
$items = db_get_all_rows_sql($sql);
//END REFACTOR ME, PLEASE
foreach ($items as $key => $item) {
$counters = $this->getCounters($item['name']);
if (!empty($counters)) {
foreach ($counters as $type => $value) {
$item[$type] = $value;
}
}
$item['name'] = $item['nombre'];
$name = str_replace(array(' ','#','/','.','(',')','¿','?','¡','!'),
array( '_articapandora_'.ord(' ').'_pandoraartica_',
'_articapandora_'.ord('#').'_pandoraartica_',