From c07daa31996a0958a540f2f943fe07d721ee318d Mon Sep 17 00:00:00 2001
From: fbsanchez <fborja.sanchez@artica.es>
Date: Mon, 29 Jun 2020 19:17:40 +0200
Subject: [PATCH] WIP Services

---
 pandora_console/extras/mr/39.sql       |   4 +
 pandora_console/include/lib/Agent.php  |  84 +++++++++++++++
 pandora_console/include/lib/Module.php | 143 +++++++++++++++++--------
 3 files changed, 187 insertions(+), 44 deletions(-)

diff --git a/pandora_console/extras/mr/39.sql b/pandora_console/extras/mr/39.sql
index 53f937b4b6..997d515a95 100644
--- a/pandora_console/extras/mr/39.sql
+++ b/pandora_console/extras/mr/39.sql
@@ -6,6 +6,10 @@ ALTER TABLE `tservice` ADD COLUMN `unknown_as_critical` tinyint(1) NOT NULL defa
 
 UPDATE `tservice` SET `auto_calculate`=0;
 
+UPDATE `tservice` SET `cps`= `cps` - 1 WHERE `cps` > 0;
+UPDATE `tagente` SET `cps`= `cps` - 1 WHERE `cps` > 0;
+UPDATE `tagente_modulo` SET `cps`= `cps` - 1 WHERE `cps` > 0;
+
 ALTER TABLE `tmensajes` ADD COLUMN `hidden_sent` TINYINT(1) UNSIGNED DEFAULT 0;
 
 COMMIT;
diff --git a/pandora_console/include/lib/Agent.php b/pandora_console/include/lib/Agent.php
index e722eb32fe..3d86b31f93 100644
--- a/pandora_console/include/lib/Agent.php
+++ b/pandora_console/include/lib/Agent.php
@@ -211,6 +211,90 @@ class Agent extends Entity
     }
 
 
