Example #1
0
 /**
  * Deploy services by host templates
  *
  * @param int $hostId
  * @param int $hostTemplateId
  */
 public static function deployServices($hostId, $hostTemplateId = null)
 {
     static $deployedServices = array();
     $aServices = array();
     $db = Di::getDefault()->get('db_centreon');
     //get host template
     $aHostTemplates = HostRepository::getTemplateChain($hostId, array(), -1);
     // get host services
     $aHostServices = HostServiceRelation::getMergedParameters(array('host_id'), array('service_id', 'service_description', 'service_alias', 'inherited'), -1, 0, null, "ASC", array('host_id' => $hostId), "OR");
     // get all service templates linked to a host by its host templates
     $aHostServiceTemplates = array();
     foreach ($aHostTemplates as $oHostTemplate) {
         $aHostTemplateServiceTemplates = HostTemplateServiceTemplateRelation::getMergedParameters(array('host_id'), array('service_id', 'service_description', 'service_alias'), -1, 0, null, "ASC", array('host_id' => $oHostTemplate['id']), "OR");
         // Remove services with same description
         foreach ($aHostTemplateServiceTemplates as $oHostTemplateServiceTemplate) {
             if (!isset($aHostServiceTemplates[$oHostTemplateServiceTemplate['service_alias']])) {
                 $aHostServiceTemplates[$oHostTemplateServiceTemplate['service_alias']] = $oHostTemplateServiceTemplate;
             }
         }
     }
     // get services linked to the host
     $aServicesDescription = array_values(array_column($aHostServices, 'service_description'));
     $repository = "CentreonConfiguration\\Repository\\ServiceRepository";
     $repository::setRelationMap(array("service_hosts" => "\\CentreonConfiguration\\Models\\Relation\\Service\\Host", "service_parents" => "\\CentreonConfiguration\\Models\\Relation\\Service\\Serviceparents", "service_children" => "\\CentreonConfiguration\\Models\\Relation\\Service\\Servicechildren"));
     $repository::setObjectName("Service");
     $repository::setObjectClass("\\CentreonConfiguration\\Models\\Service");
     $oModel = "CentreonConfiguration\\Models\\Service";
     $oSlugify = new CentreonSlugify($oModel, $repository);
     $db->beginTransaction();
     $aTemplateServicesDescription = array_keys($aHostServiceTemplates);
     foreach ($aHostServices as $service) {
         if ($service['inherited'] == 1 && !in_array($service['service_description'], $aTemplateServicesDescription)) {
             Service::delete($service['service_id']);
         }
     }
     // create services which don't yet exist
     foreach ($aHostServiceTemplates as $oHostServiceTemplate) {
         if (!in_array($oHostServiceTemplate['service_alias'], $aServicesDescription)) {
             $sHostName = Host::get($hostId, 'host_name');
             if (isset($sHostName['host_name'])) {
                 $sString = $sHostName['host_name'] . " " . $oHostServiceTemplate['service_alias'];
             } else {
                 $sString = $oHostServiceTemplate['service_alias'];
             }
             $sSlug = $oSlugify->slug($sString);
             if (!empty($oHostServiceTemplate['service_alias'])) {
                 $sData = $oHostServiceTemplate['service_alias'];
             } else {
                 $sData = $oHostServiceTemplate['service_description'];
             }
             $newService['service_slug'] = $sSlug;
             $newService['service_description'] = $sData;
             $newService['service_template_model_stm_id'] = $oHostServiceTemplate['service_id'];
             $newService['service_register'] = 1;
             $newService['service_activate'] = 1;
             $newService['organization_id'] = Di::getDefault()->get('organization');
             $newService['inherited'] = 1;
             $serviceId = Service::insert($newService);
             HostServiceRelation::insert($hostId, $serviceId);
             ServiceHostTemplateRelation::insert($serviceId, $oHostServiceTemplate['host_id']);
         }
     }
     $db->commit();
 }
Example #2
0
 /**
  * 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);
     }
 }
Example #3
0
 /**
  * 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();
     }
 }