protected function setAttributesFromIcingaObjectDefinition(IcingaObjectDefinition $object, IcingaConfig $config)
 {
     //template, parents and config
     $this->is_template = $object->isTemplate();
     $this->_parents = $object->getParents();
     $all_contacts = array();
     $all_contactgroups = array();
     $all_parents = array();
     //---- LOOP ATTRIBUTES BEGIN
     foreach ($object->getAttributes() as $key => $value) {
         //rejects
         if ($key !== null && in_array($key, $this->v1RejectedAttributeMap)) {
             continue;
         }
         //ugly 1.x hacks
         //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") {
             $this->imports = $this->migrateUseImport($value, $key);
             //DONE
             continue;
         }
         //check command arguments
         if ($key == "check_command") {
             $command_arr = explode("!", $value);
             $this->properties['check_command'] = "\"" . $command_arr[0] . "\"";
             //first is always the command name
             for ($i = 1; $i < count($command_arr); $i++) {
                 $varname = "ARG" . $i;
                 $varvalue = addslashes($command_arr[$i]);
                 //escape the string
                 //check against legacy macros and replace them
                 $varvalue = $this->migrateLegacyMacros($varvalue);
                 $this->vars($varname, $varvalue);
             }
             //DONE
             continue;
         }
         //event handler translation
         if ($key == "event_handler") {
             $eventcommand_prefix = (string) $object;
             if ($object instanceof IcingaHost) {
                 $eventcommand_prefix = "host-" . $eventcommand_prefix;
             } else {
                 if ($object instanceof IcingaService) {
                     $eventcommand_prefix = "service-" . $eventcommand_prefix;
                 }
             }
             $eventcommand_obj = $config->getObject($value, 'command');
             $eventcommand_name = $eventcommand_prefix . "-" . $value;
             $eventcommand_line = addslashes($eventcommand_obj->command_line);
             $eventcommand_line = $this->migrateLegacyMacros($eventcommand_line);
             $this->eventcommands($eventcommand_name, $eventcommand_line);
             $this->event_command = "\"" . $eventcommand_name . "\"";
             //DONE
             continue;
         }
         //parents to dependencies
         if ($key == "parents") {
             //skip templates
             if ($this->isTemplate()) {
                 continue;
             }
             $parents = $this->splitComma($value);
             foreach ($parents as $parent) {
                 $all_parents[] = trim($parent);
             }
             continue;
         }
         //convert host/service notifications
         if ($key == "contacts" && ($object instanceof IcingaService || $object instanceof IcingaHost)) {
             //skip templates
             if ($this->isTemplate()) {
                 continue;
             }
             $contacts = $this->splitComma($value);
             foreach ($contacts as $contact) {
                 $all_contacts[] = $contact;
             }
             //TODO recursive lookup in templates
             printf("//Found contacts attribute: %s on object %s\n", $value, $object);
             /*
             $lookup = $this->collectObjectAttributeRecursive($object, 'contacts');
             
             if (count($lookup) > 0) {
                 foreach($lookup as $l_contact) {
                     preg_replace('/^\+\w+/', '', $l_contact); //drop additive
                     $all_contacts[] = $l_contact;
                 }
             }
             */
             continue;
         }
         if ($key == "contact_groups" && ($object instanceof IcingaService || $object instanceof IcingaHost)) {
             //skip templates
             if ($this->isTemplate()) {
                 continue;
             }
             $contactgroups = $this->splitComma($value);
             foreach ($contactgroups as $contactgroup) {
                 $all_contactgroups[] = $contactgroup;
             }
             printf("//Found contact_groups attribute: %s on object %s\n", $value, $object);
             //TODO recursive lookup in templates
             /*
             $lookup = $this->collectObjectAttributeRecursive($object, 'contactgroups');
             
             if (count($lookup) > 0) {
                 foreach($lookup as $l_contactgroup) {
                     preg_replace('/^\+\w+/', '', $l_contactgroup); //drop additive
                     $all_contactgroups[] = $l_contactgroup;
                 }
             }
             */
             continue;
         }
         //conversion of different attributes
         $func = 'convert' . ucfirst($key);
         if (method_exists($this, $func)) {
             $this->{$func}($value);
             continue;
         }
         if ($object->getDefinitionType() == "timeperiod") {
             if (preg_match('/^\\d+/', $key)) {
                 print_r("//ERROR: Timeperiod 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(sprintf('Cannot convert the "%s" property of given v1 object: ', $key) . print_r($object, 1));
         }
         //migrate
         $value = $this->migrateValue($value, $key);
         if ($value !== null) {
             $this->{$this->v1AttributeMap[$key]} = $value;
         }
     }
     //---- LOOP ATTRIBUTES END
     //---- NOTIFICATIONS BEGIN
     if ($object instanceof IcingaService || $object instanceof IcingaHost) {
         //var_dump($all_contactgroups);
         //var_dump($all_contacts);
         //contacts -> notifications
         $all_contactgroups = array_unique($all_contactgroups);
         if (count($all_contactgroups) > 0) {
             printf("//Found contact_groups: %s\n", $this->arrayToString($all_contactgroups));
         }
         //fetch all contactgroup members as string
         foreach ($all_contactgroups as $contactgroup) {
             trim($contactgroup);
             $contact_objs = $config->getObjectsByAttributeValue('contactgroups', $contactgroup, 'contact');
             //gosh i love these attributes. contact->contactgroups, but host->contact_groups - WTF?!?
             //no contacts with 'contactgroups' attribute, try the group members instead.
             if (count($contact_objs) == 0) {
                 $contactgroup_obj = $config->getObject($contactgroup, 'contactgroup');
                 $contact_objs = $this->splitComma($contactgroup_obj->members);
             }
             foreach ($contact_objs as $contact_obj) {
                 $all_contacts[] = (string) $contact_obj;
             }
         }
         //make sure that only unique contacts exist, we do not need too much duplicated notifications
         $all_contacts = array_unique($all_contacts);
         if (count($all_contacts) > 0) {
             printf("//Found contacts: %s\n", $this->arrayToString($all_contacts));
         }
         foreach ($all_contacts as $contact) {
             //strip additive character
             $name = preg_replace('/^\\+/', '', $contact);
             trim($name);
             $contact_obj = $config->GetObject($name, 'contact');
             if ($contact_obj == false) {
                 print "//ERROR: Unknown contact '" . $name . "' referenced by object '" . $object . "'.";
                 continue;
             }
             //HOST NOTIFICATIONS
             if ($object instanceof IcingaHost) {
                 $host_notification_commands = $this->splitComma($contact_obj->host_notification_commands);
                 foreach ($host_notification_commands as $host_notification_command) {
                     trim($host_notification_command);
                     //get the command line
                     $host_notification_command_line = $config->getObject($host_notification_command, 'command');
                     //generate a unique notification command name
                     $host_notification_command_name = "host-" . (string) $object . "-notification-command-" . $host_notification_command;
                     //create a new notification command
                     $this->notificationcommands($host_notification_command_name, $host_notification_command_line);
                     $notification_filter = $this->migrateNotificationOptions($object->notification_options, true);
                     //create a new host notification object
                     $notification_object_name = "notification-" . $host_notification_command_name;
                     $notification_object_attr = array('users' => array($contact), 'import' => 'generic-host-notification', 'period' => $object->notification_period, 'interval' => $object->notification_interval, 'states' => $notification_filter['state'], 'types' => $notification_filter['type'], 'command' => $host_notification_command_name, 'host_name' => (string) $object);
                     $this->notifications($notification_object_name, $notification_object_attr);
                 }
             }
             //SERVICE NOTIFICATION
             if ($object instanceof IcingaService) {
                 $service_notification_commands = $this->splitComma($contact_obj->service_notification_commands);
                 foreach ($service_notification_commands as $service_notification_command) {
                     trim($service_notification_command);
                     if ($object->host_name) {
                         $prefix = "host-" . $object->host_name;
                     } else {
                         //some random madness
                         $prefix = substr(str_shuffle(md5(time())), 0, 10);
                         //length 10 is sufficient
                     }
                     //get the command line
                     $service_notification_command_line = $config->getObject($service_notification_command, 'command');
                     //generate a unique notification command name //TODO that's not really clear for services only, being non unique by their name - get hostname/group?
                     $service_notification_command_name = $prefix . "-service-" . (string) $object . "-notification-command-" . $service_notification_command;
                     //create a new notification command
                     $this->notificationcommands($service_notification_command_name, $service_notification_command_line);
                     //create a new host notification object
                     $notification_object_name = "notification-" . $service_notification_command_name;
                     $notification_filter = $this->migrateNotificationOptions($object->notification_options, false);
                     $notification_object_attr = array('users' => array($contact), 'import' => 'generic-service-notification', 'period' => $object->notification_period, 'interval' => $object->notification_interval, 'states' => $notification_filter['state'], 'types' => $notification_filter['type'], 'command' => $service_notification_command_name);
                     if ($object->host_name) {
                         $notification_object_attr['host_name'] = $object->host_name;
                         $notification_object_attr['service_name'] = (string) $object;
                     } else {
                         //if there is no direct relation, we need to specify the service.name as additional assign rule
                         $notification_object_attr['service_assign'] = (string) $object;
                     }
                     $this->notifications($notification_object_name, $notification_object_attr);
                 }
             }
         }
     }
     //make sure we have unique notification commands (and also notifications, even if not that accurate TODO)
     $this->notificationcommands = array_unique($this->notificationcommands);
     $this->notifications = array_unique($this->notifications);
     //---- NOTIFICATIONS END
     //---- HOST PARENTS BEGIN
     if ($object instanceof IcingaHost) {
         $all_parents = array_unique($all_parents);
         if (count($all_parents) > 0) {
             printf("//Found parents: %s\n", $this->arrayToString($all_parents));
         }
         foreach ($all_parents as $parent) {
             trim($parent);
             $host_obj = $config->GetObject($parent, 'host');
             if ($host_obj == false) {
                 print "//ERROR: Unknown parent host '" . $parent . "' referenced by object '" . $object . "'.";
                 continue;
             }
             $dep_name = "child-" . $object . "-parent-" . $parent . "-host-dependency";
             $dep_attrs = array('child_host_name' => (string) $object, 'parent_host_name' => $parent);
             $this->dependencies($dep_name, $dep_attrs);
         }
     }
     //---- HOST PARENTS END
     //itl required imports
     if ($object->getDefinitionType() == "timeperiod") {
         $this->imports("legacy-timeperiod");
     }
     if ($object->getDefinitionType() == "command") {
         $this->imports("plugin-check-command");
     }
     //custom vars
     foreach ($object->getCustomVars() as $key => $value) {
         $key = substr($key, 1);
         //drop _
         $this->vars($key, $value);
     }
 }
Ejemplo n.º 2
0
 public function v1Action()
 {
     $start = microtime(true);
     printf("//---------------------------------------------------\n");
     printf("//Migrate Icinga 1.x configuration to Icinga 2 format\n");
     printf("//Start time: " . date("Y-m-d H:i:s") . "\n");
     printf("//---------------------------------------------------\n");
     //parse 1.x objects
     $configfile = $this->params->shift();
     $config = IcingaConfig::parse($configfile);
     //dump default templates for new objects
     Icinga2ObjectDefinition::dumpDefaultTemplates();
     //migrate all objects to 2.x
     printf("//MIGRATE COMMANDS -- BEGIN\n");
     foreach ($config->getDefinitions('command') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE COMMANDS -- END\n");
     printf("//MIGRATE HOSTS -- BEGIN\n");
     foreach ($config->getDefinitions('host') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
         //direct host->service relation
         if (count($object->getServices()) > 0) {
             printf("//---- MIGRATE HOST SERVICES -- BEGIN\n");
             foreach ($object->getServices() as $service) {
                 $service->host_name = $object;
                 //force relation
                 Icinga2ObjectDefinition::fromIcingaObjectDefinition($service, $config)->dump();
             }
             printf("//---- MIGRATE HOST SERVICES -- END\n");
         }
     }
     printf("//MIGRATE HOSTS -- END\n");
     printf("//MIGRATE SERVICE -- BEGIN\n");
     //TODO only templates should dumped?
     foreach ($config->getDefinitions('service') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE SERVICE -- END\n");
     printf("//MIGRATE CONTACTS (USERS) -- BEGIN\n");
     foreach ($config->getDefinitions('contact') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE CONTACTS (USERS) -- END\n");
     printf("//MIGRATE HOSTGROUPS -- BEGIN\n");
     foreach ($config->getDefinitions('hostgroup') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
         //indirect hostgroup->service relation
         if (count($object->getServices()) > 0) {
             printf("//---- MIGRATE HOSTGROUP SERVICES -- BEGIN\n");
             foreach ($object->getServices() as $service) {
                 $service->hostgroup_name = $object;
                 //force relation
                 Icinga2ObjectDefinition::fromIcingaObjectDefinition($service, $config)->dump();
             }
             printf("//---- MIGRATE HOSTGROUP SERVICES -- END\n");
         }
     }
     printf("//MIGRATE HOSTGROUPS -- END\n");
     printf("//MIGRATE SERVICEGROUPS -- BEGIN\n");
     foreach ($config->getDefinitions('servicegroup') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE SERVICEGROUPS -- END\n");
     printf("//MIGRATE CONTACTGROUPS (USERGROUPS) -- BEGIN\n");
     foreach ($config->getDefinitions('contactgroup') as $object) {
         // TODO: Find a better way than hardcoded
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE CONTACTGROUPS (USERGROUPS) -- END\n");
     printf("//MIGRATE TIMEPERIODS -- BEGIN\n");
     foreach ($config->getDefinitions('timeperiod') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE TIMEPERIODS -- END\n");
     printf("//MIGRATE HOST DEPENDENCIES -- BEGIN\n");
     foreach ($config->getDefinitions('hostdependency') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE HOST DEPENDENCIES -- END\n");
     printf("//MIGRATE SERVICE DEPENDENCIES -- BEGIN\n");
     foreach ($config->getDefinitions('servicedependency') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE SERVICE DEPENDENCIES -- END\n");
     printf("//MIGRATE HOST ESCALATION -- BEGIN\n");
     foreach ($config->getDefinitions('hostescalation') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE HOST ESCALATION -- END\n");
     printf("//MIGRATE SERVICE ESCALATION -- BEGIN\n");
     foreach ($config->getDefinitions('serviceescalation') as $object) {
         Icinga2ObjectDefinition::fromIcingaObjectDefinition($object, $config)->dump();
     }
     printf("//MIGRATE SERVICE ESCALATION -- END\n");
     $end = microtime(true);
     $runtime = $end - $start;
     printf("//---------------------------------------------------\n");
     printf("//FINISHED :-)\n");
     printf("//End time: " . date("Y-m-d H:i:s") . "\n");
     printf("//Runtime: " . (double) $runtime . "\n");
     printf("//---------------------------------------------------\n");
 }