+    /**
+     * Calculates cascade protection service value for this service.
+     *
+     * @return integer CPS value.
+     */
+    public function calculateCPS()
+    {
+        if ($this->cps() < 0) {
+            return $this->cps();
+        }
+
+        // 1. check parents.
+        $direct_parents = db_get_all_rows_sql(
+            sprintf(
+                'SELECT id_service, cps, cascade_protection
+                 FROM `tservice_element` te
+                 INNER JOIN `tservice` t ON te.id_service = t.id
+                 WHERE te.id_agent = %d',
+                $this->id_agente()
+            )
+        );
+
+        if (is_metaconsole() === false
+            && has_metaconsole() === true
+        ) {
+            $mc_parents = [];
+            global $config;
+            $mc_db_conn = \enterprise_hook(
+                'metaconsole_load_external_db',
+                [
+                    [
+                        'dbhost' => $config['replication_dbhost'],
+                        'dbuser' => $config['replication_dbuser'],
+                        'dbpass' => io_output_password(
+                            $config['replication_dbpass']
+                        ),
+                        'dbname' => $config['replication_dbname'],
+                    ],
+                ]
+            );
+
+            if ($mc_db_conn === NOERR) {
+                $mc_parents = db_get_all_rows_sql(
+                    sprintf(
+                        'SELECT id_service,
+                                cps,
+                                cascade_protection
+                        FROM `tservice_element` te
+                        INNER JOIN `tservice` t ON te.id_service = t.id
+                        WHERE te.id_agent = %d',
+                        $this->id_agente()
+                    )
+                );
+            }
+
+            // Restore the default connection.
+            \enterprise_hook('metaconsole_restore_db');
+        }
+
+        $cps = 0;
+
+        if (is_array($direct_parents) === false) {
+            $direct_parents = [];
+        }
+
+        if (is_array($mc_parents) === false) {
+            $mc_parents = [];
+        }
+
+        // Merge all parents (node and meta).
+        $parents = array_merge($direct_parents, $mc_parents);
+
+        foreach ($parents as $parent) {
+            $cps += $parent['cps'];
+            if (((bool) $parent['cascade_protection']) === true) {
+                $cps++;
+            }
+        }
+
+        return $cps;
+
+    }
+
+
     /**
      * Creates a module in current agent.
      *
diff --git a/pandora_console/include/lib/Module.php b/pandora_console/include/lib/Module.php
index 9c809e27de..f1af58f02d 100644
--- a/pandora_console/include/lib/Module.php
+++ b/pandora_console/include/lib/Module.php
@@ -162,7 +162,13 @@ class Module extends Entity
             $this->idNode = $id_node;
 
             enterprise_include_once('include/functions_metaconsole.php');
-            \metaconsole_connect(null, $this->idNode);
+            \enterprise_hook(
+                'metaconsole_connect',
+                [
+                    null,
+                    $this->idNode,
+                ]
+            );
         }
 
         if (is_numeric($id_agent_module) === true
@@ -182,7 +188,7 @@ class Module extends Entity
                     $this->linkedAgent = new Agent($this->id_agente());
                 } catch (\Exception $e) {
                     if ($this->idNode !== null) {
-                        \metaconsole_restore_db();
+                        \enterprise_hook('metaconsole_restore_db');
                     }
 
                     // Unexistent agent.
@@ -203,9 +209,10 @@ class Module extends Entity
             $this->status = new ModuleStatus($this->fields['id_agente_modulo']);
         } catch (\Exception $e) {
             $this->status = new Modulestatus();
-            if ($this->idNode !== null) {
-                \metaconsole_restore_db();
-            }
+        }
+
+        if ($this->idNode !== null) {
+            \enterprise_hook('metaconsole_restore_db');
         }
     }
 
@@ -386,7 +393,13 @@ class Module extends Entity
             $updates = $this->fields;
             if ($this->idNode !== null) {
                 enterprise_include_once('include/functions_metaconsole.php');
-                \metaconsole_connect(null, $this->idNode);
+                \enterprise_hook(
+                    'metaconsole_connect',
+                    [
+                        null,
+                        $this->idNode,
+                    ]
+                );
             }
 
             $rs = \db_process_sql_update(
@@ -399,7 +412,7 @@ class Module extends Entity
             $error = $config['dbconnection']->error;
 
             if ($this->idNode !== null) {
-                \metaconsole_restore_db();
+                \enterprise_hook('metaconsole_restore_db');
             }
 
             if ($rs === false) {
@@ -420,7 +433,13 @@ class Module extends Entity
 
             if ($this->idNode !== null) {
                 enterprise_include_once('include/functions_metaconsole.php');
-                \metaconsole_connect(null, $this->idNode);
+                \enterprise_hook(
+                    'metaconsole_connect',
+                    [
+                        null,
+                        $this->idNode,
+                    ]
+                );
             }
 
             $rs = \modules_create_agent_module(
@@ -433,7 +452,7 @@ class Module extends Entity
             $error = $config['dbconnection']->error;
 
             if ($this->idNode !== null) {
-                \metaconsole_restore_db();
+                \enterprise_hook('metaconsole_restore_db');
             }
 
             if ($rs === false) {
@@ -458,7 +477,13 @@ class Module extends Entity
     {
         if ($this->idNode !== null) {
             enterprise_include_once('include/functions_metaconsole.php');
-            \metaconsole_connect(null, $this->idNode);
+            \enterprise_hook(
+                'metaconsole_connect',
+                [
+                    null,
+                    $this->idNode,
+                ]
+            );
         }
 
         \modules_delete_agent_module(
@@ -466,7 +491,7 @@ class Module extends Entity
         );
 
         if ($this->idNode !== null) {
-            \metaconsole_restore_db();
+            \enterprise_hook('metaconsole_restore_db');
         }
 
         unset($this->fields);
@@ -520,52 +545,82 @@ class Module extends Entity
 
 
     /**
-     * Calculates CPS value and updates its content.
+     * Calculates cascade protection service value for this service.
      *
-     * @return integer CPS value set.
+     * @return integer CPS value.
      */
     public function calculateCPS()
     {
-        $cps = 0;
-
-        if (class_exists('\PandoraFMS\Enterprise\Service') === false) {
-            return 0;
+        if ($this->cps() < 0) {
+            return $this->cps();
         }
 
-        // Check if is child of services in local console.
-        $service_parents = \db_get_all_rows_filter(
-            'tservice_element',
-            [ 'id_agente_modulo' => $this->id_agente_modulo() ],
-            'id_service'
+        // 1. check parents.
+        $direct_parents = db_get_all_rows_sql(
+            sprintf(
+                'SELECT id_service, cps, cascade_protection
+                 FROM `tservice_element` te
+                 INNER JOIN `tservice` t ON te.id_service = t.id
+                 WHERE te.id_agente_modulo = %d',
+                $this->id_agente_modulo()
+            )
         );
 
-        if (is_array($service_parents) === true) {
-            foreach ($service_parents as $ref) {
-                $service = new \PandoraFMS\Enterprise\Service(
-                    $ref['id_service']
-                );
-                if (((bool) $service->cascade_protection()) === true) {
-                    $cps += $service->cps();
-                }
-            }
-        }
-
-        // Check if is child of services in metaconsole.
-        if (is_metaconsole() === false) {
+        if (is_metaconsole() === false
+            && has_metaconsole() === true
+        ) {
+            $mc_parents = [];
             global $config;
-
-            $nodo_connect = \metaconsole_load_external_db(
+            $mc_db_conn = \enterprise_hook(
+                'metaconsole_load_external_db',
                 [
-                    'dbhost' => $config['replication_dbhost'],
-                    'dbuser' => $config['replication_dbuser'],
-                    'dbpass' => \io_output_password(
-                        $config['replication_dbpass']
-                    ),
-                    'dbname' => $config['replication_dbname'],
+                    [
+                        'dbhost' => $config['replication_dbhost'],
+                        'dbuser' => $config['replication_dbuser'],
+                        'dbpass' => io_output_password(
+                            $config['replication_dbpass']
+                        ),
+                        'dbname' => $config['replication_dbname'],
+                    ],
                 ]
             );
 
-            \metaconsole_restore_db();
+            if ($mc_db_conn === NOERR) {
+                $mc_parents = db_get_all_rows_sql(
+                    sprintf(
+                        'SELECT id_service,
+                                cps,
+                                cascade_protection
+                        FROM `tservice_element` te
+                        INNER JOIN `tservice` t ON te.id_service = t.id
+                        WHERE te.id_agente_modulo = %d',
+                        $this->id_agente_modulo()
+                    )
+                );
+            }
+
+            // Restore the default connection.
+            \enterprise_hook('metaconsole_restore_db');
+        }
+
+        $cps = 0;
+
+        if (is_array($direct_parents) === false) {
+            $direct_parents = [];
+        }
+
+        if (is_array($mc_parents) === false) {
+            $mc_parents = [];
+        }
+
+        // Merge all parents (node and meta).
+        $parents = array_merge($direct_parents, $mc_parents);
+
+        foreach ($parents as $parent) {
+            $cps += $parent['cps'];
+            if (((bool) $parent['cascade_protection']) === true) {
+                $cps++;
+            }
         }
 
         return $cps;