function getImageByIdent($ident) { zbx_value2array($ident); if (!isset($ident['name'])) { return 0; } static $images; if (is_null($images)) { $images = array(); $dbImages = API::Image()->get(array('output' => array('imageid', 'name'), 'nodeids' => get_current_nodeid(true))); foreach ($dbImages as $image) { if (!isset($images[$image['name']])) { $images[$image['name']] = array(); } $nodeName = get_node_name_by_elid($image['imageid'], true); if (!is_null($nodeName)) { $images[$image['name']][$nodeName] = $image; } else { $images[$image['name']][] = $image; } } } $ident['name'] = trim($ident['name'], ' '); if (!isset($images[$ident['name']])) { return 0; } $searchedImages = $images[$ident['name']]; if (!isset($ident['node'])) { return reset($searchedImages); } elseif (isset($searchedImages[$ident['node']])) { return $searchedImages[$ident['node']]; } else { return 0; } }
function getImageByIdent($ident) { zbx_value2array($ident); if (!isset($ident['name'])) { return 0; } static $images; if ($images === null) { $images = array(); $dbImages = API::Image()->get(array('output' => array('imageid', 'name'))); foreach ($dbImages as $image) { if (!isset($images[$image['name']])) { $images[$image['name']] = array(); } $images[$image['name']][] = $image; } } $ident['name'] = trim($ident['name'], ' '); return isset($images[$ident['name']]) ? reset($images[$ident['name']]) : 0; }
/** * Checks icon maps. * @throws APIException * @param $iconMaps * @param bool $mustExist if icon map should be checked against having at least one mapping * @return void */ protected function validateMappings($iconMaps, $mustExist = true) { $inventoryFields = getHostInventories(); $imageids = API::Image()->get(['output' => ['imageid'], 'preservekeys' => true, 'filter' => ['imagetype' => IMAGE_TYPE_ICON]]); foreach ($iconMaps as $iconMap) { if (isset($iconMap['mappings']) && empty($iconMap['mappings'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" must have at least one mapping.', $iconMap['name'])); } elseif (!isset($iconMap['mappings'])) { if ($mustExist) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" must have at least one mapping.', $iconMap['name'])); } else { continue; } } $uniqField = []; foreach ($iconMap['mappings'] as $mapping) { if (!isset($mapping['expression'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "expression" is missing in icon mapping.')); } elseif (!isset($mapping['inventory_link'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "inventory_link" is missing in icon mapping.')); } elseif (!isset($mapping['iconid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "iconid" is missing in icon mapping.')); } elseif (!isset($inventoryFields[$mapping['inventory_link']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%1$s" has mapping with incorrect inventory link "%2$s".', $iconMap['name'], $mapping['inventory_link'])); } elseif (!isset($imageids[$mapping['iconid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%1$s" has mapping with incorrect iconid "%2$s".', $iconMap['name'], $mapping['iconid'])); } try { CGlobalRegexp::isValid($mapping['expression']); } catch (Exception $e) { switch ($e->getCode()) { case CGlobalRegexp::ERROR_REGEXP_EMPTY: self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" cannot have mapping with empty expression.', $iconMap['name'])); break; case CGlobalRegexp::ERROR_REGEXP_NOT_EXISTS: self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" cannot have mapping with global expression that does not exist.', $iconMap['name'])); break; default: self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" has incorrect expression.', $iconMap['name'])); } } if (isset($uniqField[$mapping['inventory_link'] . $mapping['expression']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon mapping entry "%1$s" against "%2$s" already exists.', $mapping['expression'], $inventoryFields[$mapping['inventory_link']]['title'])); } $uniqField[$mapping['inventory_link'] . $mapping['expression']] = true; } } }
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; }
} $imageWidget = new CWidget(); $imageWidget->addPageHeader(_('CONFIGURATION OF IMAGES'), $form); $data = array('form' => get_request('form'), 'displayNodes' => is_array(get_current_nodeid()), 'widget' => &$imageWidget); if (!empty($data['form'])) { if (isset($_REQUEST['imageid'])) { $data['imageid'] = $_REQUEST['imageid']; $data['imagename'] = $dbImage['name']; $data['imagetype'] = $dbImage['imagetype']; } else { $data['imageid'] = null; $data['imagename'] = get_request('name', ''); $data['imagetype'] = get_request('imagetype', 1); } $imageForm = new CView('administration.general.image.edit', $data); } else { $data['imagetype'] = get_request('imagetype', IMAGE_TYPE_ICON); $data['images'] = API::Image()->get(array('filter' => array('imagetype' => $data['imagetype']), 'output' => array('imageid', 'imagetype', 'name'))); order_result($data['images'], 'name'); // nodes if ($data['displayNodes']) { foreach ($data['images'] as &$image) { $image['nodename'] = get_node_name_by_elid($image['imageid'], true) . NAME_DELIMITER; } unset($image); } $imageForm = new CView('administration.general.image.list', $data); } $imageWidget->addItem($imageForm->render()); $imageWidget->show(); require_once dirname(__FILE__) . '/include/page_footer.php';
/** * Get images references by image ids. * * @param array $imageIds * * @return array */ protected function getImagesReferences(array $imageIds) { $idents = array(); $images = API::Image()->get(array('imageids' => $imageIds, 'output' => API_OUTPUT_EXTEND, 'nodeids' => get_current_nodeid(true), 'preservekeys' => true)); foreach ($images as $id => $image) { $idents[$id] = array('name' => $image['name']); } return $idents; }
} /* * Display */ $generalComboBox = new CComboBox('configDropDown', 'adm.iconmapping.php', 'redirect(this.options[this.selectedIndex].value);'); $generalComboBox->addItems(array('adm.gui.php' => _('GUI'), 'adm.housekeeper.php' => _('Housekeeping'), 'adm.images.php' => _('Images'), 'adm.iconmapping.php' => _('Icon mapping'), 'adm.regexps.php' => _('Regular expressions'), 'adm.macros.php' => _('Macros'), 'adm.valuemapping.php' => _('Value mapping'), 'adm.workingtime.php' => _('Working time'), 'adm.triggerseverities.php' => _('Trigger severities'), 'adm.triggerdisplayoptions.php' => _('Trigger displaying options'), 'adm.other.php' => _('Other'))); $iconMapForm = new CForm(); $iconMapForm->cleanItems(); $iconMapForm->addItem($generalComboBox); if (!isset($_REQUEST['form'])) { $iconMapForm->addItem(new CSubmit('form', _('Create icon map'))); } $iconMapWidget = new CWidget(); $iconMapWidget->addPageHeader(_('CONFIGURATION OF ICON MAPPING'), $iconMapForm); $data = array('form_refresh' => get_request('form_refresh', 0), 'iconmapid' => get_request('iconmapid'), 'iconList' => array(), 'inventoryList' => array(), 'displayNodes' => is_array(get_current_nodeid())); $iconList = API::Image()->get(array('filter' => array('imagetype' => IMAGE_TYPE_ICON), 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true)); order_result($iconList, 'name'); foreach ($iconList as $icon) { $data['iconList'][$icon['imageid']] = $icon['name']; } $inventoryFields = getHostInventories(); foreach ($inventoryFields as $field) { $data['inventoryList'][$field['nr']] = $field['title']; } if (isset($_REQUEST['form'])) { if ($data['form_refresh'] || $_REQUEST['form'] === 'clone') { $data['iconmap'] = get_request('iconmap'); } elseif (isset($_REQUEST['iconmapid'])) { $data['iconmap'] = reset($iconMap); } else { $firstIcon = reset($iconList);
if (isset($_REQUEST['form'])) { if (!isset($_REQUEST['sysmapid']) || isset($_REQUEST['form_refresh'])) { $data = array('sysmap' => array('sysmapid' => getRequest('sysmapid'), 'name' => get_request('name', ''), 'width' => get_request('width', 800), 'height' => get_request('height', 600), 'backgroundid' => get_request('backgroundid', 0), 'iconmapid' => get_request('iconmapid', 0), 'label_format' => get_request('label_format', 0), 'label_type_host' => get_request('label_type_host', 2), 'label_type_hostgroup' => get_request('label_type_hostgroup', 2), 'label_type_trigger' => get_request('label_type_trigger', 2), 'label_type_map' => get_request('label_type_map', 2), 'label_type_image' => get_request('label_type_image', 2), 'label_string_host' => get_request('label_string_host', ''), 'label_string_hostgroup' => get_request('label_string_hostgroup', ''), 'label_string_trigger' => get_request('label_string_trigger', ''), 'label_string_map' => get_request('label_string_map', ''), 'label_string_image' => get_request('label_string_image', ''), 'label_type' => get_request('label_type', 0), 'label_location' => get_request('label_location', 0), 'highlight' => get_request('highlight', 0), 'markelements' => get_request('markelements', 0), 'expandproblem' => get_request('expandproblem', 0), 'show_unack' => get_request('show_unack', 0), 'severity_min' => get_request('severity_min', TRIGGER_SEVERITY_NOT_CLASSIFIED), 'urls' => get_request('urls', array()))); } else { $data = array('sysmap' => $sysmap); } // config $data['config'] = select_config(); // advanced labels $data['labelTypes'] = sysmapElementLabel(); $data['labelTypesLimited'] = $data['labelTypes']; unset($data['labelTypesLimited'][MAP_LABEL_TYPE_IP]); $data['labelTypesImage'] = $data['labelTypesLimited']; unset($data['labelTypesImage'][MAP_LABEL_TYPE_STATUS]); // images $data['images'] = API::Image()->get(array('output' => array('imageid', 'name'), 'filter' => array('imagetype' => IMAGE_TYPE_BACKGROUND))); order_result($data['images'], 'name'); foreach ($data['images'] as $num => $image) { $data['images'][$num]['name'] = get_node_name_by_elid($image['imageid'], null, NAME_DELIMITER) . $image['name']; } // icon maps $data['iconMaps'] = API::IconMap()->get(array('output' => array('iconmapid', 'name'), 'preservekeys' => true)); order_result($data['iconMaps'], 'name'); // render view $mapView = new CView('configuration.sysmap.edit', $data); $mapView->render(); $mapView->show(); } else { $data = array(); // get maps $sortField = getPageSortField('name');
require_once dirname(__FILE__) . '/include/maps.inc.php'; $page['file'] = 'imgstore.php'; $page['type'] = detect_page_type(PAGE_TYPE_IMAGE); require_once dirname(__FILE__) . '/include/page_header.php'; // VAR TYPE OPTIONAL FLAGS VALIDATION EXCEPTION $fields = array('css' => array(T_ZBX_INT, O_OPT, P_SYS, null, null), 'imageid' => array(T_ZBX_STR, O_OPT, P_SYS, null, null), 'iconid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, null), 'width' => array(T_ZBX_INT, O_OPT, P_SYS, BETWEEN(1, 2000), null), 'height' => array(T_ZBX_INT, O_OPT, P_SYS, BETWEEN(1, 2000), null)); check_fields($fields); $resize = false; if (isset($_REQUEST['width']) || isset($_REQUEST['height'])) { $resize = true; $width = get_request('width', 0); $height = get_request('height', 0); } if (isset($_REQUEST['css'])) { $css = 'div.sysmap_iconid_0 {' . ' height: 50px;' . ' width: 50px;' . ' background-image: url("images/general/no_icon.png"); }' . "\n"; $images = API::Image()->get(array('filter' => array('imagetype' => IMAGE_TYPE_ICON), 'output' => API_OUTPUT_EXTEND, 'select_image' => 1)); foreach ($images as $image) { $image['image'] = base64_decode($image['image']); $ico = imagecreatefromstring($image['image']); if ($resize) { $ico = imageThumb($ico, $width, $height); } $w = imagesx($ico); $h = imagesy($ico); $css .= 'div.sysmap_iconid_' . $image['imageid'] . '{' . ' height: ' . $h . 'px;' . ' width: ' . $w . 'px;' . ' background: url("imgstore.php?iconid=' . $image['imageid'] . '&width=' . $w . '&height=' . $h . '") no-repeat center center;}' . "\n"; } echo $css; } elseif (isset($_REQUEST['iconid'])) { $iconid = get_request('iconid', 0); if ($iconid > 0) { $image = get_image_by_imageid($iconid);
$data['sysmap'] = ['sysmapid' => getRequest('sysmapid'), 'name' => getRequest('name', ''), 'width' => getRequest('width', 800), 'height' => getRequest('height', 600), 'backgroundid' => getRequest('backgroundid', 0), 'iconmapid' => getRequest('iconmapid', 0), 'label_format' => getRequest('label_format', 0), 'label_type_host' => getRequest('label_type_host', 2), 'label_type_hostgroup' => getRequest('label_type_hostgroup', 2), 'label_type_trigger' => getRequest('label_type_trigger', 2), 'label_type_map' => getRequest('label_type_map', 2), 'label_type_image' => getRequest('label_type_image', 2), 'label_string_host' => getRequest('label_string_host', ''), 'label_string_hostgroup' => getRequest('label_string_hostgroup', ''), 'label_string_trigger' => getRequest('label_string_trigger', ''), 'label_string_map' => getRequest('label_string_map', ''), 'label_string_image' => getRequest('label_string_image', ''), 'label_type' => getRequest('label_type', 0), 'label_location' => getRequest('label_location', 0), 'highlight' => getRequest('highlight', 0), 'markelements' => getRequest('markelements', 0), 'expandproblem' => getRequest('expandproblem', 0), 'show_unack' => getRequest('show_unack', 0), 'severity_min' => getRequest('severity_min', TRIGGER_SEVERITY_NOT_CLASSIFIED), 'urls' => getRequest('urls', []), 'userid' => getRequest('userid', hasRequest('form_refresh') ? '' : $current_userid), 'private' => getRequest('private', PRIVATE_SHARING), 'users' => getRequest('users', []), 'userGroups' => getRequest('userGroups', [])]; } else { $data['sysmap'] = $sysmap; } $data['current_user_userid'] = $current_userid; $data['form_refresh'] = getRequest('form_refresh'); // config $data['config'] = select_config(); // advanced labels $data['labelTypes'] = sysmapElementLabel(); $data['labelTypesLimited'] = $data['labelTypes']; unset($data['labelTypesLimited'][MAP_LABEL_TYPE_IP]); $data['labelTypesImage'] = $data['labelTypesLimited']; unset($data['labelTypesImage'][MAP_LABEL_TYPE_STATUS]); // images $data['images'] = API::Image()->get(['output' => ['imageid', 'name'], 'filter' => ['imagetype' => IMAGE_TYPE_BACKGROUND]]); order_result($data['images'], 'name'); // icon maps $data['iconMaps'] = API::IconMap()->get(['output' => ['iconmapid', 'name'], 'preservekeys' => true]); order_result($data['iconMaps'], 'name'); // render view $mapView = new CView('monitoring.sysmap.edit', $data); $mapView->render(); $mapView->show(); } else { CProfile::delete('web.maps.sysmapid'); $sortField = getRequest('sort', CProfile::get('web.' . $page['file'] . '.sort', 'name')); $sortOrder = getRequest('sortorder', CProfile::get('web.' . $page['file'] . '.sortorder', ZBX_SORT_UP)); CProfile::update('web.' . $page['file'] . '.sort', $sortField, PROFILE_TYPE_STR); CProfile::update('web.' . $page['file'] . '.sortorder', $sortOrder, PROFILE_TYPE_STR); if (hasRequest('filter_set')) {
/** * Import images. * * @throws Exception */ protected function processImages() { $allImages = $this->getFormattedImages(); if (empty($allImages)) { return; } $imagesToUpdate = array(); $allImages = zbx_toHash($allImages, 'name'); $dbImages = DBselect('SELECT i.imageid,i.name FROM images i WHERE ' . dbConditionString('i.name', array_keys($allImages))); while ($dbImage = DBfetch($dbImages)) { $dbImage['image'] = $allImages[$dbImage['name']]['image']; $imagesToUpdate[] = $dbImage; unset($allImages[$dbImage['name']]); } if ($this->options['images']['createMissing']) { API::Image()->create(array_values($allImages)); } if ($this->options['images']['updateExisting']) { API::Image()->update($imagesToUpdate); } }
/** * Get images references by image ids. * * @param array $imageIds * * @return array */ protected function getImagesReferences(array $imageIds) { $ids = array(); $images = API::Image()->get(array('output' => array('imageid', 'name'), 'imageids' => $imageIds, 'preservekeys' => true)); foreach ($images as $id => $image) { $ids[$id] = array('name' => $image['name']); } return $ids; }
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; }
} /* * Display */ $generalComboBox = new CComboBox('configDropDown', 'adm.iconmapping.php', 'redirect(this.options[this.selectedIndex].value);'); $generalComboBox->addItems(array('adm.gui.php' => _('GUI'), 'adm.housekeeper.php' => _('Housekeeping'), 'adm.images.php' => _('Images'), 'adm.iconmapping.php' => _('Icon mapping'), 'adm.regexps.php' => _('Regular expressions'), 'adm.macros.php' => _('Macros'), 'adm.valuemapping.php' => _('Value mapping'), 'adm.workingtime.php' => _('Working time'), 'adm.triggerseverities.php' => _('Trigger severities'), 'adm.triggerdisplayoptions.php' => _('Trigger displaying options'), 'adm.other.php' => _('Other'))); $iconMapForm = new CForm(); $iconMapForm->cleanItems(); $iconMapForm->addItem($generalComboBox); if (!isset($_REQUEST['form'])) { $iconMapForm->addItem(new CSubmit('form', _('Create icon map'))); } $iconMapWidget = new CWidget(); $iconMapWidget->addPageHeader(_('CONFIGURATION OF ICON MAPPING'), $iconMapForm); $data = array('form_refresh' => get_request('form_refresh', 0), 'iconmapid' => get_request('iconmapid'), 'iconList' => array(), 'inventoryList' => array(), 'displayNodes' => is_array(get_current_nodeid())); $iconList = API::Image()->get(array('output' => array('imageid', 'name'), 'filter' => array('imagetype' => IMAGE_TYPE_ICON), 'preservekeys' => true)); order_result($iconList, 'name'); foreach ($iconList as $icon) { $data['iconList'][$icon['imageid']] = $icon['name']; } $inventoryFields = getHostInventories(); foreach ($inventoryFields as $field) { $data['inventoryList'][$field['nr']] = $field['title']; } if (isset($_REQUEST['form'])) { if ($data['form_refresh'] || $_REQUEST['form'] === 'clone') { $data['iconmap'] = get_request('iconmap'); } elseif (isset($_REQUEST['iconmapid'])) { $data['iconmap'] = reset($iconMap); } else { $firstIcon = reset($iconList);
} } elseif (hasRequest('delete')) { $result = API::IconMap()->delete([getRequest('iconmapid')]); if ($result) { unset($_REQUEST['form']); } show_messages($result, _('Icon map deleted'), _('Cannot delete icon map')); } elseif (isset($_REQUEST['clone'])) { unset($_REQUEST['iconmapid']); $_REQUEST['form'] = 'clone'; } /* * Display */ $data = ['iconmapid' => getRequest('iconmapid'), 'iconList' => [], 'inventoryList' => []]; $iconList = API::Image()->get(['output' => ['imageid', 'name'], 'filter' => ['imagetype' => IMAGE_TYPE_ICON], 'preservekeys' => true]); order_result($iconList, 'name'); foreach ($iconList as $icon) { $data['iconList'][$icon['imageid']] = $icon['name']; } $inventoryFields = getHostInventories(); foreach ($inventoryFields as $field) { $data['inventoryList'][$field['nr']] = $field['title']; } if (isset($_REQUEST['form'])) { if (hasRequest('form_refresh') || $_REQUEST['form'] === 'clone') { $data['iconmap'] = getRequest('iconmap'); } elseif (isset($_REQUEST['iconmapid'])) { $data['iconmap'] = reset($iconMap); } else { $firstIcon = reset($iconList);
require_once dirname(__FILE__) . '/include/maps.inc.php'; $page['file'] = 'imgstore.php'; $page['type'] = detect_page_type(PAGE_TYPE_IMAGE); require_once dirname(__FILE__) . '/include/page_header.php'; // VAR TYPE OPTIONAL FLAGS VALIDATION EXCEPTION $fields = ['css' => [T_ZBX_INT, O_OPT, P_SYS, null, null], 'imageid' => [T_ZBX_STR, O_OPT, P_SYS, null, null], 'iconid' => [T_ZBX_INT, O_OPT, P_SYS, DB_ID, null], 'width' => [T_ZBX_INT, O_OPT, P_SYS, BETWEEN(1, 2000), null], 'height' => [T_ZBX_INT, O_OPT, P_SYS, BETWEEN(1, 2000), null]]; check_fields($fields); $resize = false; if (isset($_REQUEST['width']) || isset($_REQUEST['height'])) { $resize = true; $width = getRequest('width', 0); $height = getRequest('height', 0); } if (isset($_REQUEST['css'])) { $css = 'div.sysmap_iconid_0 {' . ' height: 50px;' . ' width: 50px;' . ' background-image: url("images/general/no_icon.png"); }' . "\n"; $images = API::Image()->get(['output' => ['imageid'], 'filter' => ['imagetype' => IMAGE_TYPE_ICON], 'select_image' => true]); foreach ($images as $image) { $image['image'] = base64_decode($image['image']); $ico = imagecreatefromstring($image['image']); if ($resize) { $ico = imageThumb($ico, $width, $height); } $w = imagesx($ico); $h = imagesy($ico); $css .= 'div.sysmap_iconid_' . $image['imageid'] . '{' . ' height: ' . $h . 'px;' . ' width: ' . $w . 'px;' . ' background: url("imgstore.php?iconid=' . $image['imageid'] . '&width=' . $w . '&height=' . $h . '") no-repeat center center;}' . "\n"; } echo $css; } elseif (isset($_REQUEST['iconid'])) { $iconid = getRequest('iconid', 0); if ($iconid > 0) { $image = get_image_by_imageid($iconid);
if ($result) { add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_IMAGE, 'Image [' . $image['name'] . '] deleted'); unset($_REQUEST['form'], $image, $_REQUEST['imageid']); } $result = DBend($result); show_messages($result, _('Image deleted'), _('Cannot delete image')); } /* * Display */ $data = ['form' => getRequest('form')]; if (!empty($data['form'])) { if (isset($_REQUEST['imageid'])) { $data['imageid'] = $_REQUEST['imageid']; $data['imagename'] = $dbImage['name']; $data['imagetype'] = $dbImage['imagetype']; } else { $data['imageid'] = null; $data['imagename'] = getRequest('name', ''); $data['imagetype'] = getRequest('imagetype', IMAGE_TYPE_ICON); } $view = new CView('administration.general.image.edit', $data); } else { $data['imagetype'] = getRequest('imagetype', IMAGE_TYPE_ICON); $data['images'] = API::Image()->get(['filter' => ['imagetype' => $data['imagetype']], 'output' => ['imageid', 'imagetype', 'name']]); order_result($data['images'], 'name'); $view = new CView('administration.general.image.list', $data); } $view->render(); $view->show(); require_once dirname(__FILE__) . '/include/page_footer.php';
$cmbConf = new CComboBox('configDropDown', 'adm.images.php', 'redirect(this.options[this.selectedIndex].value);'); $cmbConf->addItems(array('adm.gui.php' => _('GUI'), 'adm.housekeeper.php' => _('Housekeeper'), 'adm.images.php' => _('Images'), 'adm.iconmapping.php' => _('Icon mapping'), 'adm.regexps.php' => _('Regular expressions'), 'adm.macros.php' => _('Macros'), 'adm.valuemapping.php' => _('Value mapping'), 'adm.workingtime.php' => _('Working time'), 'adm.triggerseverities.php' => _('Trigger severities'), 'adm.triggerdisplayoptions.php' => _('Trigger displaying options'), 'adm.other.php' => _('Other'))); $form->addItem($cmbConf); if (!isset($_REQUEST['form'])) { $form->addItem(new CSubmit('form', _('Create image'))); } $cnf_wdgt = new CWidget(); $cnf_wdgt->addPageHeader(_('CONFIGURATION OF IMAGES'), $form); $data = array(); $data['form'] = get_request('form'); $data['widget'] =& $cnf_wdgt; if (!empty($data['form'])) { if (isset($_REQUEST['imageid'])) { $data['imageid'] = $_REQUEST['imageid']; $data['imagename'] = $dbImage['name']; $data['imagetype'] = $dbImage['imagetype']; } else { $data['imageid'] = null; $data['imagename'] = get_request('name', ''); $data['imagetype'] = get_request('imagetype', 1); } $imageForm = new CView('administration.general.image.edit', $data); } else { $data['imagetype'] = get_request('imagetype', IMAGE_TYPE_ICON); $options = array('filter' => array('imagetype' => $data['imagetype']), 'output' => API_OUTPUT_EXTEND, 'sortfield' => 'name'); $data['images'] = API::Image()->get($options); $imageForm = new CView('administration.general.image.list', $data); } $cnf_wdgt->addItem($imageForm->render()); $cnf_wdgt->show(); require_once dirname(__FILE__) . '/include/page_footer.php';
/** * Import images. * * @throws Exception * * @return null */ protected function processImages() { if (CWebUser::$data['type'] != USER_TYPE_SUPER_ADMIN || !$this->options['images']['updateExisting'] && !$this->options['images']['createMissing']) { return; } $allImages = $this->getFormattedImages(); if (!$allImages) { return; } $allImages = zbx_toHash($allImages, 'name'); $dbImages = API::Image()->get(array('output' => array('imageid', 'name'), 'filter' => array('name' => array_keys($allImages)))); $dbImages = zbx_toHash($dbImages, 'name'); $imagesToUpdate = array(); $imagesToCreate = array(); foreach ($allImages as $imageName => $image) { if (isset($dbImages[$imageName])) { $image['imageid'] = $dbImages[$imageName]['imageid']; unset($image['imagetype']); $imagesToUpdate[] = $image; } else { $imagesToCreate[] = $image; } } if ($this->options['images']['createMissing'] && $imagesToCreate) { API::Image()->create($imagesToCreate); } if ($this->options['images']['updateExisting'] && $imagesToUpdate) { API::Image()->update($imagesToUpdate); } }
/** * Get images references by image ids. * * @param array $imageIds * * @return array */ protected function getImagesReferences(array $imageIds) { $ids = []; $images = API::Image()->get(['output' => ['imageid', 'name'], 'imageids' => $imageIds, 'preservekeys' => true]); foreach ($images as $id => $image) { $ids[$id] = ['name' => $image['name']]; } return $ids; }
require_once dirname(__FILE__) . '/include/maps.inc.php'; $page['file'] = 'imgstore.php'; $page['type'] = detect_page_type(PAGE_TYPE_IMAGE); require_once dirname(__FILE__) . '/include/page_header.php'; // VAR TYPE OPTIONAL FLAGS VALIDATION EXCEPTION $fields = array('css' => array(T_ZBX_INT, O_OPT, P_SYS, null, null), 'imageid' => array(T_ZBX_STR, O_OPT, P_SYS, null, null), 'iconid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, null), 'width' => array(T_ZBX_INT, O_OPT, P_SYS, BETWEEN(1, 2000), null), 'height' => array(T_ZBX_INT, O_OPT, P_SYS, BETWEEN(1, 2000), null)); check_fields($fields); $resize = false; if (isset($_REQUEST['width']) || isset($_REQUEST['height'])) { $resize = true; $width = getRequest('width', 0); $height = getRequest('height', 0); } if (isset($_REQUEST['css'])) { $css = 'div.sysmap_iconid_0 {' . ' height: 50px;' . ' width: 50px;' . ' background-image: url("images/general/no_icon.png"); }' . "\n"; $images = API::Image()->get(array('output' => array('imageid'), 'filter' => array('imagetype' => IMAGE_TYPE_ICON), 'select_image' => true)); foreach ($images as $image) { $image['image'] = base64_decode($image['image']); $ico = imagecreatefromstring($image['image']); if ($resize) { $ico = imageThumb($ico, $width, $height); } $w = imagesx($ico); $h = imagesy($ico); $css .= 'div.sysmap_iconid_' . $image['imageid'] . '{' . ' height: ' . $h . 'px;' . ' width: ' . $w . 'px;' . ' background: url("imgstore.php?iconid=' . $image['imageid'] . '&width=' . $w . '&height=' . $h . '") no-repeat center center;}' . "\n"; } echo $css; } elseif (isset($_REQUEST['iconid'])) { $iconid = getRequest('iconid', 0); if ($iconid > 0) { $image = get_image_by_imageid($iconid);
$frmSysmap = new CForm(); $frmSysmap->setName('map.edit.php'); $frmSysmap->addVar('form', get_request('form', 1)); $frmSysmap->addVar('form_refresh', get_request('form_refresh', 0) + 1); if (isset($this->data['sysmapid'])) { $frmSysmap->addVar('sysmapid', $this->data['sysmapid']); } // create sysmap $sysmapList = new CFormList('sysmaplist'); $sysmapList->addRow(_('Name'), new CTextBox('name', $this->data['name'], ZBX_TEXTBOX_STANDARD_SIZE)); $sysmapList->addRow(_('Width'), new CNumericBox('width', $this->data['width'], 5)); $sysmapList->addRow(_('Height'), new CNumericBox('height', $this->data['height'], 5)); // append background image $cmbImg = new CComboBox('backgroundid', $this->data['backgroundid']); $cmbImg->addItem(0, _('No image')); $images = API::Image()->get(array('filter' => array('imagetype' => 2), 'output' => API_OUTPUT_EXTEND)); order_result($images, 'name'); foreach ($images as $image) { $cmbImg->addItem($image['imageid'], get_node_name_by_elid($image['imageid'], null, ': ') . $image['name']); } $sysmapList->addRow(_('Background image'), $cmbImg); // append iconmapping to sysmap $iconMappingComboBox = new CComboBox('iconmapid', $this->data['iconmapid']); $iconMappingComboBox->addItem(0, _('<manual>')); $iconMaps = API::IconMap()->get(array('output' => array('iconmapid', 'name'), 'preservekeys' => true)); order_result($iconMaps, 'name'); foreach ($iconMaps as $iconMap) { $iconMappingComboBox->addItem($iconMap['iconmapid'], $iconMap['name']); } $iconMappingsLink = new CLink(_('show icon mappings'), 'adm.iconmapping.php'); $iconMappingsLink->setAttribute('target', '_blank');