1 && (++$i) > $limit) { break; } $modules[] = self::build($row); } return $modules; } else { return self::build($rs[0]); } } /** * Returns current object as array. * * @return array Of fields. */ public function toArray() { return $this->fields; } /** * Creates a module object from given data. Avoid query duplication. * * @param array $data Module information. * * @return PandoraFMS\Module Object. */ public static function build(array $data=[]) { $obj = new Module(); // Set values. foreach ($data as $k => $v) { $obj->{$k}($v); } if ($obj->nombre() === 'delete_pending') { return null; } // Customize certain fields. $obj->status = new ModuleStatus($obj->id_agente_modulo()); return $obj; } /** * Builds a PandoraFMS\Module object from given id. * * @param integer|null $id_agent_module Module id. * @param boolean $link_agent Link agent object. * @param integer|null $id_node Metaconsole only. ID node. * * @throws \Exception On error. */ public function __construct( ?int $id_agent_module=null, bool $link_agent=false, ?int $id_node=null ) { if ($id_node !== null) { $this->idNode = $id_node; enterprise_include_once('include/functions_metaconsole.php'); \enterprise_hook( 'metaconsole_connect', [ null, $this->idNode, ] ); } if (is_numeric($id_agent_module) === true && $id_agent_module > 0 ) { parent::__construct( 'tagente_modulo', ['id_agente_modulo' => $id_agent_module] ); if ($this->nombre() === 'delete_pending') { return null; } if ($link_agent === true) { try { $this->linkedAgent = new Agent($this->id_agente()); } catch (\Exception $e) { if ($this->idNode !== null) { \enterprise_hook('metaconsole_restore_db'); } // Unexistent agent. throw new \Exception( __METHOD__.__( ' error: Module has no agent assigned.' ) ); } } } else { // Create empty skel. parent::__construct('tagente_modulo'); } try { // Customize certain fields. $this->status = new ModuleStatus($this->fields['id_agente_modulo']); } catch (\Exception $e) { $this->status = new Modulestatus(); } if ($this->idNode !== null) { \enterprise_hook('metaconsole_restore_db'); } } /** * Return agent object where module is defined. * * @return PandoraFMS\Agent Where module is defined. */ public function agent() { if ($this->linkedAgent === null) { try { $this->linkedAgent = new Agent($this->id_agente()); } catch (\Exception $e) { // Unexistent agent. return null; } } return $this->linkedAgent; } /** * Return last value reported by the module. * * @return mixed Data depending on module type. */ public function lastValue() { return $this->status->datos(); } /** * Return last status reported by the module. * * @return mixed Data depending on module type. */ public function lastStatus() { return $this->status->estado(); } /** * Returns current status. * * @return PandoraFMS\ModuleStatus Status of the module. */ public function getStatus() { return $this->status; } /** * Retrieve all alert templates (ids) assigned to current module. * * @return array Of ids. */ public function alertTemplatesAssigned() { if ($this->id_agente_modulo() === null) { // Need to be stored first. return []; } $result = db_get_all_rows_filter( 'talert_template_modules', ['id_agent_module' => $this->id_agente_modulo()], 'id_alert_template' ); if ($result === false) { return []; } return array_reduce( $result, function ($carry, $item) { $carry[] = $item['id_alert_template']; return $carry; }, [] ); } /** * Remove a alert template assignment. * * @param integer $id_alert_template Target id. * * @return boolean Success or not. */ public function unassignAlertTemplate(int $id_alert_template) { if ($this->id_agente_modulo() === null) { // Need to be stored first. return false; } if (is_numeric($id_alert_template) === false || $id_alert_template <= 0 ) { // Invalid alert template. return false; } return (bool) \db_process_sql_delete( 'talert_template_modules', [ 'id_agent_module' => $this->id_agente_modulo(), 'id_alert_template' => $id_alert_template, ] ); } /** * Add an alert template to this module. * * @param integer|null $id_alert_template Target alert template. * * @return boolean Status of adding process. */ public function addAlertTemplate(?int $id_alert_template=null) { if ($this->id_agente_modulo() === null) { // Need to be stored first. return false; } if (is_numeric($id_alert_template) === false || $id_alert_template <= 0 ) { // Invalid alert template. return false; } return (bool) \db_process_sql_insert( 'talert_template_modules', [ 'id_agent_module' => $this->id_agente_modulo(), 'id_alert_template' => $id_alert_template, 'last_reference' => time(), ] ); } /** * Saves current definition to database. * * @return mixed Affected rows of false in case of error. * @throws \Exception On error. */ public function save() { if (empty($this->fields['nombre']) === true) { throw new \Exception( get_class($this).' error, "nombre" is not set' ); } if (empty($this->fields['id_agente']) === true) { throw new \Exception( get_class($this).' error, "id_agente" is not set' ); } if ($this->fields['id_agente_modulo'] > 0) { // Update. $updates = $this->fields; if ($this->idNode !== null) { enterprise_include_once('include/functions_metaconsole.php'); \enterprise_hook( 'metaconsole_connect', [ null, $this->idNode, ] ); } $rs = \db_process_sql_update( 'tagente_modulo', $updates, ['id_agente_modulo' => $this->fields['id_agente_modulo']] ); global $config; $error = $config['dbconnection']->error; if ($this->idNode !== null) { \enterprise_hook('metaconsole_restore_db'); } if ($rs === false) { throw new \Exception( __METHOD__.' error: '.$error ); } } else { // Creation. $updates = $this->fields; // Clean null fields. foreach ($updates as $k => $v) { if ($v === null) { unset($updates[$k]); } } if ($this->idNode !== null) { enterprise_include_once('include/functions_metaconsole.php'); \enterprise_hook( 'metaconsole_connect', [ null, $this->idNode, ] ); } $rs = \modules_create_agent_module( $this->fields['id_agente'], $updates['nombre'], $updates ); global $config; $error = $config['dbconnection']->error; if ($this->idNode !== null) { \enterprise_hook('metaconsole_restore_db'); } if ($rs === false) { throw new \Exception( __METHOD__.' error: '.$error ); } $this->fields['id_agente_modulo'] = $rs; } return true; } /** * Erases this module. * * @return void */ public function delete() { if ($this->idNode !== null) { enterprise_include_once('include/functions_metaconsole.php'); \enterprise_hook( 'metaconsole_connect', [ null, $this->idNode, ] ); } \modules_delete_agent_module( $this->id_agente_modulo() ); if ($this->idNode !== null) { \enterprise_hook('metaconsole_restore_db'); } unset($this->fields); unset($this->status); } /** * Transforms results from classic mode into modern exceptions. * * @param integer|boolean $result Result received from module management. * * @return integer Module id created or result. * @throws \Exception On error. */ public static function errorToException($result) { if ($result === ERR_INCOMPLETE) { throw new \Exception( __('Module name empty.') ); } if ($result === ERR_GENERIC) { throw new \Exception( __('Invalid characters in module name') ); } if ($result === ERR_EXIST) { throw new \Exception( __('Module already exists please select another name or agent.') ); } if ($result === false) { throw new \Exception( __('Insufficent permissions to perform this action') ); } if ($result === ERR_DB) { global $config; throw new \Exception( __('Error while processing: %s', $config['dbconnection']->error) ); } return $result; } /** * 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_agente_modulo = %d', $this->id_agente_modulo() ) ); 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_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; } }