From b9f3732bad3fc943a9b80a651b9fa351803bde6c Mon Sep 17 00:00:00 2001
From: zarzuelo <zarzuelo@gmail.com>
Date: Mon, 11 Mar 2013 15:57:35 +0000
Subject: [PATCH] 2013-03-11  Sergio Martin <sergio.martin@artica.es>

	* include/functions_groups.php
	include/functions_graph.php: Added extral controls in status
	count retrieving functions to avoid negative numbers for
	bug 3607315

	* extras/pandoradb_migrate_4.0.x_to_5.0.mysql.sql
	extras/pandoradb_migrate_4.0.x_to_5.0.oracle.sql
	extras/pandoradb_migrate_4.0.x_to_5.0.postgreSQL.sql: Updated
	the data of unknown modules to new fields in migration




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@7828 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     | 12 ++++++
 .../pandoradb_migrate_4.0.x_to_5.0.mysql.sql  |  6 +++
 .../pandoradb_migrate_4.0.x_to_5.0.oracle.sql |  6 +++
 ...doradb_migrate_4.0.x_to_5.0.postgreSQL.sql |  6 +++
 pandora_console/include/functions_graph.php   | 12 ++++--
 pandora_console/include/functions_groups.php  | 40 ++++++++++++++-----
 6 files changed, 68 insertions(+), 14 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index 2e61c4ab36..f5baf58a41 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,15 @@
+2013-03-11  Sergio Martin <sergio.martin@artica.es>
+
+	* include/functions_groups.php
+	include/functions_graph.php: Added extral controls in status
+	count retrieving functions to avoid negative numbers for
+	bug 3607315
+
+	* extras/pandoradb_migrate_4.0.x_to_5.0.mysql.sql
+	extras/pandoradb_migrate_4.0.x_to_5.0.oracle.sql
+	extras/pandoradb_migrate_4.0.x_to_5.0.postgreSQL.sql: Updated
+	the data of unknown modules to new fields in migration
+
 2013-03-11 Miguel de Dios <miguel.dedios@artica.es>
 	
 	* godmode/agentes/planned_downtime.list.php: fixed the lost data
diff --git a/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.mysql.sql
index 4672c8b6a8..d202f3e161 100644
--- a/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.mysql.sql
+++ b/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.mysql.sql
@@ -1108,3 +1108,9 @@ UPDATE talert_commands SET fields_descriptions = '["Community","Destination&#x20
 UPDATE talert_commands SET fields_descriptions = '["Log&#x20;Info&#x20;1","Log&#x20;Info&#x20;2","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 6;
 UPDATE talert_commands SET fields_descriptions = '["Destination&#x20;number","Message","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 9;
 UPDATE talert_commands SET fields_descriptions = '["Agent&#x20;name","Module&#x20;name","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 10;
+
+-- ---------------------------------------------------------------------
+-- Update the data of unknown modules to new fields (11/3/13)
+-- ---------------------------------------------------------------------
+
+UPDATE tagente_estado SET last_known_status = last_status, last_status = 3 WHERE estado = 3;
diff --git a/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.oracle.sql b/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.oracle.sql
index 1f44a496e3..a66f845289 100644
--- a/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.oracle.sql
+++ b/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.oracle.sql
@@ -1114,3 +1114,9 @@ UPDATE talert_commands SET fields_descriptions = '["Community","Destination&#x20
 UPDATE talert_commands SET fields_descriptions = '["Log&#x20;Info&#x20;1","Log&#x20;Info&#x20;2","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 6;
 UPDATE talert_commands SET fields_descriptions = '["Destination&#x20;number","Message","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 9;
 UPDATE talert_commands SET fields_descriptions = '["Agent&#x20;name","Module&#x20;name","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 10;
+
+-- ---------------------------------------------------------------------
+-- Update the data of unknown modules to new fields (11/3/13)
+-- ---------------------------------------------------------------------
+
+UPDATE tagente_estado SET last_known_status = last_status, last_status = 3 WHERE estado = 3;
diff --git a/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.postgreSQL.sql b/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.postgreSQL.sql
index a68925f650..99ea7536d7 100644
--- a/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.postgreSQL.sql
+++ b/pandora_console/extras/pandoradb_migrate_4.0.x_to_5.0.postgreSQL.sql
@@ -1104,3 +1104,9 @@ UPDATE "talert_commands" SET "fields_descriptions" = '["Community","Destination&
 UPDATE "talert_commands" SET "fields_descriptions" = '["Log&#x20;Info&#x20;1","Log&#x20;Info&#x20;2","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 6;
 UPDATE "talert_commands" SET "fields_descriptions" = '["Destination&#x20;number","Message","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 9;
 UPDATE "talert_commands" SET "fields_descriptions" = '["Agent&#x20;name","Module&#x20;name","","","","","","","",""]', fields_values = '["","","","","","","","","",""]' WHERE id = 10;
+
+-- ---------------------------------------------------------------------
+-- Update the data of unknown modules to new fields (11/3/13)
+-- ---------------------------------------------------------------------
+
+UPDATE tagente_estado SET last_known_status = last_status, last_status = 3 WHERE estado = 3;
diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php
index 05cb38eb59..9ced1aca80 100755
--- a/pandora_console/include/functions_graph.php
+++ b/pandora_console/include/functions_graph.php
@@ -1397,10 +1397,16 @@ function graph_agent_status ($id_agent = false, $width = 300, $height = 200, $re
 		$fields[] = 'SUM(notinit_count) "Not init"';
 	}
 	
-	$agent_status = db_get_all_rows_filter('tagente', $filter, $fields);
+	$data = db_get_row_filter('tagente', $filter, $fields);
 	
