From 925d3fa6a2820e37ff34c84832d025d0bdba1a80 Mon Sep 17 00:00:00 2001
From: Ramon Novoa <ramon.novoa@pandorafms.com>
Date: Tue, 25 Apr 2023 12:28:38 +0200
Subject: [PATCH] Improved module execution for master servers.

Previously, master servers executed modules from any agent whose
assigned server had crashed. Now they execute modules from any agent
whose assigned server cannot execute them (e.g., the server is running
but the required component is disabled).
---
 pandora_server/lib/PandoraFMS/DiscoveryServer.pm  | 2 +-
 pandora_server/lib/PandoraFMS/InventoryServer.pm  | 4 ++--
 pandora_server/lib/PandoraFMS/NetworkServer.pm    | 2 +-
 pandora_server/lib/PandoraFMS/PluginServer.pm     | 2 +-
 pandora_server/lib/PandoraFMS/PredictionServer.pm | 4 ++--
 pandora_server/lib/PandoraFMS/WMIServer.pm        | 2 +-
 pandora_server/lib/PandoraFMS/WebServer.pm        | 2 +-
 7 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm
index 2d5928333d..607faf856a 100644
--- a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm
+++ b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm
@@ -150,7 +150,7 @@ sub data_producer ($) {
         OR (status = -1 AND interval_sweep > 0 AND (utimestamp + interval_sweep) < UNIX_TIMESTAMP()))', $server_id);
   } else {
     @rows = get_db_rows ($dbh, 'SELECT * FROM trecon_task 
-      WHERE (id_recon_server = ? OR id_recon_server = ANY(SELECT id_server FROM tserver WHERE status <> 1 AND server_type = ?))
+      WHERE (id_recon_server = ? OR id_recon_server NOT IN (SELECT id_server FROM tserver WHERE status = 1 AND server_type = ?))
       AND disabled = 0
       AND ((utimestamp = 0 AND interval_sweep != 0 OR status = 1)
         OR (status = -1 AND interval_sweep > 0 AND (utimestamp + interval_sweep) < UNIX_TIMESTAMP()))', $server_id, DISCOVERYSERVER);
diff --git a/pandora_server/lib/PandoraFMS/InventoryServer.pm b/pandora_server/lib/PandoraFMS/InventoryServer.pm
index f0c92a7fd3..fcaf882436 100644
--- a/pandora_server/lib/PandoraFMS/InventoryServer.pm
+++ b/pandora_server/lib/PandoraFMS/InventoryServer.pm
@@ -122,7 +122,7 @@ sub data_producer ($) {
 			@rows = get_db_rows ($dbh,
 				'SELECT tagent_module_inventory.id_agent_module_inventory, tagent_module_inventory.flag, tagent_module_inventory.timestamp
 				FROM tagente, tagent_module_inventory, tmodule_inventory 
-				WHERE (server_name = ? OR server_name = ANY(SELECT name FROM tserver WHERE status <> 1 AND server_type = ?)) 
+				WHERE (server_name = ? OR server_name NOT IN (SELECT name FROM tserver WHERE status = 1 AND server_type = ?)) 
 					AND tmodule_inventory.id_module_inventory = tagent_module_inventory.id_module_inventory
 					AND tmodule_inventory.id_os IS NOT NULL 
 					AND tagente.id_agente = tagent_module_inventory.id_agente
@@ -138,7 +138,7 @@ sub data_producer ($) {
 			@rows = get_db_rows ($dbh,
 				'SELECT tagent_module_inventory.id_agent_module_inventory, tagent_module_inventory.flag, tagent_module_inventory.timestamp
 				FROM tagente, tagent_module_inventory, tmodule_inventory 
-				WHERE (server_name = ? OR server_name = ANY(SELECT name FROM tserver WHERE status <> 1 AND server_type = ?)) 
+				WHERE (server_name = ? OR server_name NOT IN (SELECT name FROM tserver WHERE status = 1 AND server_type = ?)) 
 					AND tmodule_inventory.id_module_inventory = tagent_module_inventory.id_module_inventory
 					AND tmodule_inventory.id_os IS NOT NULL 
 					AND tagente.id_agente = tagent_module_inventory.id_agente
diff --git a/pandora_server/lib/PandoraFMS/NetworkServer.pm b/pandora_server/lib/PandoraFMS/NetworkServer.pm
index 594aee4293..2d245e7a04 100644
--- a/pandora_server/lib/PandoraFMS/NetworkServer.pm
+++ b/pandora_server/lib/PandoraFMS/NetworkServer.pm
@@ -112,7 +112,7 @@ sub data_producer ($) {
     } else {
 		@rows = get_db_rows ($dbh, 'SELECT tagente_modulo.id_agente_modulo, tagente_modulo.flag, tagente_estado.last_execution_try, tagente_estado.current_interval + tagente_estado.last_execution_try AS time_left, last_execution_try
 		FROM tagente, tagente_modulo, tagente_estado
-		WHERE ((server_name = ?) OR (server_name = ANY(SELECT name FROM tserver WHERE status <> 1 AND server_type = ?))) 
+		WHERE ((server_name = ?) OR (server_name NOT IN (SELECT name FROM tserver WHERE status = 1 AND server_type = ?))) 
 		AND tagente_modulo.id_agente = tagente.id_agente
 		AND tagente.disabled = 0
 		AND tagente_modulo.disabled = 0
diff --git a/pandora_server/lib/PandoraFMS/PluginServer.pm b/pandora_server/lib/PandoraFMS/PluginServer.pm
index 7bec29513b..9d52bd5ce0 100644
--- a/pandora_server/lib/PandoraFMS/PluginServer.pm
+++ b/pandora_server/lib/PandoraFMS/PluginServer.pm
@@ -110,7 +110,7 @@ sub data_producer ($) {
     } else {
 		@rows = get_db_rows ($dbh, 'SELECT DISTINCT(tagente_modulo.id_agente_modulo), tagente_modulo.flag, tagente_estado.current_interval + tagente_estado.last_execution_try AS time_left, last_execution_try
 			FROM tagente, tagente_modulo, tagente_estado
-			WHERE ((server_name = ?) OR (server_name = ANY(SELECT name FROM tserver WHERE status <> 1 AND server_type = ?)))
+			WHERE ((server_name = ?) OR (server_name NOT IN (SELECT name FROM tserver WHERE status = 1 AND server_type = ?)))
 			AND tagente_modulo.id_agente = tagente.id_agente
 			AND tagente.disabled = 0
 			AND tagente_modulo.disabled = 0
diff --git a/pandora_server/lib/PandoraFMS/PredictionServer.pm b/pandora_server/lib/PandoraFMS/PredictionServer.pm
index 1225ab5b19..6859c001a4 100644
--- a/pandora_server/lib/PandoraFMS/PredictionServer.pm
+++ b/pandora_server/lib/PandoraFMS/PredictionServer.pm
@@ -114,9 +114,9 @@ sub data_producer ($) {
 				tagente_modulo.flag, last_execution_try
 			FROM tagente, tagente_modulo, tagente_estado
 			WHERE ((server_name = ?)
-				OR (server_name = ANY(SELECT name
+				OR (server_name NOT IN (SELECT name
 					FROM tserver
-					WHERE status <> 1 AND server_type = ?))
+					WHERE status = 1 AND server_type = ?))
 				OR ((server_name = 0 OR server_name IS NULL) AND 1=?)
 				)
 				AND tagente_modulo.id_agente = tagente.id_agente
diff --git a/pandora_server/lib/PandoraFMS/WMIServer.pm b/pandora_server/lib/PandoraFMS/WMIServer.pm
index 06d6a47292..018d7e86ac 100644
--- a/pandora_server/lib/PandoraFMS/WMIServer.pm
+++ b/pandora_server/lib/PandoraFMS/WMIServer.pm
@@ -109,7 +109,7 @@ sub data_producer ($) {
 	} else {
 		@rows = get_db_rows ($dbh, 'SELECT DISTINCT(tagente_modulo.id_agente_modulo), tagente_modulo.flag, tagente_estado.current_interval + tagente_estado.last_execution_try AS time_left, last_execution_try
 			FROM tagente, tagente_modulo, tagente_estado, tserver
-			WHERE ((server_name = ?) OR (server_name = ANY(SELECT name FROM tserver WHERE status <> 1 AND server_type = ?)))
+			WHERE ((server_name = ?) OR (server_name NOT IN (SELECT name FROM tserver WHERE status = 1 AND server_type = ?)))
 			AND tagente_modulo.id_agente = tagente.id_agente
 			AND tagente.disabled = 0
 			AND tagente_modulo.disabled = 0
diff --git a/pandora_server/lib/PandoraFMS/WebServer.pm b/pandora_server/lib/PandoraFMS/WebServer.pm
index 776ef20456..e87e860c29 100644
--- a/pandora_server/lib/PandoraFMS/WebServer.pm
+++ b/pandora_server/lib/PandoraFMS/WebServer.pm
@@ -137,7 +137,7 @@ sub data_producer ($) {
     } else {
 		@rows = get_db_rows ($dbh, 'SELECT DISTINCT(tagente_modulo.id_agente_modulo), tagente_modulo.flag, tagente_estado.current_interval + tagente_estado.last_execution_try  AS time_left, last_execution_try
 			FROM tagente, tagente_modulo, tagente_estado, tserver
-			WHERE ((server_name = ?) OR (server_name = ANY(SELECT server_name FROM tserver WHERE status <> 1 AND server_type = ?)))
+			WHERE ((server_name = ?) OR (server_name NOT IN (SELECT server_name FROM tserver WHERE status = 1 AND server_type = ?)))
 			AND tagente_modulo.id_agente = tagente.id_agente
 			AND tagente.disabled = 0
 			AND tagente_modulo.disabled = 0