Add Disk Usage

This commit is contained in:
andreas.dinauer 2025-11-16 15:50:37 +01:00
parent eebb2714df
commit eacb11d6dd

View File

@ -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<String, String> 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<Pod> 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<String, NodeDiskMetrics> entry = getDiskMetricsForPod(pod);
if (entry != null)
{
HttpRequest request = HttpRequest.newBuilder().uri(new URI(String.format("http://%s:%s", ip, PORT))).GET().build();
HttpResponse<String> 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<String, NodeDiskMetrics> 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<String> 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<String, String> result = new HashMap<>();