Esempio n. 1
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;
}
Esempio n. 2
0
 /**
  * Parse key and parameters and put them into $this->parameters array.
  *
  * @param string	$data
  * @param int		$offset
  */
 public function parse($data, $offset = 0)
 {
     $this->length = 0;
     $this->match = '';
     $this->key = '';
     $this->parameters = [];
     for ($p = $offset; isset($data[$p]) && $this->isKeyChar($data[$p]); $p++) {
         // Code is not missing here.
     }
     // is key empty?
     if ($p == $offset) {
         $this->error = isset($data[$p]) ? $this->errorMessage(substr($data, $offset), 0) : _('key is empty');
         return self::PARSE_FAIL;
     }
     $_18_simple_check = false;
     // old-style simple checks
     if ($this->options['18_simple_checks'] && isset($data[$p]) && $data[$p] === ',') {
         $p++;
         $user_macro_parser = new CUserMacroParser();
         if ($user_macro_parser->parse($data, $p) != CParser::PARSE_FAIL) {
             $p += $user_macro_parser->getLength();
         } else {
             for (; isset($data[$p]) && $data[$p] > '0' && $data[$p] < '9'; $p++) {
                 // Code is not missing here.
             }
         }
         $_18_simple_check = true;
     }
     $this->key = substr($data, $offset, $p - $offset);
     $p2 = $p;
     if (!$_18_simple_check) {
         // Zapcat compatibility.
         for (; isset($data[$p2]) && $data[$p2] == '['; $p = $p2) {
             $_parameters = ['type' => self::PARAM_ARRAY, 'raw' => '', 'pos' => $p2 - $offset, 'parameters' => []];
             if (!$this->parseKeyParameters($data, $p2, $_parameters['parameters'])) {
                 break;
             }
             $_parameters['raw'] = substr($data, $p, $p2 - $p);
             $this->parameters[] = $_parameters;
         }
     }
     $this->length = $p - $offset;
     $this->match = substr($data, $offset, $this->length);
     if (!isset($data[$p])) {
         $this->error = '';
         return self::PARSE_SUCCESS;
     }
     $this->error = !isset($data[$p2]) ? _('unexpected end of key') : $this->errorMessage(substr($data, $offset), $p2 - $offset);
     return self::PARSE_SUCCESS_CONT;
 }
 /**
  * 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;
 }