예제 #1
0
function expand_trigger_description_by_data($row, $flag = ZBX_FLAG_TRIGGER)
{
    if ($row) {
        $description = expand_trigger_description_constants($row['description'], $row);
        if (is_null($row['host'])) {
            $row['host'] = '{HOSTNAME}';
        }
        $description = str_replace('{HOSTNAME}', $row['host'], $description);
        if (zbx_strstr($description, '{ITEM.LASTVALUE}')) {
            $functionid = trigger_get_N_functionid($row['expression'], 1);
            if (isset($functionid)) {
                $sql = 'SELECT i.lastvalue, i.value_type, i.itemid ' . ' FROM items i, functions f ' . ' WHERE i.itemid=f.itemid ' . ' AND f.functionid=' . $functionid;
                $row2 = DBfetch(DBselect($sql));
                if ($row2['value_type'] != ITEM_VALUE_TYPE_LOG) {
                    $description = str_replace('{ITEM.LASTVALUE}', $row2['lastvalue'], $description);
                } else {
                    $sql = 'SELECT MAX(clock) as max FROM history_log WHERE itemid=' . $row2['itemid'];
                    $row3 = DBfetch(DBselect($sql));
                    if ($row3 && !is_null($row3['max'])) {
                        $sql = 'SELECT value FROM history_log WHERE itemid=' . $row2['itemid'] . ' AND clock=' . $row3['max'];
                        $row4 = DBfetch(DBselect($sql));
                        $description = str_replace('{ITEM.LASTVALUE}', $row4['value'], $description);
                    }
                }
            }
        }
        if (zbx_strstr($description, '{ITEM.VALUE}')) {
            $value = $flag == ZBX_FLAG_TRIGGER ? trigger_get_func_value($row['expression'], ZBX_FLAG_TRIGGER, 1, 1) : trigger_get_func_value($row['expression'], ZBX_FLAG_EVENT, 1, $row['clock']);
            $description = str_replace('{ITEM.VALUE}', $value, $description);
        }
        for ($i = 1; $i < 10; $i++) {
            if (zbx_strstr($description, '{ITEM.VALUE' . $i . '}')) {
                $value = $flag == ZBX_FLAG_TRIGGER ? trigger_get_func_value($row['expression'], ZBX_FLAG_TRIGGER, $i, 1) : trigger_get_func_value($row['expression'], ZBX_FLAG_EVENT, $i, $row['clock']);
                $description = str_replace('{ITEM.VALUE' . $i . '}', $value, $description);
            }
        }
    } else {
        $description = '*ERROR*';
    }
    return $description;
}
예제 #2
0
function expand_trigger_description_by_data($row, $flag = ZBX_FLAG_TRIGGER)
{
    if ($row) {
        $description = expand_trigger_description_constants($row['description'], $row);
        for ($i = 0; $i < 10; $i++) {
            $macro = '{HOSTNAME' . ($i ? $i : '') . '}';
            if (zbx_strstr($description, $macro)) {
                $functionid = trigger_get_N_functionid($row['expression'], $i ? $i : 1);
                if (isset($functionid)) {
                    $sql = 'SELECT DISTINCT h.host' . ' FROM functions f,items i,hosts h' . ' WHERE f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND f.functionid=' . $functionid;
                    $host = DBfetch(DBselect($sql));
                    if (is_null($host['host'])) {
                        $host['host'] = $macro;
                    }
                    $description = str_replace($macro, $host['host'], $description);
                }
            }
        }
        for ($i = 0; $i < 10; $i++) {
            $macro = '{ITEM.LASTVALUE' . ($i ? $i : '') . '}';
            if (zbx_strstr($description, $macro)) {
                $functionid = trigger_get_N_functionid($row['expression'], $i ? $i : 1);
                if (isset($functionid)) {
                    $sql = 'SELECT i.lastvalue, i.value_type, i.itemid, i.valuemapid, i.units ' . ' FROM items i, functions f ' . ' WHERE i.itemid=f.itemid ' . ' AND f.functionid=' . $functionid;
                    $row2 = DBfetch(DBselect($sql));
                    $description = str_replace($macro, format_lastvalue($row2), $description);
                }
            }
        }
        for ($i = 0; $i < 10; $i++) {
            $macro = '{ITEM.VALUE' . ($i ? $i : '') . '}';
            if (zbx_strstr($description, $macro)) {
                $value = $flag == ZBX_FLAG_TRIGGER ? trigger_get_func_value($row['expression'], ZBX_FLAG_TRIGGER, $i ? $i : 1, 1) : trigger_get_func_value($row['expression'], ZBX_FLAG_EVENT, $i ? $i : 1, $row['clock']);
                $description = str_replace($macro, $value, $description);
            }
        }
        if ($res = preg_match_all('/' . ZBX_PREG_EXPRESSION_USER_MACROS . '/', $description, $arr)) {
            $macros = CUserMacro::getMacros($arr[1], array('triggerid' => $row['triggerid']));
            $search = array_keys($macros);
            $values = array_values($macros);
            $description = str_replace($search, $values, $description);
        }
    } else {
        $description = '*ERROR*';
    }
    return $description;
}