diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index 53cf2232ad..a19b84db25 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,16 @@
+2012-07-12 Juan Manuel Ramon <juanmanuel.ramon@artica.es>
+
+		* include/functions_modules.php
+		 include/functions_groups.php
+		 include/functions_os.php
+		 include/functions_tags.php
+		 operation/tree.php: Fixes in tree view counts.
+		 
+		* operation/agentes/status_monitor.php: Fixed module group 
+		filter.
+		 
+		 Pending to port to branches.
+
 2012-07-10 Juan Manuel Ramon <juanmanuel.ramon@artica.es>
 
 	* include/functions_agents.php
diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php
index 6cefab17bd..b59e9e8bb7 100644
--- a/pandora_console/include/functions_groups.php
+++ b/pandora_console/include/functions_groups.php
@@ -853,9 +853,51 @@ function groups_agent_unknown ($group_array) {
 	$group_clause = implode (",", $group_array);
 	$group_clause = "(" . $group_clause . ")";
 	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT(min_estado) FROM (SELECT MIN(tagente_estado.estado) as min_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente_estado.estado != 0 AND tagente.id_grupo IN $group_clause GROUP BY tagente.id_agente HAVING min_estado = 3) AS S1");
+	// Agent of module group X and critical status
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";		
+
+	// Agent of module group X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";
+
+	// Agent of module group X and unknown status		
+	$agents_unknown = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 3
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";	
+
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente.id_grupo IN $group_clause 
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente NOT IN ($agents_warning)
+						AND tagente.id_agente IN ($agents_unknown) ) AS t");
 	
 }
 
@@ -873,17 +915,65 @@ function groups_agent_ok ($group_array) {
 	$group_clause = implode (",", $group_array);
 	$group_clause = "(" . $group_clause . ")";
 	
-	//!!!Query explanation!!!
-	//An agent is OK if all its modules are OK
-	//The status values are: 0 OK; 1 Critical; 2 Warning; 3 Unkown
-	//This query grouped all modules by agents and select the MAX value for status which has the value 0 
-	//If MAX(estado) is 0 it means all modules has status 0 => OK
-	//Then we count the agents of the group selected to know how many agents are in OK status
-	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT(max_estado) FROM (SELECT MAX(tagente_estado.estado) as max_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente.id_grupo IN $group_clause GROUP BY tagente.id_agente HAVING max_estado = 0) AS S1");
+	// Agent of module group X and critical status
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";		
+
+	// Agent of module group X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";
+
+	// Agent of module group X and unknown status		
+	$agents_unknown = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 3
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";
+						
+	// Agent of module group X and ok status		
+	$agents_ok = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 0
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";
 	
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente.id_grupo IN $group_clause 
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente NOT IN ($agents_warning)
+						AND tagente.id_agente NOT IN ($agents_unknown)
+						AND tagente.id_agente IN ($agents_ok) ) AS t");	
+
 }
 
 // Get critical agents by using the status code in modules.
@@ -926,16 +1016,40 @@ function groups_agent_warning ($group_array) {
 	$group_clause = implode (",", $group_array);
 	$group_clause = "(" . $group_clause . ")";
 	
-	//!!!Query explanation!!!
-	//An agent is Warning when has at least one module in warning status and nothing more in critical status
-	//The status values are: 0 OK; 1 Critical; 2 Warning; 3 Unkown
-	//This query grouped all modules by agents and select the MIN value for status which has the value 0 
-	//If MIN(estado) is 2 it means at least one module is warning and there is no critical modules
-	//Then we count the agents of the group selected to know how many agents are in warning status
+
+	// Agent of group X and critical status	
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";
+						
+	// Agent of group X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_grupo IN $group_clause
+						group by tagente.id_agente";
+
 	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT(min_estado) FROM (SELECT MIN(tagente_estado.estado) as min_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente.id_grupo IN $group_clause GROUP BY tagente.id_agente HAVING min_estado = 2) AS S1");
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente.id_grupo IN $group_clause
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente IN ($agents_warning) ) AS t");
 	
 }
 
diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php
index ddd0ac22f7..9a18602436 100644
--- a/pandora_console/include/functions_modules.php
+++ b/pandora_console/include/functions_modules.php
@@ -1290,7 +1290,14 @@ function modules_agents_critical ($module_name) {
 	
 	//TODO REVIEW ORACLE AND POSTGRES
 	
-	return db_get_sql ("SELECT COUNT( DISTINCT tagente_estado.id_agente) FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND estado = 1 AND tagente_estado.id_agente = tagente.id_agente AND tagente_modulo.nombre = '$module_name'");
+	return db_get_sql ("SELECT COUNT( DISTINCT tagente_estado.id_agente) 
+						FROM tagente_estado, tagente, tagente_modulo 
+						WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo 
+						AND tagente_modulo.disabled = 0 
+						AND estado = 1 
+						AND tagente_estado.id_agente = tagente.id_agente 
+						AND tagente_modulo.nombre = '$module_name'");
 	
 }
 
@@ -1307,7 +1314,16 @@ function modules_agents_warning ($module_name) {
 	
 	//TODO REVIEW ORACLE AND POSTGRES
 	
-	return db_get_sql ("SELECT COUNT(min_estado) FROM (SELECT MIN(tagente_estado.estado) as min_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente_modulo.nombre = '$module_name' GROUP BY tagente.id_agente HAVING min_estado = 2) AS S1");
+	return db_get_sql ("SELECT COUNT(min_estado) 
+						FROM (SELECT MAX(tagente_estado.estado) as min_estado 
+								FROM tagente_estado, tagente, tagente_modulo 
+								WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 
+								AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo 
+								AND tagente_modulo.disabled = 0 
+								AND tagente_estado.id_agente = tagente.id_agente 
+								AND tagente_modulo.nombre = '$module_name' 
+								GROUP BY tagente.id_agente 
+								HAVING min_estado = 2) AS S1");
 	
 }
 
@@ -1315,9 +1331,51 @@ function modules_agents_warning ($module_name) {
 
 function modules_group_agent_unknown ($module_group) {
 	
-	//TODO REVIEW ORACLE AND POSTGRES
+	// Agent of module group X and critical status
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";		
 
-	return db_get_sql ("SELECT COUNT(min_estado) FROM (SELECT MIN(tagente_estado.estado) as min_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente_estado.estado != 0 AND tagente_modulo.id_module_group = $module_group GROUP BY tagente.id_agente HAVING min_estado = 3) AS S1");
+	// Agent of module group X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";
+
+	// Agent of module group X and unknown status		
+	$agents_unknown = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 3
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";	
+
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente_modulo.id_module_group = $module_group 
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente NOT IN ($agents_warning)
+						AND tagente.id_agente IN ($agents_unknown) ) AS t");
 	
 }
 
@@ -1325,16 +1383,64 @@ function modules_group_agent_unknown ($module_group) {
 
 function modules_group_agent_ok ($module_group) {
 		
-	//!!!Query explanation!!!
-	//An agent is OK if all its modules are OK
-	//The status values are: 0 OK; 1 Critical; 2 Warning; 3 Unkown
-	//This query grouped all modules by agents and select the MAX value for status which has the value 0 
-	//If MAX(estado) is 0 it means all modules has status 0 => OK
-	//Then we count the agents of the group selected to know how many agents are in OK status
-	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT(max_estado) FROM (SELECT MAX(tagente_estado.estado) as max_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente_modulo.id_module_group = $module_group GROUP BY tagente.id_agente HAVING max_estado = 0) AS S1");
+	// Agent of module group X and critical status
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";		
+
+	// Agent of module group X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";
+
+	// Agent of module group X and unknown status		
+	$agents_unknown = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 3
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";
+						
+	// Agent of module group X and ok status		
+	$agents_ok = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 0
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";								
+
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente_modulo.id_module_group = $module_group 
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente NOT IN ($agents_warning)
+						AND tagente.id_agente NOT IN ($agents_unknown)
+						AND tagente.id_agente IN ($agents_ok) ) AS t");
 	
 }
 
@@ -1342,15 +1448,25 @@ function modules_group_agent_ok ($module_group) {
 
 function modules_group_agent_critical ($module_group) {
 	
-	//!!!Query explanation!!!
-	//An agent is Warning when has at least one module in warning status and nothing more in critical status
-	//The status values are: 0 OK; 1 Critical; 2 Warning; 3 Unkown
-	//If estado = 1 it means at leas 1 module is in critical status so the agent is critical
-	//Then we count the agents of the group selected to know how many agents are in critical status	
+	// Agent of module group X and critical status	
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";		
 	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT( DISTINCT tagente_estado.id_agente) FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND estado = 1 AND tagente_estado.id_agente = tagente.id_agente AND tagente_modulo.id_module_group = $module_group");
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente_modulo.id_module_group = $module_group 
+						AND tagente.id_agente IN ($agents_critical) ) AS t");
 	
 }
 
@@ -1358,17 +1474,39 @@ function modules_group_agent_critical ($module_group) {
 
 function modules_group_agent_warning ($module_group) {
 
-	//!!!Query explanation!!!
-	//An agent is Warning when has at least one module in warning status and nothing more in critical status
-	//The status values are: 0 OK; 1 Critical; 2 Warning; 3 Unkown
-	//This query grouped all modules by agents and select the MIN value for status which has the value 0 
-	//If MIN(estado) is 2 it means at least one module is warning and there is no critical modules
-	//Then we count the agents of the group selected to know how many agents are in warning status
-	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT(min_estado) FROM (SELECT MIN(tagente_estado.estado) as min_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente_modulo.id_module_group = $module_group GROUP BY tagente.id_agente HAVING min_estado = 2) AS S1");
+	// Agent of module group X and critical status	
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";
+						
+	// Agent of module group X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente_modulo.id_module_group = $module_group
+						group by tagente.id_agente";
 	
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente_modulo.id_module_group = $module_group 
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente IN ($agents_warning) ) AS t");
+
 }
 
 ?>
diff --git a/pandora_console/include/functions_os.php b/pandora_console/include/functions_os.php
index de4bc87c58..d6e4036d49 100755
--- a/pandora_console/include/functions_os.php
+++ b/pandora_console/include/functions_os.php
@@ -26,33 +26,104 @@ function os_agents_critical ($id_os) {
 // Get ok agents by using the status code in modules.
 
 function os_agents_ok($id_os) {
+		
+	// Agent of OS X and critical status
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";		
+
+	// Agent of OS X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";
+
+	// Agent of OS X and unknown status		
+	$agents_unknown = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 3
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";
+						
+	// Agent of OS X and ok status		
+	$agents_ok = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 0
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os
+						group by tagente.id_agente";
 	
-	//!!!Query explanation!!!
-	//An agent is OK if all its modules are OK
-	//The status values are: 0 OK; 1 Critical; 2 Warning; 3 Unkown
-	//This query grouped all modules by agents and select the MAX value for status which has the value 0 
-	//If MAX(estado) is 0 it means all modules has status 0 => OK
-	//Then we count the agents of the group selected to know how many agents are in OK status
-	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT(max_estado) FROM (SELECT MAX(tagente_estado.estado) as max_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente.id_os = $id_os GROUP BY tagente.id_agente HAVING max_estado = 0) AS S1");
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente.id_os = $id_os 
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente NOT IN ($agents_warning)
+						AND tagente.id_agente NOT IN ($agents_unknown)
+						AND tagente.id_agente IN ($agents_ok) ) AS t");	
+
 }
 
 // Get warning agents by using the status code in modules.
 
 function os_agents_warning ($id_os) {
 	
-	//!!!Query explanation!!!
-	//An agent is Warning when has at least one module in warning status and nothing more in critical status
-	//The status values are: 0 OK; 1 Critical; 2 Warning; 3 Unkown
-	//This query grouped all modules by agents and select the MIN value for status which has the value 0 
-	//If MIN(estado) is 2 it means at least one module is warning and there is no critical modules
-	//Then we count the agents of the group selected to know how many agents are in warning status
+	// Agent of OS X and critical status
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";
+
+	// Agent of OS X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";
 	
-	//TODO REVIEW ORACLE AND POSTGRES
-	
-	return db_get_sql ("SELECT COUNT(min_estado) FROM (SELECT MIN(tagente_estado.estado) as min_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente.id_os = $id_os GROUP BY tagente.id_agente HAVING min_estado = 2) AS S1");
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente.id_os = $id_os
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente IN ($agents_warning) ) AS t");	
 	
 }
 
@@ -60,9 +131,51 @@ function os_agents_warning ($id_os) {
 
 function os_agents_unknown ($id_os) {
 	
-	//TODO REVIEW ORACLE AND POSTGRES
-		
-	return db_get_sql ("SELECT COUNT(min_estado) FROM (SELECT MIN(tagente_estado.estado) as min_estado FROM tagente_estado, tagente, tagente_modulo WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tagente_modulo.disabled = 0 AND tagente_estado.id_agente = tagente.id_agente AND tagente_estado.estado != 0 AND tagente.id_os = $id_os GROUP BY tagente.id_agente HAVING min_estado = 3) AS S1");
+	// Agent of module group X and critical status
+	$agents_critical = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 1 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";		
+
+	// Agent of module group X and warning status	
+	$agents_warning = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 2 
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";
+
+	// Agent of module group X and unknown status		
+	$agents_unknown = "SELECT tagente.id_agente 
+						FROM tagente_estado, tagente, tagente_modulo
+						WHERE tagente_estado.id_agente = tagente.id_agente
+						AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo
+						AND tagente.disabled = 0
+						AND tagente_modulo.disabled = 0
+						AND estado = 3
+						AND tagente_estado.utimestamp != 0
+						AND tagente.id_os = $id_os 
+						group by tagente.id_agente";	
+
+	return db_get_sql ("SELECT COUNT(*) FROM ( SELECT DISTINCT tagente.id_agente
+						FROM tagente, tagente_modulo, tagente_estado 
+						WHERE tagente.id_agente = tagente_modulo.id_agente
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+
+						AND tagente.id_os = $id_os 
+						AND tagente.id_agente NOT IN ($agents_critical)
+						AND tagente.id_agente NOT IN ($agents_warning)
+						AND tagente.id_agente IN ($agents_unknown) ) AS t");
 	
 }
 
diff --git a/pandora_console/include/functions_tags.php b/pandora_console/include/functions_tags.php
index 06eb0d0993..72ddc4b36f 100644
--- a/pandora_console/include/functions_tags.php
+++ b/pandora_console/include/functions_tags.php
@@ -18,6 +18,123 @@
  * @package Include
  * @subpackage TAGS
  */
+
+ /**
+ * Get critical agents by using the status code in modules by filtering by id_tag.
+ * 
+ * @param int $id_tag Id of the tag to search module with critical state
+ * 
+ * @return mixed Returns count of agents in critical status or false if they aren't.
+ */
+function tags_agent_critical ($id_tag) {
+	 
+	if (empty($id_tag))
+		return false;
+		
+	//TODO REVIEW ORACLE AND POSTGRES
+
+	return db_get_sql ("SELECT COUNT( DISTINCT tagente_estado.id_agente) 
+						FROM tagente_estado, tagente, tagente_modulo 
+						WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 
+						AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo 
+						AND tagente_modulo.disabled = 0 
+						AND estado = 1 
+						AND tagente_estado.id_agente = tagente.id_agente 
+						AND tagente.id_agente IN (SELECT id_agente 
+												  FROM tagente_modulo, ttag_module 
+												  WHERE tagente_modulo.id_agente_modulo = ttag_module.id_agente_modulo
+												  AND ttag_module.id_tag = $id_tag)");
+}
+
+ /**
+ * Get unknown agents by using the status code in modules by filtering by id_tag.
+ * 
+ * @param int $id_tag Id of the tag to search module with unknown state
+ * 
+ * @return mixed Returns count of agents in unknown status or false if they aren't.
+ */
+function tags_agent_unknown ($id_tag) {
+	 
+	if (empty($id_tag))
+		return false;
+		
+	//TODO REVIEW ORACLE AND POSTGRES
+
+	return db_get_sql ("SELECT COUNT(min_estado) 
+						FROM (SELECT MIN(tagente_estado.estado) as min_estado 
+								FROM tagente_estado, tagente, tagente_modulo 
+								WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 
+								AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo 
+								AND tagente_modulo.disabled = 0 
+								AND tagente_estado.id_agente = tagente.id_agente 
+								AND tagente_estado.estado != 0 
+								AND tagente.id_agente IN (SELECT id_agente
+														  FROM tagente_modulo, ttag_module
+														  WHERE tagente_modulo.id_agente_modulo = ttag_module.id_agente_modulo
+														  AND ttag_module.id_tag = $id_tag)
+								GROUP BY tagente.id_agente HAVING min_estado = 3
+							) AS S1");
+
+}
+
+ /**
+ * Get normal agents by using the status code in modules by filtering by id_tag.
+ * 
+ * @param int $id_tag Id of the tag to search module with normal state
+ * 
+ * @return mixed Returns count of agents in normal status or false if they aren't.
+ */
+function tags_agent_ok ($id_tag) {
+	 
+	if (empty($id_tag))
+		return false;
+		
+	//TODO REVIEW ORACLE AND POSTGRES
+
+	return db_get_sql ("SELECT COUNT(max_estado) 
+						FROM (SELECT MAX(tagente_estado.estado) as max_estado 
+								FROM tagente_estado, tagente, tagente_modulo 
+								WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 
+								AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo 
+								AND tagente_modulo.disabled = 0 
+								AND tagente_estado.id_agente = tagente.id_agente 
+								AND tagente.id_agente IN (SELECT id_agente
+														  FROM tagente_modulo, ttag_module
+														  WHERE tagente_modulo.id_agente_modulo = ttag_module.id_agente_modulo
+														  AND ttag_module.id_tag = $id_tag) 
+								GROUP BY tagente.id_agente HAVING max_estado = 0) AS S1");
+
+}
+
+ /**
+ * Get warning agents by using the status code in modules by filtering by id_tag.
+ * 
+ * @param int $id_tag Id of the tag to search module with warning state
+ * 
+ * @return mixed Returns count of agents in warning status or false if they aren't.
+ */
+function tags_agent_warning ($id_tag) {
+	 
+	if (empty($id_tag))
+		return false;
+		
+	//TODO REVIEW ORACLE AND POSTGRES
+
+	return db_get_sql ("SELECT COUNT(min_estado) 
+						FROM (SELECT MAX(tagente_estado.estado) as min_estado 
+								FROM tagente_estado, tagente, tagente_modulo 
+								WHERE tagente.disabled = 0 AND tagente_estado.utimestamp != 0 
+								AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo 
+								AND tagente_modulo.disabled = 0 
+
+								AND tagente_estado.id_agente = tagente.id_agente 
+								AND tagente.id_agente IN (SELECT id_agente
+														  FROM tagente_modulo, ttag_module
+														  WHERE tagente_modulo.id_agente_modulo = ttag_module.id_agente_modulo
+														  AND ttag_module.id_tag = $id_tag) 
+								GROUP BY tagente.id_agente HAVING min_estado = 2) AS S1");
+
+}
  
  /**
  * Find a tag searching by tag's or description name. 
diff --git a/pandora_console/operation/agentes/status_monitor.php b/pandora_console/operation/agentes/status_monitor.php
index 20d4a4359b..f0ba2a786a 100644
--- a/pandora_console/operation/agentes/status_monitor.php
+++ b/pandora_console/operation/agentes/status_monitor.php
@@ -79,8 +79,14 @@ echo '<td valign="middle">'.__('Module group').'</td>';
 echo '<td valign="middle">';
 $rows = db_get_all_rows_sql("SELECT * FROM tmodule_group ORDER BY name");
 $rows = io_safe_output($rows);
-$rows[0] = __('Not assigned');
-html_print_select($rows, 'modulegroup', $modulegroup, '', __('All'), -1);
+$rows_select = array();
+if (!empty($rows))
+	foreach ($rows as $module_group)
+		$rows_select[$module_group['id_mg']] = $module_group['name'];
+
+$rows_select[0] = __('Not assigned');
+
+html_print_select($rows_select, 'modulegroup', $modulegroup, '', __('All'), -1);
 
 echo '</td></tr><tr><td valign="middle">'.__('Module name').'</td>';
 echo '<td valign="middle">';
diff --git a/pandora_console/operation/tree.php b/pandora_console/operation/tree.php
index c4c958654e..146fd1891c 100644
--- a/pandora_console/operation/tree.php
+++ b/pandora_console/operation/tree.php
@@ -242,7 +242,7 @@ if (is_ajax ())
 			$countRows = 0;
 			
 			if (!empty($avariableGroupsIds)) {
-				$extra_sql = enterprise_hook('policies_get_agents_sql_condition');
+				$extra_sql = '';
 				if($extra_sql != '') {
 					$extra_sql .= ' OR';
 				}
@@ -256,45 +256,125 @@ if (is_ajax ())
 				
 				//Extract all rows of data for each type
 				switch ($type) {
-					case 'group':
-						$sql = sprintf('SELECT * FROM tagente 
-								WHERE id_grupo = %s AND (%s id_grupo IN (%s))', $id, $extra_sql, $groups_sql);
+					case 'group':								
+
+						$sql = agents_get_agents(array (
+						'order' => 'nombre COLLATE utf8_general_ci ASC',
+						'id_grupo' => $id,
+						'disabled' => 0,
+						'status' => $statusSel,
+						'search' => $search_sql),
+
+						array ('*'),
+						'AR',
+						false,
+						true);	
+						
+						// Skip agents without modules
+						$sql .= ' AND id_agente IN
+										(SELECT tagente.id_agente
+										FROM tagente, tagente_modulo 
+										WHERE tagente.id_agente = tagente_modulo.id_agente
+										AND tagente.id_grupo = ' . $id . ' AND tagente.disabled = 0 
+										AND tagente_modulo.disabled = 0
+										group by tagente.id_agente
+										having COUNT(*) > 0)';							
+	
 						break;
 					case 'os':
-						$sql = sprintf('SELECT * FROM tagente 
-								WHERE id_os = %s AND (%s id_grupo IN (%s))', $id, $extra_sql, $groups_sql);
+						$sql = agents_get_agents(array (
+						'order' => 'nombre COLLATE utf8_general_ci ASC',
+						'id_os' => $id,
+						'disabled' => 0,
+						'status' => $statusSel,
+						'search' => $search_sql),
+
+						array ('*'),
+						'AR',
+						false,
+						true);	
+						
+						// Skip agents without modules
+						$sql .= ' AND id_agente IN
+										(SELECT tagente.id_agente
+										FROM tagente, tagente_modulo 
+										WHERE tagente.id_agente = tagente_modulo.id_agente
+										AND tagente.id_os = ' . $id . ' AND tagente.disabled = 0 
+										AND tagente_modulo.disabled = 0
+										group by tagente.id_agente
+										having COUNT(*) > 0)';	
 						break;
 					case 'module_group':
-						$extra_sql = substr($extra_sql,1);
-						$extra_sql = "tagente_modulo.".$extra_sql;  
-						$sql = sprintf('SELECT * FROM tagente 
-								WHERE id_agente IN (SELECT tagente_modulo.id_agente FROM tagente_modulo, tagente_estado
-								WHERE tagente_modulo.id_agente = tagente_estado.id_agente AND tagente_estado.utimestamp !=0 AND 
-								tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo 
-								AND id_module_group = %s 
-								AND ((%s id_grupo IN (%s)))', $id, $extra_sql, $groups_sql);					
+						
+						$sql = agents_get_agents(array (
+						'order' => 'nombre COLLATE utf8_general_ci ASC',
+						'disabled' => 0,
+						'status' => $statusSel,
+						'search' => $search_sql),
+
+						array ('*'),
+						'AR',
+						false,
+						true);	
+						
+						// Skip agents without modules
+						$sql .= ' AND id_agente IN
+										(SELECT tagente.id_agente
+										FROM tagente, tagente_modulo 
+										WHERE tagente.id_agente = tagente_modulo.id_agente
+										AND id_module_group = ' . $id . ' AND tagente.disabled = 0 
+										AND tagente_modulo.disabled = 0
+										group by tagente.id_agente
+										having COUNT(*) > 0)';	
+			
 						break;
 					case 'policies':
 					
 						$sql = "";
 						
+						$sql = agents_get_agents(array (
+						'order' => 'nombre COLLATE utf8_general_ci ASC',
+						'disabled' => 0,
+						'status' => $statusSel,
+						'search' => $search_sql),
+
+						array ('*'),
+						'AR',
+						false,
+						true);	
+						
 						if ($id == 0) {
-							$queryWhere = 'id_agente NOT IN (SELECT id_agent FROM tpolicy_agents)';
 							
-							$sql = sprintf('SELECT DISTINCT tagente.id_agente as id_agente, tagente.nombre as nombre FROM tagente, tagente_modulo, tagente_estado WHERE 
-							tagente_estado.id_agente = tagente.id_agente AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND
-							tagente_modulo.id_policy_module = 0 AND tagente_estado.utimestamp != 0 AND
-							tagente.id_agente IN (SELECT id_agente FROM tagente WHERE %s AND ( %s id_grupo IN (%s)))', 
-							$queryWhere, $extra_sql, $groups_sql);
-								
+							// Skip agents without modules
+							$sql .= ' AND tagente.id_agente IN
+										(SELECT tagente.id_agente
+										FROM tagente, tagente_modulo, tagente_estado
+										WHERE tagente.id_agente = tagente_modulo.id_agente
+										AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+										AND tagente.disabled = 0 
+										AND tagente_modulo.disabled = 0
+										AND tagente_estado.utimestamp != 0
+										AND tagente_modulo.id_policy_module = 0
+										AND tagente.id_agente NOT IN (SELECT id_agent FROM tpolicy_agents)
+										group by tagente.id_agente
+										having COUNT(*) > 0)';
+	
 						} else {
-							$queryWhere = sprintf('id_agente IN (SELECT id_agent FROM tpolicy_agents WHERE id_policy = %s)',$id);
 							
-							$sql = sprintf('SELECT DISTINCT tagente.id_agente as id_agente, tagente.nombre as nombre FROM tagente, tagente_modulo, tagente_estado, tpolicy_modules WHERE 
-							tagente_estado.id_agente = tagente.id_agente AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND tpolicy_modules.id = tagente_modulo.id_policy_module AND
-							tpolicy_modules.id_policy = %s AND tagente_modulo.id_policy_module != 0 AND tagente_estado.utimestamp != 0 AND
-							tagente.id_agente IN (SELECT id_agente FROM tagente WHERE %s AND ( %s id_grupo IN (%s)))', 
-							$id, $queryWhere, $extra_sql, $groups_sql);
+							// Skip agents without modules
+							$sql .= ' AND tagente.id_agente IN
+										(SELECT tagente.id_agente
+										FROM tagente, tagente_modulo, tagente_estado
+										WHERE tagente.id_agente = tagente_modulo.id_agente
+										AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo
+										AND tagente.disabled = 0 
+										AND tagente_modulo.disabled = 0
+										AND tagente_estado.utimestamp != 0
+										AND tagente_modulo.id_policy_module != 0
+										AND tagente.id_agente IN (SELECT id_agent FROM tpolicy_agents)
+										group by tagente.id_agente
+										having COUNT(*) > 0)';							
+							
 						}
 
 						break;
@@ -307,14 +387,24 @@ if (is_ajax ())
 						
 						$name = io_safe_input($name);
 						
-						$sql = sprintf('SELECT *
-							FROM tagente
-							WHERE id_agente IN (
+					
+						$sql = agents_get_agents(array (
+						'order' => 'nombre COLLATE utf8_general_ci ASC',
+						'disabled' => 0,
+						'status' => $statusSel,
+						'search' => $search_sql),
+
+						array ('*'),
+						'AR',
+						false,
+						true);		
+
+						$sql .= sprintf('AND  id_agente IN (
 									SELECT id_agente
 									FROM tagente_modulo
 									WHERE nombre = \'%s\'
 								)
-								AND (%s id_grupo IN (%s))', $name, $extra_sql, $groups_sql);
+								', $name);
 						
 						break;
 				}
@@ -446,21 +536,7 @@ if (is_ajax ())
 						break;
 				}
 
-				if ($statusSel == ALL) {
-				}
-				else if ($statusSel == NORMAL) {
-					if ($agent_info['status'] != 'agent_ok.png')
-						continue;
-				} else if ($statusSel == WARNING) {
-					if ($agent_info['status'] != 'agent_warning.png')
-						continue;
-				} else if ($statusSel  == CRITICAL) {
-					if ($agent_info['status'] != 'agent_critical.png')
-						continue;
-				} else if ($statusSel  == UNKNOWN) {
-					if ($agent_info['status'] != 'agent_down.png')
-						continue;
-				}
+
 			
 				$less = $lessBranchs;
 				if ($count != $countRows)
@@ -750,18 +826,105 @@ function printTree_($type) {
 	if($avariableGroupsIds == ''){
 		$avariableGroupsIds == -1;
 	}
+	
+	// Filter groups by agent status
+	switch ($select_status) {
+		case NORMAL:
+
+			foreach ($avariableGroups as $group_name) {
+				$id_group = db_get_value_sql('SELECT id_grupo FROM tgrupo where nombre ="' . $group_name . '"');
+
+				$num_ok = groups_agent_ok($id_group);
+
+				if ($num_ok <= 0)
+					unset($avariableGroups[$id_group]);
+			}
+			break;
+
+		case WARNING:
+		
+			foreach ($avariableGroups as $group_name) {
+				$id_group = db_get_value_sql('SELECT id_grupo FROM tgrupo where nombre ="' . $group_name . '"');
+
+				$num_warning = groups_agent_warning($id_group);
+
+				if ($num_warning <= 0)
+					unset($avariableGroups[$id_group]);
+			}				
+			break;
+			
+		case CRITICAL:
+		
+			foreach ($avariableGroups as $group_name) {
+				$id_group = db_get_value_sql('SELECT id_grupo FROM tgrupo where nombre ="' . $group_name . '"');
+
+				$num_critical = groups_agent_critical($id_group);
+
+				if ($num_critical <= 0)
+					unset($avariableGroups[$id_group]);
+			}				
+			break;	
+		
+		case UNKNOWN:
+		
+			foreach ($avariableGroups as $group_name) {
+				$id_group = db_get_value_sql('SELECT id_grupo FROM tgrupo where nombre ="' . $group_name . '"');
+
+				$num_unknown = groups_agent_unknown($id_group);
+
+				if ($num_unknown <= 0)
+					unset($avariableGroups[$id_group]);
+			}				
+			break;	
+
+	}
+	
+	// If there are not groups display error and return
+	if (empty($avariableGroups)) {
+		ui_print_error_message("There aren't agents in this agrupation");
+		return;
+	}			
+	
+	if ($search_free != '') {
+		$sql_search = " AND id_grupo IN (SELECT id_grupo FROM tagente
+						WHERE nombre COLLATE utf8_general_ci LIKE '%$search_free%')";
+	} else {
+		$sql_search ='';
+	}	
+	
+	
 	switch ($type) {
 		default:
 		case 'os':
-			if ($search_free != '') {
-				$sql = "SELECT * FROM tconfig_os 
-						WHERE id_os IN (SELECT id_os FROM tagente 
-									WHERE nombre COLLATE utf8_general_ci LIKE '%$search_free%')";
-				$list = db_get_all_rows_sql($sql);
-			} else {
-				$list = db_get_all_rows_sql("SELECT DISTINCT (tagente.id_os), tconfig_os.name FROM tagente, tconfig_os WHERE tagente.id_os = tconfig_os.id_os");
-			}
+
+			$sql = agents_get_agents(array (
+			'order' => 'nombre COLLATE utf8_general_ci ASC',
+			'disabled' => 0,
+			'status' => $select_status,
+			'search' => $sql_search),
+
+			array ('tagente.id_os'),
+			'AR',
+			false,
+			true);	
+			echo $sql;
+			echo "<br>";
+			// Skip agents without modules
+			$sql .= ' AND id_agente IN
+							(SELECT tagente.id_agente
+							FROM tagente, tagente_modulo 
+							WHERE tagente.id_agente = tagente_modulo.id_agente
+							AND tagente.disabled = 0 
+							AND tagente_modulo.disabled = 0
+							group by tagente.id_agente
+							having COUNT(*) > 0)';			
+
+			$sql_os = sprintf("SELECT * FROM tconfig_os WHERE id_os IN (%s)", $sql);
+
+			$list = db_get_all_rows_sql($sql_os);
+			
 			break;
+
 		case 'group':
 			$stringAvariableGroups = (
 					implode(', ',
@@ -770,12 +933,7 @@ function printTree_($type) {
 							)
 						)
 				);
-				if ($search_free != '') {
-					$sql_search = " AND id_grupo IN (SELECT id_grupo FROM tagente
-									WHERE nombre COLLATE utf8_general_ci LIKE '%$search_free%')";
-				} else {
-					$sql_search ='';
-				}
+
 			switch ($config["dbtype"]) {
 				case "mysql":
 				case "postgresql":
@@ -786,49 +944,84 @@ function printTree_($type) {
 					break;				
 			}
 			break;
+
 		case 'module_group':
-			if ($search_free != '') {
-				$sql = "SELECT * FROM tmodule_group
-						WHERE id_mg IN (SELECT id_module_group FROM tagente_modulo 
-										WHERE id_agente IN (SELECT id_agente FROM tagente
-														WHERE nombre COLLATE utf8_general_ci LIKE '%$search_free%'))";
-				$list = db_get_all_rows_sql($sql);
-			} else {
-				$list = db_get_all_rows_sql("SELECT distinct id_mg, name FROM tmodule_group, tagente_modulo WHERE tmodule_group.id_mg = tagente_modulo.id_module_group");
-				if ($list !== false)
-					array_push($list, array('id_mg' => 0, 'name' => 'Not assigned'));
-			}
+		
+			$sql = agents_get_agents(array (
+			'order' => 'nombre COLLATE utf8_general_ci ASC',
+			'disabled' => 0,
+			'status' => $select_status,
+			'search' => $sql_search),
+
+			array ('id_agente'),
+			'AR',
+			false,
+			true);	
+		
+			// Skip agents without modules
+			$sql .= ' AND id_agente IN
+							(SELECT tagente.id_agente
+							FROM tagente, tagente_modulo 
+							WHERE tagente.id_agente = tagente_modulo.id_agente
+							AND tagente.disabled = 0 
+							AND tagente_modulo.disabled = 0
+							group by tagente.id_agente
+							having COUNT(*) > 0)';		
+
+			$sql_module_groups = sprintf("SELECT * FROM tmodule_group
+					WHERE id_mg IN (SELECT id_module_group FROM tagente_modulo WHERE id_agente IN (%s))", $sql);			
+		
+
+			$list = db_get_all_rows_sql($sql_module_groups);
+			
+			if ($list !== false)
+				array_push($list, array('id_mg' => 0, 'name' => 'Not assigned'));		
+
 			break;
+
 		case 'policies':
 			$groups_id = array_keys($avariableGroups);
 			$groups = implode(',',$groups_id);
 			
 			if ($search_free != '') {
 
-				$sql = "SELECT * FROM tpolicies
-						WHERE id_group IN ($groups) 
-						AND id IN (SELECT id_policy FROM tpolicy_agents
-								WHERE id_agent IN (SELECT id_agente FROM tagente
-											WHERE nombre COLLATE utf8_general_ci LIKE '%$search_free%'))";
+				$sql = "SELECT DISTINCT tpolicies.id, tpolicies.name FROM tpolicies, tpolicy_modules, tagente_estado, tagente, tagente_modulo WHERE 
+				tagente.id_agente = tagente_estado.id_agente AND tagente_modulo.id_agente = tagente_estado.id_agente AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND 
+				tagente_estado.utimestamp != 0 AND tagente_modulo.id_policy_module != 0 AND tpolicy_modules.id = tagente_modulo.id_policy_module AND tpolicies.id = tpolicy_modules.id_policy AND
+				tpolicies.id_group IN  ($groups) AND tagente.nombre LIKE '%$search_free%'";
+				
 				$list = db_get_all_rows_sql($sql);
+				
+				if ($list !== false)
+					array_push($list, array('id' => 0, 'name' => 'No policy'));				
 			} else {
 				
 				$list = db_get_all_rows_sql("SELECT DISTINCT tpolicies.id, tpolicies.name FROM tpolicies, tpolicy_modules, tagente_estado, tagente, tagente_modulo WHERE 
 				tagente.id_agente = tagente_estado.id_agente AND tagente_modulo.id_agente = tagente_estado.id_agente AND tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo AND 
 				tagente_estado.utimestamp != 0 AND tagente_modulo.id_policy_module != 0 AND tpolicy_modules.id = tagente_modulo.id_policy_module AND tpolicies.id = tpolicy_modules.id_policy AND
 				tpolicies.id_group IN  ($groups)");
-				
+
 				if ($list !== false)
 					array_push($list, array('id' => 0, 'name' => 'No policy'));
 			}
 			break;
+
 		case 'module':
+			$avariableGroupsIds = implode(',',array_keys($avariableGroups));
+			if($avariableGroupsIds == ''){
+				$avariableGroupsIds == -1;
+			}		
+		
 			if ($search_free != '') {
 					$sql_search = " AND t1.id_agente IN (SELECT id_agente FROM tagente
 									WHERE nombre COLLATE utf8_general_ci LIKE '%$search_free%')";
-				} else {
-					$sql_search ='';
-				}
+			} else {
+					$sql_search = '';
+			}
+			
+			if ($select_status != -1)
+				$sql_search .= " AND estado = " . $select_status . " ";
+
 			switch ($config["dbtype"]) {
 				case "mysql":
 				case "postgresql":
@@ -845,7 +1038,9 @@ function printTree_($type) {
 						AND t3.utimestamp !=0 GROUP BY dbms_lob.substr(t1.nombre,4000,1) ORDER BY dbms_lob.substr(t1.nombre,4000,1) ASC');
 					break;
 			}		
+
 			break;
+
 	}
 	
 	if ($list === false) {
@@ -1004,8 +1199,8 @@ ui_print_page_header (__('Tree view')." - ".__('Sort the agents by ') .$order, "
 
 
 echo "<br>";
-echo '<form id="tree_search" method="post" action="index.php??extension_in_menu=estado&sec=estado&sec2=operation/tree&refr=0&sort_by='.$activeTab.'">';
-echo "<b>" . __('Monitor status') . "</b>";
+echo '<form id="tree_search" method="post" action="index.php?extension_in_menu=estado&sec=estado&sec2=operation/tree&refr=0&sort_by='.$activeTab.'">';
+echo "<b>" . __('Agent status') . "</b>";
 
 $search_free = get_parameter('search_free', '');
 $select_status = get_parameter('status', -1);
@@ -1020,7 +1215,7 @@ $fields[UNKNOWN] = __('Unknown');
 html_print_select ($fields, "status", $select_status);
 
 echo "&nbsp;&nbsp;&nbsp;";
-echo "<b>" . __('Search agent') .  ui_print_help_tip (__('Case sensitive'))."</b>";
+echo "<b>" . __('Search agent') . "</b>";
 echo "&nbsp;";
 html_print_input_text ("search_free", $search_free, '', 40,30, false);
 echo "&nbsp;&nbsp;&nbsp;";