public function testGenerate() { $fileList = array(); $pollerId = 1; CommandRepository::generate($fileList, $pollerId, $this->tmpDir . '/', 'command.cfg', CommandRepository::CHECK_TYPE); $this->assertEquals(array('cfg_file' => array($this->tmpDir . '/1/command.cfg')), $fileList); $content = file_get_contents($this->tmpDir . '/1/command.cfg'); /* Remove line with the generate date */ $lines = explode("\n", $content); $lines = preg_grep('/^#\\s+Last.*#$/', $lines, PREG_GREP_INVERT); $content = join("\n", $lines); $resultContent = file_get_contents(dirname(__DIR__) . '/data/configfiles/command1.cfg'); $this->assertEquals($resultContent, $content); $fileList = array(); CommandRepository::generate($fileList, $pollerId, $this->tmpDir . '/', 'command.cfg', CommandRepository::NOTIF_TYPE); $this->assertEquals(array('cfg_file' => array($this->tmpDir . '/1/command.cfg')), $fileList); $content = file_get_contents($this->tmpDir . '/1/command.cfg'); /* Remove line with the generate date */ $lines = explode("\n", $content); $lines = preg_grep('/^#\\s+Last.*#$/', $lines, PREG_GREP_INVERT); $content = join("\n", $lines); $resultContent = file_get_contents(dirname(__DIR__) . '/data/configfiles/command2.cfg'); $this->assertEquals($resultContent, $content); }
/** * * @param int $poller_id * @param array $filesList * @param int $testing * @return array */ private static function getContent($poller_id, &$filesList, $testing) { $di = Di::getDefault(); /* Get Database Connexion */ $dbconn = $di->get('db_centreon'); /* Init Content Array */ $content = array(); /* Default values that can be overwritten by template and user */ $defaultValues = static::getDefaultValues(); /* Template values that can be overwritten by user */ $templateValues = static::getTemplateValues($poller_id); /* For command name resolution */ $commandIdFields = static::getCommandIdField(); /* Get configuration files */ $content = static::getFilesList($filesList, $content, $testing, $poller_id); /* Get values from the table, those are saved by user */ $query = "SELECT * FROM cfg_engine WHERE poller_id = ?"; $stmt = $dbconn->prepare($query); $stmt->execute(array($poller_id)); $row = $stmt->fetch(\PDO::FETCH_ASSOC); if (false === $row) { throw new Exception(sprintf('Could not find parameters for poller %s.', $poller_id)); } $userValues = array(); foreach ($row as $key => $val) { if (!is_null($val) && $val != "") { $userValues[$key] = $val; } } /* Overwrite parameter */ $tmpConf = array_merge($defaultValues, $templateValues); $finalConf = array_merge($tmpConf, $userValues); /* For object directory */ $objectDirectives = static::getConfigFiles($poller_id); $finalConf = array_merge($finalConf, $objectDirectives); /* Add illegal chars */ $finalConf['illegal_macro_output_chars'] = CustomMacroRepository::$forbidenCHar; $finalConf['illegal_object_name_chars'] = CENTREON_ILLEGAL_CHAR_OBJ; /* Set real etc path of the poller */ static::$finalPath = $finalConf['conf_dir']; /* Replace path macros */ foreach ($finalConf as $k => $v) { $arr = array(); if (!is_array($v)) { $arr[] = $v; } else { $arr = $v; } foreach ($arr as $key => $val) { if (preg_match('/%([a-z_]+)%/', $val, $matches)) { $macro = $matches[1]; if (isset($finalConf[$macro])) { $finalConf[$macro] = rtrim($finalConf[$macro], '/'); if (is_array($finalConf[$k])) { $finalConf[$k][$key] = str_replace("%{$macro}%", $finalConf[$macro], $val); } else { $finalConf[$k] = str_replace("%{$macro}%", $finalConf[$macro], $val); } } if ($macro == 'conf_dir' && $testing) { $finalConf[$k][$key] = str_replace('%conf_dir%', static::$path . "/" . $poller_id, $val); } } } } /* Replace commands */ foreach ($commandIdFields as $fieldName) { if (isset($finalConf[$fieldName]) && $finalConf[$fieldName]) { $finalConf[$fieldName] = CommandRepository::getCommandName($finalConf[$fieldName]); } } /* Exclude parameters */ static::unsetParameters($finalConf); return $finalConf; }
/** * Generate all object files (host, service, etc...) * */ public static function generateObjectsFiles() { $event = static::$event; /* Generate Configuration files */ CommandRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "check-command.cfg", CommandRepository::CHECK_TYPE); $event->setOutput('check-command.cfg'); CommandRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "misc-command.cfg", CommandRepository::NOTIF_TYPE); $event->setOutput('misc-command.cfg'); ConfigGenerateResourcesRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "resources.cfg"); $event->setOutput('resources.cfg'); ConfigGenerateModulesRepository::generate(static::$fileList, $event->getPollerId(), static::$path, $event); TimePeriodRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "timeperiods.cfg"); $event->setOutput('timeperiods.cfg'); ConnectorRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "connectors.cfg"); $event->setOutput('connectors.cfg'); /* Retrieve all extra macros by emitting event */ $events = Di::getDefault()->get('events'); $hostMacroEvent = new HostMacroEvent($event->getPollerId()); $events->emit('centreon-engine.get.macro.host', array($hostMacroEvent)); $serviceMacroEvent = new ServiceMacroEvent($event->getPollerId()); $events->emit('centreon-engine.get.macro.service', array($serviceMacroEvent)); $notificationRulesEvent = new NotificationRuleEvent($event->getPollerId()); $events->emit('centreon-engine.set.notifications.rules', array($notificationRulesEvent)); /* Templates config files */ HostTemplateRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "hostTemplates.cfg", $hostMacroEvent); $event->setOutput('hostTemplates.cfg'); ServicetemplateRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "serviceTemplates.cfg", $serviceMacroEvent); $event->setOutput('serviceTemplate.cfg'); /* Monitoring Resources files */ HostRepository::generate(static::$fileList, $event->getPollerId(), static::$path, "resources/", $hostMacroEvent, $serviceMacroEvent); $event->setOutput('host configuration files'); }