/** * Apply value mapping to value. * If value map or mapping is not found unchanged value returned, * otherwise mapped value returned in format: "<mapped_value> (<initial_value>)". * * @param string $value value that mapping should be applied to * @param int $valueMapId value map id which should be used * * @return string */ function applyValueMap($value, $valueMapId) { $mapping = getMappedValue($value, $valueMapId); return $mapping === false ? $value : $mapping . ' (' . $value . ')'; }
/** * Apply value mapping to value. * If value map or mapping is not found, unchanged value is returned, * otherwise mapped value returned in format: "<mapped_value> (<initial_value>)". * * @param string $value Value that mapping should be applied to. * @param string $valuemapid Value map ID which should be used. * * @return string */ function applyValueMap($value, $valuemapid) { $newvalue = getMappedValue($value, $valuemapid); return $newvalue === false ? $value : $newvalue . ' (' . $value . ')'; }
/** * Format history value. * First format the value according to the configuration of the item. Then apply the value mapping to the formatted (!) * value. * * @param mixed $value * @param array $item * @param int $item['value_type'] type of the value: ITEM_VALUE_TYPE_FLOAT, ITEM_VALUE_TYPE_UINT64, ... * @param string $item['units'] units of item * @param int $item['valuemapid'] id of mapping set of values * @param bool $trim * * @return string */ function formatHistoryValue($value, array $item, $trim = true) { $mapping = false; // format value if ($item['value_type'] == ITEM_VALUE_TYPE_FLOAT || $item['value_type'] == ITEM_VALUE_TYPE_UINT64) { $value = convert_units(array('value' => $value, 'units' => $item['units'])); } elseif ($item['value_type'] != ITEM_VALUE_TYPE_STR && $item['value_type'] != ITEM_VALUE_TYPE_TEXT && $item['value_type'] != ITEM_VALUE_TYPE_LOG) { $value = _('Unknown value type'); } // apply value mapping switch ($item['value_type']) { case ITEM_VALUE_TYPE_STR: $mapping = getMappedValue($value, $item['valuemapid']); // break; is not missing here // break; is not missing here case ITEM_VALUE_TYPE_TEXT: case ITEM_VALUE_TYPE_LOG: if ($trim && zbx_strlen($value) > 20) { $value = zbx_substr($value, 0, 20) . '...'; } if ($mapping !== false) { $value = $mapping . ' (' . $value . ')'; } break; default: $value = applyValueMap($value, $item['valuemapid']); } return $value; }
/** * Expand functional macros in given map label. * * @param string $label label to expand * @param array $replaceHosts list of hosts in order which they appear in trigger expression if trigger label is given, * or single host when host label is given * * @return string expanded label */ function resolveMapLabelMacros($label, $replaceHosts = null) { // find functional macro pattern $pattern = null === $replaceHosts ? '/{' . ZBX_PREG_HOST_FORMAT . ":.+\\.(last|max|min|avg)\\([0-9]+[smhdwKMGT]?\\)}/Uu" : '/{(' . ZBX_PREG_HOST_FORMAT . "|{HOSTNAME[0-9]?}|{HOST.HOST[0-9]?}):.+\\.(last|max|min|avg)\\([0-9]+[smhdwKMGT]?\\)}/Uu"; preg_match_all($pattern, $label, $matches); // for each functional macro foreach ($matches[0] as $expr) { $macro = $expr; if ($replaceHosts !== null) { // search for macros with all possible indecies foreach ($replaceHosts as $i => $host) { $macroTmp = $macro; // repalce only macro in first position $macro = preg_replace('/{({HOSTNAME' . $i . '}|{HOST.HOST' . $i . '}):(.*)}/U', '{' . $host['host'] . ':$2}', $macro); // only one simple macro possible inside functional macro if ($macro != $macroTmp) { break; } } } // try to create valid expression $expressionData = new CTriggerExpression(); if (!$expressionData->parse($macro) || !isset($expressionData->expressions[0])) { continue; } // look in DB for coressponding item $itemHost = $expressionData->expressions[0]['host']; $key = $expressionData->expressions[0]['item']; $function = $expressionData->expressions[0]['functionName']; $parameter = convertFunctionValue($expressionData->expressions[0]['functionParamList'][0]); $item = API::Item()->get(array('webitems' => true, 'filter' => array('host' => $itemHost, 'key_' => $key), 'output' => array('lastclock', 'value_type', 'lastvalue', 'units', 'valuemapid'))); $item = reset($item); // if no corresponding item found with functional macro key and host if (!$item) { $label = str_replace($expr, '???', $label); continue; } // do function type (last, min, max, avg) related actions if (0 == strcmp($function, 'last')) { if ($item['lastclock'] == 0) { $label = str_replace($expr, '(' . _('no data') . ')', $label); } else { $mapping = false; $value = formatItemValueType($item); switch ($item['value_type']) { case ITEM_VALUE_TYPE_STR: $mapping = getMappedValue($value, $item['valuemapid']); // break; is not missing here // break; is not missing here case ITEM_VALUE_TYPE_TEXT: case ITEM_VALUE_TYPE_LOG: if ($mapping !== false) { $value = $mapping . ' (' . $value . ')'; } break; default: $value = applyValueMap($value, $item['valuemapid']); } } if (isset($value)) { $label = str_replace($expr, $value, $label); } } elseif (0 == strcmp($function, 'min') || 0 == strcmp($function, 'max') || 0 == strcmp($function, 'avg')) { // allowed item types for min, max and avg function $history_table = array(ITEM_VALUE_TYPE_FLOAT => 'history', ITEM_VALUE_TYPE_UINT64 => 'history_uint'); if (!isset($history_table[$item['value_type']])) { $label = str_replace($expr, '???', $label); continue; } // search for item function data in DB corresponding history tables $result = DBselect('SELECT ' . $function . '(value) AS value' . ' FROM ' . $history_table[$item['value_type']] . ' WHERE clock>' . (time() - $parameter) . ' AND itemid=' . zbx_dbstr($item['itemid'])); if (null === ($row = DBfetch($result))) { $label = str_replace($expr, '(' . _('no data') . ')', $label); } else { $label = str_replace($expr, convert_units($row['value'], $item['units']), $label); } } } return $label; }