Compare commits
No commits in common. "b9514ba98eaebd31bc04c70b30624f1030181200" and "14c78e167b915817f0d9381044a23747dd5f849d" have entirely different histories.
b9514ba98e
...
14c78e167b
@ -23,7 +23,7 @@ public class MonitoringService
|
|||||||
{
|
{
|
||||||
case LABEL ->
|
case LABEL ->
|
||||||
{
|
{
|
||||||
return podService.findByNamespaceAndLabels(targetConfig.getNamespace(), targetConfig.getLabels()).stream().filter(pod -> pod.getStatus().getPhase().equals("Running")).toList();
|
return podService.findByLabels(targetConfig.getNamespace(), targetConfig.getLabels()).stream().filter(pod -> pod.getStatus().getPhase().equals("Running")).toList();
|
||||||
}
|
}
|
||||||
case DEPLOYMENT, STATEFUL_SET ->
|
case DEPLOYMENT, STATEFUL_SET ->
|
||||||
{
|
{
|
||||||
|
|||||||
@ -3,8 +3,6 @@ package dev.dinauer.monitoring;
|
|||||||
import dev.dinauer.ProcessRunner;
|
import dev.dinauer.ProcessRunner;
|
||||||
import dev.dinauer.monitoring.nodes.MonitoredNode;
|
import dev.dinauer.monitoring.nodes.MonitoredNode;
|
||||||
import dev.dinauer.monitoring.nodes.NodeMetrics;
|
import dev.dinauer.monitoring.nodes.NodeMetrics;
|
||||||
import dev.dinauer.monitoring.nodes.client.NodeDiskMetrics;
|
|
||||||
import dev.dinauer.monitoring.nodes.client.NodeDiskService;
|
|
||||||
import dev.dinauer.service.PodService;
|
import dev.dinauer.service.PodService;
|
||||||
import dev.dinauer.utils.ClientProvider;
|
import dev.dinauer.utils.ClientProvider;
|
||||||
import io.fabric8.kubernetes.api.model.Node;
|
import io.fabric8.kubernetes.api.model.Node;
|
||||||
@ -26,16 +24,12 @@ public class TopNodesService
|
|||||||
@Inject
|
@Inject
|
||||||
PodService podService;
|
PodService podService;
|
||||||
|
|
||||||
@Inject
|
|
||||||
NodeDiskService nodeDiskService;
|
|
||||||
|
|
||||||
public List<MonitoredNode> findAll()
|
public List<MonitoredNode> findAll()
|
||||||
{
|
{
|
||||||
List<MonitoredNode> result = new ArrayList<>();
|
List<MonitoredNode> result = new ArrayList<>();
|
||||||
|
|
||||||
List<String> stats = runTopNodesCommand();
|
List<String> stats = runTopNodesCommand();
|
||||||
Map<String, Integer> podsOnNodes = countPods();
|
Map<String, Integer> podsOnNodes = countPods();
|
||||||
Map<String, NodeDiskMetrics> nodeDiskMetrics = nodeDiskService.getDiskMetrics();
|
|
||||||
for(String nodeName : stats)
|
for(String nodeName : stats)
|
||||||
{
|
{
|
||||||
String[] parts = nodeName.split("\\s+");
|
String[] parts = nodeName.split("\\s+");
|
||||||
@ -50,17 +44,8 @@ public class TopNodesService
|
|||||||
Integer totalCpu = Integer.parseInt(node.getStatus().getAllocatable().get("cpu").getAmount()) * 1000;
|
Integer totalCpu = Integer.parseInt(node.getStatus().getAllocatable().get("cpu").getAmount()) * 1000;
|
||||||
Integer totalMemory = extractMemory(node.getStatus().getAllocatable().get("memory").getAmount());
|
Integer totalMemory = extractMemory(node.getStatus().getAllocatable().get("memory").getAmount());
|
||||||
Integer totalPods = podsOnNodes.get(node.getMetadata().getName());
|
Integer totalPods = podsOnNodes.get(node.getMetadata().getName());
|
||||||
NodeDiskMetrics diskMetrics = nodeDiskMetrics.get(node.getMetadata().getName());
|
NodeMetrics metrics = new NodeMetrics(absoluteCpu, relativeCpu, totalCpu, absoluteMemory, relativeMemory, totalMemory, totalPods, null, null);
|
||||||
if (diskMetrics != null)
|
result.add(new MonitoredNode(node, metrics));
|
||||||
{
|
|
||||||
NodeMetrics metrics = new NodeMetrics(absoluteCpu, relativeCpu, totalCpu, absoluteMemory, relativeMemory, totalMemory, totalPods, diskMetrics.relativeDiskUsage(), diskMetrics.totalDiskSpace());
|
|
||||||
result.add(new MonitoredNode(node, metrics));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
NodeMetrics metrics = new NodeMetrics(absoluteCpu, relativeCpu, totalCpu, absoluteMemory, relativeMemory, totalMemory, totalPods, null, null);
|
|
||||||
result.add(new MonitoredNode(node, metrics));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
package dev.dinauer.monitoring.nodes;
|
package dev.dinauer.monitoring.nodes;
|
||||||
|
|
||||||
public record NodeMetrics(Integer absoluteCpuUsage, Integer relativeCpuUsage, Integer totalCpu, Integer absoluteMemory, Integer relativeMemory, Integer totalMemory, Integer runningPods, Integer relativeDiskUsage, Long totalDiskSpace)
|
public record NodeMetrics(Integer absoluteCpuUsage, Integer relativeCpuUsage, Integer totalCpu, Integer absoluteMemory, Integer relativeMemory, Integer totalMemory, Integer runningPods, Integer relativeDiskUsage, Integer totalDiskSpace)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +0,0 @@
|
|||||||
package dev.dinauer.monitoring.nodes.client;
|
|
||||||
|
|
||||||
public record NodeDiskMetrics(Integer relativeDiskUsage, Long totalDiskSpace)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@ -4,69 +4,15 @@ import dev.dinauer.service.PodService;
|
|||||||
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.jboss.logging.Logger;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.net.http.HttpResponse;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
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 Integer PORT = 8080;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
Logger LOG;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PodService podService;
|
PodService podService;
|
||||||
|
|
||||||
public Map<String, NodeDiskMetrics> getDiskMetrics()
|
|
||||||
{
|
|
||||||
Map<String, NodeDiskMetrics> result = new HashMap<>();
|
|
||||||
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())
|
|
||||||
{
|
|
||||||
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", nodeName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private NodeDiskMetrics parse(String input)
|
|
||||||
{
|
|
||||||
Map<String, String> result = new HashMap<>();
|
|
||||||
for (String line : input.split("\\s+"))
|
|
||||||
{
|
|
||||||
String[] sections = line.split(":");
|
|
||||||
if (sections.length == 2)
|
|
||||||
{
|
|
||||||
result.put(sections[0], sections[1]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG.errorf("Cannot parse metrics line '%s'", line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new NodeDiskMetrics(Integer.parseInt(result.get("percentage_used")), Long.parseLong(result.get("total-space")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package dev.dinauer.service;
|
|||||||
|
|
||||||
import dev.dinauer.monitoring.TopNodesService;
|
import dev.dinauer.monitoring.TopNodesService;
|
||||||
import dev.dinauer.monitoring.nodes.MonitoredNode;
|
import dev.dinauer.monitoring.nodes.MonitoredNode;
|
||||||
|
import io.fabric8.kubernetes.api.model.Node;
|
||||||
import io.fabric8.kubernetes.client.Watch;
|
import io.fabric8.kubernetes.client.Watch;
|
||||||
import io.fabric8.kubernetes.client.Watcher;
|
import io.fabric8.kubernetes.client.Watcher;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
|||||||
@ -57,22 +57,17 @@ public class PodService implements ResourceService<Pod>
|
|||||||
StatefulSet set = apps.statefulSets().inNamespace(namespace).withName(name).get();
|
StatefulSet set = apps.statefulSets().inNamespace(namespace).withName(name).get();
|
||||||
if (set != null)
|
if (set != null)
|
||||||
{
|
{
|
||||||
return findByNamespaceAndLabels(namespace, set.getSpec().getSelector().getMatchLabels());
|
return findByLabels(namespace, set.getSpec().getSelector().getMatchLabels());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Pod> findByNamespaceAndLabels(String namespace, Map<String, String> labels)
|
public List<Pod> findByLabels(String namespace, Map<String, String> labels)
|
||||||
{
|
{
|
||||||
return clientProvider.getClient().pods().inNamespace(namespace).withLabels(labels).list().getItems();
|
return clientProvider.getClient().pods().inNamespace(namespace).withLabels(labels).list().getItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Pod> findByLabels(Map<String, String> labels)
|
|
||||||
{
|
|
||||||
return clientProvider.getClient().pods().inAnyNamespace().withLabels(labels).list().getItems();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Pod> findAll()
|
public List<Pod> findAll()
|
||||||
{
|
{
|
||||||
return clientProvider.getClient().pods().inAnyNamespace().list().getItems();
|
return clientProvider.getClient().pods().inAnyNamespace().list().getItems();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user