-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseClient.php
More file actions
164 lines (140 loc) · 4.64 KB
/
Copy pathBaseClient.php
File metadata and controls
164 lines (140 loc) · 4.64 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
<?php
namespace Icinga\Module\Perfdatagraphselasticsearch\Client;
use Icinga\Module\Perfdatagraphselasticsearch\Transport\Transport;
use Icinga\Application\Logger;
use Icinga\Util\Json;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Query;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
use DateInterval;
use DateTime;
use Exception;
/**
* BaseClient
*/
abstract class BaseClient
{
protected Transport $transport;
protected int $maxDataPoints;
/**
* parseDuration parses the duration string from the frontend
* into something we can use with the API (from parameter).
*
* @param string $duration ISO8601 Duration
* @param DateTime $now current time (used in testing)
* @return string
*/
public function parseDuration(\DateTime $now, string $duration): string
{
try {
$int = new DateInterval($duration);
} catch (Exception $e) {
Logger::error('Failed to parse date interval: %s', $e);
$int = new DateInterval('PT12H');
}
$now->sub($int);
return $now->format('Y-m-d\TH:i:s');
}
/**
* isIncluded checks if the given metric should be included in the response
*
* @param string $metricname
* @param array $includeMetrics
* @return bool
*/
protected function isIncluded($metricname, array $includeMetrics = []): bool
{
// All are included if not set
if (count($includeMetrics) === 0) {
return true;
}
foreach ($includeMetrics as $pattern) {
if (fnmatch($pattern, $metricname)) {
return true;
}
}
return false;
}
/**
* isExcluded checks if the given metric should be excluded from the response
*
* @param string $metricname
* @param array $excludeMetrics
* @return bool
*/
protected function isExcluded($metricname, array $excludeMetrics = []): bool
{
// None are exlucded if not set
if (count($excludeMetrics) === 0) {
return false;
}
foreach ($excludeMetrics as $pattern) {
if (fnmatch($pattern, $metricname)) {
return true;
}
}
return false;
}
protected function createQuery(array $params): string
{
return Query::build($params);
}
protected function extractArgument(array &$params, string $arg): mixed
{
if (array_key_exists($arg, $params) === true) {
$value = $params[$arg];
$value = (is_object($value) && !is_iterable($value)) ?
(array) $value :
$value;
unset($params[$arg]);
return $value;
} else {
return null;
}
}
public function search(array $params = [])
{
$index = $this->extractArgument($params, 'index');
$body = $this->extractArgument($params, 'body');
$query = $this->createQuery($params);
$uri = isset($index) ? "/$index/_search" : '_search';
$uri = $uri . '?' . $query;
$method = isset($body) ? 'POST' : 'GET';
$body = isset($body) ? Json::encode($body) : null;
$req = new Request($method, $uri, [], $body);
$response = $this->transport->sendRequest($req);
$responseBody = $response->getBody()->getContents();
try {
$d = Json::decode($responseBody, true);
} catch (JsonDecodeException $e) {
Logger::error('Failed to decode response: %s', $e);
return [];
}
return $d;
}
/**
* status tests connectivity to the Elasticsearch cluster
* @return array
*/
public function status(): array
{
$req = new Request('GET', '/', [], null);
try {
$response = $this->transport->sendRequest($req);
return ['output' => $response->getBody()->getContents()];
} catch (ConnectException $e) {
return ['output' => 'Connection error: ' . $e->getMessage(), 'error' => true];
} catch (RequestException $e) {
if ($e->hasResponse()) {
return ['output' => 'HTTP error: ' . $e->getResponse()->getStatusCode() . ' - ' .
$e->getResponse()->getReasonPhrase(), 'error' => true];
} else {
return ['output' => 'Request error: ' . $e->getMessage(), 'error' => true];
}
} catch (Exception $e) {
return ['output' => 'General error: ' . $e->getMessage(), 'error' => true];
}
return ['output' => 'Unknown error', 'error' => true];
}
}