comparison 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
comparison
equal deleted inserted replaced
16:7626b099aefd 17:68d7834dc28e
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.ComponentModel; 3 using System.ComponentModel;
4 using System.Linq; 4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7 5
8 namespace ServerMonitorApp 6 namespace ServerMonitorApp
9 { 7 {
8 /// <summary>Checks the available disk space on a remote server.</summary>
10 [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)] 9 [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)]
11 public class DiskSpaceCheck : SshCheck 10 public class DiskSpaceCheck : SshCheck
12 { 11 {
12 /// <summary>The command to execute. Must return the POSIX output format of df(1).</summary>
13 public override string Command => string.Format(DiskSpaceCommand, Device); 13 public override string Command => string.Format(DiskSpaceCommand, Device);
14 14
15 /// <summary>The command to execute, with a placeholder of {0} for the device to check.</summary>
15 protected string DiskSpaceCommand => "df -P -k {0}"; 16 protected string DiskSpaceCommand => "df -P -k {0}";
16 17
18 /// <summary>The device or file on the device to check.</summary>
17 public string Device { get; set; } 19 public string Device { get; set; }
18 20
21 /// <summary>The minimum free space allowed for the check to pass.</summary>
19 public double MinFreeSpace { get; set; } 22 public double MinFreeSpace { get; set; }
20 23
24 /// <summary>The storage units or percentage for MinFreeSpace.</summary>
21 public FreeSpaceUnits FreeSpaceUnits { get; set; } 25 public FreeSpaceUnits FreeSpaceUnits { get; set; }
22 26
23 public DiskSpaceCheck() 27 public DiskSpaceCheck()
24 { 28 {
29 // Set general SSH check settings for disk space checks.
25 CheckExitCode = true; 30 CheckExitCode = true;
26 ExitCode = 0; 31 ExitCode = 0;
27 } 32 }
28 33
34 /// <summary>Processes the output of the disk space check command.</summary>
29 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) 35 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
30 { 36 {
37 // Check for general SSH failures.
31 List<CheckResult> results = base.ProcessCommandResult(output, exitCode); 38 List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
39 // If there was an error running the command, fail immediately.
32 if (results.Any(r => r.Failed)) 40 if (results.Any(r => r.Failed))
33 return results; 41 return results;
34 42
43 /* Parse the command results, expected in the POSIX output format of df(1):
44 Filesystem 1024-blocks Used Available Capacity Mounted on
45 <file system name> <total space> <space used> <space free> <percentage used> <file system root>
46 */
47 // Split the output into lines and remove the header row.
35 List<string> lines = output.Split('\n').ToList(); 48 List<string> lines = output.Split('\n').ToList();
36 lines.RemoveAt(0); 49 lines.RemoveAt(0);
50 // Make sure there is only a single line of output remaining.
37 if (lines.Count > 1) 51 if (lines.Count > 1)
38 { 52 {
39 results.Add(Fail("df output was more than one line: " + string.Join("\n", lines))); 53 results.Add(Fail("df output was more than one line: " + string.Join("\n", lines)));
40 } 54 }
41 else 55 else
42 { 56 {
57 // Split the string into tokens on whitespace.
43 string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries); 58 string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
44 if (FreeSpaceUnits == FreeSpaceUnits.percent) 59 if (FreeSpaceUnits == FreeSpaceUnits.percent)
45 { 60 {
61 // Test on percentage: calculate the capacity free percent from the capacity used percent reported.
46 if (int.TryParse(tokens[4].Replace("%", ""), out int percent)) 62 if (int.TryParse(tokens[4].Replace("%", ""), out int percent))
47 { 63 {
48 percent = 100 - percent; 64 percent = 100 - percent;
49 string message = string.Format("Free disk space is {0}%", percent); 65 string message = string.Format("Free disk space is {0}%", percent);
50 if (percent < MinFreeSpace) 66 if (percent < MinFreeSpace)
57 results.Add(Fail("Unable to parse df output as integer: " + tokens[4].Replace("%", ""))); 73 results.Add(Fail("Unable to parse df output as integer: " + tokens[4].Replace("%", "")));
58 } 74 }
59 } 75 }
60 else 76 else
61 { 77 {
78 // Test on bytes: calculate the remaining available space from the reported available space.
62 if (int.TryParse(tokens[3], out int freeSpace)) 79 if (int.TryParse(tokens[3], out int freeSpace))
63 { 80 {
81 // Available space is returned in KB. Convert to MB (our default unit).
64 freeSpace /= 1024; 82 freeSpace /= 1024;
83 // If the unit is GB, convert MB to GB.
65 if (FreeSpaceUnits == FreeSpaceUnits.GB) 84 if (FreeSpaceUnits == FreeSpaceUnits.GB)
66 freeSpace /= 1024; 85 freeSpace /= 1024;
67 string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits); 86 string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits);
68 if (freeSpace < MinFreeSpace) 87 if (freeSpace < MinFreeSpace)
69 results.Add(Fail(message)); 88 results.Add(Fail(message));
77 } 96 }
78 } 97 }
79 return results; 98 return results;
80 } 99 }
81 100
101 /// <summary>Validates disk space check options.</summary>
82 public override string Validate(bool saving = true) 102 public override string Validate(bool saving = true)
83 { 103 {
84 string message = base.Validate(); 104 string message = base.Validate();
85 if (Device.IsNullOrEmpty()) 105 if (Device.IsNullOrEmpty())
86 message += "Device is required." + Environment.NewLine; 106 message += "Device is required." + Environment.NewLine;
90 message += "Free space percent must be between 0 and 100." + Environment.NewLine; 110 message += "Free space percent must be between 0 and 100." + Environment.NewLine;
91 return message; 111 return message;
92 } 112 }
93 } 113 }
94 114
95 public enum FreeSpaceUnits { MB = 0, GB = 1, percent = 2 } 115 /// <summary>The units to use when testing available disk space.</summary>
116 public enum FreeSpaceUnits
117 {
118 /// <summary>Tests available disk space in megabytes.</summary>
119 MB = 0,
120 /// <summary>Tests available disk space in gigabytes.</summary>
121 GB = 1,
122 /// <summary>Tests available disk space as a percentage of the total space.</summary>
123 percent = 2
124 }
96 } 125 }