Exemplo n.º 1
0
function validate_expression($expression)
{
    global $ZBX_TR_EXPR_ALLOWED_MACROS, $ZBX_TR_EXPR_REPLACE_TO, $ZBX_TR_EXPR_ALLOWED_FUNCTIONS;
    if (empty($expression)) {
        error('Expression can\'t be empty');
    }
    $expr = $expression;
    $h_status = array();
    /* Replace all {server:key.function(param)} and {MACRO} with '$ZBX_TR_EXPR_REPLACE_TO' */
    while (ereg(ZBX_EREG_EXPRESSION_TOKEN_FORMAT, $expr, $arr)) {
        if ($arr[ZBX_EXPRESSION_MACRO_ID] && !isset($ZBX_TR_EXPR_ALLOWED_MACROS[$arr[ZBX_EXPRESSION_MACRO_ID]])) {
            error('Unknown macro [' . $arr[ZBX_EXPRESSION_MACRO_ID] . ']');
            return false;
        } else {
            if (!$arr[ZBX_EXPRESSION_MACRO_ID]) {
                $host =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_HOST_ID];
                $key =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_KEY_ID];
                $function =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_NAME_ID];
                $parameter =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_PARAM_ID];
                /* Check host */
                $sql = 'SELECT COUNT(*) as cnt,min(status) as status,min(hostid) as hostid ' . ' FROM hosts h ' . ' WHERE h.host=' . zbx_dbstr($host) . ' AND ' . DBin_node('h.hostid', get_current_nodeid(false)) . ' AND status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ') ';
                $row = DBfetch(DBselect($sql));
                if ($row['cnt'] == 0) {
                    error('No such host (' . $host . ')');
                    return false;
                } else {
                    if ($row['cnt'] != 1) {
                        error('Too many hosts (' . $host . ')');
                        return false;
                    }
                }
                $h_status[$row['status']][$row['hostid']] = $row['cnt'];
                /* Check key */
                $sql = 'SELECT i.itemid,i.value_type ' . ' FROM hosts h,items i ' . ' WHERE h.host=' . zbx_dbstr($host) . ' AND i.key_=' . zbx_dbstr($key) . ' AND h.hostid=i.hostid ' . ' AND ' . DBin_node('h.hostid', get_current_nodeid(false));
                if (!($item = DBfetch(DBselect($sql)))) {
                    error('No such monitored parameter (' . $key . ') for host (' . $host . ')');
                    return false;
                }
                /* Check function */
                if (!isset($ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function])) {
                    error('Unknown function [' . $function . ']');
                    return false;
                }
                $fnc_valid =& $ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function];
                if (is_array($fnc_valid['item_types']) && !uint_in_array($item['value_type'], $fnc_valid['item_types'])) {
                    $allowed_types = array();
                    foreach ($fnc_valid['item_types'] as $type) {
                        $allowed_types[] = item_value_type2str($type);
                    }
                    info('Function (' . $function . ') available only for items with value types [' . implode(',', $allowed_types) . ']');
                    error('Incorrect value type [' . item_value_type2str($item['value_type']) . '] for function (' . $function . ') of key (' . $host . ':' . $key . ')');
                    return false;
                }
                if (!is_null($fnc_valid['args'])) {
                    $parameter = zbx_get_params($parameter);
                    if (!is_array($fnc_valid['args'])) {
                        $fnc_valid['args'] = array($fnc_valid['args']);
                    }
                    foreach ($fnc_valid['args'] as $pid => $params) {
                        if (!isset($parameter[$pid])) {
                            if (!isset($params['mandat'])) {
                                continue;
                            } else {
                                error('Missed mandatory parameter for function (' . $function . ')');
                                return false;
                            }
                        }
                        if ('sec' == $params['type'] && validate_float($parameter[$pid]) != 0) {
                            error('[' . $parameter[$pid] . '] is not a float for function (' . $function . ')');
                            return false;
                        }
                        if ('sec_num' == $params['type'] && validate_ticks($parameter[$pid]) != 0) {
                            error('[' . $parameter[$pid] . '] is not a float or counter for function (' . $function . ')');
                            return false;
                        }
                    }
                }
            }
        }
        $expr = $arr[ZBX_EXPRESSION_LEFT_ID] . $ZBX_TR_EXPR_REPLACE_TO . $arr[ZBX_EXPRESSION_RIGHT_ID];
    }
    if (isset($h_status[HOST_STATUS_TEMPLATE]) && (count($h_status) > 1 || count($h_status[HOST_STATUS_TEMPLATE]) > 1)) {
        error("Incorrect trigger expression. You can't use template hosts" . " in mixed expressions.");
        return false;
    }
    /* Replace all calculations and numbers with '$ZBX_TR_EXPR_REPLACE_TO' */
    $expt_number = '(' . $ZBX_TR_EXPR_REPLACE_TO . '|' . ZBX_EREG_NUMBER . ')';
    $expt_term = '((\\(' . $expt_number . '\\))|(' . $expt_number . '))';
    $expr_format = '((' . $expt_term . ZBX_EREG_SPACES . ZBX_EREG_SIGN . ZBX_EREG_SPACES . $expt_term . ')|(\\(' . $expt_term . '\\)))';
    $expr_full_format = '((\\(' . $expr_format . '\\))|(' . $expr_format . '))';
    while ($res = ereg($expr_full_format . '([[:print:]]*)$', $expr, $arr)) {
        $expr = substr($expr, 0, strpos($expr, $arr[1])) . $ZBX_TR_EXPR_REPLACE_TO . $arr[58];
    }
    if ($ZBX_TR_EXPR_REPLACE_TO != $expr) {
        error('Incorrect trigger expression. [' . str_replace($ZBX_TR_EXPR_REPLACE_TO, ' ... ', $expr) . ']');
        return false;
    }
    return true;
}
Exemplo n.º 2
0
function get_n_param($key, $num)
{
    $param = "";
    $num--;
    if (preg_match('/^' . ZBX_PREG_ITEM_KEY_FORMAT . '$/', $key, $arr)) {
        if (!isset($arr[ZBX_KEY_PARAM_ID])) {
            $arr[ZBX_KEY_PARAM_ID] = false;
        }
        $params = zbx_get_params($arr[ZBX_KEY_PARAM_ID]);
        if (isset($params[$num])) {
            $param = $params[$num];
        }
    }
    return $param;
}
Exemplo n.º 3
0
function get_n_param($key, $num)
{
    $param = "";
    $num--;
    if (ereg('^' . ZBX_EREG_ITEM_KEY_FORMAT . '$', $key, $arr)) {
        $params = zbx_get_params($arr[ZBX_KEY_PARAM_ID]);
        if (isset($params[$num])) {
            $param = $params[$num];
        }
    }
    return $param;
}
Exemplo n.º 4
0
function validate_expression($expression)
{
    global $ZBX_TR_EXPR_SIMPLE_MACROS, $ZBX_TR_EXPR_REPLACE_TO, $ZBX_TR_EXPR_ALLOWED_FUNCTIONS;
    if (empty($expression)) {
        error(S_EXPRESSION_CANNOT_BE_EMPTY);
        return false;
    }
    $expr = $expression;
    $h_status = array();
    $item_count = 0;
    // Replace all {server:key.function(param)} and {MACRO} with '$ZBX_TR_EXPR_REPLACE_TO'
    //		while(ereg(ZBX_EREG_EXPRESSION_TOKEN_FORMAT, $expr, $arr)){
    while (preg_match('/' . ZBX_PREG_EXPRESSION_TOKEN_FORMAT . '/u', $expr, $arr)) {
        if ($arr[ZBX_EXPRESSION_MACRO_ID] && !isset($ZBX_TR_EXPR_SIMPLE_MACROS[$arr[ZBX_EXPRESSION_MACRO_ID]])) {
            error('Unknown macro [' . $arr[ZBX_EXPRESSION_MACRO_ID] . ']');
            return false;
        } else {
            if (!$arr[ZBX_EXPRESSION_MACRO_ID]) {
                $host =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_HOST_ID];
                $key =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_KEY_ID];
                $function =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_NAME_ID];
                $parameter =& $arr[ZBX_EXPRESSION_SIMPLE_EXPRESSION_ID + ZBX_SIMPLE_EXPRESSION_FUNCTION_PARAM_ID];
                // Check host
                $sql = 'SELECT COUNT(*) as cnt,min(status) as status,min(hostid) as hostid ' . ' FROM hosts h ' . ' WHERE h.host=' . zbx_dbstr($host) . ' AND ' . DBin_node('h.hostid', false) . ' AND status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ') ';
                $row = DBfetch(DBselect($sql));
                if ($row['cnt'] == 0) {
                    error(S_NO_SUCH_HOST . ' (' . $host . ')');
                    return false;
                } else {
                    if ($row['cnt'] != 1) {
                        error(S_TOO_MANY_HOSTS . ' (' . $host . ')');
                        return false;
                    }
                }
                $h_status[$row['status']][$row['hostid']] = $row['cnt'];
                // Check key
                $sql = 'SELECT i.itemid,i.value_type ' . ' FROM hosts h,items i ' . ' WHERE h.host=' . zbx_dbstr($host) . ' AND i.key_=' . zbx_dbstr($key) . ' AND h.hostid=i.hostid ' . ' AND ' . DBin_node('h.hostid', false);
                if (!($item = DBfetch(DBselect($sql)))) {
                    error(S_NO_SUCH_MONITORED_PARAMETER . ' (' . $key . ') ' . S_FOR_HOST_SMALL . ' (' . $host . ')');
                    return false;
                }
                // Check function
                if (!isset($ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function])) {
                    error(S_UNKNOWN_FUNCTION . SPACE . '[' . $function . ']');
                    return false;
                }
                $fnc_valid =& $ZBX_TR_EXPR_ALLOWED_FUNCTIONS[$function];
                if (is_array($fnc_valid['item_types']) && !uint_in_array($item['value_type'], $fnc_valid['item_types'])) {
                    $allowed_types = array();
                    foreach ($fnc_valid['item_types'] as $type) {
                        $allowed_types[] = item_value_type2str($type);
                    }
                    info(S_FUNCTION . ' (' . $function . ') ' . S_AVAILABLE_ONLY_FOR_ITEMS_WITH_VALUE_TYPES_SMALL . ' [' . implode(',', $allowed_types) . ']');
                    error(S_INCORRECT_VALUE_TYPE . ' [' . item_value_type2str($item['value_type']) . '] ' . S_FOR_FUNCTION_SMALL . ' (' . $function . ') ' . S_OF_KEY_SMALL . ' (' . $host . ':' . $key . ')');
                    return false;
                }
                if (!is_null($fnc_valid['args'])) {
                    $parameter = zbx_get_params($parameter);
                    if (!is_array($fnc_valid['args'])) {
                        $fnc_valid['args'] = array($fnc_valid['args']);
                    }
                    foreach ($fnc_valid['args'] as $pid => $params) {
                        if (!isset($parameter[$pid])) {
                            if (!isset($params['mandat'])) {
                                continue;
                            } else {
                                error(S_MISSING_MANDATORY_PARAMETER_FOR_FUNCTION . ' (' . $function . ')');
                                return false;
                            }
                        }
                        if (preg_match('/^' . ZBX_PREG_EXPRESSION_USER_MACROS . '$/', $parameter[$pid])) {
                            continue;
                        }
                        if ('sec' == $params['type'] && validate_sec($parameter[$pid]) != 0) {
                            error('[' . $parameter[$pid] . '] ' . S_NOT_FLOAT_OR_MACRO_FOR_FUNCTION_SMALL . ' (' . $function . ')');
                            return false;
                        }
                        if ('sec_num' == $params['type'] && validate_secnum($parameter[$pid]) != 0) {
                            error('[' . $parameter[$pid] . '] ' . S_NOT_FLOAT_OR_MACRO_OR_COUNTER_FOR_FUNCTION_SMALL . ' (' . $function . ')');
                            return false;
                        }
                    }
                }
                $item_count++;
            }
        }
        $expr = $arr[ZBX_EXPRESSION_LEFT_ID] . $ZBX_TR_EXPR_REPLACE_TO . $arr[ZBX_EXPRESSION_RIGHT_ID];
    }
    if ($item_count == 0) {
        error(S_ITEM_KEY_MUST_BE_USED_IN_TRIGGER_EXPRESSION);
        return false;
    }
    if (isset($h_status[HOST_STATUS_TEMPLATE]) && (count($h_status) > 1 || count($h_status[HOST_STATUS_TEMPLATE]) > 1)) {
        error(S_INCORRECT_TRIGGER_EXPRESSION . '.' . SPACE . S_YOU_CAN_NOT_USE_TEMPLATE_HOSTS_MIXED_EXPR);
        return false;
    }
    // Replace all calculations and numbers with '$ZBX_TR_EXPR_REPLACE_TO'
    $expt_number = '(' . $ZBX_TR_EXPR_REPLACE_TO . '|' . ZBX_PREG_NUMBER . '|' . ZBX_PREG_EXPRESSION_USER_MACROS . ')';
    $expt_term = '((\\(' . $expt_number . '\\))|(' . $expt_number . '))';
    $expr_format = '((' . $expt_term . ZBX_PREG_SPACES . ZBX_PREG_SIGN . ZBX_PREG_SPACES . $expt_term . ')|(\\(' . $expt_term . '\\)))';
    $expr_full_format = '((\\(' . $expr_format . '\\))|(' . $expr_format . '))';
    while ($res = preg_match('/' . $expr_full_format . '(.*)$/u', $expr, $arr)) {
        $expr = substr($expr, 0, zbx_strpos($expr, $arr[1])) . $ZBX_TR_EXPR_REPLACE_TO . $arr[82];
    }
    /* OLD EREG
    //Replace all calculations and numbers with '$ZBX_TR_EXPR_REPLACE_TO'
    		$expt_number = '('.$ZBX_TR_EXPR_REPLACE_TO.'|'.ZBX_EREG_NUMBER.'|'.ZBX_EREG_EXPRESSION_USER_MACROS.')';
    
    		$expt_term = '((\('.$expt_number.'\))|('.$expt_number.'))';
    		$expr_format = '(('.$expt_term.ZBX_EREG_SPACES.ZBX_EREG_SIGN.ZBX_EREG_SPACES.$expt_term.')|(\('.$expt_term.'\)))';
    		$expr_full_format = '((\('.$expr_format.'\))|('.$expr_format.'))';
    
    		while($res = ereg($expr_full_format.'([[:print:]]*)$', $expr, $arr)){
    			$expr = substr($expr, 0, zbx_strpos($expr, $arr[1])).$ZBX_TR_EXPR_REPLACE_TO.$arr[58];
    		}
    */
    if ($ZBX_TR_EXPR_REPLACE_TO != $expr) {
        error(S_INCORRECT_TRIGGER_EXPRESSION . '. [' . str_replace($ZBX_TR_EXPR_REPLACE_TO, ' ... ', $expr) . ']');
        return false;
    }
    return true;
}