diff --git a/.gitignore b/.gitignore index ca1c7a3..16ca49f 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/.idea diff --git a/RAIDAlert.sln.DotSettings b/RAIDAlert.sln.DotSettings new file mode 100644 index 0000000..fed8894 --- /dev/null +++ b/RAIDAlert.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/Watchdog.cs b/Watchdog.cs index 8eb71ee..6a6e7d0 100644 --- a/Watchdog.cs +++ b/Watchdog.cs @@ -1,8 +1,11 @@ +using System.Text.RegularExpressions; + using CliWrap; +using CliWrap.Buffered; namespace RAIDAlert; -public class Watchdog : BackgroundService +public partial class Watchdog : BackgroundService { private const string HpCliUtil = "hpssacli.exe"; private const string PcBeeperUtil = "pc-beeper.exe"; @@ -13,19 +16,38 @@ public class Watchdog : BackgroundService _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) { - string hpSsaCli = Path.Combine(Environment.ProcessPath!, HpCliUtil); - string pcBeeper = Path.Combine(Environment.ProcessPath!, PcBeeperUtil); + var workingDirectory = Path.GetDirectoryName(Environment.ProcessPath!)!; + string hpSsaCli = Path.Combine(workingDirectory, HpCliUtil); + string pcBeeper = Path.Combine(workingDirectory, PcBeeperUtil); while (!stoppingToken.IsCancellationRequested) { - var result = await Cli.Wrap("path/to/exe") - .WithArguments(new[] {"--foo", "bar"}) - .WithWorkingDirectory("work/dir/path") - .ExecuteAsync(); + BufferedCommandResult hpSsaCliResult = await Cli.Wrap(hpSsaCli) + .WithArguments(new[] { "controller", "slot=1", "physicaldrive", "all", "show" }) + .WithWorkingDirectory(workingDirectory) + .ExecuteBufferedAsync(stoppingToken); - await Task.Delay(1000, 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); } } } \ No newline at end of file