| | | 1 | | using System.Globalization; |
| | | 2 | | using Serilog; |
| | | 3 | | using Serilog.Events; |
| | | 4 | | |
| | | 5 | | namespace Kestrun.Health; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Probe that reports free disk space for a target drive / mount point. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// By default it inspects the drive containing the current process executable (AppContext.BaseDirectory). |
| | | 12 | | /// Status mapping (unless overridden): |
| | | 13 | | /// Healthy: free percent greater-or-equal to warn threshold (default 10%) |
| | | 14 | | /// Degraded: free percent between critical and warn thresholds (default 5% - 10%) |
| | | 15 | | /// Unhealthy: free percent below critical threshold (default under 5%) |
| | | 16 | | /// On error (e.g., drive missing) the probe returns Unhealthy with the exception message. |
| | | 17 | | /// </remarks> |
| | | 18 | | public sealed class DiskSpaceProbe : IProbe |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Number of bytes per kilobyte (1024). |
| | | 22 | | /// </summary> |
| | | 23 | | private const double BytesPerKilobyte = 1024.0; |
| | | 24 | | /// <summary> |
| | | 25 | | /// Units for formatting byte sizes. |
| | | 26 | | /// </summary> |
| | 1 | 27 | | private static readonly string[] sizes = ["B", "KB", "MB", "GB", "TB", "PB"]; |
| | | 28 | | |
| | | 29 | | private const double CriticalThresholdDefault = 5.0; |
| | | 30 | | private const double WarnThresholdDefault = 10.0; |
| | | 31 | | |
| | | 32 | | private readonly string _path; |
| | | 33 | | private readonly double _criticalPercent; |
| | | 34 | | private readonly double _warnPercent; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Creates a new <see cref="DiskSpaceProbe"/>. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="name">Probe name (e.g., "disk").</param> |
| | | 40 | | /// <param name="tags">Probe tags (e.g., ["live"], ["ready"]).</param> |
| | | 41 | | /// <param name="path">Directory path whose containing drive should be measured. Defaults to AppContext.BaseDirector |
| | | 42 | | /// <param name="criticalPercent">Below this free percentage the probe is Unhealthy. Default 5.</param> |
| | | 43 | | /// <param name="warnPercent">Below this free percentage (but above critical) the probe is Degraded. Default 10.</pa |
| | | 44 | | /// <param name="logger">Optional logger; if null a context logger is created.</param> |
| | | 45 | | /// <exception cref="ArgumentException">Thrown when thresholds are invalid.</exception> |
| | 57 | 46 | | public DiskSpaceProbe( |
| | 57 | 47 | | string name, |
| | 57 | 48 | | string[] tags, |
| | 57 | 49 | | string? path = null, |
| | 57 | 50 | | double criticalPercent = CriticalThresholdDefault, |
| | 57 | 51 | | double warnPercent = WarnThresholdDefault, |
| | 57 | 52 | | Serilog.ILogger? logger = null) |
| | | 53 | | { |
| | 57 | 54 | | if (criticalPercent <= 0 || warnPercent <= 0 || warnPercent <= criticalPercent || warnPercent > 100) |
| | | 55 | | { |
| | 0 | 56 | | throw new ArgumentException("Invalid threshold configuration. Must satisfy: 0 < critical < warn <= 100."); |
| | | 57 | | } |
| | | 58 | | |
| | 57 | 59 | | Name = name; |
| | 57 | 60 | | Tags = tags; |
| | 57 | 61 | | _path = string.IsNullOrWhiteSpace(path) ? AppContext.BaseDirectory : path!; |
| | 57 | 62 | | _criticalPercent = criticalPercent; |
| | 57 | 63 | | _warnPercent = warnPercent; |
| | 57 | 64 | | Logger = logger ?? Log.ForContext("HealthProbe", name).ForContext("Probe", name); |
| | 57 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Probe name. |
| | | 69 | | /// </summary> |
| | 63 | 70 | | public string Name { get; } |
| | | 71 | | /// <summary> |
| | | 72 | | /// Probe tags used for filtering. |
| | | 73 | | /// </summary> |
| | 0 | 74 | | public string[] Tags { get; } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | 72 | 77 | | public Serilog.ILogger Logger { get; init; } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Executes the disk space check. |
| | | 81 | | /// </summary> |
| | | 82 | | public Task<ProbeResult> CheckAsync(CancellationToken ct = default) |
| | | 83 | | { |
| | | 84 | | try |
| | | 85 | | { |
| | | 86 | | // Resolve drive info |
| | 3 | 87 | | var drive = ResolveDrive(_path); |
| | 3 | 88 | | if (drive is null) |
| | | 89 | | { |
| | 0 | 90 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 91 | | { |
| | 0 | 92 | | Logger.Debug("DiskSpaceProbe {Probe} drive not found for path {Path}", Name, _path); |
| | | 93 | | } |
| | 0 | 94 | | return Task.FromResult(new ProbeResult(ProbeStatus.Unhealthy, $"Drive not found for path '{_path}'.")); |
| | | 95 | | } |
| | | 96 | | |
| | 3 | 97 | | if (!drive.IsReady) |
| | | 98 | | { |
| | 0 | 99 | | return Task.FromResult(new ProbeResult(ProbeStatus.Unhealthy, $"Drive '{drive.Name}' is not ready.")); |
| | | 100 | | } |
| | | 101 | | |
| | 3 | 102 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 103 | | { |
| | 3 | 104 | | Logger.Debug("DiskSpaceProbe {Probe} checking drive {Drive}", Name, drive.Name); |
| | 3 | 105 | | Logger.Debug("DiskSpaceProbe {Probe} drive is ready {Drive}", Name, drive.Name); |
| | | 106 | | } |
| | | 107 | | |
| | 3 | 108 | | var total = drive.TotalSize; // bytes |
| | 3 | 109 | | var free = drive.AvailableFreeSpace; // bytes (user-available) |
| | 3 | 110 | | if (total <= 0) |
| | | 111 | | { |
| | 0 | 112 | | return Task.FromResult(new ProbeResult(ProbeStatus.Unhealthy, $"Drive '{drive.Name}' total size reported |
| | | 113 | | } |
| | | 114 | | |
| | 3 | 115 | | var freePercent = (double)free / total * 100.0; |
| | 3 | 116 | | var status = freePercent < _criticalPercent |
| | 3 | 117 | | ? ProbeStatus.Unhealthy |
| | 3 | 118 | | : freePercent < _warnPercent |
| | 3 | 119 | | ? ProbeStatus.Degraded |
| | 3 | 120 | | : ProbeStatus.Healthy; |
| | | 121 | | |
| | 3 | 122 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 123 | | { |
| | 3 | 124 | | Logger.Debug("DiskSpaceProbe {Probe} free percent={Percent:F1}", Name, freePercent); |
| | | 125 | | } |
| | | 126 | | |
| | 3 | 127 | | var data = new Dictionary<string, object> |
| | 3 | 128 | | { |
| | 3 | 129 | | ["path"] = _path, |
| | 3 | 130 | | ["driveName"] = drive.Name, |
| | 3 | 131 | | ["totalBytes"] = total, |
| | 3 | 132 | | ["freeBytes"] = free, |
| | 3 | 133 | | ["freePercent"] = Math.Round(freePercent, 2), |
| | 3 | 134 | | ["criticalPercent"] = _criticalPercent, |
| | 3 | 135 | | ["warnPercent"] = _warnPercent |
| | 3 | 136 | | }; |
| | | 137 | | |
| | 3 | 138 | | var desc = $"Free {FormatBytes(free)} of {FormatBytes(total)} ({freePercent:F2}% free)"; |
| | | 139 | | |
| | 3 | 140 | | return Task.FromResult(new ProbeResult(status, desc, data)); |
| | | 141 | | } |
| | 0 | 142 | | catch (OperationCanceledException) when (ct.IsCancellationRequested) |
| | | 143 | | { |
| | 0 | 144 | | return Task.FromResult(new ProbeResult(ProbeStatus.Degraded, "Canceled", new Dictionary<string, object> { [" |
| | | 145 | | } |
| | 0 | 146 | | catch (Exception ex) |
| | | 147 | | { |
| | 0 | 148 | | Logger.Error(ex, "DiskSpaceProbe {Probe} failed", Name); |
| | 0 | 149 | | return Task.FromResult(new ProbeResult(ProbeStatus.Unhealthy, ex.Message)); |
| | | 150 | | } |
| | 3 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Resolves the <see cref="DriveInfo"/> for the given path. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <param name="path">The path to resolve.</param> |
| | | 157 | | /// <returns>The <see cref="DriveInfo"/> if found; otherwise, null.</returns> |
| | | 158 | | private static DriveInfo? ResolveDrive(string path) |
| | | 159 | | { |
| | | 160 | | try |
| | | 161 | | { |
| | 3 | 162 | | if (string.IsNullOrWhiteSpace(path)) |
| | | 163 | | { |
| | 0 | 164 | | return null; |
| | | 165 | | } |
| | 3 | 166 | | var root = Path.GetPathRoot(path); |
| | 3 | 167 | | return string.IsNullOrEmpty(root) ? null : new DriveInfo(root); |
| | | 168 | | } |
| | 0 | 169 | | catch |
| | | 170 | | { |
| | 0 | 171 | | return null; |
| | | 172 | | } |
| | 3 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Formats a byte count into a human-readable string using binary (1024) units. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <param name="bytes">The number of bytes.</param> |
| | | 179 | | /// <returns>A human-readable string representation of the byte count.</returns> |
| | | 180 | | private static string FormatBytes(long bytes) |
| | | 181 | | { |
| | 6 | 182 | | double len = bytes; |
| | 6 | 183 | | var order = 0; |
| | 24 | 184 | | while (len >= BytesPerKilobyte && order < sizes.Length - 1) |
| | | 185 | | { |
| | 18 | 186 | | order++; |
| | 18 | 187 | | len /= BytesPerKilobyte; |
| | | 188 | | } |
| | 6 | 189 | | return string.Create(CultureInfo.InvariantCulture, $"{len:0.##} {sizes[order]}"); |
| | | 190 | | } |
| | | 191 | | } |