/** * Converts simple check item keys used in trigger expressions. * * @param string $expression * * @return string */ public function convert($expression) { $new_expression = ''; for ($pos = 0; isset($expression[$pos]); $pos++) { if ($this->function_macro_parser->parse($expression, $pos) != CParser::PARSE_FAIL) { $new_expression .= '{' . $this->function_macro_parser->getHost() . ':' . $this->itemKeyConverter->convert($this->function_macro_parser->getItem()) . '.' . $this->function_macro_parser->getFunction() . '}'; $pos += $this->function_macro_parser->getLength() - 1; } else { $new_expression .= $expression[$pos]; } } return $new_expression; }
/** * Convert a trigger expression from 2.2 format to 2.4. * * The method will replace old operators with their analogues: "&" with "and", "|" - "or" and "#" - "<>". * * @param string $expression * * @return string */ public function convert($expression) { // find all the operators that need to be replaced $found_operators = []; for ($pos = 0; isset($expression[$pos]); $pos++) { switch ($expression[$pos]) { case '{': // skip function macros if ($this->function_macro_parser->parse($expression, $pos) != CParser::PARSE_FAIL) { $new_expression = '{' . $this->function_macro_parser->getHost() . ':' . $this->itemKeyConverter->convert($this->function_macro_parser->getItem()) . '.' . $this->function_macro_parser->getFunction() . '}'; $expression = substr_replace($expression, $new_expression, $pos, $this->function_macro_parser->getLength()); $pos += strlen($new_expression) - 1; } else { // if it's not a function macro, try to parse it as an LLD macro if ($this->lld_macro_parser->parse($expression, $pos) != CParser::PARSE_FAIL) { $pos += $this->lld_macro_parser->getLength() - 1; } } // otherwise just continue as is, other macros don't contain any of these characters break; case '&': case '|': case '#': $found_operators[$pos] = $expression[$pos]; break; } } // replace the operators foreach (array_reverse($found_operators, true) as $pos => $operator) { switch ($operator) { case '&': $expression = $this->replaceLogicalOperator($expression, 'and', $pos); break; case '|': $expression = $this->replaceLogicalOperator($expression, 'or', $pos); break; case '#': $expression = substr_replace($expression, '<>', $pos, 1); break; } } return $expression; }
/** * Purpose: Replaces host in trigger expression. * {localhost:agent.ping.nodata(5m)} => {localhost6:agent.ping.nodata(5m)} * * @param string $expression full expression with host names and item keys * @param string $src_host * @param string $dst_host * * @return string */ function triggerExpressionReplaceHost($expression, $src_host, $dst_host) { $new_expression = ''; $function_macro_parser = new CFunctionMacroParser(); $user_macro_parser = new CUserMacroParser(); $macro_parser = new CMacroParser(['{TRIGGER.VALUE}']); $lld_macro_parser = new CLLDMacroParser(); for ($pos = 0, $pos_left = 0; isset($expression[$pos]); $pos++) { if ($function_macro_parser->parse($expression, $pos) != CParser::PARSE_FAIL) { $host = $function_macro_parser->getHost(); $item = $function_macro_parser->getItem(); $function = $function_macro_parser->getFunction(); if ($host === $src_host) { $host = $dst_host; } $new_expression .= substr($expression, $pos_left, $pos - $pos_left); $new_expression .= '{' . $host . ':' . $item . '.' . $function . '}'; $pos_left = $pos + $function_macro_parser->getLength(); $pos += $function_macro_parser->getLength() - 1; } elseif ($user_macro_parser->parse($expression, $pos) != CParser::PARSE_FAIL) { $pos += $user_macro_parser->getLength() - 1; } elseif ($macro_parser->parse($expression, $pos) != CParser::PARSE_FAIL) { $pos += $macro_parser->getLength() - 1; } elseif ($lld_macro_parser->parse($expression, $pos) != CParser::PARSE_FAIL) { $pos += $lld_macro_parser->getLength() - 1; } } $new_expression .= substr($expression, $pos_left, $pos - $pos_left); return $new_expression; }
/** * Parses a trigger function macro constant in the trigger expression and * moves a current position ($this->pos) on a last symbol of the macro * * @return bool returns true if parsed successfully, false otherwise */ private function parseFunctionMacro() { $startPos = $this->pos; if ($this->function_macro_parser->parse($this->expression, $this->pos) == CParser::PARSE_FAIL) { return false; } if ($this->function_parser->parse($this->function_macro_parser->getFunction()) == CParser::PARSE_FAIL) { return false; } $this->pos += $this->function_macro_parser->getLength() - 1; $function_param_list = []; for ($n = 0; $n < $this->function_parser->getParamsNum(); $n++) { $function_param_list[] = $this->function_parser->getParam($n); } $this->result->addToken(CTriggerExpressionParserResult::TOKEN_TYPE_FUNCTION_MACRO, $this->function_macro_parser->getMatch(), $startPos, $this->function_macro_parser->getLength(), ['host' => $this->function_macro_parser->getHost(), 'item' => $this->function_macro_parser->getItem(), 'function' => $this->function_macro_parser->getFunction(), 'functionName' => $this->function_parser->getFunction(), 'functionParams' => $function_param_list]); $this->expressions[] = ['expression' => $this->function_macro_parser->getMatch(), 'pos' => $startPos, 'host' => $this->function_macro_parser->getHost(), 'item' => $this->function_macro_parser->getItem(), 'function' => $this->function_macro_parser->getFunction(), 'functionName' => $this->function_parser->getFunction(), 'functionParam' => $this->function_parser->getParameters(), 'functionParamList' => $function_param_list]; return true; }