Mercurial > servermonitor
diff ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 17:68d7834dc28e
More comments.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sat, 25 May 2019 15:14:26 -0400 |
parents | a36cc5c123f4 |
children |
line wrap: on
line diff
--- a/ServerMonitor/Objects/Checks/DiskSpaceCheck.cs Tue Apr 30 20:40:58 2019 -0400 +++ b/ServerMonitor/Objects/Checks/DiskSpaceCheck.cs Sat May 25 15:14:26 2019 -0400 @@ -2,47 +2,63 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ServerMonitorApp { + /// <summary>Checks the available disk space on a remote server.</summary> [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)] public class DiskSpaceCheck : SshCheck { + /// <summary>The command to execute. Must return the POSIX output format of df(1).</summary> public override string Command => string.Format(DiskSpaceCommand, Device); + /// <summary>The command to execute, with a placeholder of {0} for the device to check.</summary> protected string DiskSpaceCommand => "df -P -k {0}"; + /// <summary>The device or file on the device to check.</summary> public string Device { get; set; } + /// <summary>The minimum free space allowed for the check to pass.</summary> public double MinFreeSpace { get; set; } + /// <summary>The storage units or percentage for MinFreeSpace.</summary> public FreeSpaceUnits FreeSpaceUnits { get; set; } public DiskSpaceCheck() { + // Set general SSH check settings for disk space checks. CheckExitCode = true; ExitCode = 0; } + /// <summary>Processes the output of the disk space check command.</summary> protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) { + // Check for general SSH failures. List<CheckResult> results = base.ProcessCommandResult(output, exitCode); + // If there was an error running the command, fail immediately. if (results.Any(r => r.Failed)) return results; + /* Parse the command results, expected in the POSIX output format of df(1): + Filesystem 1024-blocks Used Available Capacity Mounted on + <file system name> <total space> <space used> <space free> <percentage used> <file system root> + */ + // Split the output into lines and remove the header row. List<string> lines = output.Split('\n').ToList(); lines.RemoveAt(0); + // Make sure there is only a single line of output remaining. if (lines.Count > 1) { results.Add(Fail("df output was more than one line: " + string.Join("\n", lines))); } else { + // Split the string into tokens on whitespace. string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries); if (FreeSpaceUnits == FreeSpaceUnits.percent) { + // Test on percentage: calculate the capacity free percent from the capacity used percent reported. if (int.TryParse(tokens[4].Replace("%", ""), out int percent)) { percent = 100 - percent; @@ -59,9 +75,12 @@ } else { + // Test on bytes: calculate the remaining available space from the reported available space. if (int.TryParse(tokens[3], out int freeSpace)) { + // Available space is returned in KB. Convert to MB (our default unit). freeSpace /= 1024; + // If the unit is GB, convert MB to GB. if (FreeSpaceUnits == FreeSpaceUnits.GB) freeSpace /= 1024; string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits); @@ -79,6 +98,7 @@ return results; } + /// <summary>Validates disk space check options.</summary> public override string Validate(bool saving = true) { string message = base.Validate(); @@ -92,5 +112,14 @@ } } - public enum FreeSpaceUnits { MB = 0, GB = 1, percent = 2 } + /// <summary>The units to use when testing available disk space.</summary> + public enum FreeSpaceUnits + { + /// <summary>Tests available disk space in megabytes.</summary> + MB = 0, + /// <summary>Tests available disk space in gigabytes.</summary> + GB = 1, + /// <summary>Tests available disk space as a percentage of the total space.</summary> + percent = 2 + } } \ No newline at end of file