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; package dev.dinauer.monitoring.nodes.client;
import dev.dinauer.service.PodService; import dev.dinauer.service.PodService;
import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.api.model.Pod;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import java.io.IOException; import java.io.IOException;
@ -20,7 +23,6 @@ import java.util.Map;
public class NodeDiskService public class NodeDiskService
{ {
private static final Map<String, String> label = Map.ofEntries(Map.entry("dev.dinauer.kubooboo/component", "node-monitor")); private static final Map<String, String> label = Map.ofEntries(Map.entry("dev.dinauer.kubooboo/component", "node-monitor"));
private static final Integer PORT = 8080;
@Inject @Inject
Logger LOG; Logger LOG;
@ -33,23 +35,44 @@ public class NodeDiskService
Map<String, NodeDiskMetrics> result = new HashMap<>(); Map<String, NodeDiskMetrics> result = new HashMap<>();
List<Pod> pods = podService.findByLabels(label); List<Pod> pods = podService.findByLabels(label);
for (Pod pod : pods) for (Pod pod : pods)
{
Map.Entry<String, NodeDiskMetrics> entry = getDiskMetricsForPod(pod);
if (entry != null)
{
result.put(entry.getKey(), entry.getValue());
}
}
return result;
}
private Map.Entry<String, NodeDiskMetrics> getDiskMetricsForPod(Pod pod)
{ {
String nodeName = pod.getSpec().getNodeName(); String nodeName = pod.getSpec().getNodeName();
String ip = pod.getStatus().getPodIP(); String ip = pod.getStatus().getPodIP();
LOG.infof("Collect disk monitoring for node %s", nodeName); String port = "8080";
try (HttpClient client = HttpClient.newBuilder().build()) try (HttpClient client = HttpClient.newBuilder().build())
{ {
HttpRequest request = HttpRequest.newBuilder().uri(new URI(String.format("http://%s:%s", ip, PORT))).GET().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()); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
NodeDiskMetrics metrics = parse(response.body()); NodeDiskMetrics metrics = parse(response.body());
result.put(nodeName, metrics); return Map.entry(nodeName, metrics);
} }
catch (URISyntaxException | IOException | InterruptedException e) catch (URISyntaxException | IOException | InterruptedException e)
{ {
LOG.errorf("Failed to collect disk monitoring for node %s (Pod IP: %s)", nodeName, ip); LOG.errorf("Failed to collect disk monitoring for node %s (Pod IP: %s)", nodeName, ip);
} }
return null;
} }
return result;
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) private NodeDiskMetrics parse(String input)