예제 #1
0
 /**
  * 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)
 {
     // don't try to parse the expression if there's nothing to replace
     if (strpos($expression, '#') === false && strpos($expression, '&') === false && strpos($expression, '|') === false) {
         return $expression;
     }
     // find all the operators that need to be replaced
     $foundOperators = array();
     $pos = 0;
     while (isset($expression[$pos])) {
         switch ($expression[$pos]) {
             case '{':
                 // skip function macros
                 $result = $this->functionMacroParser->parse($expression, $pos);
                 // if it's not a function macro, try to parse it as an LLD macro
                 if (!$result) {
                     $result = $this->lldMacroParser->parse($expression, $pos);
                 }
                 if ($result) {
                     $pos += $result->length - 1;
                 }
                 // otherwise just continue as is, other macros don't contain any of these characters
                 break;
             case '&':
             case '|':
             case '#':
                 $foundOperators[$pos] = $expression[$pos];
                 break;
         }
         $pos++;
     }
     // replace the operators
     foreach (array_reverse($foundOperators, 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;
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
/**
 * 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;
}
 /**
  * Find function ids in trigger expression.
  *
  * @param string $expression
  *
  * @return array	where key is function id position in expression and value is function id
  */
 protected function findFunctions($expression)
 {
     $functionids = [];
     $functionid_parser = new CFunctionIdParser();
     $macro_parser = new CMacroParser(['{TRIGGER.VALUE}']);
     $user_macro_parser = new CUserMacroParser();
     for ($pos = 0, $i = 1; isset($expression[$pos]); $pos++) {
         if ($functionid_parser->parse($expression, $pos) != CParser::PARSE_FAIL) {
             $pos += $functionid_parser->getLength() - 1;
             $functionids[$i++] = substr($functionid_parser->getMatch(), 1, -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;
         }
     }
     if (array_key_exists(1, $functionids)) {
         $functionids[0] = $functionids[1];
     }
     return $functionids;
 }