/** * 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); } }
public function testInsert() { $insert = array('service_description' => 'test', 'service_alias' => 'test alias', 'service_template_model_stm_id' => '1', 'service_activate' => '1', 'organization_id' => 1); Service::insert($insert); $this->tableEqualsXml('cfg_services', dirname(__DIR__) . '/data/service.insert.xml'); }
/** * 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(); }