[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaServiceSet: introduce a new object type

git at icinga.org git at icinga.org
Wed Oct 12 09:19:15 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: b7018627a086c88a713ff24f1c18a6e91707905c
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=b7018627a086c88a713ff24f1c18a6e91707905c

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Oct 12 07:19:02 2016 +0000

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(+)

diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php
index 289bcf0..92f76b7 100644
--- a/library/Director/Objects/IcingaObject.php
+++ b/library/Director/Objects/IcingaObject.php
@@ -368,6 +368,16 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
     }
 
     /**
+     * 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
      * render an object even when no such host exists. Think of the activity log,
diff --git a/library/Director/Objects/IcingaServiceSet.php b/library/Director/Objects/IcingaServiceSet.php
new file mode 100644
index 0000000..5ec7de1
--- /dev/null
+++ b/library/Director/Objects/IcingaServiceSet.php
@@ -0,0 +1,179 @@
+<?php
+
+namespace Icinga\Module\Director\Objects;
+
+use Icinga\Exception\ProgrammingError;
+use Icinga\Module\Director\IcingaConfig\IcingaConfig;
+use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
+use Icinga\Module\Director\Objects\IcingaService;
+
+class IcingaServiceSet extends IcingaObject
+{
+    protected $table = 'icinga_service_set';
+
+    protected $defaultProperties = array(
+        'id'                    => 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 0000000..65fdd2f
--- /dev/null
+++ b/library/Director/Objects/IcingaServiceSetAssignment.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Icinga\Module\Director\Objects;
+
+class IcingaServiceAssignment extends IcingaObject
+{
+    protected $table = 'icinga_service_set_assignment';
+
+    protected $keyName = 'id';
+
+    protected $defaultProperties = array(
+        'id'             => null,
+        'service_set_id' => null,
+        'filter_string'  => null,
+    );
+
+    protected $relations = array(
+        'service_set' => 'IcingaServiceSet',
+    );
+}



More information about the icinga-checkins mailing list