Пример #1
0
/**
 * Implodes expression, replaces names and keys with IDs.
 *
 * Fro example: localhost:procload.last(0)>10 will translated to {12}>10 and created database representation.
 *
 * @throws Exception if error occureed
 *
 * @param string $expression Full expression with host names and item keys
 * @param numeric $triggerid
 * @param array optional $hostnames Reference to array which will be filled with unique visible host names.
 *
 * @return string Imploded expression (names and keys replaced by IDs)
 */
function implode_exp($expression, $triggerId, &$hostnames = array())
{
    $expressionData = new CTriggerExpression();
    if (!$expressionData->parse($expression)) {
        throw new Exception($expressionData->error);
    }
    $newFunctions = array();
    $functions = array();
    $items = array();
    $triggerFunctionValidator = new CTriggerFunctionValidator();
    foreach ($expressionData->expressions as $exprPart) {
        if (isset($newFunctions[$exprPart['expression']])) {
            continue;
        }
        if (!isset($items[$exprPart['host']][$exprPart['item']])) {
            $result = DBselect('SELECT i.itemid,i.value_type,h.name' . ' FROM items i,hosts h' . ' WHERE i.key_=' . zbx_dbstr($exprPart['item']) . ' AND ' . dbConditionInt('i.flags', array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED, ZBX_FLAG_DISCOVERY_PROTOTYPE)) . ' AND h.host=' . zbx_dbstr($exprPart['host']) . ' AND h.hostid=i.hostid' . andDbNode('i.itemid'));
            if ($row = DBfetch($result)) {
                $hostnames[] = $row['name'];
                $items[$exprPart['host']][$exprPart['item']] = array('itemid' => $row['itemid'], 'valueType' => $row['value_type']);
            } else {
                throw new Exception(_s('Incorrect item key "%1$s" provided for trigger expression on "%2$s".', $exprPart['item'], $exprPart['host']));
            }
        }
        if (!$triggerFunctionValidator->validate(array('function' => $exprPart['function'], 'functionName' => $exprPart['functionName'], 'functionParamList' => $exprPart['functionParamList'], 'valueType' => $items[$exprPart['host']][$exprPart['item']]['valueType']))) {
            throw new Exception($triggerFunctionValidator->getError());
        }
        $newFunctions[$exprPart['expression']] = 0;
        $functions[] = array('itemid' => $items[$exprPart['host']][$exprPart['item']]['itemid'], 'triggerid' => $triggerId, 'function' => $exprPart['functionName'], 'parameter' => $exprPart['functionParam']);
    }
    $functionIds = DB::insert('functions', $functions);
    $num = 0;
    foreach ($newFunctions as &$newFunction) {
        $newFunction = $functionIds[$num++];
    }
    unset($newFunction);
    $exprPart = end($expressionData->expressions);
    do {
        $expression = substr_replace($expression, '{' . $newFunctions[$exprPart['expression']] . '}', $exprPart['pos'], strlen($exprPart['expression']));
    } while ($exprPart = prev($expressionData->expressions));
    $hostnames = array_unique($hostnames);
    return $expression;
}
Пример #2
0
 }
 if ($data['paramtype'] == PARAM_TYPE_TIME && in_array($data['function'], array('last', 'band', 'strlen'))) {
     $data['param'][0] = '';
 }
 // quote function param
 $params = array();
 foreach ($data['param'] as $param) {
     $params[] = quoteFunctionParam($param);
 }
 $data['expression'] = sprintf('{%s:%s.%s(%s)}%s%s', $data['item_host'], $data['item_key'], $data['function'], rtrim(implode(',', $params), ','), $data['operator'], $data['value']);
 // validate trigger expression
 $triggerExpression = new CTriggerExpression();
 if ($triggerExpression->parse($data['expression'])) {
     $expressionData = reset($triggerExpression->expressions);
     // validate trigger function
     $triggerFunctionValidator = new CTriggerFunctionValidator();
     $isValid = $triggerFunctionValidator->validate(array('function' => $expressionData['function'], 'functionName' => $expressionData['functionName'], 'functionParamList' => $expressionData['functionParamList'], 'valueType' => $data['itemValueType']));
     if (!$isValid) {
         unset($data['insert']);
         throw new Exception($triggerFunctionValidator->getError());
     }
 } else {
     unset($data['insert']);
     throw new Exception($triggerExpression->error);
 }
 // quote function param
 if (isset($data['insert'])) {
     foreach ($data['param'] as $pnum => $param) {
         $data['param'][$pnum] = quoteFunctionParam($param);
     }
 }