annotate ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 12:d92176c5398a

Better display of schedule name.
author Brad Greco <brad@bgreco.net>
date Mon, 15 Apr 2019 19:24:55 -0400
parents 3142e52cbe69
children a36cc5c123f4
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 using System.Text;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Threading.Tasks;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 namespace ServerMonitorApp
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 [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
11 public class DiskSpaceCheck : SshCheck
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
13 public override string Command => string.Format(base.Command, Device);
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
14
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 public string Device { get; set; }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 public double MinFreeSpace { get; set; }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 public FreeSpaceUnits FreeSpaceUnits { get; set; }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 public DiskSpaceCheck()
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 {
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
23 //Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
24 Command = "df -P -k {0}";
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 CheckExitCode = true;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 ExitCode = 0;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
32 if (results.Any(r => r.Failed))
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
33 return results;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
34
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
35 List<string> lines = output.Split('\n').ToList();
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
36 lines.RemoveAt(0);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
37 if (lines.Count > 1)
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 {
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
39 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
40 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 else
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 {
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
43 string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 if (FreeSpaceUnits == FreeSpaceUnits.percent)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 {
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
46 if (int.TryParse(tokens[4].Replace("%", ""), out int percent))
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 percent = 100 - percent;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 string message = string.Format("Free disk space is {0}%", percent);
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 if (percent < MinFreeSpace)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 results.Add(Fail(message));
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 else
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 results.Add(Pass(message));
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 {
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
57 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
58 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 else
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 {
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
62 if (int.TryParse(tokens[3], out int freeSpace))
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 freeSpace /= 1024;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 if (FreeSpaceUnits == FreeSpaceUnits.GB)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 freeSpace /= 1024;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits);
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 if (freeSpace < MinFreeSpace)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 results.Add(Fail(message));
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 else
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 results.Add(Pass(message));
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 else
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 {
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
75 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
76 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 return results;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 public override string Validate(bool saving = true)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 string message = base.Validate();
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 if (Device.IsNullOrEmpty())
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 message += "Device is required." + Environment.NewLine;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 if (MinFreeSpace <= 0)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 message += "Free space must be greater than 0." + Environment.NewLine;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 else if (FreeSpaceUnits == FreeSpaceUnits.percent && MinFreeSpace > 100)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90 message += "Free space percent must be between 0 and 100." + Environment.NewLine;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
91 return message;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
92 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
95 public enum FreeSpaceUnits { MB = 0, GB = 1, percent = 2 }
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
96 }