Esempio n. 1
0
 public static function parseMap($rules)
 {
     $importMaps = self::XMLtoArray(self::$xml);
     if (!isset($importMaps['zabbix_export'])) {
         $importMaps['zabbix_export'] = $importMaps;
     }
     if (CWebUser::$data['type'] == USER_TYPE_SUPER_ADMIN && isset($importMaps['zabbix_export']['images'])) {
         $images = $importMaps['zabbix_export']['images'];
         $images_to_add = array();
         $images_to_update = array();
         foreach ($images as $image) {
             if (API::Image()->exists($image)) {
                 if ($image['imagetype'] == IMAGE_TYPE_ICON && !empty($rules['images']['updateExisting']) || $image['imagetype'] == IMAGE_TYPE_BACKGROUND && !empty($rules['images']['updateExisting'])) {
                     $options = array('filter' => array('name' => $image['name']), 'output' => array('imageid'));
                     $imgs = API::Image()->get($options);
                     $img = reset($imgs);
                     $image['imageid'] = $img['imageid'];
                     // image will be decoded in class.image.php
                     $image['image'] = $image['encodedImage'];
                     unset($image['encodedImage']);
                     $images_to_update[] = $image;
                 }
             } else {
                 if ($image['imagetype'] == IMAGE_TYPE_ICON && !empty($rules['images']['createMissing']) || $image['imagetype'] == IMAGE_TYPE_BACKGROUND && !empty($rules['images']['createMissing'])) {
                     // No need to decode_base64
                     $image['image'] = $image['encodedImage'];
                     unset($image['encodedImage']);
                     $images_to_add[] = $image;
                 }
             }
         }
         if (!empty($images_to_add)) {
             $result = API::Image()->create($images_to_add);
             if (!$result) {
                 throw new Exception(_('Cannot add image.'));
             }
         }
         if (!empty($images_to_update)) {
             $result = API::Image()->update($images_to_update);
             if (!$result) {
                 throw new Exception(_('Cannot update image.'));
             }
         }
     }
     if (!isset($importMaps['zabbix_export']['sysmaps'])) {
         return true;
     }
     $importMaps = $importMaps['zabbix_export']['sysmaps'];
     foreach ($importMaps as $mnum => &$sysmap) {
         unset($sysmap['sysmapid']);
         $exists = API::Map()->exists(array('name' => $sysmap['name']));
         if (!isset($sysmap['label_format'])) {
             $sysmap['label_format'] = SYSMAP_LABEL_ADVANCED_OFF;
         }
         if ($exists && !empty($rules['maps']['updateExisting'])) {
             $db_maps = API::Map()->getObjects(array('name' => $sysmap['name']));
             if (empty($db_maps)) {
                 throw new Exception(_s('No permissions for map "%1$s".', $sysmap['name']));
             }
             $db_map = reset($db_maps);
             $sysmap['sysmapid'] = $db_map['sysmapid'];
         } else {
             if ($exists || empty($rules['maps']['createMissing'])) {
                 info(_s('Map "%1$s" skipped - user rule.', $sysmap['name']));
                 unset($importMaps[$mnum]);
                 continue;
                 // break if not update updateExisting
             }
         }
         if (isset($sysmap['backgroundid'])) {
             $image = getImageByIdent($sysmap['backgroundid']);
             if (!$image) {
                 error(_s('Cannot find background image "%1$s" used in map "%2$s".', $sysmap['backgroundid']['name'], $sysmap['name']));
                 $sysmap['backgroundid'] = 0;
             } else {
                 $sysmap['backgroundid'] = $image['imageid'];
             }
         } else {
             $sysmap['backgroundid'] = 0;
         }
         if (!isset($sysmap['selements'])) {
             $sysmap['selements'] = array();
         } else {
             $sysmap['selements'] = array_values($sysmap['selements']);
         }
         if (!isset($sysmap['links'])) {
             $sysmap['links'] = array();
         } else {
             $sysmap['links'] = array_values($sysmap['links']);
         }
         foreach ($sysmap['selements'] as &$selement) {
             $nodeCaption = isset($selement['elementid']['node']) ? $selement['elementid']['node'] . ':' : '';
             if (!isset($selement['elementid'])) {
                 $selement['elementid'] = 0;
             }
             switch ($selement['elementtype']) {
                 case SYSMAP_ELEMENT_TYPE_MAP:
                     $db_sysmaps = API::Map()->getObjects($selement['elementid']);
                     if (empty($db_sysmaps)) {
                         $error = _s('Cannot find map "%1$s" used in exported map "%2$s".', $nodeCaption . $selement['elementid']['name'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_sysmaps);
                     $selement['elementid'] = $tmp['sysmapid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
                     $db_hostgroups = API::HostGroup()->getObjects($selement['elementid']);
                     if (empty($db_hostgroups)) {
                         $error = _s('Cannot find group "%1$s" used in map "%2$s".', $nodeCaption . $selement['elementid']['name'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_hostgroups);
                     $selement['elementid'] = $tmp['groupid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_HOST:
                     $db_hosts = API::Host()->getObjects($selement['elementid']);
                     if (empty($db_hosts)) {
                         $error = _s('Cannot find host "%1$s" used in map "%2$s".', $nodeCaption . $selement['elementid']['host'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_hosts);
                     $selement['elementid'] = $tmp['hostid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_TRIGGER:
                     $db_triggers = API::Trigger()->getObjects($selement['elementid']);
                     if (empty($db_triggers)) {
                         $error = _s('Cannot find trigger "%1$s" used in map "%2$s".', $nodeCaption . $selement['elementid']['host'] . ':' . $selement['elementid']['description'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_triggers);
                     $selement['elementid'] = $tmp['triggerid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_IMAGE:
                 default:
             }
             $icons = array('iconid_off', 'iconid_on', 'iconid_disabled', 'iconid_maintenance');
             foreach ($icons as $icon) {
                 if (isset($selement[$icon])) {
                     $image = getImageByIdent($selement[$icon]);
                     if (!$image) {
                         $error = _s('Cannot find icon "%1$s" used in map "%2$s".', $selement[$icon]['name'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $selement[$icon] = $image['imageid'];
                 } else {
                     $selement[$icon] = 0;
                 }
             }
         }
         unset($selement);
         foreach ($sysmap['links'] as &$link) {
             if (!isset($link['linktriggers'])) {
                 continue;
             }
             foreach ($link['linktriggers'] as &$linktrigger) {
                 $db_triggers = API::Trigger()->getObjects($linktrigger['triggerid']);
                 if (empty($db_triggers)) {
                     $nodeCaption = isset($linktrigger['triggerid']['node']) ? $linktrigger['triggerid']['node'] . ':' : '';
                     $error = _s('Cannot find trigger "%1$s" used in map "%2$s".', $nodeCaption . $linktrigger['triggerid']['host'] . ':' . $linktrigger['triggerid']['description'], $sysmap['name']);
                     throw new Exception($error);
                 }
                 $tmp = reset($db_triggers);
                 $linktrigger['triggerid'] = $tmp['triggerid'];
             }
             unset($linktrigger);
         }
         unset($link);
     }
     unset($sysmap);
     foreach ($importMaps as $importMap) {
         if (isset($importMap['sysmapid'])) {
             $result = API::Map()->update($importMap);
             if ($result === false) {
                 throw new Exception(_s('Cannot update map "%s".', $importMap['name']));
             } else {
                 info(_s('Map "%s" updated.', $importMap['name']));
             }
         } else {
             $result = API::Map()->create($importMap);
             if ($result === false) {
                 throw new Exception(_s('Cannot create map "%s".', $importMap['name']));
             } else {
                 info(_s('Map "%s" created.', $importMap['name']));
             }
         }
     }
     return true;
 }
Esempio n. 2
0
 /**
  * Resolves the iconmap and background images for the maps.
  *
  * @throws Exception if icon map or background image is not found.
  *
  * @param array $maps
  *
  * @return array
  */
 protected function resolveMapElementReferences(array $maps)
 {
     foreach ($maps as &$map) {
         if ($map['iconmap']) {
             $map['iconmapid'] = $this->referencer->resolveIconMap($map['iconmap']['name']);
             if (!$map['iconmapid']) {
                 throw new Exception(_s('Cannot find icon map "%1$s" used in map "%2$s".', $map['iconmap']['name'], $map['name']));
             }
         }
         if ($map['background']) {
             $image = getImageByIdent($map['background']);
             if (!$image) {
                 throw new Exception(_s('Cannot find background image "%1$s" used in map "%2$s".', $map['background']['name'], $map['name']));
             }
             $map['backgroundid'] = $image['imageid'];
         }
     }
     unset($map);
     return $maps;
 }
Esempio n. 3
0
 public static function parseMap($rules)
 {
     $importMaps = self::XMLtoArray(self::$xml);
     if (!isset($importMaps['zabbix_export'])) {
         $importMaps['zabbix_export'] = $importMaps;
     }
     if (CWebUser::$data['type'] == USER_TYPE_SUPER_ADMIN && isset($importMaps['zabbix_export']['images'])) {
         $allImages = $importMaps['zabbix_export']['images'];
         $allImages = zbx_toHash($allImages, 'name');
         $dbImages = API::Image()->get(array('output' => array('imageid', 'name'), 'filter' => array('name' => zbx_objectValues($allImages, 'name'))));
         $dbImages = zbx_toHash($dbImages, 'name');
         $imagesToCreate = array();
         $imagesToUpdate = array();
         foreach ($allImages as $imageName => $image) {
             if (isset($dbImages[$imageName])) {
                 $image['imageid'] = $dbImages[$imageName]['imageid'];
                 $image['image'] = $image['encodedImage'];
                 unset($image['encodedImage'], $image['imagetype']);
                 $imagesToUpdate[] = $image;
             } else {
                 $image['image'] = $image['encodedImage'];
                 unset($image['encodedImage']);
                 $imagesToCreate[] = $image;
             }
         }
         if ($rules['images']['createMissing'] && $imagesToCreate) {
             API::Image()->create($imagesToCreate);
         }
         if ($rules['images']['updateExisting'] && $imagesToUpdate) {
             API::Image()->update($imagesToUpdate);
         }
     }
     if (!isset($importMaps['zabbix_export']['sysmaps'])) {
         return true;
     }
     $importMaps = $importMaps['zabbix_export']['sysmaps'];
     foreach ($importMaps as $mnum => &$sysmap) {
         unset($sysmap['sysmapid']);
         if (!isset($sysmap['label_format'])) {
             $sysmap['label_format'] = SYSMAP_LABEL_ADVANCED_OFF;
         }
         $mapExists = API::Map()->get(array('output' => array('sysmapid'), 'filter' => array('name' => $sysmap['name']), 'nopermissions' => true, 'limit' => 1));
         if ($mapExists && $rules['maps']['updateExisting']) {
             $db_maps = API::Map()->get(array('filter' => array('name' => $sysmap['name']), 'output' => array('sysmapid')));
             if (empty($db_maps)) {
                 throw new Exception(_s('No permissions for map "%1$s".', $sysmap['name']));
             }
             $db_map = reset($db_maps);
             $sysmap['sysmapid'] = $db_map['sysmapid'];
         } elseif ($mapExists || !$rules['maps']['createMissing']) {
             info(_s('Map "%1$s" skipped - user rule.', $sysmap['name']));
             unset($importMaps[$mnum]);
             continue;
         }
         if (isset($sysmap['backgroundid'])) {
             $image = getImageByIdent($sysmap['backgroundid']);
             if (!$image) {
                 error(_s('Cannot find background image "%1$s" used in map "%2$s".', $sysmap['backgroundid']['name'], $sysmap['name']));
                 $sysmap['backgroundid'] = 0;
             } else {
                 $sysmap['backgroundid'] = $image['imageid'];
             }
         } else {
             $sysmap['backgroundid'] = 0;
         }
         if (!isset($sysmap['selements'])) {
             $sysmap['selements'] = array();
         } else {
             $sysmap['selements'] = array_values($sysmap['selements']);
         }
         if (!isset($sysmap['links'])) {
             $sysmap['links'] = array();
         } else {
             $sysmap['links'] = array_values($sysmap['links']);
         }
         foreach ($sysmap['selements'] as &$selement) {
             if (!isset($selement['elementid'])) {
                 $selement['elementid'] = 0;
             }
             switch ($selement['elementtype']) {
                 case SYSMAP_ELEMENT_TYPE_MAP:
                     $db_sysmaps = API::Map()->get(array('filter' => array($selement['elementid']), 'output' => array('sysmapid')));
                     if (empty($db_sysmaps)) {
                         $error = _s('Cannot find map "%1$s" used in exported map "%2$s".', $selement['elementid']['name'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_sysmaps);
                     $selement['elementid'] = $tmp['sysmapid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
                     $db_hostgroups = API::HostGroup()->get(array('filter' => array($selement['elementid']), 'output' => array('groupid')));
                     if (empty($db_hostgroups)) {
                         $error = _s('Cannot find group "%1$s" used in map "%2$s".', $selement['elementid']['name'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_hostgroups);
                     $selement['elementid'] = $tmp['groupid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_HOST:
                     $db_hosts = API::Host()->get(array('filter' => array($selement['elementid']), 'output' => array('hostid')));
                     if (empty($db_hosts)) {
                         $error = _s('Cannot find host "%1$s" used in map "%2$s".', $selement['elementid']['host'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_hosts);
                     $selement['elementid'] = $tmp['hostid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_TRIGGER:
                     $db_triggers = API::Trigger()->get(array('filter' => array($selement['elementid']), 'output' => array('triggerid')));
                     if (empty($db_triggers)) {
                         $error = _s('Cannot find trigger "%1$s" used in map "%2$s".', $selement['elementid']['host'] . ':' . $selement['elementid']['description'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $tmp = reset($db_triggers);
                     $selement['elementid'] = $tmp['triggerid'];
                     break;
                 case SYSMAP_ELEMENT_TYPE_IMAGE:
                 default:
             }
             $icons = array('iconid_off', 'iconid_on', 'iconid_disabled', 'iconid_maintenance');
             foreach ($icons as $icon) {
                 if (isset($selement[$icon])) {
                     $image = getImageByIdent($selement[$icon]);
                     if (!$image) {
                         $error = _s('Cannot find icon "%1$s" used in map "%2$s".', $selement[$icon]['name'], $sysmap['name']);
                         throw new Exception($error);
                     }
                     $selement[$icon] = $image['imageid'];
                 } else {
                     $selement[$icon] = 0;
                 }
             }
         }
         unset($selement);
         foreach ($sysmap['links'] as &$link) {
             if (!isset($link['linktriggers'])) {
                 continue;
             }
             foreach ($link['linktriggers'] as &$linktrigger) {
                 $triggerData = $linktrigger['triggerid'];
                 $dbTriggers = API::Trigger()->get(array('output' => array('triggerid', 'expression'), 'filter' => array('host' => $triggerData['host'], 'description' => $triggerData['description']), 'expandExpression' => true));
                 $error = _s('Cannot find trigger "%1$s" used in map "%2$s".', $triggerData['host'] . ':' . $triggerData['description'], $sysmap['name']);
                 if (!$dbTriggers) {
                     throw new Exception($error);
                 }
                 $dbTriggerId = null;
                 foreach ($dbTriggers as $dbTrigger) {
                     if ($dbTrigger['expression'] === $triggerData['expression']) {
                         $dbTriggerId = $dbTrigger['triggerid'];
                         break;
                     }
                 }
                 if (!$dbTriggerId) {
                     throw new Exception($error);
                 }
                 $linktrigger['triggerid'] = $dbTriggerId;
             }
             unset($linktrigger);
         }
         unset($link);
     }
     unset($sysmap);
     foreach ($importMaps as $importMap) {
         if (isset($importMap['sysmapid'])) {
             $result = API::Map()->update($importMap);
             if ($result === false) {
                 throw new Exception(_s('Cannot update map "%s".', $importMap['name']));
             } else {
                 info(_s('Map "%s" updated.', $importMap['name']));
             }
         } else {
             $result = API::Map()->create($importMap);
             if ($result === false) {
                 throw new Exception(_s('Cannot create map "%s".', $importMap['name']));
             } else {
                 info(_s('Map "%s" created.', $importMap['name']));
             }
         }
     }
     return true;
 }
 /**
  * Change all references in map to database ids.
  *
  * @throws Exception
  *
  * @param array $map
  *
  * @return array
  */
 protected function resolveMapReferences(array $map)
 {
     // resolve icon map
     if (!empty($map['iconmap'])) {
         $map['iconmapid'] = $this->referencer->resolveIconMap($map['iconmap']['name']);
         if (!$map['iconmapid']) {
             throw new Exception(_s('Cannot find icon map "%1$s" used in map "%2$s".', $map['iconmap']['name'], $map['name']));
         }
     }
     if (!empty($map['background'])) {
         $image = getImageByIdent($map['background']);
         if (!$image) {
             throw new Exception(_s('Cannot find background image "%1$s" used in map "%2$s".', $map['background']['name'], $map['name']));
         }
         $map['backgroundid'] = $image['imageid'];
     }
     if (isset($map['selements'])) {
         foreach ($map['selements'] as &$selement) {
             switch ($selement['elementtype']) {
                 case SYSMAP_ELEMENT_TYPE_MAP:
                     $selement['elementid'] = $this->referencer->resolveMap($selement['element']['name']);
                     if (!$selement['elementid']) {
                         throw new Exception(_s('Cannot find map "%1$s" used in map "%2$s".', $selement['element']['name'], $map['name']));
                     }
                     break;
                 case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
                     $selement['elementid'] = $this->referencer->resolveGroup($selement['element']['name']);
                     if (!$selement['elementid']) {
                         throw new Exception(_s('Cannot find group "%1$s" used in map "%2$s".', $selement['element']['name'], $map['name']));
                     }
                     break;
                 case SYSMAP_ELEMENT_TYPE_HOST:
                     $selement['elementid'] = $this->referencer->resolveHost($selement['element']['host']);
                     if (!$selement['elementid']) {
                         throw new Exception(_s('Cannot find host "%1$s" used in map "%2$s".', $selement['element']['host'], $map['name']));
                     }
                     break;
                 case SYSMAP_ELEMENT_TYPE_TRIGGER:
                     $el = $selement['element'];
                     $selement['elementid'] = $this->referencer->resolveTrigger($el['description'], $el['expression']);
                     if (!$selement['elementid']) {
                         throw new Exception(_s('Cannot find trigger "%1$s" used in map "%2$s".', $selement['element']['description'], $map['name']));
                     }
                     break;
                 case SYSMAP_ELEMENT_TYPE_IMAGE:
                     $selement['elementid'] = 0;
                     break;
             }
             $icons = array('icon_off' => 'iconid_off', 'icon_on' => 'iconid_on', 'icon_disabled' => 'iconid_disabled', 'icon_maintenance' => 'iconid_maintenance');
             foreach ($icons as $element => $field) {
                 if (!empty($selement[$element])) {
                     $image = getImageByIdent($selement[$element]);
                     if (!$image) {
                         throw new Exception(_s('Cannot find icon "%1$s" used in map "%2$s".', $selement[$element]['name'], $map['name']));
                     }
                     $selement[$field] = $image['imageid'];
                 }
             }
         }
         unset($selement);
     }
     if (isset($map['links'])) {
         foreach ($map['links'] as &$link) {
             if (empty($link['linktriggers'])) {
                 unset($link['linktriggers']);
                 continue;
             }
             foreach ($link['linktriggers'] as &$linktrigger) {
                 $dbTriggers = API::Trigger()->getObjects($linktrigger['trigger']);
                 if (empty($dbTriggers)) {
                     throw new Exception(_s('Cannot find trigger "%1$s" used in map "%2$s".', $linktrigger['trigger']['description'], $map['name']));
                 }
                 $tmp = reset($dbTriggers);
                 $linktrigger['triggerid'] = $tmp['triggerid'];
             }
             unset($linktrigger);
         }
         unset($link);
     }
     return $map;
 }
Esempio n. 5
0
 public static function parseMap($rules)
 {
     global $USER_DETAILS;
     $importMaps = self::XMLtoArray(self::$xml);
     if (!isset($importMaps['zabbix_export'])) {
         $importMaps['zabbix_export'] = $importMaps;
     }
     try {
         if ($USER_DETAILS['type'] == USER_TYPE_SUPER_ADMIN) {
             $images = $importMaps['zabbix_export']['images'];
             $images_to_add = array();
             $images_to_update = array();
             foreach ($images as $inum => $image) {
                 if (CImage::exists($image)) {
                     if ($image['imagetype'] == IMAGE_TYPE_ICON && isset($rules['icons']['exist']) || $image['imagetype'] == IMAGE_TYPE_BACKGROUND && isset($rules['background']['exist'])) {
                         $options = array('filter' => array('name' => $image['name']), 'output' => API_OUTPUT_SHORTEN);
                         $imgs = CImage::get($options);
                         $img = reset($imgs);
                         $image['imageid'] = $img['imageid'];
                         // image will be decoded in class.image.php
                         $image['image'] = $image['encodedImage'];
                         unset($image['encodedImage']);
                         $images_to_update[] = $image;
                     }
                 } else {
                     if ($image['imagetype'] == IMAGE_TYPE_ICON && isset($rules['icons']['missed']) || $image['imagetype'] == IMAGE_TYPE_BACKGROUND && isset($rules['background']['missed'])) {
                         // No need to decode_base64
                         $image['image'] = $image['encodedImage'];
                         unset($image['encodedImage']);
                         $images_to_add[] = $image;
                     }
                 }
             }
             //sdi($images_to_add);
             if (!empty($images_to_add)) {
                 $result = CImage::create($images_to_add);
                 if (!$result) {
                     throw new Exception(S_CANNOT_ADD_IMAGE);
                 }
             }
             //sdi($images_to_update);
             if (!empty($images_to_update)) {
                 $result = CImage::update($images_to_update);
                 if (!$result) {
                     throw new Exception(S_CANNOT_UPDATE_IMAGE);
                 }
             }
         }
         $importMaps = $importMaps['zabbix_export']['sysmaps'];
         $sysmaps = array();
         foreach ($importMaps as $mnum => &$sysmap) {
             unset($sysmap['sysmapid']);
             $exists = CMap::exists(array('name' => $sysmap['name']));
             if ($exists && isset($rules['maps']['exist'])) {
                 $db_maps = CMap::getObjects(array('name' => $sysmap['name']));
                 if (empty($db_maps)) {
                     throw new Exception(S_NO_PERMISSIONS_FOR_MAP . ' [' . $sysmap['name'] . '] import');
                 }
                 $db_map = reset($db_maps);
                 $sysmap['sysmapid'] = $db_map['sysmapid'];
             } else {
                 if ($exists || !isset($rules['maps']['missed'])) {
                     info('Map [' . $sysmap['name'] . '] skipped - user rule');
                     unset($importMaps[$mnum]);
                     continue;
                     // break if not update exist
                 }
             }
             if (isset($sysmap['backgroundid'])) {
                 $image = getImageByIdent($sysmap['backgroundid']);
                 if (!$image) {
                     error(S_CANNOT_FIND_BACKGROUND_IMAGE . ' "' . $sysmap['backgroundid']['name'] . '" ' . S_USED_IN_EXPORTED_MAP_SMALL . ' "' . $sysmap['name'] . '"');
                     $sysmap['backgroundid'] = 0;
                 } else {
                     $sysmap['backgroundid'] = $image['imageid'];
                 }
             } else {
                 $sysmap['backgroundid'] = 0;
             }
             if (!isset($sysmap['selements'])) {
                 $sysmap['selements'] = array();
             }
             if (!isset($sysmap['links'])) {
                 $sysmap['links'] = array();
             }
             foreach ($sysmap['selements'] as $snum => &$selement) {
                 $nodeCaption = isset($selement['elementid']['node']) ? $selement['elementid']['node'] . ':' : '';
                 if (!isset($selement['elementid'])) {
                     $selement['elementid'] = 0;
                 }
                 switch ($selement['elementtype']) {
                     case SYSMAP_ELEMENT_TYPE_MAP:
                         $db_sysmaps = CMap::getObjects($selement['elementid']);
                         if (empty($db_sysmaps)) {
                             $error = S_CANNOT_FIND_MAP . ' "' . $nodeCaption . $selement['elementid']['name'] . '" ' . S_USED_IN_EXPORTED_MAP_SMALL . ' "' . $sysmap['name'] . '"';
                             throw new Exception($error);
                         }
                         $tmp = reset($db_sysmaps);
                         $selement['elementid'] = $tmp['sysmapid'];
                         break;
                     case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
                         $db_hostgroups = CHostgroup::getObjects($selement['elementid']);
                         if (empty($db_hostgroups)) {
                             $error = S_CANNOT_FIND_HOSTGROUP . ' "' . $nodeCaption . $selement['elementid']['name'] . '" ' . S_USED_IN_EXPORTED_MAP_SMALL . ' "' . $sysmap['name'] . '"';
                             throw new Exception($error);
                         }
                         $tmp = reset($db_hostgroups);
                         $selement['elementid'] = $tmp['groupid'];
                         break;
                     case SYSMAP_ELEMENT_TYPE_HOST:
                         $db_hosts = CHost::getObjects($selement['elementid']);
                         if (empty($db_hosts)) {
                             $error = S_CANNOT_FIND_HOST . ' "' . $nodeCaption . $selement['elementid']['host'] . '" ' . S_USED_IN_EXPORTED_MAP_SMALL . ' "' . $sysmap['name'] . '"';
                             throw new Exception($error);
                         }
                         $tmp = reset($db_hosts);
                         $selement['elementid'] = $tmp['hostid'];
                         break;
                     case SYSMAP_ELEMENT_TYPE_TRIGGER:
                         $db_triggers = CTrigger::getObjects($selement['elementid']);
                         if (empty($db_triggers)) {
                             $error = S_CANNOT_FIND_TRIGGER . ' "' . $nodeCaption . $selement['elementid']['host'] . ':' . $selement['elementid']['description'] . '" ' . S_USED_IN_EXPORTED_MAP_SMALL . ' "' . $sysmap['name'] . '"';
                             throw new Exception($error);
                         }
                         $tmp = reset($db_triggers);
                         $selement['elementid'] = $tmp['triggerid'];
                         break;
                     case SYSMAP_ELEMENT_TYPE_IMAGE:
                     default:
                         break;
                 }
                 $icons = array('iconid_off', 'iconid_on', 'iconid_unknown', 'iconid_disabled', 'iconid_maintenance');
                 foreach ($icons as $icon) {
                     if (isset($selement[$icon])) {
                         $image = getImageByIdent($selement[$icon]);
                         if (!$image) {
                             $error = S_CANNOT_FIND_IMAGE . ' "' . $selement[$icon]['name'] . '" ' . S_USED_IN_EXPORTED_MAP_SMALL . ' "' . $sysmap['name'] . '"';
                             throw new Exception($error);
                         }
                         $selement[$icon] = $image['imageid'];
                     } else {
                         $selement[$icon] = 0;
                     }
                 }
             }
             unset($selement);
             foreach ($sysmap['links'] as $lnum => &$link) {
                 if (!isset($link['linktriggers'])) {
                     continue;
                 }
                 foreach ($link['linktriggers'] as $ltnum => &$linktrigger) {
                     $nodeCaption = isset($linktrigger['triggerid']['node']) ? $linktrigger['triggerid']['node'] . ':' : '';
                     $db_triggers = CTrigger::getObjects($linktrigger['triggerid']);
                     if (empty($db_triggers)) {
                         $error = S_CANNOT_FIND_TRIGGER . ' "' . $nodeCaption . $linktrigger['triggerid']['host'] . ':' . $linktrigger['triggerid']['description'] . '" ' . S_USED_IN_EXPORTED_MAP_SMALL . ' "' . $sysmap['name'] . '"';
                         throw new Exception($error);
                     }
                     $tmp = reset($db_triggers);
                     $linktrigger['triggerid'] = $tmp['triggerid'];
                 }
                 unset($linktrigger);
             }
             unset($link);
             $sysmaps[] = $sysmap;
         }
         unset($sysmap);
         $importMaps = $sysmaps;
         foreach ($importMaps as $mnum => $importMap) {
             $sysmap = $importMap;
             if (isset($importMap['sysmapid'])) {
                 $result = CMap::update($importMap);
                 $sysmapids = $result['sysmapids'];
                 // Deleteing all selements (with links)
                 $db_selementids = array();
                 $res = DBselect('SELECT selementid FROM sysmaps_elements WHERE sysmapid=' . $sysmap['sysmapid']);
                 while ($db_selement = DBfetch($res)) {
                     $db_selementids[$db_selement['selementid']] = $db_selement['selementid'];
                 }
                 delete_sysmaps_element($db_selementids);
                 //----
             } else {
                 // first we must create an empty map without any elements (they will be added below)
                 $mapToCreate = $importMap;
                 $mapToCreate['selements'] = array();
                 $mapToCreate['links'] = array();
                 $result = CMap::create($mapToCreate);
                 $sysmapids = $result['sysmapids'];
                 $sysmap['sysmapid'] = reset($sysmapids);
             }
             // adding elements and links
             $selements = $importMap['selements'];
             $links = $importMap['links'];
             foreach ($selements as $id => $selement) {
                 if (!isset($selement['elementid']) || $selement['elementid'] == 0) {
                     $selement['elementid'] = 0;
                     $selement['elementtype'] = SYSMAP_ELEMENT_TYPE_IMAGE;
                 }
                 if (!isset($selement['iconid_off']) || $selement['iconid_off'] == 0) {
                     throw new Exception(S_NO_ICON_FOR_MAP_ELEMENT . ' ' . $sysmap['name'] . ':' . $selement['label']);
                 }
                 $selement['sysmapid'] = $sysmap['sysmapid'];
                 $selementids = CMap::addElements($selement);
                 $selementid = reset($selementids);
                 foreach ($links as $id => &$link) {
                     if ($link['selementid1'] == $selement['selementid']) {
                         $links[$id]['selementid1'] = $selementid;
                     } else {
                         if ($link['selementid2'] == $selement['selementid']) {
                             $links[$id]['selementid2'] = $selementid;
                         }
                     }
                 }
                 unset($link);
             }
             foreach ($links as $id => $link) {
                 if (!isset($link['linktriggers'])) {
                     $link['linktriggers'] = array();
                 }
                 $link['sysmapid'] = $sysmap['sysmapid'];
                 $result = CMap::addLinks($link);
             }
             if (isset($importMap['sysmapid'])) {
                 info(S_MAP . ' [' . $sysmap['name'] . '] ' . S_UPDATED_SMALL);
             } else {
                 info(S_MAP . ' [' . $sysmap['name'] . '] ' . S_ADDED_SMALL);
             }
         }
         return true;
     } catch (Exception $e) {
         error($e->getMessage());
         return false;
     }
 }