[icinga-checkins] icinga.org: icingaweb2-module-director/master: PropertyModifierDnsRecords: new property modifier

git at icinga.org git at icinga.org
Mon Oct 3 17:29:22 CEST 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Mon Oct  3 15:28:41 2016 +0000

PropertyModifierDnsRecords: new property modifier

---

 .../PropertyModifierDnsRecords.php                 |  109 ++++++++++++++++++++
 run.php                                            |    1 +
 2 files changed, 110 insertions(+)

diff --git a/library/Director/PropertyModifier/PropertyModifierDnsRecords.php b/library/Director/PropertyModifier/PropertyModifierDnsRecords.php
new file mode 100644
index 0000000..d8e221b
--- /dev/null
+++ b/library/Director/PropertyModifier/PropertyModifierDnsRecords.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace Icinga\Module\Director\PropertyModifier;
+
+use Icinga\Exception\InvalidPropertyException;
+use Icinga\Module\Director\Hook\PropertyModifierHook;
+use Icinga\Module\Director\Web\Form\QuickForm;
+
+class PropertyModifierDnsRecords extends PropertyModifierHook
+{
+    protected static $types = array(
+        'A'     => DNS_A,
+        'AAAA'  => DNS_AAAA,
+        'CNAME' => DNS_CNAME,
+        'MX'    => DNS_MX,
+        'NS'    => DNS_NS,
+        'PTR'   => DNS_PTR,
+        'TXT'   => DNS_TXT,
+    );
+
+    public static function addSettingsFormFields(QuickForm $form)
+    {
+        $form->addElement('select', 'on_failure', array(
+            'label'        => 'On failure',
+            'description'  => $form->translate('What should we do if the DNS lookup fails?'),
+            'multiOptions' => $form->optionalEnum(array(
+                'null' => $form->translate('Set no value (null)'),
+                'keep' => $form->translate('Keep the property as is'),
+                'fail' => $form->translate('Let the whole import run fail'),
+            )),
+            'required'    => true,
+        ));
+
+        $form->addElement('select', 'record_type', array(
+            'label'        => 'Record type',
+            'description'  => $form->translate('DNS record type'),
+            'multiOptions' => $form->optionalEnum(static::enumTypes()),
+            'required'    => true,
+        ));
+
+    }
+
+    protected static function enumTypes()
+    {
+        $types = array_keys(self::$types);
+        return array_combine($types, $types);
+    }
+
+    public function getName()
+    {
+        return 'Get DNS records of a specific type';
+    }
+
+    public function transform($value)
+    {
+        $type = self::$types[$this->getSetting('record_type')];
+        $response = dns_get_record($value, $type);
+
+        if ($response === false) {
+            switch ($this->getSetting('on_failure')) {
+                case 'null':
+                    return null;
+                case 'keep':
+                    return $value;
+                case 'fail':
+                default:
+                    throw new InvalidPropertyException(
+                        'DNS lookup failed for "%s"',
+                        $value
+                    );
+            }
+        }
+
+        $result = array();
+        switch ($type) {
+            case DNS_A:
+                return $this->extractProperty('ip', $response);
+            case DNS_AAAA:
+                return $this->extractProperty('ipv6', $response);
+            case DNS_CNAME:
+            case DNS_MX:
+            case DNS_NS:
+            case DNS_PTR:
+                return $this->extractProperty('target', $response);
+            case DNS_TXT:
+                return $this->extractProperty('txt', $response);
+                return $response;
+        }
+
+        return $result;
+    }
+
+    protected function extractProperty($key, $response)
+    {
+        $result = array();
+        foreach ($response as $entry) {
+            $result[] = $entry[$key];
+        }
+
+        if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
+            sort($result, SORT_NATURAL);
+        } else {
+            natsort($result);
+            $result = array_values($result);
+        }
+
+        return $result;
+    }
+}
diff --git a/run.php b/run.php
index 8eea869..0b6cf2d 100644
--- a/run.php
+++ b/run.php
@@ -29,6 +29,7 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierSplit');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierJoin');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierGetHostByName');
+$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierDnsRecords');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierExtractFromDN');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromAdSid');
 $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromLatin1');



More information about the icinga-checkins mailing list