Пример #1
0
 /**
  * Import configuration data.
  *
  * @todo   for 1.8 version import old class CXmlImport18 is used
  *
  * @throws Exception
  * @throws UnexpectedValueException
  * @return bool
  */
 public function import()
 {
     if (empty($this->reader)) {
         throw new UnexpectedValueException('Reader is not set.');
     }
     try {
         // hack to make api throw exceptions
         // this made to not check all api calls results for false return
         czbxrpc::$useExceptions = true;
         DBstart();
         $this->data = $this->reader->read($this->source);
         $version = $this->getImportVersion();
         // if import version is 1.8 we use old class that support it.
         // old import class process hosts, maps and screens separately.
         if ($version == '1.8') {
             CXmlImport18::import($this->source);
             if ($this->options['maps']['updateExisting'] || $this->options['maps']['createMissing']) {
                 CXmlImport18::parseMap($this->options);
             }
             if ($this->options['screens']['updateExisting'] || $this->options['screens']['createMissing']) {
                 CXmlImport18::parseScreen($this->options);
             }
             if ($this->options['hosts']['updateExisting'] || $this->options['hosts']['createMissing'] || $this->options['templates']['updateExisting'] || $this->options['templates']['createMissing']) {
                 CXmlImport18::parseMain($this->options);
             }
         } else {
             $this->formatter = $this->getFormatter($version);
             // pass data to formatter
             // export has root key "zabbix_export" which is not passed
             $this->formatter->setData($this->data['zabbix_export']);
             $this->referencer = new CImportReferencer();
             // parse all import for references to resolve them all together with less sql count
             $this->gatherReferences();
             $this->processGroups();
             $this->processTemplates();
             $this->processHosts();
             $this->processApplications();
             $this->processItems();
             $this->processDiscoveryRules();
             $this->processTriggers();
             $this->processGraphs();
             $this->processImages();
             $this->processMaps();
             // screens should be created after all other elements
             $this->processTemplateScreens();
             $this->processScreens();
         }
         // prevent api from throwing exception
         czbxrpc::$useExceptions = false;
         return DBend(true);
     } catch (Exception $e) {
         czbxrpc::$useExceptions = false;
         DBend(false);
         throw new Exception($e->getMessage(), $e->getCode());
     }
 }
Пример #2
0
 public static function import($file)
 {
     libxml_use_internal_errors(true);
     $xml = new DOMDocument();
     if (!$xml->loadXML($file)) {
         $text = '';
         foreach (libxml_get_errors() as $error) {
             switch ($error->level) {
                 case LIBXML_ERR_WARNING:
                     $text .= _('XML file contains errors') . '. Warning ' . $error->code . ': ';
                     break;
                 case LIBXML_ERR_ERROR:
                     $text .= _('XML file contains errors') . '. Error ' . $error->code . ': ';
                     break;
                 case LIBXML_ERR_FATAL:
                     $text .= _('XML file contains errors') . '. Fatal Error ' . $error->code . ': ';
                     break;
             }
             $text .= trim($error->message) . ' [ Line: ' . $error->line . ' | Column: ' . $error->column . ' ]';
             break;
         }
         libxml_clear_errors();
         throw new Exception($text);
     }
     if ($xml->childNodes->item(0)->nodeName != 'zabbix_export') {
         $xml2 = self::createDOMDocument();
         $xml2->appendChild($xml2->ownerDocument->importNode($xml->childNodes->item(0), true));
         self::$xml = $xml2->ownerDocument;
     } else {
         self::$xml = $xml;
     }
     return true;
 }
Пример #3
0
 /**
  * Import configuration data.
  *
  * @todo   for 1.8 version import old class CXmlImport18 is used
  *
  * @throws Exception
  * @throws UnexpectedValueException
  *
  * @return bool
  */
 public function import()
 {
     if (empty($this->reader)) {
         throw new UnexpectedValueException('Reader is not set.');
     }
     $this->data = $this->reader->read($this->source);
     $version = $this->getImportVersion();
     // if import version is 1.8 we use old class that support it.
     // old import class process hosts, maps and screens separately.
     if ($version == '1.8') {
         CXmlImport18::import($this->source);
         if ($this->options['maps']['updateExisting'] || $this->options['maps']['createMissing']) {
             CXmlImport18::parseMap($this->options);
         }
         if ($this->options['screens']['updateExisting'] || $this->options['screens']['createMissing']) {
             CXmlImport18::parseScreen($this->options);
         }
         if ($this->options['hosts']['updateExisting'] || $this->options['hosts']['createMissing'] || $this->options['templates']['updateExisting'] || $this->options['templates']['createMissing']) {
             CXmlImport18::parseMain($this->options);
         }
     } else {
         $this->formatter = $this->getFormatter($version);
         // pass data to formatter
         // export has root key "zabbix_export" which is not passed
         $this->formatter->setData($this->data['zabbix_export']);
         // parse all import for references to resolve them all together with less sql count
         $this->gatherReferences();
         $this->processGroups();
         $this->processTemplates();
         $this->processHosts();
         // delete missing objects from processed hosts and templates
         $this->deleteMissingDiscoveryRules();
         $this->deleteMissingTriggers();
         $this->deleteMissingGraphs();
         $this->deleteMissingItems();
         $this->deleteMissingApplications();
         // import objects
         $this->processApplications();
         $this->processItems();
         $this->processDiscoveryRules();
         $this->processTriggers();
         $this->processGraphs();
         $this->processImages();
         $this->processMaps();
         $this->processTemplateScreens();
         $this->processScreens();
     }
     return true;
 }
Пример #4
0
 public static function import($source)
 {
     libxml_use_internal_errors(true);
     libxml_disable_entity_loader(true);
     $xml = new DOMDocument();
     if (!$xml->loadXML($source, LIBXML_IMPORT_FLAGS)) {
         $text = '';
         foreach (libxml_get_errors() as $error) {
             switch ($error->level) {
                 case LIBXML_ERR_WARNING:
                     $text .= _('XML file contains errors') . '. Warning ' . $error->code . ': ';
                     break;
                 case LIBXML_ERR_ERROR:
                     $text .= _('XML file contains errors') . '. Error ' . $error->code . ': ';
                     break;
                 case LIBXML_ERR_FATAL:
                     $text .= _('XML file contains errors') . '. Fatal Error ' . $error->code . ': ';
                     break;
             }
             $text .= trim($error->message) . ' [ Line: ' . $error->line . ' | Column: ' . $error->column . ' ]';
             break;
         }
         libxml_clear_errors();
         throw new Exception($text);
     }
     self::$xml = $xml;
     return true;
 }