Skip to content

Commit b6f535a

Browse files
committed
[squash]
1 parent 03a4b38 commit b6f535a

2 files changed

Lines changed: 34 additions & 43 deletions

File tree

application/forms/PerfdataGraphsElasticsearchConfigForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function createElements(array $formData)
8282
. ' The module will use an aggregations query to downsample to this number.'
8383
),
8484
'required' => false,
85-
'placeholder' => 5000,
85+
'placeholder' => 10000,
8686
]);
8787
}
8888

library/Perfdatagraphselasticsearch/Client/OTLPMetricsClient.php

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct(
5050
$transport->setBasicAuth($username, $password);
5151
}
5252

53+
$this->index = $index;
5354
$this->transport = $transport;
5455
$this->maxDataPoints = $maxDataPoints;
5556
}
@@ -66,7 +67,7 @@ public static function fromConfig(Config $moduleConfig = null): ESInterface
6667
'api_url' => 'http://localhost:9200',
6768
'api_index' => 'icinga2',
6869
'api_timeout' => 10,
69-
'api_max_data_points' => 5000,
70+
'api_max_data_points' => 10000,
7071
'api_username' => '',
7172
'api_password' => '',
7273
'api_tls_insecure' => false,
@@ -141,26 +142,40 @@ public function fetchMetrics(
141142
$now = new DateTime();
142143
$from = $this->parseDuration($now, $from);
143144

144-
$esql = sprintf(
145-
"TS %s",
146-
'.ds-metrics-generic.otel-*'
147-
);
145+
// The index for the query
146+
$esql = sprintf("TS %s", $this->index);
147+
148+
// The service or host filter
149+
if (!$isHostCheck) {
150+
$esql .= sprintf(
151+
"| WHERE resource.attributes.icinga2.host.name == \"%s\""
152+
. "AND resource.attributes.icinga2.service.name == \"%s\""
153+
. "AND resource.attributes.icinga2.command.name == \"%s\"",
154+
$hostName,
155+
$serviceName,
156+
$checkCommand,
157+
);
158+
} else {
159+
$esql .= sprintf(
160+
"| WHERE resource.attributes.icinga2.host.name == \"%s\""
161+
. "AND resource.attributes.icinga2.command.name == \"%s\"",
162+
$hostName,
163+
$checkCommand,
164+
);
165+
}
148166

149-
$esql .= sprintf(
150-
"| WHERE resource.attributes.icinga2.host.name == \"%s\" AND resource.attributes.icinga2.service.name == \"%s\" AND resource.attributes.icinga2.command.name == \"%s\" AND @timestamp >= TO_DATETIME(\"%s\") AND @timestamp <= NOW()",
151-
$hostName,
152-
$serviceName,
153-
$checkCommand,
154-
$from
155-
);
167+
$esql .= sprintf("AND @timestamp >= TO_DATETIME(\"%s\") AND @timestamp <= NOW()", $from);
156168

169+
// The aggregated values we want
157170
$esql .= sprintf(
158-
"| STATS avg_threshold = AVG(AVG_OVER_TIME(metrics.state_check.threshold)), avg_perfdata = AVG(AVG_OVER_TIME(metrics.state_check.perfdata)) BY attributes.perfdata_label, attributes.threshold_type, bucket = TBUCKET(%s seconds)",
171+
"| STATS avg_threshold = AVG(AVG_OVER_TIME(metrics.state_check.threshold)),"
172+
. "avg_perfdata = AVG(AVG_OVER_TIME(metrics.state_check.perfdata)) "
173+
. "BY attributes.perfdata_label, attributes.threshold_type, bucket = TBUCKET(%s seconds)",
159174
$step,
160175
);
161176

162-
163-
$esql .= "| EVAL bucket_epoch_ms = TO_LONG(bucket), bucket_epoch_s = TO_LONG(bucket) / 1000 | DROP bucket | SORT bucket_epoch_ms";
177+
// Sort and transforming the bucket timestamp to seconds
178+
$esql .= "| EVAL bucket_epoch_s = TO_LONG(bucket) / 1000 | DROP bucket | SORT bucket_epoch_s";
164179

165180
$pfr = new PerfdataResponse();
166181

@@ -176,32 +191,6 @@ public function fetchMetrics(
176191
$values = $response['values'];
177192
}
178193

179-
// "columns" : [
180-
// {
181-
// "name" : "avg_threshold",
182-
// "type" : "double"
183-
// },
184-
// {
185-
// "name" : "avg_perfdata",
186-
// "type" : "double"
187-
// },
188-
// {
189-
// "name" : "attributes.perfdata_label",
190-
// "type" : "keyword"
191-
// },
192-
// {
193-
// "name" : "attributes.threshold_type",
194-
// "type" : "keyword"
195-
// },
196-
// {
197-
// "name" : "bucket_epoch_ms",
198-
// "type" : "long"
199-
// },
200-
// {
201-
// "name" : "bucket_epoch_s",
202-
// "type" : "long"
203-
// }
204-
// ],
205194
$timestamps = [];
206195

207196
$lastTS = 0;
@@ -210,7 +199,7 @@ public function fetchMetrics(
210199
$avgPerfdata = $val[1] ?? null;
211200
$label = $val[2] ?? null;
212201
$type = $val[3] ?? null;
213-
$ts = $val[5] ?? null;
202+
$ts = $val[4] ?? null;
214203

215204
if (!$this->isIncluded($label, $includeMetrics)) {
216205
continue;
@@ -219,6 +208,8 @@ public function fetchMetrics(
219208
continue;
220209
}
221210

211+
// The timestamp for all labels is the same, so we only add a new ts if it increases.
212+
// There might be a better way to do this via the query?
222213
if ($ts > $lastTS) {
223214
$timestamps[] = $ts;
224215
$lastTS = $ts;

0 commit comments

Comments
 (0)