/** * 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(array('output' => API_OUTPUT_SHORTEN, 'preservekeys' => true, 'filter' => array('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 = array(); 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 { GlobalRegExp::isValid($mapping['expression']); } catch (Exception $e) { switch ($e->getCode()) { case GlobalRegExp::ERROR_REGEXP_EMPTY: self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" cannot have mapping with empty expression.', $iconMap['name'])); break; case GlobalRegExp::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; } } }
$page['type'] = detect_page_type(); require_once dirname(__FILE__) . '/include/page_header.php'; // VAR TYPE OPTIONAL FLAGS VALIDATION EXCEPTION $fields = array('regexpids' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, null), 'regexpid' => array(T_ZBX_INT, O_OPT, P_SYS, DB_ID, 'isset({form})&&{form}=="update"'), 'name' => array(T_ZBX_STR, O_OPT, null, NOT_EMPTY, 'isset({save})', _('Name')), 'test_string' => array(T_ZBX_STR, O_OPT, null, null, 'isset({save})', _('Test string')), 'expressions' => array(T_ZBX_STR, O_OPT, null, null, 'isset({save})'), 'save' => array(T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null), 'delete' => array(T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null), 'clone' => array(T_ZBX_STR, O_OPT, null, null, null), 'go' => array(T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null), 'form' => array(T_ZBX_STR, O_OPT, P_SYS, null, null), 'form_refresh' => array(T_ZBX_INT, O_OPT, null, null, null), 'output' => array(T_ZBX_STR, O_OPT, P_ACT, null, null), 'ajaxaction' => array(T_ZBX_STR, O_OPT, P_ACT, null, null), 'ajaxdata' => array(T_ZBX_STR, O_OPT, P_ACT, null, null)); check_fields($fields); /* * Ajax */ if (isset($_REQUEST['output']) && $_REQUEST['output'] == 'ajax') { $ajaxResponse = new AjaxResponse(); $ajaxData = get_request('ajaxdata', array()); if (isset($_REQUEST['ajaxaction']) && $_REQUEST['ajaxaction'] == 'test') { $result = array('expressions' => array(), 'final' => true); $testString = $ajaxData['testString']; foreach ($ajaxData['expressions'] as $id => $expression) { $match = GlobalRegExp::matchExpression($expression, $testString); $result['expressions'][$id] = $match; $result['final'] = $result['final'] && $match; } $ajaxResponse->success($result); } $ajaxResponse->send(); require_once dirname(__FILE__) . '/include/page_footer.php'; exit; } /* * Permissions */ if (isset($_REQUEST['regexpid'])) { $regExp = DBfetch(DBSelect('SELECT re.regexpid FROM regexps re WHERE re.regexpid=' . zbx_dbstr(get_request('regexpid')))); if (empty($regExp)) {
/** * Get icon id by mapping. * * @param array $iconMap * @param array $inventory * * @return int */ function getIconByMapping($iconMap, $inventory) { if (!empty($inventory['inventory'])) { $inventories = getHostInventories(); foreach ($iconMap['mappings'] as $mapping) { try { $expr = new GlobalRegExp($mapping['expression']); if ($expr->match($inventory['inventory'][$inventories[$mapping['inventory_link']]['db_field']])) { return $mapping['iconid']; } } catch (Exception $e) { continue; } } } return $iconMap['default_iconid']; }
/** * @param array $iconMap * @param array $inventory * * @return int icon id */ function getIconByMapping($iconMap, $inventory) { $iconid = null; $inventories = getHostInventories(); if (isset($inventory['inventory'])) { foreach ($iconMap['mappings'] as $mapping) { try { $expr = new GlobalRegExp($mapping['expression']); if ($expr->match($inventory['inventory'][$inventories[$mapping['inventory_link']]['db_field']])) { $iconid = $mapping['iconid']; break; } } catch (Exception $e) { continue; } } } if (null === $iconid) { $iconid = $iconMap['default_iconid']; } return $iconid; }