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:40:13 +02:00
|
|
|
string workingDirectory = Path.GetDirectoryName(Environment.ProcessPath!)!;
|
2023-07-12 15:24:30 +02:00
|
|
|
string hpSsaCli = Path.Combine(workingDirectory, HpCliUtil);
|
|
|
|
string pcBeeper = Path.Combine(workingDirectory, PcBeeperUtil);
|
2023-07-12 15:05:59 +02:00
|
|
|
|
2023-07-12 15:40:13 +02:00
|
|
|
// Ready beep sequence
|
|
|
|
await Cli.Wrap(pcBeeper)
|
|
|
|
.WithArguments(new[] { "-f", "1500", "-d", "100" })
|
|
|
|
.WithWorkingDirectory(workingDirectory)
|
|
|
|
.ExecuteAsync(stoppingToken);
|
|
|
|
await Task.Delay(TimeSpan.FromMilliseconds(10), stoppingToken);
|
|
|
|
await Cli.Wrap(pcBeeper)
|
|
|
|
.WithArguments(new[] { "-f", "2000", "-d", "100" })
|
|
|
|
.WithWorkingDirectory(workingDirectory)
|
|
|
|
.ExecuteAsync(stoppingToken);
|
|
|
|
|
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
|
|
|
|
{
|
2023-07-12 15:40:13 +02:00
|
|
|
bool allHealthy = matches.All(m => m.Groups[1].Value.Equals("OK", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
if (!allHealthy)
|
2023-07-12 15:24:30 +02:00
|
|
|
{
|
2023-07-12 15:40:13 +02:00
|
|
|
_logger.LogError("One or more disks reported faulty status");
|
|
|
|
|
|
|
|
await Cli.Wrap(pcBeeper)
|
|
|
|
.WithArguments(new[] { "-f", "3000", "-d", "500" })
|
|
|
|
.WithWorkingDirectory(workingDirectory)
|
|
|
|
.ExecuteAsync(stoppingToken);
|
|
|
|
await Task.Delay(TimeSpan.FromMilliseconds(100), stoppingToken);
|
|
|
|
await Cli.Wrap(pcBeeper)
|
|
|
|
.WithArguments(new[] { "-f", "3000", "-d", "500" })
|
|
|
|
.WithWorkingDirectory(workingDirectory)
|
|
|
|
.ExecuteAsync(stoppingToken);
|
|
|
|
await Task.Delay(TimeSpan.FromMilliseconds(100), stoppingToken);
|
|
|
|
await Cli.Wrap(pcBeeper)
|
|
|
|
.WithArguments(new[] { "-f", "3000", "-d", "500" })
|
|
|
|
.WithWorkingDirectory(workingDirectory)
|
|
|
|
.ExecuteAsync(stoppingToken);
|
2023-07-12 15:24:30 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|