Where it sits
The records and the rules beside them in the Domain's Admin feature, the ports and handlers in the Application's Cloud feature, and the source, the card, its rows, its words and its routes in the host's Cloud slice beside the network log's. The agent and its unit are in scripts/box-agent/.
public static class BoxRows
{
public static readonly TimeSpan Silent = TimeSpan.FromMinutes(3);
public static readonly IReadOnlyList<string> MustRun = ["claude-remote-control.service", "jdrive.service", "tailscaled.service", "ssh.service", "jobhunt-6am.timer"];
public const string MorningRun = "jobhunt-6am.service";
public static IReadOnlyList<CloudRow> All(BoxSnapshot? snapshot, DateTimeOffset? newestKept, DateTimeOffset started, DateTimeOffset now, int keptLastHour)
{
var reporting = Reporting(snapshot, newestKept, started, now, keptLastHour);
if (snapshot is null)
{
string[] keys = ["cores", "memory", "network", "disks", "gpus", "temps", "services"];
string[] questions = ["What is each core doing?", "Is the memory and swap fine?", "Is the network moving?", "Are the disks and file systems healthy?", "What are the GPUs doing?", "How hot is it?", "Are the job-hunt services up?"];
return [.. keys.Select((key, i) => new CloudRow(key, questions[i], BoxSource.Waiting, [], "info")), reporting];
}
return [Cores(snapshot), Memory(snapshot), Network(snapshot), Disks(snapshot), Gpus(snapshot), Temps(snapshot), Services(snapshot, now), reporting];
}
public static CloudRow Cores(BoxSnapshot snapshot)
{
var loads = snapshot.Cores.Select(core => core.Load).DefaultIfEmpty(0).ToList();
string spread = $"{loads.Min().ToString("0", CultureInfo.InvariantCulture)} to {loads.Max().ToString("0", CultureInfo.InvariantCulture)} %";
return new CloudRow("cores", "What is each core doing?", $"{snapshot.Cores.Count} cores",
[new($"all {snapshot.Cores.Count} at {spread}; the box idles under 10 % and the local gate takes every core to 100 % for about 8 minutes", null)], "good")
{
Table = Table(["Core", "Load", "Clock", "Temp"], snapshot.Cores.Select(core => new[] { $"CPU{core.Index + 1}", BoxWords.Percent(core.Load), $"{(core.Mhz / 1000).ToString("0.00", CultureInfo.InvariantCulture)} GHz", BoxWords.Degrees(core.Temp) })),
};
}
public static CloudRow Memory(BoxSnapshot snapshot)
{
double share = snapshot.Memory.TotalB > 0 ? 100.0 * snapshot.Memory.UsedB / snapshot.Memory.TotalB : 0;
double swapShare = snapshot.Swap.TotalB > 0 ? 100.0 * snapshot.Swap.UsedB / snapshot.Swap.TotalB : 0;
var (amber, red) = CloudThresholds.BoxMemory;
string tone = share >= red ? "alarm" : share >= amber || SwapWorthALook(snapshot) ? "warning" : "good";
return new CloudRow("memory", "Is the memory and swap fine?", $"{BoxWords.Bytes(snapshot.Memory.UsedB)} of {BoxWords.Bytes(snapshot.Memory.TotalB)}",
[new($"cache {BoxWords.Bytes(snapshot.Memory.CacheB)}, free {BoxWords.Bytes(snapshot.Memory.FreeB)} · swap {BoxWords.Bytes(snapshot.Swap.UsedB)} of {BoxWords.Bytes(snapshot.Swap.TotalB)}{Paging(snapshot)} · amber {amber} %, red {red} %, {SwapLine(snapshot)}", null)], tone)
{
Table = Table(["Pool", "Used", "Of", "Share"],
[
["Memory", BoxWords.Bytes(snapshot.Memory.UsedB), BoxWords.Bytes(snapshot.Memory.TotalB), BoxWords.Percent(Math.Round(share, 1))],
["Cache", BoxWords.Bytes(snapshot.Memory.CacheB), "", BoxWords.Percent(snapshot.Memory.TotalB > 0 ? Math.Round(100.0 * snapshot.Memory.CacheB / snapshot.Memory.TotalB, 1) : null)],
["Swap", BoxWords.Bytes(snapshot.Swap.UsedB), BoxWords.Bytes(snapshot.Swap.TotalB), BoxWords.Percent(Math.Round(swapShare, 1))],
]),
};
}
public static bool SwapWorthALook(BoxSnapshot snapshot)
{
var (memory, swap) = (snapshot.Memory, snapshot.Swap);
if (swap.InBps is not { } swapIn || swap.OutBps is not { } swapOut)
{
return swap.UsedB > Math.Min(CloudThresholds.BoxSwapBytes, (long)(swap.TotalB * CloudThresholds.BoxSwapShare / 100));
}
bool full = swap.TotalB > 0 && 100.0 * swap.UsedB / swap.TotalB >= CloudThresholds.BoxSwapNearlyFull && memory.AvailableB is { } available && available < swap.UsedB;
return swapIn + swapOut >= CloudThresholds.BoxPagingBps || memory.PressurePercent >= CloudThresholds.BoxMemoryPressure || full;
}
private static string Paging(BoxSnapshot snapshot) =>
snapshot.Swap is { InBps: { } swapIn, OutBps: { } swapOut }
? $", moving {BoxWords.Rate(swapIn)} in and {BoxWords.Rate(swapOut)} out over five minutes" + (snapshot.Memory.PressurePercent is { } pressure ? $", memory pressure {BoxWords.Percent(pressure)}" : "")
: "";
private static string SwapLine(BoxSnapshot snapshot) =>
snapshot.Swap.InBps is null || snapshot.Swap.OutBps is null
? $"swap amber past {CloudThresholds.BoxSwapShare} % of it or {BoxWords.Bytes(CloudThresholds.BoxSwapBytes)}, whichever is smaller (the agent reads no paging yet)"
: $"swap amber when {BoxWords.Rate(CloudThresholds.BoxPagingBps)} moves through it, memory pressure reaches {CloudThresholds.BoxMemoryPressure} %, or it is {CloudThresholds.BoxSwapNearlyFull} % full with less memory available than it holds; parked pages alone are not pressure";
public static string RestartCommand(string unit, string scope) =>
scope == "user" ? $"systemctl --user restart {unit}" : $"sudo systemctl restart {unit}";
public static CloudRow Network(BoxSnapshot snapshot)
{
var real = snapshot.Nets.Where(net => net.Name != "lo").ToList();
return new CloudRow("network", "Is the network moving?", $"{BoxWords.Rate(real.Sum(net => net.InBps))} in, {BoxWords.Rate(real.Sum(net => net.OutBps))} out",
[new($"totals since boot {BoxWords.Bytes(real.Sum(net => (double)net.InTotalB))} received, {BoxWords.Bytes(real.Sum(net => (double)net.OutTotalB))} sent", null)], "good")
{
Table = Table(["Interface", "In now", "Out now", "Received", "Sent"], real.Select(net => new[] { BoxWords.Scrub(net.Name), BoxWords.Rate(net.InBps), BoxWords.Rate(net.OutBps), BoxWords.Bytes(net.InTotalB), BoxWords.Bytes(net.OutTotalB) })),
};
}
public static CloudRow Disks(BoxSnapshot snapshot)
{
var (amber, red) = CloudThresholds.BoxFileSystem;
double Fill(BoxDisk disk) => disk.SizeB > 0 ? 100.0 * disk.UsedB / disk.SizeB : 0;
var array = snapshot.Disks.FirstOrDefault(disk => disk.RaidState is not null);
bool broken = array is not null && (array.MembersActive < array.Members || array.RaidState!.Contains("(F)", StringComparison.Ordinal)
|| array.RaidState.Contains("inactive", StringComparison.OrdinalIgnoreCase) || array.RaidState.Contains("degraded", StringComparison.OrdinalIgnoreCase));
double fullest = snapshot.Disks.Select(Fill).DefaultIfEmpty(0).Max();
string tone = broken || fullest >= red ? "alarm" : fullest >= amber ? "warning" : "good";
string answer = array is null ? "no array" : broken ? $"the array is not whole: {array.MembersActive} of {array.Members} active" : $"{BoxWords.Scrub(array.RaidState)}, {array.MembersActive} of {array.Members} active";
return new CloudRow("disks", "Are the disks and file systems healthy?", answer,
[new($"red when the array reports a missing or failed member, or any file system passes {red} %; amber from {amber} %", null)], tone)
{
Table = Table(["Mount", "Type", "Size", "Used", "Share", "State"], snapshot.Disks.Select(disk => new[] { $"{disk.Mount} ({BoxWords.Scrub(disk.Device)})", disk.Fstype, BoxWords.Bytes(disk.SizeB), BoxWords.Bytes(disk.UsedB), BoxWords.Percent(Math.Round(Fill(disk), 1)), disk.RaidState is null ? "" : $"{BoxWords.Scrub(disk.RaidState)}, {disk.MembersActive} of {disk.Members} active" })),
};
}
public static CloudRow Gpus(BoxSnapshot snapshot)
{
var (amber, red) = CloudThresholds.BoxGpuMemory;
string ToneOf(BoxGpu gpu)
{
double memory = gpu.MemTotalB > 0 ? 100.0 * gpu.MemUsedB / gpu.MemTotalB : 0;
return memory >= red || gpu.SlowdownTemp is { } hot && gpu.Temp >= hot ? "alarm"
: memory >= amber || gpu.SlowdownTemp is { } warm && gpu.Temp >= warm - CloudThresholds.BoxTemperatureUnder ? "warning" : "good";
}
string tone = Worst(snapshot.Gpus.Select(ToneOf));
string answer = snapshot.Gpus.Count == 0 ? "no GPU read" : $"{snapshot.Gpus.Count} x {snapshot.Gpus[0].Name.Replace("NVIDIA GeForce ", "", StringComparison.Ordinal)}";
return new CloudRow("gpus", "What are the GPUs doing?", answer,
[new($"amber at {amber} % of a GPU's memory or {CloudThresholds.BoxTemperatureUnder} °C under its own slowdown mark, red at {red} % or the mark", null)], tone)
{
Table = Table(["GPU", "Load", "Memory", "Temp", "Power", "State"], snapshot.Gpus.Select(gpu => new[]
{
$"GPU {gpu.Index} {gpu.Name}", BoxWords.Percent(gpu.Load), $"{BoxWords.Bytes(gpu.MemUsedB)} of {BoxWords.Bytes(gpu.MemTotalB)}",
gpu.SlowdownTemp is { } mark ? $"{BoxWords.Degrees(gpu.Temp)}, slowdown {BoxWords.Degrees(mark)}" : BoxWords.Degrees(gpu.Temp),
gpu.PowerW is { } watts ? $"{watts.ToString("0", CultureInfo.InvariantCulture)} W of {gpu.PowerLimitW?.ToString("0", CultureInfo.InvariantCulture) ?? "?"} W" : "not read", gpu.Pstate,
})),
};
}
public static CloudRow Temps(BoxSnapshot snapshot)
{
static string ToneOf(BoxTemp temp) =>
temp.High is not { } high ? "good" : temp.Now >= high ? "alarm" : temp.Now >= high - CloudThresholds.BoxTemperatureUnder ? "warning" : "good";
var package = BoxSource.Package(snapshot);
return new CloudRow("temps", "How hot is it?", package is null ? "no sensor read" : $"{BoxWords.Degrees(package.Now)} {BoxWords.Scrub(package.Sensor)}",
[new($"amber at {CloudThresholds.BoxTemperatureUnder} °C under each sensor's own high mark, red at the mark; the marks come from the sensors, never typed here", null)], Worst(snapshot.Temps.Select(ToneOf)))
{
Table = Table(["Sensor", "Now", "Marks"], snapshot.Temps.Select(temp => new[]
{
BoxWords.Scrub(temp.Sensor), BoxWords.Degrees(temp.Now),
string.Join(", ", new[] { temp.High is { } h ? $"high {BoxWords.Degrees(h)}" : null, temp.Critical is { } c ? $"critical {BoxWords.Degrees(c)}" : null }.OfType<string>()),
})),
};
}
public static CloudRow Services(BoxSnapshot snapshot, DateTimeOffset now)
{
bool Active(BoxService service) => service.ActiveState == "active";
bool Waiting(BoxService service) => service.Unit == MorningRun && service.ActiveState == "inactive" && service.Note is { } note && note.Contains("last result success", StringComparison.Ordinal);
var stopped = MustRun.Where(unit => snapshot.Services.FirstOrDefault(service => service.Unit == unit) is not { } found || !Active(found)).ToList();
bool morningFailed = snapshot.Services.FirstOrDefault(service => service.Unit == MorningRun)?.Note is { } note && note.Contains("last result", StringComparison.Ordinal) && !note.Contains("success", StringComparison.Ordinal);
string tone = stopped.Count > 0 ? "alarm" : morningFailed ? "warning" : "good";
int up = snapshot.Services.Count(service => Active(service) || Waiting(service));
return new CloudRow("services", "Are the job-hunt services up?", $"{up} of {snapshot.Services.Count} up",
[new(stopped.Count > 0 ? $"not running: {string.Join("; ", stopped.Select(unit => snapshot.Services.FirstOrDefault(service => service.Unit == unit) is { } found ? $"{unit}, restart with {RestartCommand(unit, found.Scope)}" : $"{unit}, not in the box's report"))}" : $"red when {string.Join(", ", MustRun)} is not running; amber when the morning run's last result failed", null)], tone)
{
Table = Table(["Service", "State", "Up for", "Note"], snapshot.Services.Select(service => new[]
{
$"{service.Unit} ({service.Scope})", Waiting(service) ? "waiting for its timer" : $"{service.ActiveState}, {service.SubState}",
service.Since is { } since && Active(service) ? BoxWords.Span(now - since) : "", BoxWords.Scrub(service.Note),
})),
};
}
public static CloudRow Reporting(BoxSnapshot? snapshot, DateTimeOffset? newestKept, DateTimeOffset started, DateTimeOffset now, int keptLastHour)
{
DateTimeOffset? newest = new[] { snapshot?.At, newestKept }.Where(at => at is not null).Max();
var from = newest is { } at && at > started ? at : started;
var silence = now - from;
string count = $"{keptLastHour} snapshots kept in the last hour; the agent adds nothing to the bill, one small write a minute on the free tier";
if (newest is null)
{
return silence > Silent
? new CloudRow("reporting", "Is the box reporting?", $"no: no snapshot in the {BoxWords.Span(silence)} since this site started", [new(count, null)], "alarm")
: new CloudRow("reporting", "Is the box reporting?", BoxSource.Waiting, [new(count, null)], "info");
}
return silence > Silent
? new CloudRow("reporting", "Is the box reporting?", $"no: silent for {BoxWords.Span(silence)}", [new(count, null)], "alarm")
: new CloudRow("reporting", "Is the box reporting?", $"yes, the newest snapshot {BoxWords.Span(now - newest.Value)} ago", [new(count, null)], "good");
}
public static string Worst(IEnumerable<string> tones) =>
tones.OrderByDescending(tone => tone switch { "alarm" => 3, "warning" => 2, "info" => 1, _ => 0 }).FirstOrDefault() ?? "good";
private static CloudTable Table(IReadOnlyList<string> columns, IEnumerable<IEnumerable<string>> rows) =>
new(columns, [.. rows.Select(row => (IReadOnlyList<CloudLink>)[.. row.Select(cell => new CloudLink(cell, null))])]);
}
Live from api/TheYard.Api/Features/Admin/Cloud/BoxRows.cs, region box-rows, read from this build at 99df7a8.