comparison ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 3:96f0b028176d

File check
author Brad Greco <brad@bgreco.net>
date Fri, 11 Jan 2019 22:34:18 -0500
parents 453ecc1ed9ea
children 3142e52cbe69
comparison
equal deleted inserted replaced
2:453ecc1ed9ea 3:96f0b028176d
8 namespace ServerMonitorApp 8 namespace ServerMonitorApp
9 { 9 {
10 [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)] 10 [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)]
11 public class DiskSpaceCheck : SshCheck 11 public class DiskSpaceCheck : SshCheck
12 { 12 {
13 public override string Command { get => string.Format(base.Command, Device); }
14
13 public string Device { get; set; } 15 public string Device { get; set; }
14 16
15 public double MinFreeSpace { get; set; } 17 public double MinFreeSpace { get; set; }
16 18
17 public FreeSpaceUnits FreeSpaceUnits { get; set; } 19 public FreeSpaceUnits FreeSpaceUnits { get; set; }
18 20
19 public DiskSpaceCheck() 21 public DiskSpaceCheck()
20 { 22 {
21 Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5"; 23 //Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
24 Command = "df -P -k {0}";
22 CheckExitCode = true; 25 CheckExitCode = true;
23 ExitCode = 0; 26 ExitCode = 0;
24 }
25
26 protected override string GetCommand()
27 {
28 return string.Format(base.GetCommand(), Device);
29 } 27 }
30 28
31 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) 29 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
32 { 30 {
33 List<CheckResult> results = base.ProcessCommandResult(output, exitCode); 31 List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
34 if (output.Split('\n').Length > 1) 32 if (results.Any(r => r.CheckStatus != CheckStatus.Success))
33 return results;
34
35 List<string> lines = output.Split('\n').ToList();
36 lines.RemoveAt(0);
37 if (lines.Count > 1)
35 { 38 {
36 results.Add(Fail("df output was more than one line: " + output)); 39 results.Add(Fail("df output was more than one line: " + string.Join("\n", lines)));
37 } 40 }
38 else 41 else
39 { 42 {
40 43 string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
41 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
42 if (FreeSpaceUnits == FreeSpaceUnits.percent) 44 if (FreeSpaceUnits == FreeSpaceUnits.percent)
43 { 45 {
44 if (int.TryParse(tokens[1].Replace("%", ""), out int percent)) 46 if (int.TryParse(tokens[4].Replace("%", ""), out int percent))
45 { 47 {
46 percent = 100 - percent; 48 percent = 100 - percent;
47 string message = string.Format("Free disk space is {0}%", percent); 49 string message = string.Format("Free disk space is {0}%", percent);
48 if (percent < MinFreeSpace) 50 if (percent < MinFreeSpace)
49 results.Add(Fail(message)); 51 results.Add(Fail(message));
50 else 52 else
51 results.Add(Pass(message)); 53 results.Add(Pass(message));
52 } 54 }
53 else 55 else
54 { 56 {
55 results.Add(Fail("Unable to parse df output as integer: " + tokens[1].Replace("%", ""))); 57 results.Add(Fail("Unable to parse df output as integer: " + tokens[4].Replace("%", "")));
56 } 58 }
57 } 59 }
58 else 60 else
59 { 61 {
60 if (int.TryParse(tokens[0], out int freeSpace)) 62 if (int.TryParse(tokens[3], out int freeSpace))
61 { 63 {
62 freeSpace /= 1024; 64 freeSpace /= 1024;
63 if (FreeSpaceUnits == FreeSpaceUnits.GB) 65 if (FreeSpaceUnits == FreeSpaceUnits.GB)
64 freeSpace /= 1024; 66 freeSpace /= 1024;
65 string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits); 67 string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits);
68 else 70 else
69 results.Add(Pass(message)); 71 results.Add(Pass(message));
70 } 72 }
71 else 73 else
72 { 74 {
73 results.Add(Fail("Unable to parse df output as integer: " + tokens[0])); 75 results.Add(Fail("Unable to parse df output as integer: " + tokens[3]));
74 } 76 }
75 } 77 }
76 } 78 }
77 return results; 79 return results;
78 } 80 }