RAIDAlert/Watchdog.cs

53 lines
1.7 KiB
C#
Raw Normal View History

2023-07-12 15:24:30 +02:00
using System.Text.RegularExpressions;
2023-07-12 15:05:59 +02:00
using CliWrap;
2023-07-12 15:24:30 +02:00
using CliWrap.Buffered;
2023-07-12 15:05:59 +02:00
namespace RAIDAlert;
2023-07-12 15:24:30 +02:00
public partial class Watchdog : BackgroundService
2023-07-12 15:05:59 +02:00
{
private const string HpCliUtil = "hpssacli.exe";
private const string PcBeeperUtil = "pc-beeper.exe";
private readonly ILogger<Watchdog> _logger;
public Watchdog(ILogger<Watchdog> logger)
{
_logger = logger;
}
2023-07-12 15:24:30 +02:00
[GeneratedRegex(@"physicaldrive \d*I:\d*:\d* \(port \d*I:box \d*:bay \d*, SATA, \d*(?:.\d*)? [GT]B, (.+)\)",
RegexOptions.Multiline)]
private partial Regex RegexArrayStatus();
2023-07-12 15:05:59 +02:00
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
2023-07-12 15:24:30 +02:00
var workingDirectory = Path.GetDirectoryName(Environment.ProcessPath!)!;
string hpSsaCli = Path.Combine(workingDirectory, HpCliUtil);
string pcBeeper = Path.Combine(workingDirectory, PcBeeperUtil);
2023-07-12 15:05:59 +02:00
while (!stoppingToken.IsCancellationRequested)
{
2023-07-12 15:24:30 +02:00
BufferedCommandResult hpSsaCliResult = await Cli.Wrap(hpSsaCli)
.WithArguments(new[] { "controller", "slot=1", "physicaldrive", "all", "show" })
.WithWorkingDirectory(workingDirectory)
.ExecuteBufferedAsync(stoppingToken);
MatchCollection matches = RegexArrayStatus().Matches(hpSsaCliResult.StandardOutput);
if (matches.Count == 0)
{
_logger.LogError("Checking for physical disk status yielded no results");
}
else
{
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1].Value);
}
}
2023-07-12 15:05:59 +02:00
2023-07-12 15:24:30 +02:00
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
2023-07-12 15:05:59 +02:00
}
}
}