/** * Display the configuration snapshot of a service * with template inheritance * * @method get * @route /service/snapshotslide/[i:id] */ public function snapshotslideAction() { $params = $this->getParams(); $data = ServiceRepository::getConfigurationData($params['id']); $serviceId = Service::getPrimaryKey(); $serviceDescription = Service::getUniqueLabelField(); $hostId = Host::getPrimaryKey(); $hostName = Host::getUniqueLabelField(); $filters = array($serviceId => $params['id']); //If service inherits a template if (isset($data['service_template_model_stm_id'])) { $data = ServiceRepository::getConfigurationData($data['service_template_model_stm_id']); } else { $data = ServiceRepository::getConfigurationData($params['id']); } $list = HostService::getMergedParameters(array($hostId, $hostName), array($serviceId, $serviceDescription), -1, 0, null, "ASC", $filters, "OR"); foreach ($list as $obj) { $data[$serviceDescription] = $obj[$hostName] . '|' . $obj[$serviceDescription]; } $serviceConfiguration = ServiceRepository::formatDataForSlider($data); $edit_url = $this->router->getPathFor("/centreon-configuration/service/" . $params['id']); $this->router->response()->json(array('serviceConfig' => $serviceConfiguration, 'edit_url' => $edit_url, 'success' => true)); }
/** * Deploy services by host templates * * @param int $hostId * @param int $hostTemplateId */ public static function deployServices($hostId, $hostTemplateId = null) { static $deployedServices = array(); $db = Di::getDefault()->get('db_centreon'); $hid = is_null($hostTemplateId) ? $hostId : $hostTemplateId; $services = HostServiceRelation::getMergedParameters(array(), array('service_id', 'service_description', 'service_alias'), -1, 0, null, 'ASC', array(HostServiceRelation::getFirstKey() => $hid), 'AND'); foreach ($services as $service) { if (is_null($hostTemplateId)) { $deployedServices[$hostId][$service['service_description']] = true; } elseif (!isset($deployedServices[$hostId][$service['service_alias']])) { $oRepositorie = "CentreonConfiguration\\Repository\\ServiceRepository"; $oSlugify = new CentreonSlugify(self, $oRepositorie); if (!empty($service['service_alias'])) { $sData = $service['service_alias']; } else { $sData = $service['service_description']; } $sHostName = Host::get($hostId, 'host_name'); $sString = $sHostName['host_name'] . " " . $sData; $sSlug = $oSlugify->slug($sString); $serviceId = Service::insert(array('service_slug' => $sSlug, 'service_description' => $sData, 'service_template_model_stm_id' => $service['service_id'], 'service_register' => 1, 'service_activate' => 1)); HostServiceRelation::insert($hostId, $serviceId); $deployedServices[$hostId][$service['service_alias']] = true; } } $templates = HostHosttemplateRelation::getTargetIdFromSourceId('host_tpl_id', 'host_host_id', $hid); foreach ($templates as $tplId) { self::deployServices($hostId, $tplId); } }
/** * Used for duplicate a host * * @param int $sourceObjectId The source host id * @param int $duplicateEntries The number entries * @return array List of new host id */ public static function duplicate($sourceObjectId, $duplicateEntries = 1) { $db = Di::getDefault()->get(static::$databaseName); $sourceParams = static::getParameters($sourceObjectId, '*'); if (false === $sourceParams) { throw new \Exception(static::OBJ_NOT_EXIST); } unset($sourceParams['host_id']); $originalName = $sourceParams['host_name']; $explodeOriginalName = explode('_', $originalName); $j = 0; if (($count = count($explodeOriginalName)) > 1 && is_numeric($explodeOriginalName[$count - 1])) { $originalName = join('_', array_slice($explodeOriginalName, 0, -1)); $j = $explodeOriginalName[$count - 1]; } $listDuplicateId = array(); for ($i = 0; $i < $duplicateEntries; $i++) { /* Search the unique name for duplicate host */ do { $j++; $unique = self::isUnique($originalName . '_' . $j); } while (false === $unique); $sourceParams['host_name'] = $originalName . '_' . $j; /* Insert the duplicate host */ $lastId = static::insert($sourceParams); if (false === is_numeric($lastId)) { throw new \Exception("The value is not numeric"); } $listDuplicateId[] = $lastId; /* Insert relation */ /* Duplicate service */ /* Get service for the source host */ $listSvc = HostServiceRelation::getTargetIdFromSourceId('service_service_id', 'host_host_id', $sourceObjectId); foreach ($listSvc as $svcId) { /* Duplicate service */ $newSvcId = Service::duplicate($svcId, 1, true); if (count($newSvcId) > 0) { /* Attach the new service to the new host */ HostServiceRelation::insert($lastId, $newSvcId[0]); } } $db->beginTransaction(); /* Duplicate macros */ $queryDupMacros = "INSERT INTO cfg_customvariables_hosts (host_macro_name, host_macro_value, is_password, host_host_id)\n SELECT host_macro_name, host_macro_value, is_password, " . $lastId . " FROM cfg_customvariables_hosts\n WHERE host_host_id = :sourceObjectId"; $stmt = $db->prepare($queryDupMacros); $stmt->bindParam(':sourceObjectId', $sourceObjectId); $stmt->execute(); /* Host template */ $queryDupTemplate = "INSERT INTO cfg_hosts_templates_relations (host_host_id, host_tpl_id, `order`)\n SELECT " . $lastId . ", host_tpl_id, `order` FROM cfg_hosts_templates_relations\n WHERE host_host_id = :sourceObjectId"; $stmt = $db->prepare($queryDupTemplate); $stmt->bindParam(':sourceObjectId', $sourceObjectId); $stmt->execute(); /* Host global tags */ $queryDupTag = "INSERT INTO cfg_tags_hosts (tag_id, resource_id)\n SELECT th.tag_id, " . $lastId . " FROM cfg_tags_hosts th, cfg_tags t\n WHERE t.user_id IS NULL AND t.tag_id = th.tag_id AND th.resource_id = :sourceObjectId"; $stmt = $db->prepare($queryDupTag); $stmt->bindParam(':sourceObjectId', $sourceObjectId); $stmt->execute(); $db->commit(); } }
/** * Update slug services by host * * @param int $iHostId * @param string $sHostName */ public static function updateSlugServices($iHostId, $sHostName) { $aServices = array(); $db = Di::getDefault()->get('db_centreon'); $repository = "CentreonConfiguration\\Repository\\ServiceRepository"; $repository::setObjectName("Service"); $repository::setObjectClass("\\CentreonConfiguration\\Models\\Service"); $oModel = "CentreonConfiguration\\Models\\Service"; $oSlugify = new CentreonSlugify($oModel, $repository); // get services $aHostServices = HostServiceRelation::getMergedParameters(array('host_id'), array('service_id', 'service_description', 'service_alias'), -1, 0, null, "ASC", array('host_id' => $iHostId), "OR"); foreach ($aHostServices as $key => $oService) { $sString = $sHostName . " " . $oService['service_description']; $sSlug = $oSlugify->slug($sString); Service::updateSlug($oService['service_id'], $sSlug); } }
/** * Return Service name * * @param int or array $svcId The service ID * @return array */ public static function getName($svcId) { $di = Di::getDefault(); $router = $di->get('router'); $serviceId = Service::getPrimaryKey(); $serviceDescription = Service::getUniqueLabelField(); $hostId = Host::getPrimaryKey(); $hostName = Host::getUniqueLabelField(); $filters = array($serviceId => $svcId); $list = HostServiceRelation::getMergedParameters(array($hostId, $hostName), array($serviceId, $serviceDescription), -1, 0, null, "ASC", $filters, "OR"); $finalList = array(); foreach ($list as $obj) { $finalList[] = array("id" => $obj[$serviceId], "text" => $obj[$hostName] . ' ' . $obj[$serviceDescription]); } return $finalList; }