/** * @param Core\Events\ManageCommandOptions $event */ public static function execute(ManageCommandOptionsEvent $event) { $options = $event->getOptions(); $args = $event->getArgs(); $newInfos = array(); if ($event->getObjectName() == 'poller') { if ($event->getAction() == 'createAction' && isset($args['template'])) { $newInfos = PollerRepository::addCommandTemplateInfos($args['template']); } else { if ($event->getAction() == 'updateAction' && isset($args['poller'])) { $pollerIds = Poller::getIdByParameter('slug', array($args['poller'])); if (isset($pollerIds[0])) { $pollerId = $pollerIds[0]; $templateName = Poller::getParameters($pollerId, 'tmpl_name'); $newInfos = PollerRepository::addCommandTemplateInfos($templateName['tmpl_name']); } } } } foreach ($newInfos as $newInfo) { $newOption = array($newInfo['name'] => array('paramType' => 'params', 'help' => '', 'type' => 'string', 'toTransform' => $newInfo['name'], 'multiple' => '', 'required' => '0')); if (isset($newInfo['help'])) { $newOption[$newInfo['name']]['help'] = $newInfo['help']; } if (isset($newInfo['require']) && $newInfo['require'] == true && $event->getAction() == 'createAction') { $newOption[$newInfo['name']]['required'] = '1'; } $event->addOption($newOption); } }
/** * @param CentreonMain\Events\PostSave $event */ public static function execute(PostSaveEvent $event) { $parameters = $event->getParameters(); $extraParameters = $event->getExtraParameters(); if ($event->getObjectName() === 'poller') { foreach ($parameters as $key => $value) { $extraParameters['centreon-broker'][$key] = $value; } if ($event->getAction() === 'update' && !isset($extraParameters['centreon-broker']['tmpl_name'])) { $templateName = Poller::getParameters($extraParameters['centreon-broker']['object_id'], 'tmpl_name'); $extraParameters['centreon-broker']['tmpl_name'] = $templateName['tmpl_name']; } BrokerRepository::save($event->getObjectId(), $extraParameters['centreon-broker']); } }
/** * Get scheduling informations for a specific host * * @method get * @route /host/[i:id]/scheduling-infos */ public function schedulingInfosHostAction() { $params = $this->getParams(); $schedulingInfos = HostRealtime::get($params['id'], array('execution_time', 'latency')); $poller = HostConf::get($params['id'], 'poller_id'); $schedulingInfos['poller_name'] = !is_null($poller['poller_id']) ? Poller::get($poller['poller_id'], 'name') : ""; unset($schedulingInfos['poller_id']); $this->router->response()->json(array('scheduling_infos' => $schedulingInfos, 'success' => true)); }
/** * * @param integer $pollerId * @return type * @throws Exception */ public static function getTemplate($pollerId) { $paramsPoller = Poller::get($pollerId, 'tmpl_name'); if (!isset($paramsPoller['tmpl_name']) || is_null($paramsPoller['tmpl_name'])) { throw new Exception('Not template defined'); } $tmplName = $paramsPoller['tmpl_name']; /* Load template information for poller */ $listTpl = TemplateManager::buildTemplatesList(); if (!isset($listTpl[$tmplName])) { throw new Exception('The template is not found on list of templates', 255); } return $listTpl[$tmplName]; }
/** * Get default template for a poller * * @method get * @route /poller/[i:id]/template */ public function getPollerDefaultTemplateAction() { $di = Di::getDefault(); $router = $di->get('router'); $params = $this->getParams(); $returnData = array(); if ($params['id'] == 0) { PollerRepository::getPollerTemplates(); $defaultPoller = array_slice($di->get('pollerTemplate'), 0, 1, true); $p = key($defaultPoller); $returnData['id'] = $p; $returnData['text'] = ucfirst($p); } else { $poller = PollerModel::get($params['id']); $returnData['id'] = $poller['tmpl_name']; $returnData['text'] = ucfirst($poller['tmpl_name']); } $router->response()->json($returnData); }
/** * Load macros for replace in default configuration * * @param int $pollerId The poller id */ private function loadMacros($pollerId) { $config = Di::getDefault()->get('config'); /* Load contant values */ $this->baseConfig['broker_central_ip'] = getHostByName(getHostName()); /* Load user value */ $this->baseConfig = array_merge($this->baseConfig, BrokerRepository::loadValues($pollerId)); /* Load paths */ $paths = BrokerRepository::getPathsFromPollerId($pollerId); $pathsValue = array_values($paths); $pathsKeys = array_map(function ($name) { switch ($name) { case 'directory_modules': $str = 'modules_directory'; break; case 'directory_config': $str = 'etc_directory'; break; case 'directory_logs': $str = 'logs_directory'; break; case 'directory_data': $str = 'data_directory'; break; default: $str = ''; break; } return 'global_broker_' . $str; }, array_keys($paths)); $paths = array_combine($pathsKeys, $pathsValue); $this->baseConfig = array_merge($this->baseConfig, $paths); $this->baseConfig['poller_id'] = $this->pollerId; /* Information for database */ $dbInformation = CentreonDb::parseDsn($config->get('db_centreon', 'dsn'), $config->get('db_centreon', 'username'), $config->get('db_centreon', 'password')); $dbKeys = array_map(function ($name) { return 'global_' . $name; }, array_keys($dbInformation)); $dbInformation = array_combine($dbKeys, array_values($dbInformation)); $this->baseConfig = array_merge($dbInformation, $this->baseConfig); /* Load general poller information */ $pollerInformation = Poller::get($pollerId); $this->baseConfig['poller_name'] = $pollerInformation['name']; /* Load configuration information from Centren Engine */ $eventObj = new GenericEvent(array('poller_id' => $pollerId)); Di::getDefault()->get('events')->emit('centreon-broker.poller.configuration', array($eventObj)); $this->baseConfig = array_merge($eventObj->getOutput(), $this->baseConfig); /* get global value in database */ $globalOptions = BrokerRepository::getGlobalValues(); $this->baseConfig = array_merge($globalOptions, $this->baseConfig); /* Add % in begin and end of keys */ $keys = array_keys($this->baseConfig); $values = array_values($this->baseConfig); $keys = array_map(function ($key) { return '%' . $key . '%'; }, $keys); $this->baseConfig = array_combine($keys, $values); }
/** * Get broker config from poller id * * @param int $pollerId */ public static function getBrokerConfigFromPollerId($pollerId) { $poller = Poller::getParameters($pollerId, 'tmpl_name'); $tmpl = $poller['tmpl_name']; $listTpl = PollerTemplateManager::buildTemplatesList(); $fileTplList = $listTpl[$tmpl]->getBrokerPath(); $information = array(); foreach ($fileTplList as $fileTpl) { $information = static::mergeBrokerConf($information, $fileTpl); } return $information; }
/** * * @param array $resultSet */ protected function formatDatas(&$resultSet) { $router = Di::getDefault()->get('router'); foreach ($resultSet as &$myHostSet) { $myHostSet['host_name'] = '<span class="icoListing">' . HostRepository::getIconImage($myHostSet['host_name']) . '</span>' . $myHostSet['host_name']; $sideMenuCustom = new SlideMenu($myHostSet['host_id']); $events = Di::getDefault()->get('events'); $events->emit('centreon-configuration.slide.menu.host', array($sideMenuCustom)); $myHostSet['DT_RowData']['right_side_menu_list'] = $sideMenuCustom->getMenu(); $myHostSet['DT_RowData']['right_side_default_menu'] = $sideMenuCustom->getDefaultMenu(); /* Host State */ $myHostSet['host_name'] .= RealTimeHostRepository::getStatusBadge(RealTimeHostRepository::getStatus($myHostSet['host_id'])); /* Poller */ if (isset($myHostSet["poller_id"]) && $myHostSet["poller_id"] != "") { $poller = Poller::getParameters($myHostSet["poller_id"], 'name'); $myHostSet["poller_id"] = $poller['name']; } else { $myHostSet["poller_id"] = ""; } /* Templates */ $myHostSet['host_template'] = ""; $templates = HostRepository::getTemplateChain($myHostSet['host_id'], array(), 1); foreach ($templates as $template) { $myHostSet['host_template'] .= '<a href="' . $router->getPathFor("/centreon-configuration/hosttemplate/[i:id]", array('id' => $template['id'])) . '"><i class="icon-template ico-20"></i></a>'; } /* Display human readable the check/retry interval */ $myHostSet['host_check_interval'] = HumanReadable::convert($myHostSet['host_check_interval'], 's', $units, null, true); $myHostSet['host_retry_check_interval'] = HumanReadable::convert($myHostSet['host_retry_check_interval'], 's', $units, null, true); /* Get personal tags */ $myHostSet['tagname'] = ''; $aTagUsed = array(); $aTags = TagsRepository::getList('host', $myHostSet['host_id'], 0, 0); foreach ($aTags as $oTags) { if (!in_array($oTags['id'], $aTagUsed)) { $aTagUsed[] = $oTags['id']; $myHostSet['tagname'] .= TagsRepository::getTag('host', $myHostSet['host_id'], $oTags['id'], $oTags['text'], $oTags['user_id'], 1); } } $myHostSet['tagname'] .= TagsRepository::getAddTag('host', $myHostSet['host_id']); } }
/** * Returns the template configuration values * * @param int $pollerId * @return array * @throws \Centreon\Internal\Exception */ private static function getTemplateValues($pollerId) { $templateValues = array(); /* Retrieve template name */ $pollerParam = Poller::get($pollerId, 'tmpl_name'); if (!isset($pollerParam['tmpl_name']) || is_null($pollerParam['tmpl_name'])) { return $templateValues; } /* Look for template file */ $config = Di::getDefault()->get('config'); $centreonPath = rtrim($config->get('global', 'centreon_path'), '/'); /* Get template engine file */ $listTpl = PollerTemplateManager::buildTemplatesList(); if (!isset($listTpl[$pollerParam['tmpl_name']])) { throw new Exception('The template is not found on list of templates'); } $jsonFile = $listTpl[$pollerParam['tmpl_name']]->getEnginePath(); if (!file_exists($jsonFile)) { throw new Exception('Engine template file not found: ' . $pollerParam['tmpl_name'] . '.json'); } /* Checks whether or not template file has all the sections */ $arr = json_decode(file_get_contents($jsonFile), true); if (!isset($arr['content']) || !isset($arr['content']['engine']) || !isset($arr['content']['engine']['setup'])) { return $templateValues; } /* Retrieve parameter values */ foreach ($arr['content']['engine']['setup'] as $setup) { if (isset($setup['params'])) { foreach ($setup['params'] as $k => $v) { $templateValues[$k] = $v; } } } return $templateValues; }
/** * * @param type $sModule * @param type $sNameObject * @param array $aDatas */ public static function validateDataResource($sModule, $sNameObject, $aDatas) { $iObjectId = ''; $return = ''; $oRepository = $sModule . "\\Repository\\" . ucfirst($sNameObject . "Repository"); $oModel = $sModule . "\\Models\\" . ucfirst($sNameObject); $sChampUnique = $oModel::getUniqueLabelField(); $sChampPk = $oModel::getPrimaryKey(); if (isset($aDatas['extraParams'][$sChampUnique])) { $sLabel = $aDatas['extraParams'][$sChampUnique]; } if (isset($aDatas['extraParams']['resource_pollers'])) { $aPollers = explode(",", $aDatas['extraParams']['resource_pollers']); $aPollers = array_map('trim', $aPollers); $aPollers = array_diff($aPollers, array('')); if (isset($aDatas['extraParams']['resource_name']) && count($aPollers) > 0) { $aParams['resources'] = $sLabel; foreach ($aPollers as $iIdPoller) { $sPollerName = ""; $aPollerName = Poller::getParameters($iIdPoller, 'name'); if (is_array($aPollerName) && isset($aPollerName['name']) & !empty($aPollerName['name'])) { $sPollerName = $aPollerName['name']; } $aParams['poller'] = $sPollerName; try { $idReturned = $oRepository::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($aDatas['extraParams']['object_id']) && !empty($aDatas['extraParams']['object_id'])) { $iObjectId = $aDatas['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } } } return $return; }