1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* A parser for Prometheus pattern.
*/
class CPrometheusPatternParser extends CParser {
private $options = [
'usermacros' => false,
'lldmacros' => false
];
private $user_macro_parser;
private $lld_macro_parser;
public function __construct($options = []) {
if (array_key_exists('usermacros', $options)) {
$this->options['usermacros'] = $options['usermacros'];
}
if (array_key_exists('lldmacros', $options)) {
$this->options['lldmacros'] = $options['lldmacros'];
}
if ($this->options['usermacros']) {
$this->user_macro_parser = new CUserMacroParser();
}
if ($this->options['lldmacros']) {
$this->lld_macro_parser = new CLLDMacroParser();
}
}
/**
* Parse the given source string.
*
* metric { label1 =~ "value1" , label2 =" value2" } == number
*
* @param string $source Source string that needs to be parsed.
* @param int $pos Position offset.
*/
public function parse($source, $pos = 0) {
$this->length = 0;
$this->match = '';
$has_metric_name = false;
$p = $pos;
if ($this->parseMetric($source, $p)) {
$has_metric_name = true;
}
$p_tmp = $p;
if ($has_metric_name) {
self::skipWhitespaces($source, $p_tmp);
}
if ($this->parseLabelsValues($source, $p_tmp, $has_metric_name)) {
$p = $p_tmp;
}
elseif (!$has_metric_name) {
return self::PARSE_FAIL;
}
$p_tmp = $p;
self::skipWhitespaces($source, $p_tmp);
if ($this->parseComparisonOperator($source, $p_tmp)) {
self::skipWhitespaces($source, $p_tmp);
if ($this->parseNumber($source, $p_tmp)) {
$p = $p_tmp;
}
}
$this->length = $p - $pos;
$this->match = substr($source, $pos, $this->length);
return isset($source[$p]) ? self::PARSE_SUCCESS_CONT : self::PARSE_SUCCESS;
}
/**
* Move pointer at the end of whitespaces.
*
* @param string $source [IN] Source string that needs to be parsed.
* @param int $pos [IN/OUT] Position offset.
*
* @return bool
*/
private static function skipWhitespaces($source, &$pos) {
while (isset($source[$pos]) && ($source[$pos] === ' ' || $source[$pos] === "\t")) {
$pos++;
}
}
/**
* Parse metric parameter. Must follow the [a-zA-Z_:][a-zA-Z0-9_:]* regular expression. User macros and LLD macros
* are allowed.
*
* @param string $source [IN] Source string that needs to be parsed.
* @param int $pos [IN/OUT] Position offset.
*
* @return bool
*/
private function parseMetric($source, &$pos) {
if (preg_match('/^[a-zA-Z_:][a-zA-Z0-9_:]*/', substr($source, $pos), $matches)) {
$pos += strlen($matches[0]);
return true;
}
elseif ($this->options['usermacros'] && $this->user_macro_parser->parse($source, $pos) != self::PARSE_FAIL) {
$pos += $this->user_macro_parser->getLength();
return true;
}
elseif ($this->options['lldmacros'] && $this->lld_macro_parser->parse($source, $pos) != self::PARSE_FAIL) {
$pos += $this->lld_macro_parser->getLength();
return true;
}
return false;
}
/**
* Parse label names and label values. Label name must follow the [a-zA-Z_][a-zA-Z0-9_]* regular expression. After
* label name, an operator must follow. Allowed operators are: = and =~ After operator a quoted value must follow.
* Value can contain any string and can be empty. Each label name and label value pair can be separated by a comma.
* Trailing comma is allowed. Spaces are trimmed before and after each unit (label name, operator, label value
* and comma).
*
* @param string $source [IN] Source string that needs to be parsed.
* @param int $pos [IN/OUT] Position offset.
* @param bool $has_metric_label [IN/OUT] Returns true if __name__ is present.
*
* @return bool
*/
private function parseLabelValuePair($source, &$pos, &$has_metric_label) {
$p = $pos;
// Parse label name.
if (preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*/', substr($source, $p), $matches)) {
if ($matches[0] === '__name__') {
if ($has_metric_label) {
return false;
}
$has_metric_label = true;
}
$p += strlen($matches[0]);
}
elseif ($this->options['usermacros'] && $this->user_macro_parser->parse($source, $p) != self::PARSE_FAIL) {
$p += $this->user_macro_parser->getLength();
}
elseif ($this->options['lldmacros'] && $this->lld_macro_parser->parse($source, $pos) != self::PARSE_FAIL) {
$p += $this->lld_macro_parser->getLength();
}
else {
return false;
}
self::skipWhitespaces($source, $p);
// Parse operator.
if (!isset($source[$p]) || $source[$p] !== '=') {
return false;
}
$p++;
if (isset($source[$p]) && $source[$p] === '~') {
$p++;
}
self::skipWhitespaces($source, $p);
// Parse label value.
if (!isset($source[$p]) || $source[$p] !== '"') {
return false;
}
$p++;
while (isset($source[$p])) {
switch ($source[$p]) {
case '\\':
switch (isset($source[$p + 1]) ? $source[$p + 1] : null) {
case '\\':
case '"':
case 'n':
$p++;
break;
default:
return false;
}
break;
case '"':
break 2;
}
$p++;
}
if (!isset($source[$p]) || $source[$p] !== '"') {
return false;
}
$p++;
$pos = $p;
return true;
}
/**
* Parse label names and label value pairs as one parameter that is wrapped in curly braces. Spaces are trimmed
* before and after each curly braces and label value pairs. Two __name__ labels are not allowed.
*
* @param string $source [IN] Source string that needs to be parsed.
* @param int $pos [IN/OUT] Position offset.
* @param bool $has_metric_name [IN] Metric name is present in the pattern.
*
* @return bool
*/
private function parseLabelsValues($source, &$pos, $has_metric_name) {
$p = $pos;
if (!isset($source[$p]) || $source[$p] !== '{') {
return false;
}
$p++;
$has_metric_label = false;
while (true) {
self::skipWhitespaces($source, $p);
if (!$this->parseLabelValuePair($source, $p, $has_metric_label)) {
break;
}
self::skipWhitespaces($source, $p);
if (!isset($source[$p]) || $source[$p] !== ',') {
break;
}
$p++;
}
if ($has_metric_name && $has_metric_label) {
return false;
}
if (!isset($source[$p]) || $source[$p] !== '}') {
return false;
}
$p++;
$pos = $p;
return true;
}
/**
* Parse number. It can be with plus or minus sign, can use scientific notation, decimals points, can even be
* not a number or infinity. User and LLD macros are allowed.
*
* @param string $source [IN] Source string that needs to be parsed.
* @param int $pos [IN/OUT] Position offset.
*
* @return bool
*/
private function parseNumber($source, &$pos) {
$pattern_num = '[+-]?([0-9]+(\.[0-9]*)?|\.[0-9]+)(e[+-]?[0-9]+)?';
$pattern_inf = '[+-]?inf';
$pattern_nan = 'nan';
if (preg_match('/^('.$pattern_num.'|'.$pattern_inf.'|'.$pattern_nan.')/i', substr($source, $pos), $matches)) {
$pos += strlen($matches[0]);
return true;
}
elseif ($this->options['usermacros'] && $this->user_macro_parser->parse($source, $pos) != self::PARSE_FAIL) {
$pos += $this->user_macro_parser->getLength();
return true;
}
elseif ($this->options['lldmacros'] && $this->lld_macro_parser->parse($source, $pos) != self::PARSE_FAIL) {
$pos += $this->lld_macro_parser->getLength();
return true;
}
return false;
}
/**
* Parse the comparison operator. Currently only one comparison operator is allowed: ==
*
* @param string $source [IN] Source string that needs to be parsed.
* @param int $pos [IN/OUT] Position offset.
*
* @return bool
*/
private function parseComparisonOperator($source, &$pos) {
if (isset($source[$pos]) && substr($source, $pos, 2) === '==') {
$pos += 2;
return true;
}
return false;
}
}
|