Example #1
0
 public function import()
 {
     $job = $this->getEngine()->getJob();
     $config = $this->getEngine()->getConfig();
     $segment = $this->getSegment();
     $values = $segment->getValues();
     $fileName = $segment->getFilename();
     // We need to determine if we are a template
     $isTemplate = false;
     if (isset($values['name'])) {
         $obj = new NagiosHostTemplate();
         $isTemplate = true;
     } else {
         $obj = new NagiosHost();
         $isTemplate = false;
     }
     foreach ($values as $key => &$entries) {
         if ($key == "check_command") {
             foreach ($entries as $entry) {
                 if (empty($newEntry)) {
                     $newEntry = $entry;
                 } else {
                     $newEntry['value'] .= "," . $entry['value'];
                 }
             }
             $entries = array($newEntry);
         }
     }
     foreach ($values as $key => $entries) {
         foreach ($entries as $entry) {
             // Skips
             $value = $entry['value'];
             $lineNum = $entry['line'];
             if ($key == 'register') {
                 continue;
             }
             if ($key == 'use') {
                 // We need to add a template inheritance
                 $obj->addTemplateInheritance($entry['value']);
                 continue;
             }
             if ($key == 'check_command') {
                 $options = explode("!", $entry['value']);
                 // Okay, first we add the check command parameter.
                 $obj->setCheckCommandByName($options[0]);
                 if (count($options > 0)) {
                     for ($counter = 1; $counter < count($options); $counter++) {
                         $obj->addCheckCommandParameter($options[$counter]);
                     }
                 }
                 continue;
             }
             if ($key == 'flap_detection_options') {
                 $options = explode(",", $entry['value']);
                 foreach ($options as $option) {
                     switch (strtolower(trim($option))) {
                         case 'o':
                             $obj->setFlapDetectionOnUp(true);
                             break;
                         case 'd':
                             $obj->setFlapDetectionOnDown(true);
                             break;
                         case 'u':
                             $obj->setFlapDetectionOnUnreachable(true);
                             break;
                     }
                 }
                 continue;
             }
             if ($key == 'stalking_options') {
                 $options = explode(",", $entry['value']);
                 foreach ($options as $option) {
                     switch (strtolower(trim($option))) {
                         case 'o':
                             $obj->setStalkingOnUp(true);
                             break;
                         case 'd':
                             $obj->setStalkingOnDown(true);
                             break;
                         case 'u':
                             $obj->setStalkingOnUnreachable(true);
                             break;
                     }
                 }
                 continue;
             }
             if ($key == 'notification_options') {
                 $options = explode(",", $entry['value']);
                 foreach ($options as $option) {
                     switch (strtolower(trim($option))) {
                         case 'd':
                             $obj->setNotificationOnDown(true);
                             break;
                         case 'u':
                             $obj->setNotificationOnUnreachable(true);
                             break;
                         case 'r':
                             $obj->setNotificationOnRecovery(true);
                             break;
                         case 'f':
                             $obj->setNotificationOnFlapping(true);
                             break;
                         case 's':
                             $obj->setNotificationOnScheduledDowntime(true);
                             break;
                     }
                 }
                 continue;
             }
             // Okay, let's check that the method DOES exist
             if (!method_exists($obj, $this->fieldMethods[$key])) {
                 $job->addError("Method " . $this->fieldMethods[$key] . " does not exist for variable: " . $key . " on line " . $lineNum . " in file " . $fileName);
                 if (!$config->getVar('continue_error')) {
                     return false;
                 }
             } else {
                 call_user_method($this->fieldMethods[$key], $obj, $value);
             }
         }
     }
     $obj->save();
     $obj->clearAllReferences(true);
     if ($isTemplate) {
         $job->addNotice("NagiosHostImporter finished importing host template: " . $obj->getName());
     } else {
         $job->addNotice("NagiosHostImporter finished importing host: " . $obj->getName());
     }
     return true;
 }