74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
// Libraries, liberals even
|
|
using System.Diagnostics;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
// Local functions
|
|
var theHttpClient = new HttpClient();
|
|
theHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("rwYGDkbh52tXy62AbcCfBmppX");
|
|
|
|
string systemHostname = System.Environment.MachineName ?? "UNKNOWN_HOST";
|
|
|
|
async Task<string> runProcess(string file, string args)
|
|
{
|
|
var psi = new ProcessStartInfo
|
|
{
|
|
FileName = file,
|
|
Arguments = args,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using var process = Process.Start(psi) ?? throw new Exception("Error starting process (runProcess)");
|
|
string processStdErr = await process.StandardError.ReadToEndAsync();
|
|
if (processStdErr != "")
|
|
{
|
|
throw new Exception($"Process stderr was not empty: {processStdErr}");
|
|
}
|
|
|
|
string processStdOut = await process.StandardOutput.ReadToEndAsync();
|
|
await process.WaitForExitAsync();
|
|
return processStdOut;
|
|
}
|
|
|
|
// Entrypoint
|
|
string queryPoolsStdOut = await runProcess("/usr/sbin/zpool","list -H");
|
|
|
|
List<string> poolList = new List<string>();
|
|
foreach (string poolName in queryPoolsStdOut.Split("\n", StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
string[] actualName = poolName.Split("\t");
|
|
poolList.Add(actualName[0]);
|
|
}
|
|
|
|
foreach (string poolName in poolList)
|
|
{
|
|
string queryPoolInfoStdOut = await runProcess("/usr/sbin/zpool",$"status {poolName}");
|
|
|
|
var emailRequestBody = new emailRequestBodyTemplate { recipient = "nathan@enstrayed.com", subject = $"Zpool {poolName} on {systemHostname} is unhealthy", message = $"The pool {poolName} on {systemHostname} is no longer marked as healthy. Status output:\n\n{queryPoolInfoStdOut}"};
|
|
var serializedEmailRequestBody = new StringContent(JsonSerializer.Serialize(emailRequestBody, emailRequestBodyContext.Default.emailRequestBodyTemplate), Encoding.UTF8, "application/json");
|
|
|
|
Console.WriteLine("Got to email send part");
|
|
|
|
HttpResponseMessage emailRequestResponse = await theHttpClient.PostAsync("https://enstrayed.com/api/sendemail", serializedEmailRequestBody);
|
|
|
|
Console.WriteLine(emailRequestResponse.StatusCode.ToString());
|
|
}
|
|
|
|
// Classes
|
|
public class emailRequestBodyTemplate
|
|
{
|
|
public required string recipient { get; set; }
|
|
public required string subject { get; set; }
|
|
public required string message { get; set; }
|
|
}
|
|
|
|
[JsonSerializable(typeof(emailRequestBodyTemplate))]
|
|
internal partial class emailRequestBodyContext : JsonSerializerContext
|
|
{
|
|
} |