annotate ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 32:2342e9459444

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