function validate_ticks($str) { // echo "Validating float:$str<br>"; if (eregi('^[ ]*#([0-9]+)((\\.)?)([0-9]*)[ ]*$', $str, $arr)) { return 0; } else { return validate_float($str); } }
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; }
function validate_percent_over($number) { $safe_number = validate_float($number); $safe_number = abs($safe_number); return $safe_number; }