From ba4e926831e0f3f3c8b65710561b57dc776b50b6 Mon Sep 17 00:00:00 2001
From: slerena <slerena@gmail.com>
Date: Thu, 17 Apr 2008 15:27:25 +0000
Subject: [PATCH] 2008-04-17  Sancho Lerena <slerena@artica.es>

    * include/functions_db.php: Fixed problem in show_alert_row_mini()

    * operation/agentes/status_monitor.php: Almost all code changed.
    Added pagination, replaced query for a paginated version and a lot
    more efficient. Added freetext search on name / description of
    monitors.

    * operation/agentes/estado_ultimopaquete.php: Fixed problem with
    refresh / push buttons on data server modules.

    * godmode/admin_access_logs.php: Fixed several problems. Added a
    better pagination.

    * godmode/agentes/module_manager_editor.php,
    godmode/agentes/module_manager_editor_network.php: When pushed
    SNMP walk, lost all fields. Fixed. Also fixed problems with min/max.

    * godmode/agentes/module_manager_editor_data.php,
    godmode/agentes/module_manager_editor_wmi.php,
    godmode/agentes/module_manager_editor_plugin.php
    godmode/agentes/module_manager_editor_prediction.php: Fixed problems
    with inverted max/min.

    * godmode/agentes/configurar_agente.php: Keepalive modules now are
    from id_module=1 (data server). before was type 0 and that gives a lot
    of problems.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@810 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     |  31 ++
 pandora_console/godmode/admin_access_logs.php |  43 ++-
 .../godmode/agentes/configurar_agente.php     |   8 +-
 .../godmode/agentes/module_manager_editor.php |   2 +-
 .../agentes/module_manager_editor_data.php    |   4 +-
 .../agentes/module_manager_editor_network.php |   6 +-
 .../agentes/module_manager_editor_plugin.php  |   4 +-
 .../module_manager_editor_prediction.php      |   4 +-
 .../agentes/module_manager_editor_wmi.php     |   4 +-
 pandora_console/include/config.php            |   6 +-
 pandora_console/include/functions_db.php      |   2 +-
 .../agentes/estado_ultimopaquete.php          |   2 +-
 .../operation/agentes/status_monitor.php      | 289 +++++++++---------
 13 files changed, 211 insertions(+), 194 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index c7511a2ec9..d71ed62d11 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,34 @@
+
+
+2008-04-17  Sancho Lerena <slerena@artica.es>
+
+    * include/functions_db.php: Fixed problem in show_alert_row_mini()
+
+    * operation/agentes/status_monitor.php: Almost all code changed.
+    Added pagination, replaced query for a paginated version and a lot
+    more efficient. Added freetext search on name / description of 
+    monitors.
+
+    * operation/agentes/estado_ultimopaquete.php: Fixed problem with
+    refresh / push buttons on data server modules.
+
+    * godmode/admin_access_logs.php: Fixed several problems. Added a 
+    better pagination.
+
+    * godmode/agentes/module_manager_editor.php,
+    godmode/agentes/module_manager_editor_network.php: When pushed 
+    SNMP walk, lost all fields. Fixed. Also fixed problems with min/max.
+    
+    * godmode/agentes/module_manager_editor_data.php,
+    godmode/agentes/module_manager_editor_wmi.php,
+    godmode/agentes/module_manager_editor_plugin.php
+    godmode/agentes/module_manager_editor_prediction.php: Fixed problems
+    with inverted max/min.
+
+    * godmode/agentes/configurar_agente.php: Keepalive modules now are
+    from id_module=1 (data server). before was type 0 and that gives a lot
+    of problems.
+
 2008-04-10  Sancho Lerena <slerena@artica.es>
 
 	* godmode/agentes/configurar_agente.php: Fixe problem with server
diff --git a/pandora_console/godmode/admin_access_logs.php b/pandora_console/godmode/admin_access_logs.php
index 05a983a374..2b57b48e66 100644
--- a/pandora_console/godmode/admin_access_logs.php
+++ b/pandora_console/godmode/admin_access_logs.php
@@ -97,33 +97,26 @@
 	else
 		$url = "index.php?sec=godmode&sec2=godmode/admin_access_logs";
 
