Beispiel #1
0
 private function parse_object_file($fileName, $importJob)
 {
     $fp = @fopen($fileName, 'r');
     $config = unserialize($importJob->getConfig());
     if (!$fp) {
         $importJob->addLogEntry("Failed to open object file: " . $fileName, ImportLogEntry::TYPE_ERROR);
         if (!$config->getVar('continue_error')) {
             return false;
         }
     }
     $lineNumber = 0;
     while ($line = fgets($fp)) {
         $lineNumber++;
         $line = trim($line);
         if (preg_match('/^\\s*(|#.*)$/', $line)) {
             // This is a comment
             continue;
         }
         // Need to merge lines that have a \ at the end
         if (preg_match('/\\\\$/', $line)) {
             // We need to merge, so remove the last character of the line,
             // then merge with next
             $newLine = substr($line, 0, strlen($line) - 2);
             do {
                 $line = fgets($fp);
                 $line = trim($line);
                 $newLine .= $line;
                 if (preg_match('/\\\\$/', $newLine)) {
                     // Chop off the \ again
                     $newLine = substr($newLine, 0, strlen($newLine) - 2);
                 }
             } while (preg_match('/\\\\$/', $line));
             $line = $newLine;
         }
         if (preg_match('/^\\s*define\\s+(\\S+)\\s*{\\s*$/', $line, $regs)) {
             // Setup object name
             $objectName = $regs[1];
             $segment = new NagiosImportFileSegment($fileName);
             continue;
         }
         if (preg_match('/\\s*(\\S+)\\s+([^#;]+)/', $line, $regs)) {
             if ($regs[1] != ";") {
                 // Check for a blank line (this is ugly, should fix the regex)
                 // See if the line has a \ on the end
                 $values = explode(',', $regs[2]);
                 foreach ($values as $val) {
                     if (trim($val) != "") {
                         $segment->add($lineNumber, trim($regs[1]), trim($val), $line);
                     }
                 }
             }
             continue;
         }
         if (preg_match('/^\\s*}/', $line)) {
             //Completed object End curley bracket must be on it's own line
             switch ($objectName) {
                 case 'contactgroup':
                     $importer = new NagiosContactGroupImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'contact':
                     $importer = new NagiosContactImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'host':
                     $importer = new NagiosHostImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'hostgroup':
                     $importer = new NagiosHostGroupImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'timeperiod':
                     $importer = new NagiosTimeperiodImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'command':
                     $importer = new NagiosCommandImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'service':
                     $importer = new NagiosServiceImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'servicegroup':
                     $importer = new NagiosServiceGroupImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'hostextinfo':
                     $importer = new NagiosHostExtInfoImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'serviceextinfo':
                     $importer = new NagiosServiceExtInfoImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'hostdependency':
                     $importer = new NagiosHostDependencyImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'servicedependency':
                     $importer = new NagiosServiceDependencyImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'hostescalation':
                     $importer = new NagiosHostEscalationImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
                 case 'serviceescalation':
                     $importer = new NagiosServiceEscalationImporter($this, $segment);
                     if (!$importer->init()) {
                         return false;
                     }
                     if (!$importer->valid()) {
                         $this->addQueuedImporter($importer);
                     } else {
                         if (!$importer->import()) {
                             return false;
                         }
                     }
                     break;
             }
             // switch
             $objectName = '';
             $importLines = array();
             continue;
         }
     }
     return true;
 }
 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
     if (isset($values['name'])) {
         // We are a template
         $job->addNotice("Saving internal service ext info template: " . $values['name'][0]['value']);
         NagiosServiceExtInfoImporter::saveTemplate($values['name'][0]['value'], $segment);
         return true;
     }
     // Get our host obj
     if (isset($values['use'])) {
         // We sure are using a template!
         // Okay, hokey multi-inheritance support for the importer
         $tempValues = $this->getTemplateValues($values['use'][0]['value']);
         // Okay, go through each
         foreach ($tempValues as $key => $val) {
             if (!isset($values[$key])) {
                 $values[$key] = $val;
             }
         }
     }
     $service = NagiosServicePeer::getByHostAndDescription($values['host_name'][0]['value'], $values['service_description'][0]['value']);
     foreach ($values as $key => $entries) {
         foreach ($entries as $entry) {
             // Skips
             $value = $entry['value'];
             $lineNum = $entry['line'];
             if ($key == 'use' || $key == 'name' || $key == 'register' || $key == 'host_name' || $key == 'service_description') {
                 continue;
             }
             // Okay, let's check that the method DOES exist
             if (!method_exists($service, $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], $service, $value);
             }
         }
     }
     $service->save();
     $service->clearAllReferences(true);
     $job->addNotice("NagiosServiceExtInfo finished importing extended info for: " . $service->getNagiosHost()->getName() . ":" . $service->getDescription());
     return true;
 }