-	$data = reset($agent_status);
-			
+	// If any value is negative, truncate it to 0
+	function truncate_negatives(&$element) {
+		if($element < 0) {
+			$element = 0;
+		}
+	}
+	array_walk($data, 'truncate_negatives');
+				
 	$water_mark = array('file' => $config['homedir'] .  "/images/logo_vertical_water.png",
 		'url' => ui_get_full_url("/images/logo_vertical_water.png"));
 	
diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php
index ab836ec7b1..fef773981c 100644
--- a/pandora_console/include/functions_groups.php
+++ b/pandora_console/include/functions_groups.php
@@ -868,7 +868,9 @@ function groups_agent_not_init ($group_array) {
 	$group_clause = implode (",", $group_array);
 	$group_clause = "(" . $group_clause . ")";
 	
-	return db_get_sql ("SELECT COUNT(*) FROM tagente WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count=0 AND notinit_count>0 AND id_grupo IN $group_clause");	
+	$count = db_get_sql ("SELECT COUNT(*) FROM tagente WHERE disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count=0 AND notinit_count>0 AND id_grupo IN $group_clause");	
+	
+	return $count > 0 ? $count : 0;
 }
 
 
@@ -886,7 +888,9 @@ function groups_agent_unknown ($group_array) {
 	$group_clause = implode (",", $group_array);
 	$group_clause = "(" . $group_clause . ")";
 	
-	return db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count>0 AND id_grupo IN $group_clause");
+	$count = db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND critical_count=0 AND warning_count=0 AND unknown_count>0 AND id_grupo IN $group_clause");
+	
+	return $count > 0 ? $count : 0;
 }
 
 // Get ok agents by using the status code in modules.
@@ -904,7 +908,9 @@ function groups_agent_ok ($group_array) {
 	$group_clause = implode (",", $group_array);
 	$group_clause = "(" . $group_clause . ")";
 	
-	return db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND normal_count=total_count AND id_grupo IN $group_clause");
+	$count = db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND normal_count=total_count AND id_grupo IN $group_clause");
+
+	return $count > 0 ? $count : 0;
 }
 
 // Get critical agents by using the status code in modules.
@@ -923,8 +929,9 @@ function groups_agent_critical ($group_array) {
 	$group_clause = "(" . $group_clause . ")";
 	
 	//TODO REVIEW ORACLE AND POSTGRES
-	return db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND critical_count>0 AND id_grupo IN $group_clause");
+	$count = db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND critical_count>0 AND id_grupo IN $group_clause");
 	
+	return $count > 0 ? $count : 0;	
 }
 
 // Get warning agents by using the status code in modules.
@@ -942,7 +949,9 @@ function groups_agent_warning ($group_array) {
 	$group_clause = implode (",", $group_array);
 	$group_clause = "(" . $group_clause . ")";
 	
-	return db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND critical_count=0 AND warning_count>0 AND id_grupo IN $group_clause");	
+	$count = db_get_sql ("SELECT COUNT(*) FROM tagente WHERE tagente.disabled=0 AND critical_count=0 AND warning_count>0 AND id_grupo IN $group_clause");	
+	
+	return $count > 0 ? $count : 0;
 }
 
 
@@ -964,7 +973,9 @@ function groups_monitor_not_init ($group_array) {
 	$group_clause = "(" . $group_clause . ")";
 	
 	//TODO REVIEW ORACLE AND POSTGRES
-	return db_get_sql ("SELECT SUM(notinit_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");	
+	$count = db_get_sql ("SELECT SUM(notinit_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");	
+	
+	return $count > 0 ? $count : 0;
 }
 
 // Get monitor OK, except disabled and non-init
@@ -985,7 +996,9 @@ function groups_monitor_ok ($group_array) {
 	$group_clause = "(" . $group_clause . ")";
 	
 	//TODO REVIEW ORACLE AND POSTGRES
-	return db_get_sql ("SELECT SUM(normal_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");
+	$count = db_get_sql ("SELECT SUM(normal_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");
+	
+	return $count > 0 ? $count : 0;
 }
 
 // Get monitor CRITICAL, except disabled and non-init
@@ -1006,7 +1019,9 @@ function groups_monitor_critical ($group_array) {
 	$group_clause = "(" . $group_clause . ")";
 	
 	//TODO REVIEW ORACLE AND POSTGRES
-	return db_get_sql ("SELECT SUM(critical_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");
+	$count = db_get_sql ("SELECT SUM(critical_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");
+	
+	return $count > 0 ? $count : 0;
 }
 
 // Get monitor WARNING, except disabled and non-init
@@ -1027,7 +1042,9 @@ function groups_monitor_warning ($group_array) {
 	$group_clause = "(" . $group_clause . ")";
 	
 	//TODO REVIEW ORACLE AND POSTGRES
-	return db_get_sql ("SELECT SUM(warning_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");						
+	$count =  db_get_sql ("SELECT SUM(warning_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");						
+	
+	return $count > 0 ? $count : 0;
 }
 
 // Get monitor UNKNOWN, except disabled and non-init
@@ -1048,7 +1065,9 @@ function groups_monitor_unknown ($group_array) {
 	$group_clause = "(" . $group_clause . ")";
 	
 	//TODO REVIEW ORACLE AND POSTGRES
-	return db_get_sql ("SELECT SUM(unknown_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");
+	$count = db_get_sql ("SELECT SUM(unknown_count) FROM tagente WHERE disabled = 0 AND id_grupo IN $group_clause");
+	
+	return $count > 0 ? $count : 0;
 }
 
 // Get alerts defined for a given group, except disabled 
@@ -1162,7 +1181,6 @@ function groups_agent_disabled ($group_array) {
 	$sql = "SELECT COUNT(*) FROM tagente WHERE id_grupo IN $group_clause AND disabled = 1";
 	
 	return db_get_sql ($sql);
-	
 }
 
 ?>