import org.springframework.boot.actuate.system.DiskSpaceHealthIndicator;
import org.springframework.boot.actuate.system.DiskSpaceHealthIndicatorProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DiskUsageController {
private final DiskSpaceHealthIndicator diskSpaceHealthIndicator;
private final DiskSpaceHealthIndicatorProperties properties;
public DiskUsageController(DiskSpaceHealthIndicator diskSpaceHealthIndicator,
DiskSpaceHealthIndicatorProperties properties) {
this.diskSpaceHealthIndicator = diskSpaceHealthIndicator;
this.properties = properties;
}
@GetMapping("/disk-usage")
public DiskUsage getDiskUsage() {
DiskSpaceHealthIndicator.DiskSpaceHealthDetails details =
diskSpaceHealthIndicator.health().getDetails();
long total = details.getTotal();
long free = details.getFree();
long threshold = properties.getThreshold().getBytes();
long used = total - free;
return new DiskUsage(total, used, free, threshold);
}
public static class DiskUsage {
private final long total;
private final long used;
private final long free;
private final long threshold;
public DiskUsage(long total, long used, long free, long threshold) {
this.total = total;
this.used = used;
this.free = free;
this.threshold = threshold;
}
// Getter methods
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DiskUsage {
public static void main(String[] args) {
List<DiskUsageInfo> diskUsageList = getFilteredDiskUsage("/data"); // マウントポイントを指定
for (DiskUsageInfo diskUsage : diskUsageList) {
System.out.println("Total: " + diskUsage.getTotalBlocks() + " blocks");
System.out.println("Used: " + diskUsage.getUsedBlocks() + " blocks");
System.out.println("Available: " + diskUsage.getAvailableBlocks() + " blocks");
System.out.println("Usage Percentage: " + diskUsage.getUsagePercentage() + "%");
System.out.println();
}
}
public static List<DiskUsageInfo> getFilteredDiskUsage(String mountPoint) {
List<DiskUsageInfo> diskUsageList = new ArrayList<>();
try {
Process process = new ProcessBuilder("df").start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (!line.isEmpty()) {
DiskUsageInfo diskUsage = parseDiskUsageLine(line);
if (diskUsage != null && line.contains(mountPoint)) { // マウントポイントのフィルタリング
diskUsageList.add(diskUsage);
}
}
}
process.waitFor();
reader.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return diskUsageList;
}
private static DiskUsageInfo parseDiskUsageLine(String line) {
// 正規表現を使用して行から必要な情報を抽出
Pattern pattern = Pattern.compile("\\S+\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%.*");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
long totalBlocks = Long.parseLong(matcher.group(1));
long usedBlocks = Long.parseLong(matcher.group(2));
long availableBlocks = Long.parseLong(matcher.group(3));
int usagePercentage = Integer.parseInt(matcher.group(4));
return new DiskUsageInfo(totalBlocks, usedBlocks, availableBlocks, usagePercentage);
}
return null;
}
public static class DiskUsageInfo {
private final long totalBlocks;
private final long usedBlocks;
private final long availableBlocks;
private final int usagePercentage;
public DiskUsageInfo(long totalBlocks, long usedBlocks, long availableBlocks, int usagePercentage) {
this.totalBlocks = totalBlocks;
this.usedBlocks = usedBlocks;
this.availableBlocks = availableBlocks;
this.usagePercentage = usagePercentage;
}
public long getTotalBlocks() {
return totalBlocks;
}
public long getUsedBlocks() {
return usedBlocks;
}
public long getAvailableBlocks() {
return availableBlocks;
}
public int getUsagePercentage() {
return usagePercentage;
}
}
}
~~~