From b7018627a086c88a713ff24f1c18a6e91707905c Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 12 Oct 2016 07:19:02 +0000 Subject: [PATCH] IcingaServiceSet: introduce a new object type --- library/Director/Objects/IcingaObject.php | 10 + library/Director/Objects/IcingaServiceSet.php | 179 ++++++++++++++++++ .../Objects/IcingaServiceSetAssignment.php | 20 ++ 3 files changed, 209 insertions(+) create mode 100644 library/Director/Objects/IcingaServiceSet.php create mode 100644 library/Director/Objects/IcingaServiceSetAssignment.php diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 289bcf07..92f76b78 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -367,6 +367,16 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return $this->isApplyRule(); } + /** + * Whether this object can be part of a 'set' + * + * @return bool + */ + public function supportsSets() + { + return $this->supportsSets; + } + /** * It sometimes makes sense to defer lookups for related properties. This * kind of lazy-loading allows us to for example set host = 'localhost' and diff --git a/library/Director/Objects/IcingaServiceSet.php b/library/Director/Objects/IcingaServiceSet.php new file mode 100644 index 00000000..5ec7de1e --- /dev/null +++ b/library/Director/Objects/IcingaServiceSet.php @@ -0,0 +1,179 @@ + null, + 'host_id' => null, + 'object_name' => null, + 'object_type' => null, + 'description' => null, + ); + + protected $supportsCustomVars = true; + + protected $supportsApplyRules = true; + + protected $relations = array( + 'host' => 'IcingaHost', + ); + + protected $multiRelations = array( + 'service' => 'IcingaService', + ); + + public function isDisabled() + { + return false; + } + + public function supportsAssignments() + { + return true; + } + + /** + * @return IcingaService + */ + public function getServiceObjects() + { + if (! $this->hasBeenLoadedFromDb()) { + return array(); + } + + $conn = $this->getConnection(); + $db = $conn->getDbAdapter(); + + $query = $db->select()->from( + array('s' => 'icinga_service'), + '*' + )->join( + array('sset' => 'icinga_service_set_service'), + 'sset.service_id = s.id', + array() + )->where( + $db->quoteInto( + 'sset.service_set_id = ?', + (int) $this->id + ) + )->order('s.object_name'); + + // TODO: This cannot be prefetched + return IcingaService::loadAll($conn, $query, 'object_name'); + } + + public function renderToConfig(IcingaConfig $config) + { + if ($this->isTemplate()) { + return; + } + + if ($config->isLegacy()) { + return $this->renderToLegacyConfig($config); + } + + // Loop over all services belonging to this set + // add our assign rules and then add the service to the config + // eventually clone them beforehand to not get into trouble with caches + // figure out whether we might need a zone property + $file = $config->configFile( + 'zones.d/' . $this->getRenderingZone($config) . '/servicesets' + ); + + $file->prepend($this->getConfigHeaderComment($config)); + + foreach ($this->getServiceObjects() as $service) { + $service->object_type = $this->object_type; + if ($this->isApplyRule()) { + $service->setAssignments($this->getAssignments()); + } + + $service->host_id = $this->host_id; + $file->addObject($service); + } + } + + protected function getConfigHeaderComment(IcingaConfig $config) + { + if ($config->isLegacy()) { + $comment = "## Service Set '%s'\n\n"; + } else { + $comment = "/** Service Set '%s' **/\n\n"; + } + + return sprintf($comment, $this->object_name); + } + + public function renderToLegacyConfig(IcingaConfig $config) + { + if ($this->isTemplate()) { + return; + } + + if ($this->isApplyRule()) { + // Not yet + return; + } + + // evaluate my assign rules once, get related hosts + // Loop over all services belonging to this set + // generate every service with host_name host1,host2... + + $file = $config->configFile( + // TODO: zones.d? + 'zones.d/' . $this->getRenderingZone($config) . '/servicesets' + ); + + $file->prepend($this->getConfigHeaderComment($config)); + + foreach ($this->getServiceObjects() as $service) { + $service->object_type = 'object'; + $service->host_id = $this->host_id; + $file->addLegacyObject($service); + } + } + + protected function resolve($what) + { + return array(); + } + + protected function getResolved($what) + { + return array(); + } + + protected function getInherited($what) + { + return array(); + } + + protected function getOrigins($what) + { + return array(); + } + + public function getRenderingZone(IcingaConfig $config = null) + { + return $this->connection->getDefaultGlobalZoneName(); + } + + public function xxisObject() + { + return true; + } + + public function getUrlParams() + { + return array('name' => $this->object_name); + } +} diff --git a/library/Director/Objects/IcingaServiceSetAssignment.php b/library/Director/Objects/IcingaServiceSetAssignment.php new file mode 100644 index 00000000..65fdd2ff --- /dev/null +++ b/library/Director/Objects/IcingaServiceSetAssignment.php @@ -0,0 +1,20 @@ + null, + 'service_set_id' => null, + 'filter_string' => null, + ); + + protected $relations = array( + 'service_set' => 'IcingaServiceSet', + ); +}