-	//echo "URLTipolog  $tipo_log";
-	pagination ($counter, $url, $offset);
-	echo '<br>';
-	// table header
-	echo '<table cellpadding="4" cellspacing="4" width="700" class="databox">';
-	echo '<tr>';
-	echo '<th width="80px">'.$lang_label["user"].'</th>';
-	echo '<th>'.$lang_label["action"].'</th>';
-	echo '<th width="130px">'.$lang_label["date"].'</th>';
-	echo '<th width="100px">'.$lang_label["src_address"].'</th>';
-	echo '<th width="200px">'.$lang_label["comments"].'</th>';
+  // Prepare query and pagination
+    $query1 = "SELECT * FROM tsesion " . $tipo_log_select." ORDER BY fecha DESC"; 
+    if ( $counter > $config["block_size"]) {
+        pagination ($counter, $url, $offset);
+        $query1 .= " LIMIT $offset , ".$config["block_size"];
+    }
+    $result=mysql_query($query1);
 
-	// Skip offset records
-	$query1="SELECT * FROM tsesion ".$tipo_log_select." ORDER BY fecha DESC";
-	$result=mysql_query($query1);
-	$offset_counter = 0;
-	while ($offset_counter < $offset){
-		if ($row=mysql_fetch_array($result))
-			$offset_counter++;
-		else
-			$offset_counter = $offset; //exit condition
-	}
+	    // table header
+    echo '<table cellpadding="4" cellspacing="4" width="700" class="databox">';
+    echo '<tr>';
+    echo '<th width="80px">'.$lang_label["user"].'</th>';
+    echo '<th>'.$lang_label["action"].'</th>';
+    echo '<th width="130px">'.$lang_label["date"].'</th>';
+    echo '<th width="100px">'.$lang_label["src_address"].'</th>';
+    echo '<th width="200px">'.$lang_label["comments"].'</th>';
 
-	$offset_counter = 0;
 	$color=1;
 	// Get data
