-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathElasticsearchClient.php
More file actions
291 lines (241 loc) · 10.2 KB
/
Copy pathElasticsearchClient.php
File metadata and controls
291 lines (241 loc) · 10.2 KB
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
<?php
namespace Icinga\Module\Perfdatagraphselasticsearch\Client;
use Icinga\Module\Perfdatagraphselasticsearch\Transport\Transport;
use Icinga\Module\Perfdatagraphselasticsearch\Transport\HostPool;
use Icinga\Module\Perfdatagraphs\Model\PerfdataResponse;
use Icinga\Module\Perfdatagraphs\Model\PerfdataSet;
use Icinga\Module\Perfdatagraphs\Model\PerfdataSeries;
use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Util\Json;
use DateTime;
use Exception;
use GuzzleHttp\Client;
/**
* ElasticsearchClient is used with with Icinga2 ElasticsearchWriter
*/
class ElasticsearchClient extends BaseClient implements ESInterface
{
protected string $index;
public function __construct(
string $urls,
string $username,
string $password,
int $maxDataPoints,
int $timeout,
bool $tlsVerify,
string $index = 'icinga2'
) {
$u = explode(',', $urls);
$HTTPClient = new Client([
'timeout' => $timeout,
'verify' => $tlsVerify
]);
$pool = new HostPool($HTTPClient);
$pool->setHosts($u);
$transport = new Transport($HTTPClient, $pool);
if (isset($username)) {
$transport->setBasicAuth($username, $password = '');
}
$this->transport = $transport;
$this->maxDataPoints = $maxDataPoints;
}
/**
* fromConfig returns a new Elasticsearch Client from this module's configuration
*
* @param Config $moduleConfig configuration to load (used for testing)
* @return $this
*/
public static function fromConfig(Config $moduleConfig = null): ESInterface
{
$default = [
'api_url' => 'http://localhost:9200',
'api_index' => 'icinga2',
'api_timeout' => 10,
'api_max_data_points' => 5000,
'api_username' => '',
'api_password' => '',
'api_tls_insecure' => false,
];
// Try to load the configuration
if ($moduleConfig === null) {
try {
Logger::debug('Loaded Perfdata Graphs Elasticsearch module configuration to get Config');
$moduleConfig = Config::module('perfdatagraphselasticsearch');
} catch (Exception $e) {
Logger::error('Failed to load Perfdata Graphs Elasticsearch module configuration: %s', $e);
return $default;
}
}
$baseURI = rtrim($moduleConfig->get('elasticsearch', 'api_url', $default['api_url']), '/');
$index = rtrim($moduleConfig->get('elasticsearch', 'api_index', $default['api_index']), 'icinga2');
$timeout = (int) $moduleConfig->get('elasticsearch', 'api_timeout', $default['api_timeout']);
$username = $moduleConfig->get('elasticsearch', 'api_username', $default['api_username']);
$password = $moduleConfig->get('elasticsearch', 'api_password', $default['api_password']);
// Hint: We use a "skip TLS" logic in the UI, but Guzzle uses "verify TLS"
$tlsVerify = !(bool) $moduleConfig->get('elasticsearch', 'api_tls_insecure', $default['api_tls_insecure']);
$maxDataPoints = (int) $moduleConfig->get('elasticsearch', 'api_max_data_points', $default['api_max_data_points']);
return new static($baseURI, $username, $password, $maxDataPoints, $timeout, $tlsVerify, $index);
}
protected function getDatasetKeys(array $fields): array
{
// There can be multiple datasets (pl, rta, etc) in a document,
// we need to get their keys to fetch the values
// "@timestamp": ["1751293383.713"],
// "check_result.perfdata./.unit.keyword": ["bytes"],
// "check_result.perfdata./.value": [14774000000],
// "check_result.perfdata./.warn": [80176000000],
// "check_result.perfdata./.unit": ["bytes"],
// "check_result.perfdata./.crit": [90198000000]
$keys = preg_grep('/check_result\.perfdata.*\.value/', array_keys($fields));
return $keys;
}
/**
* request calls the Opensearch HTTP API, decodes and returns the data.
*
* @param string $hostName host name for the performance data query
* @param string $serviceName service name for the performance data query
* @param string $checkCommand checkcommand name for the performance data query
* @param string $from specifies the beginning for which to fetch the data
* @param bool $isHostCheck is this a hostcheck, so that we can modify the query
* @param array $includeMetrics metrics that should included
* @param array $excludeMetrics metrics that are excluded
* @return PerfdataResponse
*/
public function fetchMetrics(
string $hostName,
string $serviceName,
string $checkCommand,
string $from,
bool $isHostCheck,
array $includeMetrics,
array $excludeMetrics,
int $checkInterval = 0
): PerfdataResponse {
$now = new DateTime();
$from = $this->parseDuration($now, $from);
$params = [
'size' => 2000,
'sort' => '@timestamp:asc',
'body' => [
'_source' => false,
'fields' => [
'check_result.perfdata.*',
['field' => '@timestamp', 'format' => 'epoch_second' ]
],
'query' => [
'bool' => [
'must' => [
[ 'term' => [ 'host.keyword' => $hostName ] ],
[ 'term' => [ 'service.keyword' => $serviceName ] ],
[ 'term' => [ 'check_command.keyword' => $checkCommand ] ]
],
'filter' => [
'range' => [ 'timestamp' => [ 'gte' => $from, 'lte' => 'now', ] ]
],
]
]
]
];
// If it's a hostalive check we dont need the service term
if ($isHostCheck) {
$params['body']['query']['bool']['must'] = [
[ 'term' => [ 'host.keyword' => $hostName ] ],
[ 'term' => [ 'check_command.keyword' => $checkCommand ] ]
];
}
$searchAfter = null;
$pfr = new PerfdataResponse();
// Where we store the data for each page of hits.
$timestamps = [];
$values = [];
$warnings = [];
$criticals = [];
$units = [];
do {
if ($searchAfter !== null) {
$params['body']['search_after'] = [$searchAfter];
}
$response = $this->search($params);
if (array_key_exists('error', $response)) {
$pfr->addError(Json::encode($response['error']));
return $pfr;
}
$hits = [];
if (array_key_exists('hits', $response)) {
$hits = $response['hits']['hits'] ?? [];
}
foreach ($hits as $d) {
$doc = $d['fields'];
$keys = $this->getDatasetKeys($doc);
foreach ($keys as $valueKey) {
$metricname = preg_replace('/\.value$/', '', str_replace('check_result.perfdata.', '', $valueKey));
if (!$this->isIncluded($metricname, $includeMetrics)) {
continue;
}
if ($this->isExcluded($metricname, $excludeMetrics)) {
continue;
}
$unitKey = preg_replace('/\.value$/', '.unit', $valueKey);
$warnKey = preg_replace('/\.value$/', '.warn', $valueKey);
$critKey = preg_replace('/\.value$/', '.crit', $valueKey);
if (!isset($values[$metricname])) {
$values[$metricname] = [];
}
if (!isset($warnings[$metricname])) {
$warnings[$metricname] = [];
}
if (!isset($criticals[$metricname])) {
$criticals[$metricname] = [];
}
if (array_key_exists($unitKey, $doc)) {
$units[$metricname][] = end($doc[$unitKey]);
}
$timestamps[$metricname][] = (int) end($doc['@timestamp']);
if (array_key_exists($valueKey, $doc)) {
$values[$metricname][] = end($doc[$valueKey]);
} else {
$values[$metricname][] = null;
}
if (array_key_exists($warnKey, $doc)) {
$warnings[$metricname][] = end($doc[$warnKey]);
} else {
$warnings[$metricname][] = null;
}
if (array_key_exists($critKey, $doc)) {
$criticals[$metricname][] = end($doc[$critKey]);
} else {
$criticals[$metricname][] = null;
}
}
}
$hitCount = count($hits);
$searchAfter = end($hits)['sort'][0] ?? null;
unset($response);
unset($hits);
} while ($hitCount > 0);
// Add it to the PerfdataResponse
foreach (array_keys($values) as $metric) {
$u = '';
if (array_key_exists($metric, $units)) {
$u = end($units[$metric]);
}
$s = new PerfdataSet($metric, $u);
$s->setTimestamps($timestamps[$metric]);
if (array_key_exists($metric, $values)) {
$v = new PerfdataSeries('value', $values[$metric]);
$s->addSeries($v);
}
if (array_key_exists($metric, $warnings) && !empty($warnings)) {
$w = new PerfdataSeries('warning', $warnings[$metric]);
$s->addSeries($w);
}
if (array_key_exists($metric, $criticals) && !empty($criticals)) {
$c = new PerfdataSeries('critical', $criticals[$metric]);
$s->addSeries($c);
}
$pfr->addDataset($s);
}
return $pfr;
}
}