RAIDAlert/Watchdog.cs

53 lines
1.7 KiB
C#

using System.Text.RegularExpressions;
using CliWrap;
using CliWrap.Buffered;
namespace RAIDAlert;
public partial class Watchdog : BackgroundService
{
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;
}
[GeneratedRegex(@"physicaldrive \d*I:\d*:\d* \(port \d*I:box \d*:bay \d*, SATA, \d*(?:.\d*)? [GT]B, (.+)\)",
RegexOptions.Multiline)]
private partial Regex RegexArrayStatus();
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var workingDirectory = Path.GetDirectoryName(Environment.ProcessPath!)!;
string hpSsaCli = Path.Combine(workingDirectory, HpCliUtil);
string pcBeeper = Path.Combine(workingDirectory, PcBeeperUtil);
while (!stoppingToken.IsCancellationRequested)
{
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);
}
}
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
}
}
}