-	while ($row=mysql_fetch_array($result) and ($offset_counter < $block_size) ){
+	while ($row=mysql_fetch_array($result)) {
 		if ($color == 1){
 			$tdcolor = "datos";
 			$color = 0;
@@ -132,14 +125,12 @@
 			$tdcolor = "datos2";
 			$color = 1;
 		}
-		$usuario=$row["ID_usuario"];
-		echo '<tr><td class="'.$tdcolor.'_id">'.$usuario;
+		echo '<tr><td class="'.$tdcolor.'_id">'.$row["ID_usuario"];
 		echo '<td class="'.$tdcolor.'">'.$row["accion"];
 		echo '<td class="'.$tdcolor.'f9">'.$row["fecha"];
 		echo '<td class="'.$tdcolor.'f9">'.$row["IP_origen"];
 		echo '<td class="'.$tdcolor.'">'.$row["descripcion"];
 		echo '</tr>';
-		$offset_counter++;
 	}
 
 	// end table
diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php
index c455088731..d46eea41f6 100644
--- a/pandora_console/godmode/agentes/configurar_agente.php
+++ b/pandora_console/godmode/agentes/configurar_agente.php
@@ -132,8 +132,8 @@ if ( isset ($_POST["create_agent"])) { // Create a new and shining agent
             $agent_creation_error = "";
             $id_agente = mysql_insert_id ();
             // Create special MODULE agent_keepalive
-            $sql_insert = "INSERT INTO tagente_modulo (nombre, id_agente, id_tipo_modulo, descripcion)
-                         VALUES ('agent_keepalive', ".$id_agente.",100,'Agent Keepalive monitor')";
+            $sql_insert = "INSERT INTO tagente_modulo (nombre, id_agente, id_tipo_modulo, descripcion, id_modulo)
+                         VALUES ('agent_keepalive', ".$id_agente.",100,'Agent Keepalive monitor',1)";
             $result=mysql_query($sql_insert);
             $id_agent_module = mysql_insert_id();
             // And create MODULE agent_keepalive in tagente_estado table 
@@ -264,8 +264,6 @@ if (isset($_GET["delete_alert_comp"])){ // if modified some parameter
 // Create alert
 // =============
 if (isset($_POST["insert_alert"])){ // if created alert
-	
-    
     $combined = get_parameter ("combined",0);
 	$id_agente_modulo = get_parameter ("agente_modulo",0);
     $maximo = get_parameter ("maximo",0);
@@ -618,13 +616,11 @@ if ((isset($_POST["update_module"])) || (isset($_POST["insert_module"]))) {
 		require ("general/footer.php");
 		exit;
 	}
-
 	$form_id_tipo_modulo = get_parameter ("form_id_tipo_modulo");
 	$form_name = get_parameter ("form_name");
 	$form_description = get_parameter ("form_description");
 	$form_id_module_group = get_parameter ("form_id_module_group",0);
 	$form_flag = get_parameter ("form_flag",0);
-	$form_id_tipo_modulo = get_parameter ("form_id_tipo_modulo");
 	$form_post_process = get_parameter ("form_post_process",0);
 	$form_prediction_module = get_parameter ("form_prediction_module",0);
 	$form_max_timeout = get_parameter ("form_max_timeout",0);
diff --git a/pandora_console/godmode/agentes/module_manager_editor.php b/pandora_console/godmode/agentes/module_manager_editor.php
index 8e7defef89..16b44487f7 100644
--- a/pandora_console/godmode/agentes/module_manager_editor.php
+++ b/pandora_console/godmode/agentes/module_manager_editor.php
@@ -79,7 +79,7 @@ if (($form_moduletype == "networkserver") && ($form_network_component != "") &&
     $form_prediction_module = "";
     $form_id_plugin = "";
     $form_post_process = "";
-} else {
+} elseif (!isset($_POST['oid'])){
     // Clean up specific network modules fields
     $form_name = "";
     $form_description = "";
diff --git a/pandora_console/godmode/agentes/module_manager_editor_data.php b/pandora_console/godmode/agentes/module_manager_editor_data.php
index b06b8f9bb9..4b584fceb6 100644
--- a/pandora_console/godmode/agentes/module_manager_editor_data.php
+++ b/pandora_console/godmode/agentes/module_manager_editor_data.php
@@ -46,8 +46,8 @@ if ($update_module_id != NULL){
 		$form_id_tipo_modulo = $row['id_tipo_modulo']; // It doesn't matter
 		$form_description = $row['descripcion'];
 		$form_name = $row['nombre'];
-		$form_minvalue = $row['max'];
-		$form_maxvalue = $row['min'];
+		$form_minvalue = $row['min'];
+		$form_maxvalue = $row['max'];
 		$form_interval = $row['module_interval'];
 		$form_tcp_port = $row['tcp_port'];
 		$form_tcp_send = $row['tcp_send'];
diff --git a/pandora_console/godmode/agentes/module_manager_editor_network.php b/pandora_console/godmode/agentes/module_manager_editor_network.php
index add2654ca8..0dae499b78 100644
--- a/pandora_console/godmode/agentes/module_manager_editor_network.php
+++ b/pandora_console/godmode/agentes/module_manager_editor_network.php
@@ -20,6 +20,7 @@
 // General startup for established session
 global $config;
 check_login();
+global $form_snmp_community;
 
 // get the variable form_moduletype
 $form_moduletype = get_parameter_post ("form_moduletype");
@@ -35,6 +36,7 @@ if (give_acl($config["id_user"], 0, "AW")!=1) {
     exit;
 }
 
+
 // Check whether we are updataing and get data if so
 if ($update_module_id != NULL){
     $row = get_db_row ("tagente_modulo", 'id_agente_modulo', $update_module_id);
@@ -46,8 +48,8 @@ if ($update_module_id != NULL){
 		$form_id_tipo_modulo = $row['id_tipo_modulo']; // It doesn't matter
 		$form_description = $row['descripcion'];
 		$form_name = $row['nombre'];
-		$form_minvalue = $row['max'];
-		$form_maxvalue = $row['min'];
+		$form_minvalue = $row['min'];
+		$form_maxvalue = $row['max'];
 		$form_interval = $row['module_interval'];
 		$form_tcp_port = $row['tcp_port'];
 		$form_tcp_send = $row['tcp_send'];
diff --git a/pandora_console/godmode/agentes/module_manager_editor_plugin.php b/pandora_console/godmode/agentes/module_manager_editor_plugin.php
index 0bc3981139..7ddaa3381e 100644
--- a/pandora_console/godmode/agentes/module_manager_editor_plugin.php
+++ b/pandora_console/godmode/agentes/module_manager_editor_plugin.php
@@ -46,8 +46,8 @@ if ($update_module_id != NULL){
 		$form_id_tipo_modulo = $row['id_tipo_modulo']; // It doesn't matter
 		$form_description = $row['descripcion'];
 		$form_name = $row['nombre'];
-		$form_minvalue = $row['max'];
-		$form_maxvalue = $row['min'];
+		$form_minvalue = $row['min'];
+		$form_maxvalue = $row['max'];
 		$form_interval = $row['module_interval'];
 		$form_tcp_port = $row['tcp_port'];
 		$form_tcp_send = $row['tcp_send'];
diff --git a/pandora_console/godmode/agentes/module_manager_editor_prediction.php b/pandora_console/godmode/agentes/module_manager_editor_prediction.php
index 710d7a3260..306b2eed24 100644
--- a/pandora_console/godmode/agentes/module_manager_editor_prediction.php
+++ b/pandora_console/godmode/agentes/module_manager_editor_prediction.php
@@ -46,8 +46,8 @@ if ($update_module_id != NULL){
 		$form_id_tipo_modulo = $row['id_tipo_modulo']; // It doesn't matter
 		$form_description = $row['descripcion'];
 		$form_name = $row['nombre'];
-		$form_minvalue = $row['max'];
-		$form_maxvalue = $row['min'];
+		$form_minvalue = $row['min'];
+		$form_maxvalue = $row['max'];
 		$form_interval = $row['module_interval'];
 		$form_tcp_port = $row['tcp_port'];
 		$form_tcp_send = $row['tcp_send'];
diff --git a/pandora_console/godmode/agentes/module_manager_editor_wmi.php b/pandora_console/godmode/agentes/module_manager_editor_wmi.php
index de23c1dfd0..87c95ad5b3 100644
--- a/pandora_console/godmode/agentes/module_manager_editor_wmi.php
+++ b/pandora_console/godmode/agentes/module_manager_editor_wmi.php
@@ -46,8 +46,8 @@ if ($update_module_id != NULL){
 		$form_id_tipo_modulo = $row['id_tipo_modulo']; // It doesn't matter
 		$form_description = $row['descripcion'];
 		$form_name = $row['nombre'];
-		$form_minvalue = $row['max'];
-		$form_maxvalue = $row['min'];
+		$form_minvalue = $row['min'];
+		$form_maxvalue = $row['max'];
 		$form_interval = $row['module_interval'];
 		$form_tcp_port = $row['tcp_port'];
 		$form_tcp_send = $row['tcp_send'];
diff --git a/pandora_console/include/config.php b/pandora_console/include/config.php
index 58ecfc80e8..cbb5b8b9f7 100644
--- a/pandora_console/include/config.php
+++ b/pandora_console/include/config.php
@@ -2,7 +2,7 @@
 // Begin of automatic config file
 $config["dbname"]="pandora";			// MySQL DataBase name
 $config["dbuser"]="pandora";			// DB User
-$config["dbpass"]="pandora";	// DB Password
+$config["dbpass"]="pfnfkudt";	// DB Password
 $config["dbhost"]="localhost";			// DB Host
 $config["homedir"]="/var/www/pandora_console/";		// Config homedir
 $config["homeurl"]="http://localhost/pandora_console";			// Base URL
@@ -37,10 +37,10 @@ $config["homeurl"]="http://localhost/pandora_console";			// Base URL
 // $config["homeurl"]="/pandora_console/";
 
 // Do not display any ERROR
-// error_reporting(0); // Need to use active console at this moment
+//error_reporting(0); // Need to use active console at this moment
 
 // Display ALL errors
-error_reporting(1);
+error_reporting(E_ALL);
 
 // This is directory where placed "/attachment" directory, to upload files stores. 
 // This MUST be writtable by http server user, and should be in pandora root. 
diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php
index 00471fcd62..ebd59b8aa3 100644
--- a/pandora_console/include/functions_db.php
+++ b/pandora_console/include/functions_db.php
@@ -1414,7 +1414,7 @@ function show_alert_row_mini ($id_combined_alert){
     
         // We have alert text ?
         if ($row2["alert_text"]!= "") {
-            echo "<td colspan=2 class='$tdcolor'>".$lang_label["text"]."</td>";
+            echo "<td class='$tdcolor'>".$lang_label["text"]."</td>";
         } else {
             echo "<td class='$tdcolor'>".$mymin."/".$mymax."</td>";
         }
diff --git a/pandora_console/operation/agentes/estado_ultimopaquete.php b/pandora_console/operation/agentes/estado_ultimopaquete.php
index f83185367e..abb74fe3ce 100644
--- a/pandora_console/operation/agentes/estado_ultimopaquete.php
+++ b/pandora_console/operation/agentes/estado_ultimopaquete.php
@@ -78,7 +78,7 @@ if (comprueba_login() == 0) {
 			$id_grupo = $row_t["id_grupo"];
 			$id_usuario=$_SESSION["id_usuario"];
 			if (give_acl($id_usuario, $id_grupo, "AW")==1){
-				if (($row3["id_modulo"] != 1) AND ($row3["id_tipo_modulo"] < 100)) {
+				if (($row3["id_modulo"] > 1) AND ($row3["id_tipo_modulo"] < 100)) {
 					if ($row3["flag"] == 0){
 						echo "<a href='index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=".$id_agente."&id_agente_modulo=".$row3["id_agente_modulo"]."&flag=1&tab=data&refr=60'><img src='images/target.png' border='0'></a>";
 					} else {
diff --git a/pandora_console/operation/agentes/status_monitor.php b/pandora_console/operation/agentes/status_monitor.php
index 0d78139e07..7acb72d962 100644
--- a/pandora_console/operation/agentes/status_monitor.php
+++ b/pandora_console/operation/agentes/status_monitor.php
@@ -3,7 +3,7 @@
 // ====================================
 // Copyright (c) 2004-2008 Sancho Lerena, slerena@gmail.com
 // Copyright (c) 2005-2006 Artica Soluciones Tecnologicas S.L, info@artica.es
-// Copyright (c) 2004-2008 Raul Mateos Martin, raulofpandora@gmail.com
+
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License
 // as published by the Free Software Foundation; either version 2
@@ -17,7 +17,9 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 // Load global vars
-require("include/config.php");
+global $config;
+$id_user = $config["id_user"];
+
 
 if (comprueba_login() != 0) {
         require ("general/noaccess.php");
@@ -34,23 +36,31 @@ if ((give_acl($id_user, 0, "AR")!=1) AND (give_acl($id_user,0,"AW")!=1)) {
 echo "<h2>".$lang_label["ag_title"]." &gt; ";
 echo $lang_label["monitor_listing"]."</h2>";
 
+$ag_freestring = get_parameter ("ag_freestring", "");
+$ag_modulename = get_parameter ("ag_modulename", "");
+$ag_group = get_parameter ("ag_group", -1);
+$offset = get_parameter ("offset", 0);
 
-if (isset($_POST["ag_group"]))
-	$ag_group = $_POST["ag_group"];
-elseif (isset($_GET["group_id"]))
-	$ag_group = $_GET["group_id"];
-else
-	$ag_group = -1;
-if (isset($_GET["ag_group_refresh"])){
-	$ag_group = $_GET["ag_group_refresh"];
-}
+$URL = "index.php?sec=estado&sec2=operation/agentes/status_monitor&refr=60";
+echo "<form method='post' action='";
+if ($ag_group != -1)
+	$URL .= "&ag_group_refresh=".$ag_group;
+
+// Module name selector
+// This code thanks for an idea from Nikum, nikun_h@hotmail.com
+if ($ag_modulename != "")
+    $URL .= "&ag_modulename=".$ag_modulename;
+
+
+// Freestring selector
+if ($ag_freestring != "")
+    $URL .= "&ag_freestring=".$ag_freestring ;
+
+echo $URL;
+
+// End FORM TAG
+echo "'>";
 
-if (isset($_POST["ag_group"])){
-	$ag_group = $_POST["ag_group"];
-	echo "<form method='post' action='index.php?sec=estado&sec2=operation/agentes/status_monitor&refr=60&ag_group_refresh=".$ag_group."'>";
-} else {
-	echo "<form method='post' action='index.php?sec=estado&sec2=operation/agentes/status_monitor&refr=60'>";
-}
 echo "<table cellspacing='4' cellpadding='4' width='600' class='databox'>";
 echo "<tr><td valign='middle'>".$lang_label["group"]."</td>";
 echo "<td valign='middle'>";
@@ -62,23 +72,6 @@ if ( $ag_group > 1 ){
 echo "<option value=1>".dame_nombre_grupo(1)."</option>";
 list_group ($id_user);
 echo "</select>";
-
-// Module name selector
-// This code thanks for an idea from Nikum, nikun_h@hotmail.com
-if (isset($_POST["ag_modulename"])){
-	$ag_modulename = $_POST["ag_modulename"];
-	echo "<form method='post' action='index.php?sec=estado&
-	sec2=operation/agentes/status_monitor&
-	refr=60&ag_modulename=".$ag_modulename."'>";
-} else {
-	echo "<form method='post' action='index.php?sec=estado&
-	sec2=operation/agentes/status_monitor&refr=60'>";
-}
-
-echo "<td class='f9' style='padding-left: 10px;'>";
-echo "<img src='images/pixel_green.png' width=40 height=18><br>".$lang_label["green_light"]."</td>";
-echo "<td class='f9' style='padding-left: 10px;'>";
-echo "<img src='images/pixel_red.png' width=40 height=18><br>".$lang_label["red_light"]."</td>";
 echo "</td>";
 echo "</tr>";
 echo "<tr>";
@@ -97,122 +90,126 @@ while ($row=mysql_fetch_array($result)){
 	echo "<option>".$row['0']."</option>";
 }
 echo "</select>";
-echo "<td valign='middle'>
-<noscript><input name='uptbutton' type='submit' class='sub' 
-value='".$lang_label["show"]."'></noscript>
-</form>";
-
-// Show only selected names & groups
-if ($ag_group > 1) 
-	$sql='SELECT * FROM tagente WHERE id_grupo='.$ag_group.' ORDER BY nombre';
-else 
-	$sql='SELECT * FROM tagente ORDER BY id_grupo, nombre';
+echo "<td valign='middle'>";
+echo lang_string ("Free text");
 
+echo "&nbsp;<input type=text name='ag_freestring' size=15 value='$ag_freestring'>";
+echo "<td valign='middle'>";
+echo "<input name='uptbutton' type='submit' class='sub' value='".$lang_label["show"]."'";
+echo "</form>";
 echo "</table>";
-echo "<br>";
-$color =1;
-$result=mysql_query($sql);
-if (mysql_num_rows($result)){
-	while ($row=mysql_fetch_array($result)){ //while there are agents
-		if ($row["disabled"] == 0) {
-			if ((isset($ag_modulename)) && ($ag_modulename != $lang_label["all"])){
-				$query_gen='SELECT
-				id_agente, id_tipo_modulo, module_interval, id_agente_modulo,
-				nombre, descripcion
-				FROM tagente_modulo 
-				WHERE id_agente = '.$row["id_agente"].' 
-				AND nombre = "'.entrada_limpia($_POST["ag_modulename"]).'" 
-				AND
-				id_tipo_modulo in (2, 9, 12, 18, 6, 100)';
-				// generic_proc, remote_tcp_proc, ??, remote_snmp_proc, remote_icmp_proc
-						} else {
-				$query_gen='SELECT
-				id_agente, id_tipo_modulo, module_interval, id_agente_modulo,
-				nombre, descripcion
-				FROM tagente_modulo 
-				WHERE id_agente = '.$row["id_agente"].' 
-				AND
-				id_tipo_modulo in (2, 9, 12, 18, 6, 100)';
-						}
-				$result_gen=mysql_query($query_gen);
-			if (mysql_num_rows ($result_gen)) {
-				while ($data=mysql_fetch_array($result_gen)){
-					if ($color == 1){
-						$tdcolor="datos";
-						$color =0;
-					} else {
-						$tdcolor="datos2";
-						$color =1;
-					}
-					if (!isset($string)) {$string='';}
-					$string=$string. "<tr><td class='$tdcolor'>";
-					$string=$string. "<a href='index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=".$data["id_agente"]."&id_agente_modulo=".$data["id_agente_modulo"]."&flag=1&tab=data&refr=60'>";
-					$string=$string."<img src='images/target.png'></a>";
-					$string=$string. "</td><td class='$tdcolor'>";
-					$string=$string."<b><a href='index.php?sec=estado&
-					sec2=operation/agentes/ver_agente&
-					id_agente=".$data["id_agente"]."'>".
-					strtoupper(substr(dame_nombre_agente($data["id_agente"]),0,21))."</a></b>";
-					$string=$string."</td><td class='$tdcolor'>";
-					$string=$string."
-					<img src='images/".show_icon_type($data["id_tipo_modulo"])."' border=0>
-					</td>";
-					$string=$string."<td class='$tdcolor'>".
-					substr($data["nombre"],0,21)."</td>";
-					$string=$string."<td class='".$tdcolor."f9' title='".$data["descripcion"]."'>".
-					substr($data["descripcion"],0,30)."</td>";
-					
-					$string=$string."<td class='$tdcolor' align='center' width=25>";
-					if ($data["module_interval"] == 0){
-						$my_interval = give_agentinterval($data["id_agente"]);
-					} else {
-						$my_interval = $data["module_interval"];						
-					}
-					$string .= $my_interval;
-					
-					$query_gen2='SELECT * FROM tagente_estado 
-					WHERE id_agente_modulo = '.$data["id_agente_modulo"];
-					$result_gen2=mysql_query($query_gen2);
-					$data2=mysql_fetch_array($result_gen2);
-					$string=$string."<td class='$tdcolor' align='center' width=20>";
-					if ($data2["datos"] > 0){
-						$string=$string."<img src='images/pixel_green.png' width=40 height=18 title='".$lang_label["green_light"]."'>";
-					} else {
-						$string=$string."<img src='images/pixel_red.png' width=40 height=18 title='".$lang_label["red_light"]."'>";
-					}
-					
-					$string=$string."<td class='".$tdcolor."f9'>";
-					$seconds = time() - $data2["utimestamp"];
-					if ($seconds >= ($my_interval*2))
-						$string .= "<span class='redb'>";
-					else
-						$string .= "<span>";
 
-					$string .= human_time_comparation($data2["timestamp"])."
-					</span></td></tr>";
-				}
-			}
-		}
-	}
-	if (isset($string)) {
-		echo "
-		<table cellpadding='4' cellspacing='4' width='750' class='databox'>
-		<tr>
-		<th>
-		<th>".$lang_label["agent"]."</th>
-		<th>".$lang_label["type"]."</th>
-		<th>".$lang_label["name"]."</th>
-		<th>".$lang_label["description"]."</th>
-		<th>".$lang_label["interval"]."</th>
-		<th>".$lang_label["status"]."</th>
-		<th>".$lang_label["timestamp"]."</th>";
-		echo $string; //the built table of monitors
-		echo "</table>";
-	} else {
-		echo "<div class='nf'>".$lang_label["no_monitors_g"]."</div>";
-	}
+// Begin Build SQL sentences
+
+$SQL_pre = "SELECT tagente_modulo.id_agente_modulo, tagente.nombre, tagente_modulo.nombre, tagente_modulo.descripcion, tagente.id_grupo, tagente.id_agente, tagente_modulo.id_tipo_modulo, tagente_modulo.module_interval ";
+
+$SQL_pre_count = "SELECT count(tagente_modulo.id_agente_modulo) ";
+
+$SQL = " FROM tagente, tagente_modulo WHERE tagente.id_agente = tagente_modulo.id_agente AND tagente_modulo.disabled = 0 AND tagente_modulo.id_tipo_modulo in (2, 9, 12, 18, 6, 100) ";
+
+// Agent group selector
+if ($ag_group > 1)
+    $SQL .=" AND tagente.id_grupo = ".$ag_group;
+
+// Module name selector
+// This code thanks for an idea from Nikum, nikun_h@hotmail.com
+if ($ag_modulename != "")
+    $SQL .= " AND tagente_modulo.nombre = '$ag_modulename'";
+
+// Freestring selector
+if ($ag_freestring != "")
+    $SQL .= " AND ( tagente_modulo.nombre LIKE '%".$ag_freestring."%' OR tagente_modulo.descripcion LIKE '%".$ag_freestring."%') ";
+$SQL .= " ORDER BY tagente.id_grupo, tagente.nombre";
+
+
+// Build final SQL sentences
+$SQL_FINAL = $SQL_pre . $SQL;
+$SQL_COUNT = $SQL_pre_count . $SQL;
+
+$counter = get_db_sql ($SQL_COUNT);
+    if ( $counter > $config["block_size"]) {
+        pagination ($counter, $URL, $offset);
+        $SQL_FINAL .= " LIMIT $offset , ".$config["block_size"];
+    }
+
+
+if ($counter > 0){
+    echo "
+    <table cellpadding='4' cellspacing='4' width='750' class='databox'>
+    <tr>
+    <th>
+    <th>".$lang_label["agent"]."</th>
+    <th>".$lang_label["type"]."</th>
+    <th>".$lang_label["name"]."</th>
+    <th>".$lang_label["description"]."</th>
+    <th>".$lang_label["interval"]."</th>
+    <th>".$lang_label["status"]."</th>
+    <th>".$lang_label["timestamp"]."</th>";
+    $color =1;
+    $result=mysql_query($SQL_FINAL);
+   
+    
+    while ($data=mysql_fetch_array($result)){ //while there are agents
+	    if ($color == 1){
+		    $tdcolor="datos";
+		    $color =0;
+	    } else {
+		    $tdcolor="datos2";
+		    $color =1;
+	    }
+    
+	    echo "<tr><td class='$tdcolor'>";
+	    echo "<a href='index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=".$data["id_agente"]."&id_agente_modulo=".$data[0]."&flag=1&tab=data&refr=60'>";
+	    echo "<img src='images/target.png'></a>";
+	    echo  "</td><td class='$tdcolor'>";
+	    echo  "<b><a href='index.php?sec=estado&
+	    sec2=operation/agentes/ver_agente&
+	    id_agente=".$data[5]."'>".
+	    strtoupper(substr($data[1],0,21))."</a></b>";
+	    echo "</td><td class='$tdcolor'>";
+	    echo "<img src='images/".show_icon_type($data[6])."' border=0></td>";
+	    echo "<td class='$tdcolor'>". substr($data[2],0,21). "</td>";
+	    echo "<td class='".$tdcolor."f9' title='".$data[3]."'>".substr($data[3],0,30)."</td>";
+	    echo "<td class='$tdcolor' align='center' width=25>";
+	    if ($data[7] == 0){
+		    $my_interval = give_agentinterval($data[5]);
+	    } else {
+		    $my_interval = $data[7];						
+	    }
+	    echo $my_interval;
+				    
+	    $query_gen2='SELECT * FROM tagente_estado 
+	    WHERE id_agente_modulo = '.$data[0];
+	    $result_gen2=mysql_query($query_gen2);
+	    $data2=mysql_fetch_array($result_gen2);
+	    echo "<td class='$tdcolor' align='center' width=20>";
+	    if ($data2["datos"] > 0){
+		    echo "<img src='images/pixel_green.png' width=40 height=18 title='".$lang_label["green_light"]."'>";
+	    } else {
+		    echo "<img src='images/pixel_red.png' width=40 height=18 title='".$lang_label["red_light"]."'>";
+	    }
+	    
+	    echo  "<td class='".$tdcolor."f9'>";
+	    $seconds = time() - $data2["utimestamp"];
+	    if ($seconds >= ($my_interval*2))
+		    echo "<span class='redb'>";
+	    else
+		    echo "<span>";
+    
+	    echo  human_time_comparation($data2["timestamp"]);
+        echo  "</span></td></tr>";
+    }
+    echo "</table>";
 } else {
-	echo "<div class='nf'>".$lang_label["no_agent"]."</div>";
+	echo "<div class='nf'>".$lang_label["no_monitors_g"]."</div>";
 }
 
+echo "<table width=700 border=0>";
+echo "<tr>";
+echo "<td class='f9'>";
+echo "<img src='images/pixel_green.png' width=40 height=18>&nbsp;&nbsp;".$lang_label["green_light"]."</td>";
+echo "<td class='f9'";
+echo "<img src='images/pixel_red.png' width=40 height=18>&nbsp;&nbsp;".$lang_label["red_light"]."</td>";
+echo "</table>";
+
 ?>