/** * 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)); }
/** * Update a host * * * @method post * @route /hosttemplate/update */ public function updateAction() { $givenParameters = $this->getParams('post'); $macroList = array(); $aTagList = array(); $aTags = array(); $aTagsTemplate = array(); $aTagsInTpl = array(); $aTagsIdTpl = array(); if (isset($givenParameters['macro_name']) && isset($givenParameters['macro_value'])) { $macroName = $givenParameters['macro_name']; $macroValue = $givenParameters['macro_value']; $macroHidden = $givenParameters['macro_hidden']; foreach ($macroName as $key => $name) { if (!empty($name)) { if (isset($macroHidden[$key])) { $isPassword = '******'; } else { $isPassword = '******'; } $macroList[$name] = array('value' => $macroValue[$key], 'ispassword' => $isPassword); } } } if ($givenParameters['host_create_services_from_template']) { \CentreonConfiguration\Models\Host::deployServices($givenParameters['object_id']); } if (count($macroList) > 0) { try { CustomMacroRepository::saveHostCustomMacro(self::$objectName, $givenParameters['object_id'], $macroList); } catch (\Exception $ex) { $errorMessage = $ex->getMessage(); $this->router->response()->json(array('success' => false, 'error' => $errorMessage)); } } //Delete tags directilly rattached to the object TagsRepository::deleteTagsForResource(self::$objectName, $givenParameters['object_id'], 0); //Insert tags affected to the HOST if (isset($givenParameters['host_tags'])) { $aTagList = explode(",", $givenParameters['host_tags']); foreach ($aTagList as $var) { $var = trim($var); if (!empty($var)) { array_push($aTags, $var); } } if (count($aTags) > 0) { TagsRepository::saveTagsForResource(self::$objectName, $givenParameters['object_id'], $aTags, '', false, 1); } } parent::updateAction(); }
/** * 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)); }
/** * Get snmp version for a specific host * * @method get * @route /host/[i:id]/snmp-version */ public function snmpVersionForHostAction() { $di = Di::getDefault(); $router = $di->get('router'); $requestParam = $this->getParams('named'); $snmpVersionParam = Host::getParameters($requestParam['id'], 'host_snmp_version'); $snmpVersion = array(); if (isset($snmpVersionParam['host_snmp_version'])) { $snmpVersion = array("id" => $snmpVersionParam['host_snmp_version'], "text" => $snmpVersionParam['host_snmp_version']); } $router->response()->json($snmpVersion); }
public function testGetColumns() { $this->assertEquals(array('host_id', 'host_template_model_htm_id', 'command_command_id', 'command_command_id_arg1', 'timeperiod_tp_id', 'timeperiod_tp_id2', 'command_command_id2', 'command_command_id_arg2', 'host_name', 'host_alias', 'host_address', 'display_name', 'host_max_check_attempts', 'host_check_interval', 'host_retry_check_interval', 'host_active_checks_enabled', 'host_passive_checks_enabled', 'host_checks_enabled', 'initial_state', 'host_obsess_over_host', 'host_check_freshness', 'host_freshness_threshold', 'host_event_handler_enabled', 'host_low_flap_threshold', 'host_high_flap_threshold', 'host_flap_detection_enabled', 'flap_detection_options', 'host_process_perf_data', 'host_retain_status_information', 'host_retain_nonstatus_information', 'host_notification_interval', 'host_notification_options', 'host_notifications_enabled', 'contact_additive_inheritance', 'cg_additive_inheritance', 'host_first_notification_delay', 'host_snmp_community', 'host_snmp_version', 'host_location', 'host_comment', 'host_register', 'host_activate', 'organization_id', 'environment_id', 'poller_id'), Host::getColumns()); }
/** * 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); } }
/** * 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(); }
/** * * @param type $value * @param array $params * @return boolean */ public function validate($value, $params = array(), $sContext = 'server') { $db = Di::getDefault()->get('db_centreon'); $bSuccess = true; $resultError = _("Object already exists"); $sMessage = ''; $aParams = array(); $aHost = array(); $sLabel = ''; $iId = ''; $return = ''; if (isset($params['object']) && $params['object'] == 'service') { $objClass = "CentreonConfiguration\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['service_description'])) { $sLabel = $params['extraParams']['service_description']; } if (isset($params['extraParams']['service_id'])) { $iId = $params['extraParams']['service_id']; } if (isset($params['extraParams']['service_hosts'])) { $aHosts = explode(",", $params['extraParams']['service_hosts']); $aHosts = array_diff($aHosts, array('')); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } foreach ($aHosts as $iIdHost) { $sHostName = ""; $aHostName = Host::getParameters($iIdHost, 'host_name'); if (is_array($aHostName) && isset($aHostName['host_name']) & !empty($aHostName['host_name'])) { $sHostName = $aHostName['host_name']; } $aParams['host'] = $sHostName; $aParams['service'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } } } elseif (isset($params['object']) && $params['object'] == 'servicetemplate') { $objClass = "CentreonConfiguration\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['service_description'])) { $sLabel = $params['extraParams']['service_description']; } $aParams['servicetemplate'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } elseif (isset($params['object']) && $params['object'] == 'host') { $objClass = "CentreonConfiguration\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['host_name'])) { $sLabel = $params['extraParams']['host_name']; } $aParams['host'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } elseif (isset($params['object']) && $params['object'] == 'hosttemplate') { $objClass = "CentreonConfiguration\\Repository\\HostTemplateRepository"; if (isset($params['extraParams']['host_name'])) { $sLabel = $params['extraParams']['host_name']; } $aParams['hosttemplate'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } elseif (isset($params['object']) && $params['object'] == 'command') { $objClass = "CentreonConfiguration\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['command_name'])) { $sLabel = $params['extraParams']['command_name']; } $aParams['command'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } elseif (isset($params['object']) && $params['object'] == 'contact') { $objClass = "CentreonAdministration\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['description'])) { $sLabel = $params['extraParams']['description']; } $aParams['contact'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } elseif (isset($params['object']) && $params['object'] == 'user') { $objClass = "CentreonAdministration\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['login'])) { $sLabel = $params['extraParams']['login']; } $aParams['user'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } elseif (isset($params['object']) && $params['object'] == 'businessactivity') { $objClass = "CentreonBam\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['name'])) { $sLabel = $params['extraParams']['name']; } $aParams['bam'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } elseif (isset($params['object']) && $params['object'] == 'connector') { $objClass = "CentreonConfiguration\\Repository\\" . ucfirst($params['object'] . "Repository"); if (isset($params['extraParams']['name'])) { $sLabel = $params['extraParams']['name']; } $aParams['connector'] = $sLabel; try { $idReturned = $objClass::getIdFromUnicity($aParams); $iObjectId = ''; if (isset($params['extraParams']['object_id']) && !empty($params['extraParams']['object_id'])) { $iObjectId = $params['extraParams']['object_id']; } $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } if (is_array($return)) { foreach ($return as $valeur) { if ($valeur > 0) { $bSuccess = false; $sMessage = $resultError; break; } } } else { if ($return > 0) { $bSuccess = false; $sMessage = $resultError; } } if ($sContext == 'client') { $reponse = $bSuccess; } else { $reponse = array('success' => $bSuccess, 'error' => $sMessage); } return $reponse; }
/** * Generic update function * * @param type $givenParameters * @param type $origin * @param type $route * @param type $validate * @param type $validateMandatory * * @throws \Centreon\Internal\Exception */ public static function update($givenParameters, $origin = "", $route = "", $validate = true, $validateMandatory = true) { $id = null; $db = Di::getDefault()->get('db_centreon'); $class = static::$objectClass; $pk = $class::getPrimaryKey(); $columns = $class::getColumns(); if (isset($givenParameters['service_hosts'])) { $sField = $class::getUniqueLabelField(); $aHostId = explode(",", $givenParameters['service_hosts']); $aHostId = array_filter($aHostId, 'strlen'); if (count($aHostId) > 0) { $sHostName = Host::get(current($aHostId), 'host_name'); if (isset($sField) && isset($givenParameters[$sField]) && !is_null($class::getSlugField()) && (isset($givenParameters[$class::getSlugField()]) && is_null($givenParameters[$class::getSlugField()]) or !isset($givenParameters[$class::getSlugField()]))) { $oSlugify = new CentreonSlugify($class, get_called_class()); $sString = $sHostName['host_name'] . " " . $givenParameters[$sField]; $sSlug = $oSlugify->slug($sString); $givenParameters[$class::getSlugField()] = $sSlug; } } } parent::update($givenParameters, $origin, $route, $validate, $validateMandatory); }
/** * Get service for a specific kpi * * * @method get * @route /indicator/[i:id]/service */ public function serviceForIndicatorAction() { $di = Di::getDefault(); $router = $di->get('router'); $requestParam = $this->getParams('named'); $relObj = static::$relationMap['indicator_service']; $listOfServices = $relObj::getHostIdServiceIdFromKpiId($requestParam['id']); $finalList = array(); if (isset($listOfServices[0]) && !empty($listOfServices[0]['service_id']) && !empty($listOfServices[0]['host_id'])) { $serviceDescription = Service::getParameters($listOfServices[0]['service_id'], 'service_description'); $hostName = Host::getParameters($listOfServices[0]['host_id'], 'host_name'); $finalList = array("id" => $listOfServices[0]['service_id'] . '_' . $listOfServices[0]['host_id'], "text" => $hostName['host_name'] . ' ' . $serviceDescription['service_description']); } $router->response()->json($finalList); }
/** * * @param type $sModule * @param type $sNameObject * @param array $aDatas */ public static function validateDataService($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'][$sChampPk])) { $iId = $aDatas['extraParams'][$sChampPk]; } if (isset($aDatas['extraParams']['service_hosts'])) { $aHosts = explode(",", $aDatas['extraParams']['service_hosts']); $aHosts = array_diff($aHosts, array('')); if (isset($aDatas['extraParams']['object_id']) && !empty($aDatas['extraParams']['object_id'])) { $iObjectId = $aDatas['extraParams']['object_id']; } foreach ($aHosts as $iIdHost) { $sHostName = ""; $aHostName = Host::getParameters($iIdHost, 'host_name'); if (is_array($aHostName) && isset($aHostName['host_name']) & !empty($aHostName['host_name'])) { $sHostName = $aHostName['host_name']; } $aParams['host'] = $sHostName; $aParams['service'] = $sLabel; try { $idReturned = $oRepository::getIdFromUnicity($aParams); $return[] = self::compareResponse($iObjectId, $idReturned); } catch (MissingParameterException $e) { $return[] = 0; } } } return $return; }