| | | 1 | | using System.Collections; |
| | | 2 | | using System.Management.Automation; |
| | | 3 | | using System.Management.Automation.Runspaces; |
| | | 4 | | using Kestrun.Languages; |
| | | 5 | | using Kestrun.Scripting; |
| | | 6 | | using SerilogLogger = Serilog.ILogger; |
| | | 7 | | using Serilog.Events; |
| | | 8 | | |
| | | 9 | | namespace Kestrun.Health; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A health probe implemented via a PowerShell script. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="name">The name of the probe.</param> |
| | | 15 | | /// <param name="tags">The tags associated with the probe.</param> |
| | | 16 | | /// <param name="script">The PowerShell script to execute.</param> |
| | | 17 | | /// <param name="logger">The logger to use for logging.</param> |
| | | 18 | | /// <param name="poolAccessor">A function to access the runspace pool.</param> |
| | | 19 | | /// <param name="arguments">The arguments for the script.</param> |
| | | 20 | | internal sealed class PowerShellScriptProbe( |
| | | 21 | | string name, |
| | | 22 | | IEnumerable<string>? tags, |
| | | 23 | | string script, |
| | | 24 | | SerilogLogger logger, |
| | | 25 | | Func<KestrunRunspacePoolManager> poolAccessor, |
| | 0 | 26 | | IReadOnlyDictionary<string, object?>? arguments) : Probe(name, tags, logger), IProbe |
| | | 27 | | { |
| | 0 | 28 | | private readonly Func<KestrunRunspacePoolManager> _poolAccessor = poolAccessor ?? throw new ArgumentNullException(na |
| | 0 | 29 | | private readonly IReadOnlyDictionary<string, object?>? _arguments = arguments; |
| | 0 | 30 | | private readonly string _script = string.IsNullOrWhiteSpace(script) |
| | 0 | 31 | | ? throw new ArgumentException("Probe script cannot be null or whitespace.", nameof(script)) |
| | 0 | 32 | | : script; |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public override async Task<ProbeResult> CheckAsync(CancellationToken ct = default) |
| | | 36 | | { |
| | 0 | 37 | | var pool = _poolAccessor(); |
| | 0 | 38 | | Runspace? runspace = null; |
| | | 39 | | try |
| | | 40 | | { |
| | 0 | 41 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 42 | | { |
| | 0 | 43 | | Logger.Debug("PowerShellScriptProbe {Probe} acquiring runspace", Name); |
| | | 44 | | } |
| | 0 | 45 | | runspace = await pool.AcquireAsync(ct).ConfigureAwait(false); |
| | 0 | 46 | | using var ps = CreateConfiguredPowerShell(runspace); |
| | 0 | 47 | | var output = await InvokeScriptAsync(ps, ct).ConfigureAwait(false); |
| | 0 | 48 | | return ProcessOutput(ps, output); |
| | | 49 | | } |
| | 0 | 50 | | catch (PipelineStoppedException) when (ct.IsCancellationRequested) |
| | | 51 | | { |
| | 0 | 52 | | Logger.Information("PowerShell health probe {Probe} canceled (PipelineStopped).", Name); |
| | 0 | 53 | | return new ProbeResult(ProbeStatus.Degraded, "Canceled"); |
| | | 54 | | } |
| | 0 | 55 | | catch (OperationCanceledException) when (ct.IsCancellationRequested) |
| | | 56 | | { |
| | 0 | 57 | | throw; |
| | | 58 | | } |
| | 0 | 59 | | catch (Exception ex) |
| | | 60 | | { |
| | 0 | 61 | | if (ex is PipelineStoppedException) |
| | | 62 | | { |
| | 0 | 63 | | Logger.Warning(ex, "PowerShell health probe {Probe} pipeline stopped.", Name); |
| | 0 | 64 | | return new ProbeResult(ProbeStatus.Degraded, "Canceled"); |
| | | 65 | | } |
| | 0 | 66 | | Logger.Error(ex, "PowerShell health probe {Probe} failed.", Name); |
| | 0 | 67 | | return new ProbeResult(ProbeStatus.Unhealthy, $"Exception: {ex.Message}"); |
| | | 68 | | } |
| | | 69 | | finally |
| | | 70 | | { |
| | 0 | 71 | | if (runspace is not null) |
| | | 72 | | { |
| | 0 | 73 | | try { pool.Release(runspace); } |
| | 0 | 74 | | catch { runspace.Dispose(); } |
| | | 75 | | } |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Creates and configures a PowerShell instance with the provided runspace and script. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="runspace">The runspace to use for the PowerShell instance.</param> |
| | | 83 | | /// <returns>A configured PowerShell instance.</returns> |
| | | 84 | | private PowerShell CreateConfiguredPowerShell(Runspace runspace) |
| | | 85 | | { |
| | 0 | 86 | | var ps = PowerShell.Create(); |
| | 0 | 87 | | ps.Runspace = runspace; |
| | 0 | 88 | | PowerShellExecutionHelpers.SetVariables(ps, _arguments, Logger); |
| | 0 | 89 | | PowerShellExecutionHelpers.AddScript(ps, _script); |
| | 0 | 90 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 91 | | { |
| | 0 | 92 | | Logger.Debug("PowerShellScriptProbe {Probe} invoking script length={Length}", Name, _script.Length); |
| | | 93 | | } |
| | 0 | 94 | | return ps; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Invokes the PowerShell script asynchronously. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="ps">The PowerShell instance to use.</param> |
| | | 101 | | /// <param name="ct">The cancellation token.</param> |
| | | 102 | | /// <returns>A task representing the asynchronous operation, with a list of PSObject as the result.</returns> |
| | | 103 | | private async Task<IReadOnlyList<PSObject>> InvokeScriptAsync(PowerShell ps, CancellationToken ct) |
| | | 104 | | { |
| | 0 | 105 | | var output = await PowerShellExecutionHelpers.InvokeAsync(ps, Logger, ct).ConfigureAwait(false); |
| | 0 | 106 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 107 | | { |
| | 0 | 108 | | Logger.Debug("PowerShellScriptProbe {Probe} received {Count} output objects", Name, output.Count); |
| | | 109 | | } |
| | | 110 | | // Materialize to a List to satisfy IReadOnlyList contract and avoid invalid casts. |
| | 0 | 111 | | return output.Count == 0 ? Array.Empty<PSObject>() : new List<PSObject>(output); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Processes the output from the PowerShell script. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="ps">The PowerShell instance used to invoke the script.</param> |
| | | 118 | | /// <param name="output">The output objects returned by the script.</param> |
| | | 119 | | /// <returns>A ProbeResult representing the outcome of the script execution.</returns> |
| | | 120 | | private ProbeResult ProcessOutput(PowerShell ps, IReadOnlyList<PSObject> output) |
| | | 121 | | { |
| | 0 | 122 | | if (ps.HadErrors || ps.Streams.Error.Count > 0) |
| | | 123 | | { |
| | 0 | 124 | | var errors = string.Join("; ", ps.Streams.Error.Select(static e => e.ToString())); |
| | 0 | 125 | | ps.Streams.Error.Clear(); |
| | 0 | 126 | | return new ProbeResult(ProbeStatus.Unhealthy, errors); |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | for (var i = output.Count - 1; i >= 0; i--) |
| | | 130 | | { |
| | 0 | 131 | | if (TryConvert(output[i], out var result)) |
| | | 132 | | { |
| | 0 | 133 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 134 | | { |
| | 0 | 135 | | Logger.Debug("PowerShellScriptProbe {Probe} converted output index={Index} status={Status}", Name, i |
| | | 136 | | } |
| | 0 | 137 | | return result; |
| | | 138 | | } |
| | | 139 | | } |
| | 0 | 140 | | return new ProbeResult(ProbeStatus.Unhealthy, "PowerShell probe produced no recognizable result."); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Tries to convert a PSObject to a ProbeResult. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <param name="obj">The PSObject to convert.</param> |
| | | 147 | | /// <param name="result">The resulting ProbeResult.</param> |
| | | 148 | | /// <returns>True if the conversion was successful, false otherwise.</returns> |
| | | 149 | | private static bool TryConvert(PSObject obj, out ProbeResult result) |
| | | 150 | | { |
| | | 151 | | // Direct pass-through if already a ProbeResult |
| | 0 | 152 | | if (TryUnwrapProbeResult(obj, out result)) |
| | | 153 | | { |
| | 0 | 154 | | return true; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | // Try string-based conversion |
| | 0 | 158 | | if (TryConvertFromString(obj, out result)) |
| | | 159 | | { |
| | 0 | 160 | | return true; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | // Try property-based conversion |
| | 0 | 164 | | if (TryConvertFromProperties(obj, out result)) |
| | | 165 | | { |
| | 0 | 166 | | return true; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | result = default!; |
| | 0 | 170 | | return false; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Handles string-based conversion scenario |
| | | 175 | | /// </summary> |
| | | 176 | | /// <param name="obj">The PSObject to convert.</param> |
| | | 177 | | /// <param name="result">The resulting ProbeResult.</param> |
| | | 178 | | /// <returns>True if the conversion was successful, false otherwise.</returns> |
| | | 179 | | private static bool TryConvertFromString(PSObject obj, out ProbeResult result) |
| | | 180 | | { |
| | 0 | 181 | | if (TryGetStatus(obj, out var status, out var descriptionWhenString, out var statusTextIsRaw)) |
| | | 182 | | { |
| | 0 | 183 | | if (statusTextIsRaw) |
| | | 184 | | { |
| | | 185 | | // We interpreted the entire string object as both status + description |
| | 0 | 186 | | result = new ProbeResult(status, descriptionWhenString); |
| | 0 | 187 | | return true; |
| | | 188 | | } |
| | | 189 | | } |
| | 0 | 190 | | result = default!; |
| | 0 | 191 | | return false; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Handles property-based conversion scenario |
| | | 196 | | /// </summary> |
| | | 197 | | /// <param name="obj">The PSObject to convert.</param> |
| | | 198 | | /// <param name="result">The resulting ProbeResult.</param> |
| | | 199 | | /// <returns>True if the conversion was successful, false otherwise.</returns> |
| | | 200 | | private static bool TryConvertFromProperties(PSObject obj, out ProbeResult result) |
| | | 201 | | { |
| | 0 | 202 | | if (TryGetStatus(obj, out var status, out var descriptionWhenString, out var statusTextIsRaw)) |
| | | 203 | | { |
| | 0 | 204 | | if (!statusTextIsRaw) |
| | | 205 | | { |
| | 0 | 206 | | var description = descriptionWhenString ?? GetDescription(obj); |
| | 0 | 207 | | var data = GetDataDictionary(obj); |
| | 0 | 208 | | result = new ProbeResult(status, description, data); |
| | 0 | 209 | | return true; |
| | | 210 | | } |
| | | 211 | | } |
| | 0 | 212 | | result = default!; |
| | 0 | 213 | | return false; |
| | | 214 | | } |
| | | 215 | | /// <summary> |
| | | 216 | | /// Tries to unwrap a PSObject that directly wraps a ProbeResult. |
| | | 217 | | /// </summary> |
| | | 218 | | /// <param name="obj">The PSObject to unwrap.</param> |
| | | 219 | | /// <param name="result">The unwrapped ProbeResult.</param> |
| | | 220 | | /// <returns>True if the unwrapping was successful, false otherwise.</returns> |
| | | 221 | | private static bool TryUnwrapProbeResult(PSObject obj, out ProbeResult result) |
| | | 222 | | { |
| | 0 | 223 | | if (obj.BaseObject is ProbeResult pr) |
| | | 224 | | { |
| | 0 | 225 | | result = pr; |
| | 0 | 226 | | return true; |
| | | 227 | | } |
| | 0 | 228 | | result = default!; |
| | 0 | 229 | | return false; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Parses the status from a PSObject. |
| | | 234 | | /// </summary> |
| | | 235 | | /// <param name="obj">The PSObject to parse.</param> |
| | | 236 | | /// <param name="status">The parsed ProbeStatus.</param> |
| | | 237 | | /// <param name="descriptionOrRaw">The description or raw status string.</param> |
| | | 238 | | /// <param name="isRawString">True if the status was a raw string, false otherwise.</param> |
| | | 239 | | /// <returns>True if the parsing was successful, false otherwise.</returns> |
| | | 240 | | private static bool TryGetStatus(PSObject obj, out ProbeStatus status, out string? descriptionOrRaw, out bool isRawS |
| | | 241 | | { |
| | 0 | 242 | | var statusValue = obj.Properties["status"]?.Value ?? obj.Properties["Status"]?.Value; |
| | 0 | 243 | | if (statusValue is null) |
| | | 244 | | { |
| | 0 | 245 | | if (obj.BaseObject is string statusText && TryParseStatus(statusText, out var parsedFromText)) |
| | | 246 | | { |
| | 0 | 247 | | status = parsedFromText; |
| | 0 | 248 | | descriptionOrRaw = statusText; |
| | 0 | 249 | | isRawString = true; |
| | 0 | 250 | | return true; |
| | | 251 | | } |
| | 0 | 252 | | status = default; |
| | 0 | 253 | | descriptionOrRaw = null; |
| | 0 | 254 | | isRawString = false; |
| | 0 | 255 | | return false; |
| | | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | status = TryParseStatus(statusValue.ToString(), out var parsed) |
| | 0 | 259 | | ? parsed |
| | 0 | 260 | | : ProbeStatus.Unhealthy; |
| | 0 | 261 | | descriptionOrRaw = null; // description will be resolved separately |
| | 0 | 262 | | isRawString = false; |
| | 0 | 263 | | return true; |
| | | 264 | | } |
| | | 265 | | /// <summary> |
| | | 266 | | /// Gets the description from a PSObject. |
| | | 267 | | /// </summary> |
| | | 268 | | /// <param name="obj">The PSObject to extract the description from.</param> |
| | | 269 | | /// <returns>The description string, or null if not found.</returns> |
| | | 270 | | private static string? GetDescription(PSObject obj) |
| | 0 | 271 | | => obj.Properties["description"]?.Value?.ToString() ?? obj.Properties["Description"]?.Value?.ToString(); |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// Gets the data dictionary from a PSObject. |
| | | 275 | | /// </summary> |
| | | 276 | | /// <param name="obj">The PSObject to extract the data from.</param> |
| | | 277 | | /// <returns>The data dictionary, or null if not found or empty.</returns> |
| | | 278 | | private static Dictionary<string, object>? GetDataDictionary(PSObject obj) |
| | | 279 | | { |
| | | 280 | | // Extract underlying dictionary (case-insensitive property name handling already done) |
| | 0 | 281 | | var dictionary = TryExtractDataDictionary(obj); |
| | 0 | 282 | | if (dictionary is null) |
| | | 283 | | { |
| | 0 | 284 | | return null; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | // Build normalized temporary dictionary (allows null filtering before final allocation) |
| | 0 | 288 | | var temp = BuildNormalizedData(dictionary); |
| | 0 | 289 | | if (temp.Count == 0) |
| | | 290 | | { |
| | 0 | 291 | | return null; // No meaningful data left after normalization |
| | | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | return PromoteData(temp); |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | private static IDictionary? TryExtractDataDictionary(PSObject obj) |
| | | 298 | | { |
| | 0 | 299 | | var dataProperty = obj.Properties["data"] ?? obj.Properties["Data"]; |
| | 0 | 300 | | return dataProperty?.Value is IDictionary dict && dict.Count > 0 ? dict : null; |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | private static bool IsValidDataKey(string? key) => !string.IsNullOrWhiteSpace(key); |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Builds a normalized data dictionary from the original dictionary. |
| | | 307 | | /// </summary> |
| | | 308 | | /// <param name="dictionary">The original dictionary to normalize.</param> |
| | | 309 | | /// <returns>A new dictionary with normalized data.</returns> |
| | | 310 | | private static Dictionary<string, object?> BuildNormalizedData(IDictionary dictionary) |
| | | 311 | | { |
| | 0 | 312 | | var temp = new Dictionary<string, object?>(dictionary.Count, StringComparer.OrdinalIgnoreCase); |
| | 0 | 313 | | foreach (DictionaryEntry entry in dictionary) |
| | | 314 | | { |
| | 0 | 315 | | if (entry.Key is null || entry.Value is null) |
| | | 316 | | { |
| | | 317 | | continue; |
| | | 318 | | } |
| | 0 | 319 | | var key = entry.Key.ToString(); |
| | 0 | 320 | | if (!IsValidDataKey(key)) |
| | | 321 | | { |
| | | 322 | | continue; |
| | | 323 | | } |
| | 0 | 324 | | var normalized = NormalizePsValue(entry.Value); |
| | 0 | 325 | | if (normalized is not null) |
| | | 326 | | { |
| | 0 | 327 | | temp[key!] = normalized; // key validated |
| | | 328 | | } |
| | | 329 | | } |
| | 0 | 330 | | return temp; |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | /// <summary> |
| | | 334 | | /// Promotes the data from a temporary dictionary to a final dictionary. |
| | | 335 | | /// </summary> |
| | | 336 | | /// <param name="temp">The temporary dictionary to promote.</param> |
| | | 337 | | /// <returns>The promoted dictionary.</returns> |
| | | 338 | | private static Dictionary<string, object> PromoteData(Dictionary<string, object?> temp) |
| | | 339 | | { |
| | 0 | 340 | | var final = new Dictionary<string, object>(temp.Count, StringComparer.OrdinalIgnoreCase); |
| | 0 | 341 | | foreach (var kvp in temp) |
| | | 342 | | { |
| | | 343 | | // Value cannot be null due to earlier filter |
| | 0 | 344 | | final[kvp.Key] = kvp.Value!; |
| | | 345 | | } |
| | 0 | 346 | | return final; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// Normalizes a value that may originate from PowerShell so JSON serialization stays lean. |
| | | 351 | | /// (Delegates to smaller helpers to keep cyclomatic complexity low.) |
| | | 352 | | /// </summary> |
| | | 353 | | /// <param name="value">The value to normalize.</param> |
| | | 354 | | /// <param name="depth">The current recursion depth.</param> |
| | | 355 | | /// <returns>The normalized value.</returns> |
| | | 356 | | private static object? NormalizePsValue(object? value, int depth = 0) |
| | | 357 | | { |
| | 0 | 358 | | if (value is null) |
| | | 359 | | { |
| | 0 | 360 | | return null; |
| | | 361 | | } |
| | 0 | 362 | | if (depth > 8) |
| | | 363 | | { |
| | 0 | 364 | | return CollapseAtDepth(value); |
| | | 365 | | } |
| | 0 | 366 | | if (value is PSObject psObj) |
| | | 367 | | { |
| | 0 | 368 | | return NormalizePsPsObject(psObj, depth); |
| | | 369 | | } |
| | 0 | 370 | | if (IsPrimitive(value)) |
| | | 371 | | { |
| | 0 | 372 | | return value; |
| | | 373 | | } |
| | 0 | 374 | | if (value is IDictionary dict) |
| | | 375 | | { |
| | 0 | 376 | | return NormalizeDictionary(dict, depth); |
| | | 377 | | } |
| | | 378 | | // Avoid treating strings as IEnumerable<char> |
| | 0 | 379 | | return value switch |
| | 0 | 380 | | { |
| | 0 | 381 | | string s => s, |
| | 0 | 382 | | IEnumerable seq => NormalizeEnumerable(seq, depth), |
| | 0 | 383 | | _ => value |
| | 0 | 384 | | }; |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | /// <summary> |
| | | 388 | | /// Determines if a value is a primitive type that can be directly serialized. |
| | | 389 | | /// </summary> |
| | | 390 | | /// <param name="value">The value to check.</param> |
| | | 391 | | /// <returns>True if the value is a primitive type, false otherwise.</returns> |
| | 0 | 392 | | private static bool IsPrimitive(object value) => value is IFormattable && value.GetType().IsPrimitive; |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// Collapses a value to its string representation when maximum depth is reached. |
| | | 396 | | /// </summary> |
| | | 397 | | /// <param name="value">The value to collapse.</param> |
| | | 398 | | /// <returns>The string representation of the value.</returns> |
| | 0 | 399 | | private static string CollapseAtDepth(object value) => value is PSObject pso ? pso.ToString() : value.ToString() ?? |
| | | 400 | | |
| | | 401 | | /// <summary> |
| | | 402 | | /// Normalizes a PowerShell PSObject by extracting its base object and normalizing it. |
| | | 403 | | /// </summary> |
| | | 404 | | /// <param name="psObj">The PSObject to normalize.</param> |
| | | 405 | | /// <param name="depth">The current recursion depth.</param> |
| | | 406 | | private static object? NormalizePsPsObject(PSObject psObj, int depth) |
| | | 407 | | { |
| | 0 | 408 | | var baseObj = psObj.BaseObject; |
| | 0 | 409 | | return baseObj is null || ReferenceEquals(baseObj, psObj) |
| | 0 | 410 | | ? psObj.ToString() |
| | 0 | 411 | | : NormalizePsValue(baseObj, depth + 1); |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | /// <summary> |
| | | 415 | | /// Normalizes a dictionary by converting its keys to strings and recursively normalizing its values. |
| | | 416 | | /// </summary> |
| | | 417 | | /// <param name="rawDict">The raw dictionary to normalize.</param> |
| | | 418 | | /// <param name="depth">The current recursion depth.</param> |
| | | 419 | | /// <returns>A normalized dictionary with string keys and normalized values.</returns> |
| | | 420 | | private static Dictionary<string, object?> NormalizeDictionary(IDictionary rawDict, int depth) |
| | | 421 | | { |
| | 0 | 422 | | var result = new Dictionary<string, object?>(rawDict.Count); |
| | 0 | 423 | | foreach (DictionaryEntry de in rawDict) |
| | | 424 | | { |
| | 0 | 425 | | if (de.Key is null) |
| | | 426 | | { |
| | | 427 | | continue; |
| | | 428 | | } |
| | 0 | 429 | | var k = de.Key.ToString(); |
| | 0 | 430 | | if (string.IsNullOrWhiteSpace(k)) |
| | | 431 | | { |
| | | 432 | | continue; |
| | | 433 | | } |
| | 0 | 434 | | result[k] = NormalizePsValue(de.Value, depth + 1); |
| | | 435 | | } |
| | 0 | 436 | | return result; |
| | | 437 | | } |
| | | 438 | | |
| | | 439 | | /// <summary> |
| | | 440 | | /// Normalizes an enumerable by recursively normalizing its items. |
| | | 441 | | /// </summary> |
| | | 442 | | /// <param name="enumerable">The enumerable to normalize.</param> |
| | | 443 | | /// <param name="depth">The current recursion depth.</param> |
| | | 444 | | /// <returns>A list of normalized items.</returns> |
| | | 445 | | private static List<object?> NormalizeEnumerable(IEnumerable enumerable, int depth) |
| | | 446 | | { |
| | 0 | 447 | | var list = new List<object?>(); |
| | 0 | 448 | | foreach (var item in enumerable) |
| | | 449 | | { |
| | 0 | 450 | | list.Add(NormalizePsValue(item, depth + 1)); |
| | | 451 | | } |
| | 0 | 452 | | return list; |
| | | 453 | | } |
| | | 454 | | |
| | | 455 | | /// <summary> |
| | | 456 | | /// Parses a status string into a ProbeStatus enum. |
| | | 457 | | /// </summary> |
| | | 458 | | /// <param name="value">The status string to parse.</param> |
| | | 459 | | /// <param name="status">The resulting ProbeStatus enum.</param> |
| | | 460 | | /// <returns>True if the parsing was successful, false otherwise.</returns> |
| | | 461 | | private static bool TryParseStatus(string? value, out ProbeStatus status) |
| | | 462 | | { |
| | 0 | 463 | | if (Enum.TryParse(value, ignoreCase: true, out status)) |
| | | 464 | | { |
| | 0 | 465 | | return true; |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | |
| | 0 | 469 | | switch (value?.ToLowerInvariant()) |
| | | 470 | | { |
| | | 471 | | case ProbeStatusLabels.STATUS_OK: |
| | | 472 | | case ProbeStatusLabels.STATUS_HEALTHY: |
| | 0 | 473 | | status = ProbeStatus.Healthy; |
| | 0 | 474 | | return true; |
| | | 475 | | case ProbeStatusLabels.STATUS_WARN: |
| | | 476 | | case ProbeStatusLabels.STATUS_WARNING: |
| | | 477 | | case ProbeStatusLabels.STATUS_DEGRADED: |
| | 0 | 478 | | status = ProbeStatus.Degraded; |
| | 0 | 479 | | return true; |
| | | 480 | | case ProbeStatusLabels.STATUS_FAIL: |
| | | 481 | | case ProbeStatusLabels.STATUS_FAILED: |
| | | 482 | | case ProbeStatusLabels.STATUS_UNHEALTHY: |
| | 0 | 483 | | status = ProbeStatus.Unhealthy; |
| | 0 | 484 | | return true; |
| | | 485 | | default: |
| | 0 | 486 | | status = ProbeStatus.Unhealthy; |
| | 0 | 487 | | return false; |
| | | 488 | | } |
| | | 489 | | } |
| | | 490 | | } |