Esempio n. 1
0
/**
 * 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')) {
            $value = $item['lastclock'] ? formatHistoryValue($item['lastvalue'], $item) : UNRESOLVED_MACRO_STRING;
        } elseif (0 == strcmp($function, 'min') || 0 == strcmp($function, 'max') || 0 == strcmp($function, 'avg')) {
            $value = getItemFunctionalValue($item, $function, $parameter);
        }
        if (isset($value)) {
            $label = str_replace($expr, $value, $label);
        }
    }
    return $label;
}
Esempio n. 2
0
/**
 * Retrieves from DB historical data for items and applies functional calculations.
 * If fails for some reason, returns UNRESOLVED_MACRO_STRING.
 *
 * @param array		$item
 * @param string	$item['value_type']	type of item, allowed: ITEM_VALUE_TYPE_FLOAT and ITEM_VALUE_TYPE_UINT64
 * @param string	$item['itemid']		ID of item
 * @param string	$item['units']		units of item
 * @param string	$function			function to apply to time period from param, allowed: min, max and avg
 * @param string	$parameter			formatted parameter for function, example: "2w" meaning 2 weeks
 *
 * @return string item functional value from history
 */
function getItemFunctionalValue($item, $function, $parameter)
{
    // check whether function is allowed
    if (!in_array($function, array('min', 'max', 'avg')) || $parameter === '') {
        return UNRESOLVED_MACRO_STRING;
    }
    $parameter = convertFunctionValue($parameter);
    if (bccomp($parameter, 0) == 0) {
        return UNRESOLVED_MACRO_STRING;
    }
    // allowed item types for min, max and avg function
    $historyTables = array(ITEM_VALUE_TYPE_FLOAT => 'history', ITEM_VALUE_TYPE_UINT64 => 'history_uint');
    if (!isset($historyTables[$item['value_type']])) {
        return UNRESOLVED_MACRO_STRING;
    } else {
        // search for item function data in DB corresponding history table
        $result = DBselect('SELECT ' . $function . '(value) AS value' . ' FROM ' . $historyTables[$item['value_type']] . ' WHERE clock>' . (time() - $parameter) . ' AND itemid=' . zbx_dbstr($item['itemid']) . ' HAVING COUNT(*)>0');
        if ($row = DBfetch($result)) {
            return convert_units(array('value' => $row['value'], 'units' => $item['units']));
        } else {
            return UNRESOLVED_MACRO_STRING;
        }
    }
}
/**
 * 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;
}