[icinga-checkins] icinga.org: icinga2-migration/master: Add basic timeperiod support

git at icinga.org git at icinga.org
Tue Jun 10 23:10:31 CEST 2014


Module: icinga2-migration
Branch: master
Commit: 833f5a6da71f6eed02405da880445203708f49cd
URL:    https://git.icinga.org/?p=icinga2-migration.git;a=commit;h=833f5a6da71f6eed02405da880445203708f49cd

Author: Michael Friedrich <michael.friedrich at gmail.com>
Date:   Tue Jun 10 22:19:28 2014 +0200

Add basic timeperiod support

---

 .../application/clicommands/MigrateCommand.php     |    7 ++++-
 .../Conftool/Icinga2/Icinga2ObjectDefinition.php   |   31 ++++++++++++++++++--
 .../library/Conftool/Icinga2/Icinga2Timeperiod.php |    9 ++++++
 3 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/modules/conftool/application/clicommands/MigrateCommand.php b/modules/conftool/application/clicommands/MigrateCommand.php
index 41311af..8211775 100644
--- a/modules/conftool/application/clicommands/MigrateCommand.php
+++ b/modules/conftool/application/clicommands/MigrateCommand.php
@@ -46,9 +46,14 @@ class MigrateCommand extends Command
             Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
         }
 
+        foreach ($config->getDefinitions('timeperiod') as $object) {
+            Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
+        }
         //templates
-        foreach($config->getTemplates() as $template) {
+/*
+         foreach($config->getTemplates() as $template) {
             Icinga2ObjectDefinition::fromIcingaObjectDefinition($template, $config)->dump();
         }
+ */
     }
 }
diff --git a/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php b/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php
index 93fbddc..370d8b2 100644
--- a/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php
+++ b/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php
@@ -72,13 +72,13 @@ class Icinga2ObjectDefinition
             }
 
             //ugly 1.x hacks
-        //these values must be resolved earlier already
+            //these values must be resolved earlier already
             if($this->is_template && ($key == "service_description" || $key == "host_name")) {
                 continue; //skip invalid template attributes
             }
             if (!$this->is_template && $key == "name") {
                 continue; //skip invalid object attributes
-        }
+            }
 
             // template imports
             if ($key == "use") {
@@ -98,10 +98,11 @@ class Icinga2ObjectDefinition
                     //TODO check against legacy macros and replace them
                     $this->vars($varname, $varvalue);
                 }
-        continue;
+                continue;
             }
 
             //convert host/service notifications
+            /*
             if ($key == "contacts" && ($object instanceof IcingaService || $object instanceof IcingaHost)) {
                 $arr = $this->splitComma($value);
 
@@ -142,6 +143,7 @@ class Icinga2ObjectDefinition
 
                 continue;
             }
+             */
 
             //conversion of different attributes
             $func = 'convert' . ucfirst($key);
@@ -150,6 +152,16 @@ class Icinga2ObjectDefinition
                 continue;
             }
 
+            if ($object->getDefinitionType() == "timeperiod") {
+                if (preg_match('/^\d+/', $key)) {
+                    print_r("//ERROR: Timperiod property invalid. Skipping it.\n");
+                    continue;
+                }
+                $key = "ranges.".$key;
+                $this->$key = "\"".$value."\"";
+                continue; //allow remaining items
+            }
+         
             //mapping
             if (! array_key_exists($key, $this->v1AttributeMap)) {
                 throw new Icinga2ConfigMigrationException(
@@ -165,6 +177,11 @@ class Icinga2ObjectDefinition
             }
         }
 
+        //itl required imports
+        if ($object->getDefinitionType() == "timeperiod") {
+            $this->imports("legacy-timeperiod");
+        }
+
         //custom vars
         foreach ($object->getCustomVars() as $key => $value) {
             $this->vars($key, $value);
@@ -283,6 +300,11 @@ class Icinga2ObjectDefinition
                 $new->setAttributesFromIcingaObjectDefinition($object, $config);
                 break;
 
+            case 'timeperiod':
+                $new = new Icinga2Timeperiod($object);
+                $new->setAttributesFromIcingaObjectDefinition($object, $config);
+                break;
+
             default:
                 throw new Icinga2ConfigMigrationException(
                     sprintf(
@@ -404,6 +426,9 @@ class Icinga2ObjectDefinition
         if ($this->isApply()) {
             $prefix = "apply";
         }
+
+        //print_r("prefix: ".$prefix."\n");
+        //var_dump($this);
         
         return sprintf(
             "%s %s \"%s\" {\n%s%s%s\n%s}\n\n",
diff --git a/modules/conftool/library/Conftool/Icinga2/Icinga2Timeperiod.php b/modules/conftool/library/Conftool/Icinga2/Icinga2Timeperiod.php
new file mode 100644
index 0000000..27480fc
--- /dev/null
+++ b/modules/conftool/library/Conftool/Icinga2/Icinga2Timeperiod.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Icinga\Module\Conftool\Icinga2;
+
+class Icinga2Timeperiod extends Icinga2ObjectDefinition
+{
+    protected $type = 'TimePeriod';
+
+}



More information about the icinga-checkins mailing list