diff --git a/pandora_console/extensions/module_groups.php b/pandora_console/extensions/module_groups.php
index 69838c9fb5..9a4f66935c 100644
--- a/pandora_console/extensions/module_groups.php
+++ b/pandora_console/extensions/module_groups.php
@@ -128,43 +128,20 @@ function mainModuleGroups() {
 	require_once($config['homedir'] . "/include/functions_users.php");
 	
 	//The big query
-	switch ($config["dbtype"]) {
-		case "mysql":
-			$sql = "SELECT COUNT(id_agente) AS count, case utimestamp when 0 then 5 else estado end as estado 
-				FROM tagente_estado
-				WHERE id_agente IN
-					(SELECT id_agente FROM tagente WHERE id_grupo = %d AND disabled IS FALSE)
-					AND id_agente_modulo IN
-					(SELECT id_agente_modulo 
-						FROM tagente_modulo 
-						WHERE id_module_group = %d AND disabled IS FALSE AND delete_pending IS FALSE)
-				GROUP BY estado";
-			break;
-		case "postgresql":
-			$sql = "SELECT COUNT(id_agente) AS count,
-					case utimestamp when 0 then 5 else estado end as estado
-				FROM tagente_estado
-				WHERE id_agente IN
-					(SELECT id_agente FROM tagente WHERE id_grupo = %d AND disabled = 0)
-					AND id_agente_modulo IN
-					(SELECT id_agente_modulo
-						FROM tagente_modulo
-						WHERE id_module_group = %d AND disabled = 0 AND delete_pending = 0)
-				GROUP BY estado, utimestamp";
-			break;
-		case "oracle":
-			$sql = "SELECT COUNT(id_agente) AS count, (case when utimestamp = 0 then 5 else estado end) AS estado
-				FROM tagente_estado
-				WHERE id_agente IN
-					(SELECT id_agente FROM tagente WHERE id_grupo = %d AND (disabled IS NOT NULL AND disabled <> 0))
-					AND id_agente_modulo IN
-					(SELECT id_agente_modulo 
-						FROM tagente_modulo 
-						WHERE id_module_group = %d AND (disabled IS NOT NULL AND disabled <> 0) AND (delete_pending IS NOT NULL AND delete_pending <> 0))
-				GROUP BY (case when utimestamp = 0 then 5 else estado end)";
-			break;
-	}
-	
+	$sql = "SELECT COUNT(id_agente) AS count, case utimestamp when 0 then 5 else estado end as estado 
+		FROM tagente_estado
+		WHERE id_agente IN
+			(SELECT id_agente
+				FROM tagente ta LEFT JOIN tagent_secondary_group tasg
+					ON ta.id_agente = tasg.id_agent
+				WHERE (ta.id_grupo = %d OR tasg.id_group = %d) AND disabled IS FALSE
+			)
+			AND id_agente_modulo IN
+			(SELECT id_agente_modulo 
+				FROM tagente_modulo 
+				WHERE id_module_group = %d AND disabled IS FALSE AND delete_pending IS FALSE)
+		GROUP BY estado";
+
 	ui_print_page_header (__("Combined table of agent group and module group"), "images/module_group.png", false, "", false, '');
 	
 	ui_print_info_message ( array('no_close'=>true, 'message'=>
@@ -207,7 +184,7 @@ function mainModuleGroups() {
 			
 			foreach ($modelGroups as $idModelGroup => $modelGroup) {
 				$fired = false;
-				$query = sprintf($sql, $idAgentGroup, $idModelGroup);
+				$query = sprintf($sql, $idAgentGroup, $idAgentGroup, $idModelGroup);
 				
 				$rowsDB = db_get_all_rows_sql ($query);
 				
diff --git a/pandora_console/godmode/agentes/agent_manager.php b/pandora_console/godmode/agentes/agent_manager.php
index 43faeb3ca7..ad27ff87f0 100644
--- a/pandora_console/godmode/agentes/agent_manager.php
+++ b/pandora_console/godmode/agentes/agent_manager.php
@@ -258,10 +258,7 @@ if(!$new_agent){
 	}
 }
 
-
 $groups = users_get_groups ($config["id_user"], "AR",false);
-$agents = agents_get_group_agents (array_keys ($groups));
-
 
 $modules = db_get_all_rows_sql("SELECT id_agente_modulo as id_module, nombre as name FROM tagente_modulo 
 								WHERE id_agente = " . $id_parent);
diff --git a/pandora_console/godmode/agentes/modificar_agente.php b/pandora_console/godmode/agentes/modificar_agente.php
index 670009bf2f..994b5a69ea 100644
--- a/pandora_console/godmode/agentes/modificar_agente.php
+++ b/pandora_console/godmode/agentes/modificar_agente.php
@@ -343,6 +343,7 @@ if($os != 0){
 	$search_sql .= " AND id_os = " . $os;
 }
 
+$user_groups_to_sql = "";
 // Show only selected groups
 if ($ag_group > 0) {
 	$ag_groups = array();
@@ -350,100 +351,39 @@ if ($ag_group > 0) {
 	if ($recursion) {
 		$ag_groups = groups_get_id_recursive($ag_group, true);
 	}
-	
-	switch ($config["dbtype"]) {
-		case "mysql":
-			$sql = sprintf ('SELECT *
-				FROM tagente
-				WHERE id_grupo IN (%s)
-					%s
-				ORDER BY %s %s, %s %s
-				LIMIT %d, %d',
-				implode (",", $ag_groups), $search_sql, $order['field'],$order['order'], $order['field2'], $order['order'], $offset, $config["block_size"]);
-			break;
-		case "postgresql":
-			$sql = sprintf ('SELECT *
-				FROM tagente
-				WHERE id_grupo IN (%s)
-					%s
-				ORDER BY %s %s, %s %s
-				LIMIT %d OFFSET %d',
-				implode (",", $ag_groups), $search_sql, $order['field'],$order['order'], $order['field2'], $order['order'], $config["block_size"], $offset);
-			break;
-		case "oracle":
-			$set = array ();
-			$set['limit'] = $config["block_size"];
-			$set['offset'] = $offset;
-			$sql = sprintf ('SELECT *
-				FROM tagente
-				WHERE id_grupo IN (%s)
-					%s
-				ORDER BY %s %s, %s %s',
-				implode (",", $ag_groups), $search_sql, $order['field'],$order['order'], $order['field2'], $order['order']);
-			$sql = oracle_recode_query ($sql, $set);
-			break;
-	}
-	
-	$sql_total = sprintf ('SELECT COUNT(*)
-		FROM tagente
-		WHERE id_grupo IN (%s)
-			%s',
-		implode (",", $ag_groups), $search_sql);
-	$total_agents = db_get_sql ($sql_total);
-}
-else {
+	$user_groups_to_sql = implode (",", $ag_groups);
+} else {
 	// Concatenate AW and AD permisions to get all the possible groups where the user can manage
 	$user_groupsAW = users_get_groups ($config['id_user'], 'AW');
 	$user_groupsAD = users_get_groups ($config['id_user'], 'AD');
-	
+
 	$user_groups = $user_groupsAW + $user_groupsAD;
-	
-	$sql = sprintf ('SELECT COUNT(*)
-		FROM tagente
-		WHERE id_grupo IN (%s)
-			%s',
-		implode (',', array_keys ($user_groups)),
-		$search_sql);
-	
-	$total_agents = db_get_sql ($sql);
-	
-	switch ($config["dbtype"]) {
-		case "mysql":
-			$sql = sprintf ('SELECT *
-				FROM tagente
-				WHERE id_grupo IN (%s)
-					%s
-				ORDER BY %s %s, %s %s
-				LIMIT %d, %d',
-				implode (',', array_keys ($user_groups)),
-				$search_sql, $order['field'],$order['order'], $order['field2'], $order['order'], $offset, $config["block_size"]);
-			break;
-		case "postgresql":
-			$sql = sprintf ('SELECT *
-				FROM tagente
-				WHERE id_grupo IN (%s)
-					%s
-				ORDER BY %s %s, %s %s
-				LIMIT %d OFFSET %d',
-				implode (',', array_keys ($user_groups)),
-				$search_sql, $order['field'],$order['order'], $order['field2'], $order['order'], $config["block_size"], $offset);
-			break;
-		case "oracle":
-			$set = array ();
-			$set['limit'] = $config["block_size"];
-			$set['offset'] = $offset;
-			$sql = sprintf ('SELECT *
-				FROM tagente
-				WHERE id_grupo IN (%s)
-					%s
-				ORDER BY %s %s, %s %s',
-				implode (',', array_keys ($user_groups)),
-				$search_sql, $order['field'],$order['order'], $order['field2'], $order['order']);
-			$sql = oracle_recode_query ($sql, $set);
-			break;
-	}
+	$user_groups_to_sql = implode (',', array_keys ($user_groups));
 }
 
+$sql = sprintf ('SELECT COUNT(DISTINCT(tagente.id_agente))
+	FROM tagente LEFT JOIN tagent_secondary_group tasg
+		ON tagente.id_agente = tasg.id_agent
+	WHERE (tagente.id_grupo IN (%s) OR tasg.id_group IN (%s))
+		%s',
+	$user_groups_to_sql, $user_groups_to_sql,
+	$search_sql);
+
+$total_agents = db_get_sql ($sql);
+
+$sql = sprintf ('SELECT *
+	FROM tagente LEFT JOIN tagent_secondary_group tasg
+		ON tagente.id_agente = tasg.id_agent
+	WHERE (tagente.id_grupo IN (%s) OR tasg.id_group IN (%s))
+		%s
+	GROUP BY tagente.id_agente
+	ORDER BY %s %s, %s %s
+	LIMIT %d, %d',
+	$user_groups_to_sql, $user_groups_to_sql,
+	$search_sql,
+	$order['field'], $order['order'], $order['field2'], $order['order'],
+	$offset, $config["block_size"]);
+
 $agents = db_get_all_rows_sql ($sql);
 
 // Delete rnum row generated by oracle_recode_query() function
@@ -494,13 +434,15 @@ if ($agents !== false) {
 			}
 			
 			/* End Update tagente.remote 0/1 with remote agent function return */
-			
-		$id_grupo = $agent["id_grupo"];
-		
+
+		$all_groups = agents_get_all_groups_agent($agent["id_agente"], $agent["id_grupo"]);
+		$check_aw = check_acl_one_of_groups ($config["id_user"], $all_groups, "AW");
+		$check_ad = check_acl_one_of_groups ($config["id_user"], $all_groups, "AD");
+
 		$cluster = db_get_row_sql('select id from tcluster where id_agent = '.$agent['id_agente']);
-		
-		if (! check_acl ($config["id_user"], $id_grupo, "AW", $agent['id_agente']) && ! check_acl ($config["id_user"], $id_grupo, "AD", $agent['id_agente']))
-			continue;
+
+		// Do not show the agent if there is not enough permissions
+		if (!$check_aw  && !$check_ad) continue;
 		
 		if ($color == 1) {
 			$tdcolor = "datos";
@@ -526,7 +468,7 @@ if ($agents !== false) {
 		echo '';
 		echo "";
 		
-		if (check_acl ($config["id_user"], $agent["id_grupo"], "AW")) {
+		if ($check_aw) {
 			$main_tab = 'main';
 		}
 		else {
@@ -574,7 +516,7 @@ if ($agents !== false) {
 		}
 
 		echo '
';
-		if (check_acl ($config["id_user"], $agent["id_grupo"], "AW")) {
+		if ($check_aw) {
 			if($agent["id_os"] == 100){
 				$cluster = db_get_row_sql('select id from tcluster where id_agent = '.$agent['id_agente']);
 				echo '
'.__('Edit').'';
@@ -640,7 +582,7 @@ if ($agents !== false) {
 
 
 		// Group icon and name
-		echo "
" . ui_print_group_icon ($id_grupo, true)." | ";
+		echo "
" . ui_print_group_icon ($agent["id_grupo"], true)." | ";
 		// Description
 		echo "
" .
 			ui_print_truncate_text($agent["comentarios"], 'description', true, true, true, '[…]', 'font-size: 6.5pt;')." | ";
@@ -679,7 +621,7 @@ if ($agents !== false) {
 			echo html_print_image('images/lightbulb.png', true, array('alt' => __('Disable agent'), 'title' => __('Disable agent'))) ."";
 		}
 		
-		if (check_acl ($config["id_user"], $agent["id_grupo"], "AW")) {
+		if ($check_aw) {
 			echo "  
 $id_group,
+				'id_grupo' => $id_group
+			),
+			'OR'
+		) . ')';
 	}
-	
+
 	if ($search === true) {
 		//No added search. Show both disabled and non-disabled
 	}
@@ -959,8 +966,8 @@ function agents_get_group_agents (
 	$filter['order'] = 'alias';
 	
 	if (is_metaconsole()) {
-		$table_name = 'tmetaconsole_agent';
-		
+		$table_name = 'tmetaconsole_agent LEFT JOIN tmetaconsole_agent_secondary_group ON ta.id_agente = tasg.id_agent';
+
 		$fields = array(
 				'id_tagente AS id_agente',
 				'alias',
@@ -968,16 +975,16 @@ function agents_get_group_agents (
 			);
 	}
 	else {
-		$table_name = 'tagente';
-		
+		$table_name = 'tagente LEFT JOIN tagent_secondary_group ON id_agente=id_agent';
+
 		$fields = array(
 				'id_agente',
 				'alias'
 			);
 	}
-	
+
 	$result = db_get_all_rows_filter($table_name, $filter, $fields);
-	
+
 	if ($result === false)
 		return array (); //Return an empty array
 	
@@ -985,7 +992,6 @@ function agents_get_group_agents (
 	foreach ($result as $row) {
 		if (!isset($row["id_agente"]) || !isset($row["alias"]))
 			continue;
-		
 		if ($serialized && isset($row["id_server"])) {
 			$key = $row["id_server"] . $separator . $row["id_agente"];
 		} elseif ($force_serialized) {
diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php
index b22bbad3ce..23f74bf2b3 100644
--- a/pandora_console/include/functions_groups.php
+++ b/pandora_console/include/functions_groups.php
@@ -1206,19 +1206,7 @@ function groups_get_agents_counter ($group, $agent_filter = array(), $module_fil
 	$groups_clause = "AND ta.id_grupo IN ($group_str)";
 
 	$tags_clause = "";
-	if ($strict_user && !empty($groups_and_tags)) {
-		foreach ($groups as $group_id) {
-			if (isset($groups_and_tags[$group_id]) && !empty($groups_and_tags[$group_id])) {
-				$tags_str = $groups_and_tags[$group_id];
-				$tags_clause .= " AND (ta.grupo <> $group_id
-									OR (ta.grupo = $group_id
-										AND tam.id_agente_modulo NOT IN (SELECT id_agente_modulo
-																		FROM ttag_module
-																		WHERE id_tag NOT IN ($tags_str) )))";
-			}
-		}
-	}
-	
+
 	$agent_name_filter = "";
 	$agent_status = AGENT_STATUS_ALL;
 	if (!empty($agent_filter)) {
@@ -1662,22 +1650,10 @@ function groups_get_monitors_counter ($group, $agent_filter = array(), $module_f
 	}
 	
 	$group_str = implode (",", $groups);
-	$groups_clause = "AND ta.id_grupo IN ($group_str)";
+	$groups_clause = "AND (ta.id_grupo IN ($group_str) OR tasg.id_group IN ($group_str))";
 	
 	$tags_clause = "";
-	if ($strict_user && !empty($groups_and_tags)) {
-		foreach ($groups as $group_id) {
-			if (isset($groups_and_tags[$group_id]) && !empty($groups_and_tags[$group_id])) {
-				$tags_str = $groups_and_tags[$group_id];
-				$tags_clause .= " AND (ta.grupo <> $group_id
-									OR (ta.grupo = $group_id
-										AND tam.id_agente_modulo NOT IN (SELECT id_agente_modulo
-																		FROM ttag_module
-																		WHERE id_tag NOT IN ($tags_str) )))";
-			}
-		}
-	}
-	
+
 	$agent_name_filter = "";
 	$agents_clause = "";
 	if (!empty($agent_filter)) {
@@ -1775,36 +1751,38 @@ function groups_get_monitors_counter ($group, $agent_filter = array(), $module_f
 						$modules_clause
 				INNER JOIN tagente ta
 					ON tam.id_agente = ta.id_agente
+				LEFT JOIN tagent_secondary_group tasg
+					ON ta.id_agente = tasg.id_agent
 						AND ta.disabled = 0
 						$agent_name_filter
 						$agents_clause
-						$groups_clause
 				WHERE tam.disabled = 0
 					$module_name_filter
+					$groups_clause
 					$tags_clause";
 	}
 	else {
 		$status_columns_array = array();
-		foreach ($module_status_array as $status) {
+		foreach ($status_array as $status) {
 			switch ($status) {
 				case AGENT_MODULE_STATUS_CRITICAL_ALERT:
 				case AGENT_MODULE_STATUS_CRITICAL_BAD:
-					$status_columns_array[] = 'ta.critical_count';
+					$status_columns_array['critical'] = 'critical_count';
 					break;
 				case AGENT_MODULE_STATUS_WARNING_ALERT:
 				case AGENT_MODULE_STATUS_WARNING:
-					$status_columns_array[] = 'ta.warning_count';
+					$status_columns_array['warn'] = 'warning_count';
 					break;
 				case AGENT_MODULE_STATUS_UNKNOWN:
-					$status_columns_array[] = 'ta.unknown_count';
+					$status_columns_array['unk'] = 'unknown_count';
 					break;
 				case AGENT_MODULE_STATUS_NO_DATA:
 				case AGENT_MODULE_STATUS_NOT_INIT:
-					$status_columns_array[] = 'ta.notinit_count';
+					$status_columns_array['notinit'] = 'notinit_count';
 					break;
 				case AGENT_MODULE_STATUS_NORMAL_ALERT:
 				case AGENT_MODULE_STATUS_NORMAL:
-					$status_columns_array[] = 'ta.normal_count';
+					$status_columns_array['normal'] = 'normal_count';
 					break;
 				default:
 					// The type doesn't exist
@@ -1816,13 +1794,16 @@ function groups_get_monitors_counter ($group, $agent_filter = array(), $module_f
 		
 		$status_columns_str = implode(",", $status_columns_array);
 		
-		$sql = "SELECT SUM($status_columns_str)
-				FROM tagente ta
+		$sql = "SELECT SUM($status_columns_str) FROM
+			(SELECT DISTINCT(ta.id_agente), $status_columns_str
+				FROM tagente ta LEFT JOIN tagent_secondary_group tasg
+					ON ta.id_agente = tasg.id_agent
 				WHERE ta.disabled = 0
 					$agent_name_filter
 					$agents_clause
 					$groups_clause
-					$tags_clause";
+					$tags_clause
+			) AS t1";
 	}
 			
 	$count = (int) db_get_sql ($sql);
diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index b68eb2ac06..6dcc1cd9d7 100755
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -6993,89 +6993,57 @@ function reporting_get_group_stats ($id_group = 0, $access = 'AR') {
 		}
 		
 		if (!empty($group_array)) {
-			// FOR THE FUTURE: Split the groups into groups with tags restrictions and groups without it
-			// To calculate in the light way the non tag restricted and in the heavy way the others
-			/*
-			$group_restricted_data = tags_get_acl_tags($config['id_user'], $group_array, $access, 'data');
-			$tags_restricted_groups = array_keys($group_restricted_data);
+			// Get monitor NOT INIT, except disabled AND async modules
+			$data["monitor_not_init"] += (int) groups_get_not_init_monitors ($group_array, array(), array(), false, false, true);
 			
-			$no_tags_restricted_groups = $group_array;
-			foreach ($no_tags_restricted_groups as $k => $v) {
-				if (in_array($v, $tags_restricted_groups)) {
-					unset($no_tags_restricted_groups[$k]);
-				}
-			}
-			*/
+			// Get monitor OK, except disabled and non-init
+			$data["monitor_ok"] += (int) groups_get_normal_monitors ($group_array, array(), array(), false, false, true);
 			
-			if (!empty($group_array)) {
-				// Get monitor NOT INIT, except disabled AND async modules
-				$data["monitor_not_init"] += (int) groups_get_not_init_monitors ($group_array, array(), array(), false, false, true);
-				
-				// Get monitor OK, except disabled and non-init
-				$data["monitor_ok"] += (int) groups_get_normal_monitors ($group_array, array(), array(), false, false, true);
-				
-				// Get monitor CRITICAL, except disabled and non-init
-				$data["monitor_critical"] += (int) groups_get_critical_monitors ($group_array, array(), array(), false, false, true);
-				
-				// Get monitor WARNING, except disabled and non-init
-				$data["monitor_warning"] += (int) groups_get_warning_monitors ($group_array, array(), array(), false, false, true);
-				
-				// Get monitor UNKNOWN, except disabled and non-init
-				$data["monitor_unknown"] += (int) groups_get_unknown_monitors ($group_array, array(), array(), false, false, true);
-				
-				// Get alerts configured, except disabled 
-				$data["monitor_alerts"] += groups_monitor_alerts ($group_array) ;
-				
-				// Get alert configured currently FIRED, except disabled 
-				$data["monitor_alerts_fired"] += groups_monitor_fired_alerts ($group_array);
-				
-				// Calculate totals using partial counts from above
-				
-				// Get TOTAL non-init modules, except disabled ones and async modules
-				$data["total_not_init"] += $data["monitor_not_init"];
+			// Get monitor CRITICAL, except disabled and non-init
+			$data["monitor_critical"] += (int) groups_get_critical_monitors ($group_array, array(), array(), false, false, true);
 			
-				// Get TOTAL agents in a group
-				$data["total_agents"] += (int) groups_get_total_agents ($group_array, array(), array(), false, false, true);
-				
-				// Get Agents OK
-				$data["agent_ok"] += (int) groups_get_normal_agents ($group_array, array(), array(), false, false, true);
-				
-				// Get Agents Warning 
-				$data["agent_warning"] += (int) groups_get_warning_agents ($group_array, array(), array(), false, false, true);
-				
-				// Get Agents Critical
-				$data["agent_critical"] += (int) groups_get_critical_agents ($group_array, array(), array(), false, false, true);
-				
-				// Get Agents Unknown
-				$data["agent_unknown"] += (int) groups_get_unknown_agents ($group_array, array(), array(), false, false, true);
-				
-				// Get Agents Not init
-				$data["agent_not_init"] += (int) groups_get_not_init_agents ($group_array, array(), array(), false, false, true);
-			}
+			// Get monitor WARNING, except disabled and non-init
+			$data["monitor_warning"] += (int) groups_get_warning_monitors ($group_array, array(), array(), false, false, true);
 			
+			// Get monitor UNKNOWN, except disabled and non-init
+			$data["monitor_unknown"] += (int) groups_get_unknown_monitors ($group_array, array(), array(), false, false, true);
+			
+			// Get alerts configured, except disabled 
+			$data["monitor_alerts"] += groups_monitor_alerts ($group_array) ;
+			
+			// Get alert configured currently FIRED, except disabled 
+			$data["monitor_alerts_fired"] += groups_monitor_fired_alerts ($group_array);
+			
+			// Calculate totals using partial counts from above
+			
+			// Get TOTAL non-init modules, except disabled ones and async modules
+			$data["total_not_init"] += $data["monitor_not_init"];
+		
+			// Get TOTAL agents in a group
+			$data["total_agents"] += (int) groups_get_total_agents ($group_array, array(), array(), false, false, true);
+			
+			// Get Agents OK
+			$data["agent_ok"] += (int) groups_get_normal_agents ($group_array, array(), array(), false, false, true);
+			
+			// Get Agents Warning 
+			$data["agent_warning"] += (int) groups_get_warning_agents ($group_array, array(), array(), false, false, true);
+			
+			// Get Agents Critical
+			$data["agent_critical"] += (int) groups_get_critical_agents ($group_array, array(), array(), false, false, true);
+			
+			// Get Agents Unknown
+			$data["agent_unknown"] += (int) groups_get_unknown_agents ($group_array, array(), array(), false, false, true);
+			
+			// Get Agents Not init
+			$data["agent_not_init"] += (int) groups_get_not_init_agents ($group_array, array(), array(), false, false, true);
+		
 			// Get total count of monitors for this group, except disabled.
-			$data["monitor_checks"] = $data["monitor_not_init"] + $data["monitor_unknown"] + $data["monitor_warning"] + $data["monitor_critical"] + $data["monitor_ok"];
-			
-			// Calculate not_normal monitors
-			$data["monitor_not_normal"] += $data["monitor_checks"] - $data["monitor_ok"];
+			$data["monitor_checks"] += $data["monitor_not_init"] + $data["monitor_unknown"] + $data["monitor_warning"] + $data["monitor_critical"] + $data["monitor_ok"];
 		}
-		
-		// Get total count of monitors for this group, except disabled.
-		
-		$data["monitor_checks"] = $data["monitor_not_init"] + $data["monitor_unknown"] + $data["monitor_warning"] + $data["monitor_critical"] + $data["monitor_ok"];
-		
-		/*
-		 Monitor health (percentage)
-		 Data health (percentage)
-		 Global health (percentage)
-		 Module sanity (percentage)
-		 Alert level (percentage)
-		 
-		 Server Sanity	0% Uninitialized modules
-		 
-		 */
 	}
-	
+	// Calculate not_normal monitors
+	$data["monitor_not_normal"] += $data["monitor_checks"] - $data["monitor_ok"];
+
 	if ($data["monitor_unknown"] > 0 && $data["monitor_checks"] > 0) {
 		$data["monitor_health"] = format_numeric (100 - ($data["monitor_not_normal"] / ($data["monitor_checks"] / 100)), 1);
 	}
diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm
index 0fd65b90a6..a8a26e6ad2 100644
--- a/pandora_server/lib/PandoraFMS/Core.pm
+++ b/pandora_server/lib/PandoraFMS/Core.pm
@@ -4653,6 +4653,9 @@ sub pandora_group_statistics ($$) {
 	# Get all groups
 	my @groups = get_db_rows ($dbh, 'SELECT id_grupo FROM tgrupo');
 	my $table = is_metaconsole($pa_config) ? 'tmetaconsole_agent' : 'tagente';
+	my $sec_table = is_metaconsole($pa_config)
+		? 'tmetaconsole_agent_secondary_group'
+		: 'tagent_secondary_group';
 
 	# For each valid group get the stats: Simple uh?
 	foreach my $group_row (@groups) {
@@ -4661,30 +4664,49 @@ sub pandora_group_statistics ($$) {
 
 		# NOTICE - Calculations done here MUST BE the same than used in PHP code to have
 		# the same criteria. PLEASE, double check any changes here and in functions_groups.php
-		$agents_unknown = get_db_value ($dbh, "SELECT COUNT(*) FROM $table WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count>0 AND id_grupo=?", $group);
+		$agents_unknown = get_db_value ($dbh, "SELECT COUNT(DISTINCT(id_agente))
+			FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+			WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count>0 AND (id_grupo=? OR id_group=?)", $group, $group);
 		$agents_unknown = 0 unless defined ($agents_unknown);
 
-		$agents = get_db_value ($dbh, "SELECT COUNT(*) FROM $table WHERE id_grupo = $group AND disabled=0");
+		$agents = get_db_value ($dbh, "SELECT COUNT(DISTINCT(id_agente))
+			FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+			WHERE(id_grupo=$group OR id_group=$group) AND disabled=0");
 		$agents = 0 unless defined ($agents);
 
-		$modules = get_db_value ($dbh, "SELECT SUM(total_count) FROM $table WHERE disabled=0 AND id_grupo=?", $group);
+		$modules = get_db_value ($dbh, "SELECT SUM(total_count) FROM
+			(
+				SELECT DISTINCT(id_agente), total_count
+				FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+				WHERE disabled=0 AND (id_grupo=? OR id_group=?)
+			) AS t1", $group, $group);
 		$modules = 0 unless defined ($modules);
-		
-		$normal = get_db_value ($dbh, "SELECT COUNT(*) FROM $table WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count=0 AND normal_count>0 AND id_grupo=?", $group);
+
+		$normal = get_db_value ($dbh, "SELECT COUNT(DISTINCT(id_agente))
+			FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+			WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count=0 AND normal_count>0 AND (id_grupo=? OR id_group=?)", $group, $group);
 		$normal = 0 unless defined ($normal);
-		
-		$critical = get_db_value ($dbh, "SELECT COUNT(*) FROM $table WHERE disabled=0 AND critical_count>0 AND id_grupo=?", $group);
+
+		$critical = get_db_value ($dbh, "SELECT COUNT(DISTINCT(id_agente))
+			FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+			WHERE disabled=0 AND critical_count>0 AND (id_grupo=? OR id_group=?)", $group, $group);
 		$critical = 0 unless defined ($critical);
-		
-		$warning = get_db_value ($dbh, "SELECT COUNT(*) FROM $table WHERE disabled=0 AND critical_count=0 AND warning_count>0 AND id_grupo=?", $group);
+
+		$warning = get_db_value ($dbh, "SELECT COUNT(DISTINCT(id_agente))
+			FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+			WHERE disabled=0 AND critical_count=0 AND warning_count>0 AND (id_grupo=? OR id_group=?)", $group, $group);
 		$warning = 0 unless defined ($warning);
-	
-		$unknown = get_db_value ($dbh, "SELECT COUNT(*) FROM $table WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count>0 AND id_grupo=?", $group);	
+
+		$unknown = get_db_value ($dbh, "SELECT COUNT(DISTINCT(id_agente))
+			FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+			WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count>0 AND (id_grupo=? OR id_group=?)", $group, $group);
 		$unknown = 0 unless defined ($unknown);
-		
-		$non_init = get_db_value ($dbh, "SELECT COUNT(*) FROM $table WHERE disabled=0 AND total_count=notinit_count AND id_grupo=?", $group);
+
+		$non_init = get_db_value ($dbh, "SELECT COUNT(DISTINCT(id_agente))
+			FROM $table LEFT JOIN $sec_table ON id_agente=id_agent
+			WHERE disabled=0 AND total_count=notinit_count AND (id_grupo=? OR id_group=?)", $group, $group);
 		$non_init = 0 unless defined ($non_init);
-		
+
 		# Total alert count not available on the meta console.
 		if ($table eq 'tagente') {
 			$alerts = get_db_value ($dbh, "SELECT COUNT(talert_template_modules.id)