Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DiskStorageOptions.CheckAllDrives #987

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions src/HealthChecks.System/DiskStorageHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,49 @@ public DiskStorageHealthCheck(DiskStorageOptions options)
_options = options ?? throw new ArgumentNullException(nameof(options));
}

/// <inheritdoc/>
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var configuredDrives = _options.ConfiguredDrives.Values;
var configuredDrives = _options.ConfiguredDrives;
List<string>? errors = null;

foreach (var (DriveName, MinimumFreeMegabytes) in configuredDrives)
if (configuredDrives.Count > 0)
{
var (Exists, ActualFreeMegabytes) = GetSystemDriveInfo(DriveName);
var drives = DriveInfo.GetDrives();

if (Exists)
foreach (var (DriveName, MinimumFreeMegabytes) in configuredDrives.Values)
{
if (ActualFreeMegabytes < MinimumFreeMegabytes)
var driveInfo = drives.FirstOrDefault(drive => string.Equals(drive.Name, DriveName, StringComparison.InvariantCultureIgnoreCase));

if (driveInfo != null)
{
return Task.FromResult(
new HealthCheckResult(context.Registration.FailureStatus, description: $"Minimum configured megabytes for disk {DriveName} is {MinimumFreeMegabytes} but actual free space are {ActualFreeMegabytes} megabytes"));
long actualFreeMegabytes = driveInfo.AvailableFreeSpace / 1024 / 1024;
if (actualFreeMegabytes < MinimumFreeMegabytes)
{
(errors ??= new()).Add(_options.FailedDescription(DriveName, MinimumFreeMegabytes, actualFreeMegabytes));
if (!_options.CheckAllDrives)
break;
}
}
else
{
(errors ??= new()).Add(_options.FailedDescription(DriveName, MinimumFreeMegabytes, null));
if (!_options.CheckAllDrives)
break;
}
}
else
{
return Task.FromResult(
new HealthCheckResult(context.Registration.FailureStatus, description: $"Configured drive {DriveName} is not present on system"));
}
}
return HealthCheckResultTask.Healthy;

return errors?.Count > 0
? Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, description: string.Join("; ", errors)))
: HealthCheckResultTask.Healthy;
}
catch (Exception ex)
{
return Task.FromResult(
new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
}
}

private static (bool Exists, long ActualFreeMegabytes) GetSystemDriveInfo(string driveName)
{
var driveInfo = DriveInfo.GetDrives()
.FirstOrDefault(drive => string.Equals(drive.Name, driveName, StringComparison.InvariantCultureIgnoreCase));

return driveInfo == null
? (false, 0)
: (true, driveInfo.AvailableFreeSpace / 1024 / 1024);
}
}
}
12 changes: 12 additions & 0 deletions src/HealthChecks.System/DiskStorageLivenessOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@ public DiskStorageOptions AddDrive(string driveName, long minimumFreeMegabytes =
ConfiguredDrives.Add(driveName, (driveName, minimumFreeMegabytes));
return this;
}

public bool CheckAllDrives { get; set; }

/// <summary>
/// Allows to set custom description of the failed disk check.
/// </summary>
public ErrorDescription FailedDescription = (driveName, minimumFreeMegabytes, actualFreeMegabytes)
=> actualFreeMegabytes == null
? $"Configured drive {driveName} is not present on system"
: $"Minimum configured megabytes for disk {driveName} is {minimumFreeMegabytes} but actual free space are {actualFreeMegabytes} megabytes";

public delegate string ErrorDescription(string driveName, long minimumFreeMegabytes, long? actualFreeMegabytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public async Task be_healthy_when_disks_have_more_free_space_than_configured()
});

using var server = new TestServer(webHostBuilder);
var response = await server.CreateRequest("/health")
.GetAsync();
using var response = await server.CreateRequest("/health").GetAsync();

response.EnsureSuccessStatusCode();
}
Expand Down Expand Up @@ -66,8 +65,7 @@ public async Task be_unhealthy_when_a_disk_has_less_free_space_than_configured()
});

using var server = new TestServer(webHostBuilder);
var response = await server.CreateRequest("/health")
.GetAsync();
using var response = await server.CreateRequest("/health").GetAsync();

response.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable);
}
Expand All @@ -90,8 +88,7 @@ public async Task be_unhealthy_when_a_configured_disk_does_not_exist()
});

using var server = new TestServer(webHostBuilder);
var response = await server.CreateRequest("/health")
.GetAsync();
using var response = await server.CreateRequest("/health").GetAsync();

response.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable);
}
Expand Down