// quote function param $quotedParams = []; foreach ($data['params'] as $param) { $quotedParams[] = quoteFunctionParam($param); } $data['expression'] = sprintf('{%s:%s.%s(%s)}%s%s', $itemHostData['host'], $data['item_key'], $function, rtrim(implode(',', $quotedParams), ','), $operator, $data['value']); // validate trigger expression $triggerExpression = new CTriggerExpression(); if ($triggerExpression->parse($data['expression'])) { $expressionData = reset($triggerExpression->expressions); // validate trigger function $triggerFunctionValidator = new CFunctionValidator(); $isValid = $triggerFunctionValidator->validate(['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['params'] as $pnum => $param) { $data['params'][$pnum] = quoteFunctionParam($param); } } } else { unset($data['insert']); throw new Exception(_('Item not selected')); }
/** * Implodes expression, replaces names and keys with IDs. * * For example: localhost:system.cpu.load.last(0)>10 will be translated to {12}>10 and created database representation. * * @throws Exception if error occurred * * @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 CFunctionValidator(); 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'); 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; }