Mercurial > servermonitor
annotate ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 15:23f2e0da1094
- Fix the last execution status being lost after a check is edited.
- Add comments.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Mon, 22 Apr 2019 21:11:27 -0400 |
parents | a36cc5c123f4 |
children | 68d7834dc28e |
rev | line source |
---|---|
2 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.ComponentModel; | |
4 using System.Linq; | |
5 using System.Text; | |
6 using System.Threading.Tasks; | |
7 | |
8 namespace ServerMonitorApp | |
9 { | |
10 [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)] | |
11 public class DiskSpaceCheck : SshCheck | |
12 { | |
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 |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
15 protected string DiskSpaceCommand => "df -P -k {0}"; |
3 | 16 |
2 | 17 public string Device { get; set; } |
18 | |
19 public double MinFreeSpace { get; set; } | |
20 | |
21 public FreeSpaceUnits FreeSpaceUnits { get; set; } | |
22 | |
23 public DiskSpaceCheck() | |
24 { | |
25 CheckExitCode = true; | |
26 ExitCode = 0; | |
27 } | |
28 | |
29 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) | |
30 { | |
31 List<CheckResult> results = base.ProcessCommandResult(output, exitCode); | |
4 | 32 if (results.Any(r => r.Failed)) |
3 | 33 return results; |
34 | |
35 List<string> lines = output.Split('\n').ToList(); | |
36 lines.RemoveAt(0); | |
37 if (lines.Count > 1) | |
2 | 38 { |
3 | 39 results.Add(Fail("df output was more than one line: " + string.Join("\n", lines))); |
2 | 40 } |
41 else | |
42 { | |
3 | 43 string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries); |
2 | 44 if (FreeSpaceUnits == FreeSpaceUnits.percent) |
45 { | |
3 | 46 if (int.TryParse(tokens[4].Replace("%", ""), out int percent)) |
2 | 47 { |
48 percent = 100 - percent; | |
49 string message = string.Format("Free disk space is {0}%", percent); | |
50 if (percent < MinFreeSpace) | |
51 results.Add(Fail(message)); | |
52 else | |
53 results.Add(Pass(message)); | |
54 } | |
55 else | |
56 { | |
3 | 57 results.Add(Fail("Unable to parse df output as integer: " + tokens[4].Replace("%", ""))); |
2 | 58 } |
59 } | |
60 else | |
61 { | |
3 | 62 if (int.TryParse(tokens[3], out int freeSpace)) |
2 | 63 { |
64 freeSpace /= 1024; | |
65 if (FreeSpaceUnits == FreeSpaceUnits.GB) | |
66 freeSpace /= 1024; | |
67 string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits); | |
68 if (freeSpace < MinFreeSpace) | |
69 results.Add(Fail(message)); | |
70 else | |
71 results.Add(Pass(message)); | |
72 } | |
73 else | |
74 { | |
3 | 75 results.Add(Fail("Unable to parse df output as integer: " + tokens[3])); |
2 | 76 } |
77 } | |
78 } | |
79 return results; | |
80 } | |
81 | |
82 public override string Validate(bool saving = true) | |
83 { | |
84 string message = base.Validate(); | |
85 if (Device.IsNullOrEmpty()) | |
86 message += "Device is required." + Environment.NewLine; | |
87 if (MinFreeSpace <= 0) | |
88 message += "Free space must be greater than 0." + Environment.NewLine; | |
89 else if (FreeSpaceUnits == FreeSpaceUnits.percent && MinFreeSpace > 100) | |
90 message += "Free space percent must be between 0 and 100." + Environment.NewLine; | |
91 return message; | |
92 } | |
93 } | |
94 | |
95 public enum FreeSpaceUnits { MB = 0, GB = 1, percent = 2 } | |
3 | 96 } |