From eacb11d6dd376418e575b1c8bce2911af08248ae Mon Sep 17 00:00:00 2001 From: "andreas.dinauer" Date: Sun, 16 Nov 2025 15:50:37 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Disk=20Usage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nodes/client/NodeDiskService.java | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main/java/dev/dinauer/monitoring/nodes/client/NodeDiskService.java b/src/main/java/dev/dinauer/monitoring/nodes/client/NodeDiskService.java index 06779af..a074801 100644 --- a/src/main/java/dev/dinauer/monitoring/nodes/client/NodeDiskService.java +++ b/src/main/java/dev/dinauer/monitoring/nodes/client/NodeDiskService.java @@ -1,9 +1,12 @@ package dev.dinauer.monitoring.nodes.client; import dev.dinauer.service.PodService; +import io.fabric8.kubernetes.api.model.Node; import io.fabric8.kubernetes.api.model.Pod; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import org.eclipse.microprofile.config.ConfigProvider; +import org.eclipse.microprofile.config.inject.ConfigProperty; import org.jboss.logging.Logger; import java.io.IOException; @@ -20,7 +23,6 @@ import java.util.Map; public class NodeDiskService { private static final Map label = Map.ofEntries(Map.entry("dev.dinauer.kubooboo/component", "node-monitor")); - private static final Integer PORT = 8080; @Inject Logger LOG; @@ -34,24 +36,45 @@ public class NodeDiskService List pods = podService.findByLabels(label); for (Pod pod : pods) { - String nodeName = pod.getSpec().getNodeName(); - String ip = pod.getStatus().getPodIP(); - LOG.infof("Collect disk monitoring for node %s", nodeName); - try (HttpClient client = HttpClient.newBuilder().build()) + Map.Entry entry = getDiskMetricsForPod(pod); + if (entry != null) { - HttpRequest request = HttpRequest.newBuilder().uri(new URI(String.format("http://%s:%s", ip, PORT))).GET().build(); - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - NodeDiskMetrics metrics = parse(response.body()); - result.put(nodeName, metrics); - } - catch (URISyntaxException | IOException | InterruptedException e) - { - LOG.errorf("Failed to collect disk monitoring for node %s (Pod IP: %s)", nodeName, ip); + result.put(entry.getKey(), entry.getValue()); } } return result; } + private Map.Entry getDiskMetricsForPod(Pod pod) + { + String nodeName = pod.getSpec().getNodeName(); + String ip = pod.getStatus().getPodIP(); + String port = "8080"; + try (HttpClient client = HttpClient.newBuilder().build()) + { + LOG.infof("Collect disk monitoring for node %s", nodeName); + HttpRequest request = HttpRequest.newBuilder().uri(getURI(ip, port)).GET().build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + NodeDiskMetrics metrics = parse(response.body()); + return Map.entry(nodeName, metrics); + } + catch (URISyntaxException | IOException | InterruptedException e) + { + LOG.errorf("Failed to collect disk monitoring for node %s (Pod IP: %s)", nodeName, ip); + } + return null; + } + + private URI getURI(String ip, String port) throws URISyntaxException + { + String profile = ConfigProvider.getConfig().getValue("quarkus.profile", String.class); + if ("prod".equals(profile)) + { + return new URI(String.format("http://%s:%s", ip, port)); + } + return new URI("http://localhost:8080"); + } + private NodeDiskMetrics parse(String input) { Map result = new HashMap<>();