public static function getDeploymentHosts($deployment) { if (self::$init === false) { self::init(); } $hostSearches = self::getDeploymentHostSearches($deployment); $staticHosts = self::getDeploymentStaticHosts($deployment); $miscSettings = self::getDeploymentMiscSettings($deployment); $nagHelper = new NagHelpers(); $nagHelper->setAliasTemplate($miscSettings['aliastemplate']); if (!empty($hostSearches)) { foreach ($hostSearches as $md5Key => $hsArray) { $hsObj = json_decode(json_encode($hsArray), false); $module = $hsObj->location; if (preg_match("/^RS-(\\w+)-(\\w+)\$/", $module)) { $modObj = new RightScale(); } elseif (preg_match("/^AWSEC2-(\\w+)-(\\w+)\$/", $module)) { $modObj = new AWSEC2(); } else { $modObj = new $module(); } $hostInfo = $modObj->getSearchResults($hsObj); foreach ($hostInfo as $host => $hostData) { $nagHelper->importHost($host, $hostData); } } } if (!empty($staticHosts)) { foreach ($staticHosts as $encIp => $encArray) { $nagHelper->importStaticHost($encArray); } } $hosts = $nagHelper->returnHosts(); return $hosts; }
public static function buildDeployment($deployment, $revision = false, $diff = false, $forcebuild = false, $shardposition = false) { self::checkPerms($deployment); self::$deployment = $deployment; self::$m_logger = new NagLogger(); self::deleteDeployment(); if ($revision === false) { /* No revision means we are dealing with a request from a consumer so we will fetch Deployment information from Nagios Configurator API */ $response = NagRequest::fetchDeploymentData($deployment, self::getSubDeployment()); $responseObj = json_decode($response); $helper = new NagHelpers(); $helper->setAliasTemplate($responseObj->miscsettings->aliastemplate); $helper->setGlobalNegate($responseObj->miscsettings->deploynegate); if (SHARDING === true && $responseObj->miscsettings->ensharding == 'on') { $helper->enableSharding($responseObj->miscsettings->shardkey, $responseObj->miscsettings->shardcount, SHARDING_POSITION); } } else { /* A revision means we are dealing with a request from the UI so we will fetch Deployment information from Redis store directly */ $response = RevDeploy::getDeploymentData($deployment, $revision, true); $responseObj = json_decode($response); $helper = new NagHelpers(true); $helper->setAliasTemplate($responseObj->miscsettings->aliastemplate); $helper->setGlobalNegate($responseObj->miscsettings->deploynegate); if ($shardposition !== false) { $deploymentInfo = RevDeploy::getDeploymentInfo($deployment); $helper->enableSharding($deploymentInfo['shardkey'], $deploymentInfo['shardcount'], $shardposition); } } /* Create Initial Files for Nagios */ if (!empty($responseObj->timeperiods)) { self::createTimeperiodFile($responseObj->timeperiods); } if (!empty($responseObj->commands)) { self::createCommandFile($responseObj->commands); } if (!empty($responseObj->contacttemplates)) { self::createContactTemplateFile($responseObj->contacttemplates); } if (!empty($responseObj->contacts)) { self::createContactFile($responseObj->contacts); } if (!empty($responseObj->contactgroups)) { self::createContactGroupFile($responseObj->contactgroups); } if (!empty($responseObj->hosttemplates)) { self::createHostTemplateFile($responseObj->hosttemplates); } if (!empty($responseObj->hostgroups)) { self::createHostGroupFile($responseObj->hostgroups); } if (!empty($responseObj->servicetemplates)) { self::createServiceTemplateFile($responseObj->servicetemplates); } if (!empty($responseObj->servicegroups)) { self::createServiceGroupFile($responseObj->servicegroups); } if (!empty($responseObj->resourcecfg)) { self::createResourceConfigFile($responseObj->resourcecfg); } if (!empty($responseObj->cgicfg)) { self::createCgiConfigFile($responseObj->cgicfg); } if (!empty($responseObj->modgearmancfg)) { self::createModgearmanConfigFile($responseObj->modgearmancfg); } if (!empty($responseObj->nagioscfg)) { self::createNagiosConfigFile($responseObj->nagioscfg); } /* Initialize Helper object for storing information for hosts and services */ if (!empty($responseObj->hostsearches)) { if ($diff === true && !empty(self::$m_localcache)) { foreach (self::$m_localcache as $module => $items) { foreach ($items as $host => $hostData) { $helper->importHost($host, $hostData); } } } else { foreach ($responseObj->hostsearches as $md5Key => $hsObj) { if (($subdeployment = self::getSubDeployment()) !== false) { if (isset($hsObj->subdeployment) && $hsObj->subdeployment != $subdeployment) { continue; } } $module = $hsObj->location; if (preg_match("/^RS-(\\w+)-(\\w+)\$/", $module)) { $modObj = new RightScale(); } elseif (preg_match("/^AWSEC2-(\\w+)-(\\w+)\$/", $module)) { $modObj = new AWSEC2(); } else { $modObj = new $module(); } $hostInfo = $modObj->getSearchResults($hsObj); if (empty($hostInfo)) { self::$m_logger->addToLog("Empty Results Detected for {$hsObj->location} : {$hsObj->srchparam}"); continue; } foreach ($hostInfo as $host => $hostData) { $helper->importHost($host, $hostData); if ($diff === true) { if (!isset(self::$m_localcache[$module]) || !is_array(self::$m_localcache[$module])) { self::$m_localcache[$module] = array(); } self::$m_localcache[$module][$host] = $hostData; } } } } } if (!empty($responseObj->statichosts)) { foreach ($responseObj->statichosts as $encIP => $tmpObj) { if (($subdeployment = self::getSubDeployment()) !== false) { if (isset($tmpObj->subdeployment) && $tmpObj->subdeployment != $subdeployment) { continue; } } $helper->importStaticHost(NagMisc::recursive_object_to_array($tmpObj)); } } /* Import Service and Node => Service Pointer Data */ if ($forcebuild === false) { if (empty($responseObj->services)) { return "Initial Service Data was Empty"; } elseif (empty($responseObj->nodetemplates)) { return "Initial NodeTemplate Data was Empty"; } } $helper->scrubHosts(); if (!empty($responseObj->services)) { $helper->importServices($responseObj->services); } if (!empty($responseObj->nodetemplates)) { $helper->importNodeTemplate($responseObj->nodetemplates, self::getSubDeployment()); } if (!empty($responseObj->servicedependencies)) { $helper->importServiceDependencies($responseObj->servicedependencies); } if (!empty($responseObj->serviceescalations)) { $helper->importServiceEscalations($responseObj->serviceescalations); } $hostCache = $helper->returnHosts(); $serviceCache = $helper->returnServices($hostCache); $serviceDependencyCache = $helper->returnServiceDependencies($hostCache); $serviceEscalationsCache = $helper->returnServiceEscalations($hostCache); /* Create core host / service nagios config files */ if ($forcebuild === false) { if (empty($hostCache)) { return "Host Cache is Empty, unable to create Host File"; } elseif (empty($serviceCache)) { return "Service Cache is Empty, unable to create Services File"; } } if (!empty($hostCache)) { self::createHostFile($hostCache); } if (!empty($serviceCache)) { self::createServiceFile($serviceCache); } if (!empty($serviceDependencyCache)) { self::createServiceDependencyFile($serviceDependencyCache); } if (!empty($serviceEscalationsCache)) { self::createServiceEscalationsFile($serviceEscalationsCache); } return true; }