1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22/**
23 * A parser for Prometheus output.
24 */
25class CPrometheusOutputParser extends CParser {
26
27	private $options = [
28		'usermacros' => false,
29		'lldmacros' => false
30	];
31
32	private $user_macro_parser;
33
34	public function __construct($options = []) {
35		if (array_key_exists('usermacros', $options)) {
36			$this->options['usermacros'] = $options['usermacros'];
37		}
38		if (array_key_exists('lldmacros', $options)) {
39			$this->options['lldmacros'] = $options['lldmacros'];
40		}
41
42		if ($this->options['usermacros']) {
43			$this->user_macro_parser = new CUserMacroParser();
44		}
45		if ($this->options['lldmacros']) {
46			$this->lld_macro_parser = new CLLDMacroParser();
47		}
48	}
49
50	/**
51	 * Parse the given source string.
52	 *
53	 * @param string $source  Source string that needs to be parsed.
54	 * @param int    $pos     Position offset.
55	 */
56	public function parse($source, $pos = 0) {
57		$this->length = 0;
58		$this->match = '';
59
60		$p = $pos;
61
62		if (!$this->parseLabelName($source, $p)) {
63			return self::PARSE_FAIL;
64		}
65
66		$this->length = $p - $pos;
67		$this->match = substr($source, $pos, $this->length);
68
69		return isset($source[$p]) ? self::PARSE_SUCCESS_CONT : self::PARSE_SUCCESS;
70	}
71
72	/**
73	 * Parse label names. It must follow the [a-zA-Z_][a-zA-Z0-9_]* regular expression. User macros and LLD macros
74	 * are allowed.
75	 *
76	 * @param string $source  [IN]      Source string that needs to be parsed.
77	 * @param int    $pos     [IN/OUT]  Position offset.
78	 *
79	 * @return bool
80	 */
81	private function parseLabelName($source, &$pos) {
82		if (preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*/', substr($source, $pos), $matches)) {
83			$pos += strlen($matches[0]);
84
85			return true;
86		}
87		elseif ($this->options['usermacros'] && $this->user_macro_parser->parse($source, $pos) != self::PARSE_FAIL) {
88			$pos += $this->user_macro_parser->getLength();
89
90			return true;
91		}
92		elseif ($this->options['lldmacros'] && $this->lld_macro_parser->parse($source, $pos) != self::PARSE_FAIL) {
93			$pos += $this->lld_macro_parser->getLength();
94
95			return true;
96		}
97
98		return false;
99	}
100}
101