Пример #1
0
function expand_item_key_by_data($item)
{
    $key =& $item['key_'];
    $macStack = array();
    $macroses = array('{HOSTNAME}', '{IPADDRESS}', '{HOST.DNS}', '{HOST.CONN}');
    foreach ($macroses as $macro) {
        $pos = 0;
        while ($pos = zbx_strpos($key, $macro, $pos)) {
            $pos++;
            $macStack[] = $macro;
        }
    }
    if (!empty($macStack)) {
        $host = get_host_by_itemid($item['itemid']);
        foreach ($macStack as $macro) {
            switch ($macro) {
                case '{HOSTNAME}':
                    $key = str_replace('{HOSTNAME}', $host['host'], $key);
                    break;
                case '{IPADDRESS}':
                    $key = str_replace('{IPADDRESS}', $host['ip'], $key);
                    break;
                case '{HOST.DNS}':
                    $key = str_replace('{HOST.DNS}', $host['dns'], $key);
                    break;
                case '{HOST.CONN}':
                    $key = str_replace('{HOST.CONN}', $host['useip'] ? $host['ip'] : $host['dns'], $key);
                    break;
            }
        }
    }
    CUserMacro::resolveItem($item);
    return $item['key_'];
}
Пример #2
0
function triggerExpression($trigger, $html, $template = false, $resolve_macro = false)
{
    $expression = $trigger['expression'];
    //		echo "EXPRESSION:",$expression,"<Br>";
    $functionid = '';
    $macros = '';
    if (0 == $html) {
        $exp = '';
    } else {
        $exp = array();
    }
    $state = '';
    for ($i = 0, $max = zbx_strlen($expression); $i < $max; $i++) {
        if ($expression[$i] == '{' && $expression[$i + 1] == '$') {
            $functionid = '';
            $macros = '';
            $state = 'MACROS';
        } else {
            if ($expression[$i] == '{') {
                $functionid = '';
                $state = 'FUNCTIONID';
                continue;
            }
        }
        if ($expression[$i] == '}') {
            if ($state == 'MACROS') {
                $macros .= '}';
                if ($resolve_macro) {
                    $function_data['expression'] = $macros;
                    CUserMacro::resolveTrigger($function_data);
                    $macros = $function_data['expression'];
                }
                if (1 == $html) {
                    array_push($exp, $macros);
                } else {
                    $exp .= $macros;
                }
                $macros = '';
                $state = '';
                continue;
            }
            $state = '';
            if ($functionid == 'TRIGGER.VALUE') {
                if (0 == $html) {
                    $exp .= '{' . $functionid . '}';
                } else {
                    array_push($exp, '{' . $functionid . '}');
                }
            } else {
                if (is_numeric($functionid) && isset($trigger['functions'][$functionid])) {
                    $function_data = $trigger['functions'][$functionid];
                    $function_data += $trigger['items'][$function_data['itemid']];
                    $function_data += $trigger['hosts'][$function_data['hostid']];
                    if ($template) {
                        $function_data['host'] = '{HOSTNAME}';
                    }
                    if ($resolve_macro) {
                        CUserMacro::resolveItem($function_data);
                        $function_data['expression'] = $function_data['parameter'];
                        CUserMacro::resolveTrigger($function_data);
                        $function_data['parameter'] = $function_data['expression'];
                    }
                    //SDII($function_data);
                    if ($html == 0) {
                        $exp .= '{' . $function_data['host'] . ':' . $function_data['key_'] . '.' . $function_data['function'] . '(' . $function_data['parameter'] . ')}';
                    } else {
                        $style = $function_data['status'] == ITEM_STATUS_DISABLED ? 'disabled' : 'unknown';
                        if ($function_data['status'] == ITEM_STATUS_ACTIVE) {
                            $style = 'enabled';
                        }
                        $link = new CLink($function_data['host'] . ':' . $function_data['key_'], 'items.php?form=update&itemid=' . $function_data['itemid'], $style);
                        if ($function_data['type'] == ITEM_TYPE_HTTPTEST) {
                            $link = new CSpan($function_data['host'] . ':' . $function_data['key_'], $style);
                        }
                        array_push($exp, array('{', $link, '.', bold($function_data['function'] . '('), $function_data['parameter'], bold(')'), '}'));
                    }
                } else {
                    if (1 == $html) {
                        array_push($exp, new CSpan('*ERROR*', 'on'));
                    } else {
                        $exp .= '*ERROR*';
                    }
                }
            }
            continue;
        }
        if ($state == 'FUNCTIONID') {
            $functionid = $functionid . $expression[$i];
            continue;
        } else {
            if ($state == 'MACROS') {
                $macros = $macros . $expression[$i];
                continue;
            }
        }
        if (1 == $html) {
            array_push($exp, $expression[$i]);
        } else {
            $exp .= $expression[$i];
        }
    }
    //SDII($exp);
    return $exp;
}