Files
NasRobot/src/Service/Jobs/DiskMonitorRegistry.cs

49 lines
1.3 KiB
C#
Raw Normal View History

2025-03-07 12:05:39 +08:00
using System.Diagnostics;
2025-03-07 12:10:53 +08:00
using System.Text;
2025-03-07 12:05:39 +08:00
using FluentScheduler;
using Interface.Jobs;
namespace Service.Jobs;
public class DiskMonitorRegistry : Registry, IDiskMonitorRegistry
{
public void Job()
{
// 执行 cmd 命令 获取执行结果
var command = "df -h";
var process = new Process
{
2025-03-07 12:10:53 +08:00
StartInfo = new()
2025-03-07 12:05:39 +08:00
{
FileName = "/bin/bash",
Arguments = $"-c \"{command}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
2025-03-07 12:10:53 +08:00
2025-03-07 12:05:39 +08:00
process.Start();
var result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
2025-03-07 12:10:53 +08:00
Console.WriteLine(FormatResult(result));
}
private string FormatResult(string result)
{
var lines = result.Split("\n");
var sb = new StringBuilder();
foreach (var line in lines)
{
var cols = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
if (line.Contains("/host/wd/"))
{
2025-03-07 14:21:32 +08:00
sb.AppendLine($"{cols[5].Substring("/host".Length)}, {cols[1]}, {cols[4].TrimEnd('%')}, {cols[3]}");
2025-03-07 12:10:53 +08:00
}
}
return sb.ToString();
2025-03-07 12:05:39 +08:00
}
}