Improve VPS health response DTO and metrics

This commit is contained in:
litoral05
2026-05-07 17:12:04 +01:00
parent ad9a053099
commit 6712a26b2a
3 changed files with 35 additions and 5 deletions
@@ -1,5 +1,6 @@
package com.litoralregas.vpnprovisioner.vps;
import com.litoralregas.vpnprovisioner.vps.dto.VpsHealthResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@@ -14,8 +15,8 @@ public class VpsController {
}
@GetMapping(value = "/health", produces = MediaType.APPLICATION_JSON_VALUE)
public String health() {
return wireGuardVpsService.getVpsHealthJson();
public VpsHealthResponse health() {
return wireGuardVpsService.getVpsHealth();
}
@PostMapping(
@@ -1,5 +1,8 @@
package com.litoralregas.vpnprovisioner.vps;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.litoralregas.vpnprovisioner.vps.dto.VpsHealthResponse;
import org.springframework.stereotype.Service;
import java.util.HashSet;
@@ -14,9 +17,11 @@ public class WireGuardVpsService {
Pattern.compile("\\b198\\.19\\.\\d{1,3}\\.\\d{1,3}\\b");
private final SshService sshService;
private final ObjectMapper objectMapper;
public WireGuardVpsService(SshService sshService) {
public WireGuardVpsService(SshService sshService, ObjectMapper objectMapper) {
this.sshService = sshService;
this.objectMapper = objectMapper;
}
public Set<String> findUsedVpnIps() {
@@ -66,7 +71,7 @@ public class WireGuardVpsService {
);
}
public String getVpsHealthJson() {
public VpsHealthResponse getVpsHealth() {
SshCommandResult result = sshService.executeOnConfiguredVps(
"sudo /usr/local/sbin/lr-vps-health"
);
@@ -77,7 +82,14 @@ public class WireGuardVpsService {
);
}
return result.stdout();
try {
return objectMapper.readValue(result.stdout(), VpsHealthResponse.class);
} catch (JsonProcessingException e) {
throw new IllegalStateException(
"Invalid VPS health JSON returned by script",
e
);
}
}
public String restoreLastWireGuardBackup() {
@@ -0,0 +1,17 @@
package com.litoralregas.vpnprovisioner.vps.dto;
public record VpsHealthResponse(
String wireGuardInterface,
boolean wireGuardRunning,
int wireGuardPeerCount,
boolean wireGuardConfigExists,
String udp2rawService,
boolean udp2rawActive,
String latestWireGuardBackup,
String systemUptime,
int diskUsagePercent,
int memoryUsagePercent,
String loadAverage,
String publicIp
) {
}