annotate ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 2:453ecc1ed9ea

Disk space check
author Brad Greco <brad@bgreco.net>
date Sun, 06 Jan 2019 20:49:08 -0500
parents
children 96f0b028176d
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 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 public string Device { get; set; }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 public double MinFreeSpace { 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 FreeSpaceUnits FreeSpaceUnits { 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 DiskSpaceCheck()
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 Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 CheckExitCode = true;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 ExitCode = 0;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 protected override string GetCommand()
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 return string.Format(base.GetCommand(), Device);
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 }
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 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
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 List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 if (output.Split('\n').Length > 1)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 results.Add(Fail("df output was more than one line: " + output));
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 else
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 {
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 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 if (FreeSpaceUnits == FreeSpaceUnits.percent)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 if (int.TryParse(tokens[1].Replace("%", ""), out int percent))
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 percent = 100 - percent;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47 string message = string.Format("Free disk space is {0}%", percent);
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 if (percent < MinFreeSpace)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 results.Add(Fail(message));
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 else
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 results.Add(Pass(message));
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 else
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 results.Add(Fail("Unable to parse df output as integer: " + tokens[1].Replace("%", "")));
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 else
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 if (int.TryParse(tokens[0], out int freeSpace))
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 freeSpace /= 1024;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 if (FreeSpaceUnits == FreeSpaceUnits.GB)
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 string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits);
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 if (freeSpace < 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 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 results.Add(Fail("Unable to parse df output as integer: " + tokens[0]));
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 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 return results;
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
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 public override string Validate(bool saving = true)
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 string message = base.Validate();
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 if (Device.IsNullOrEmpty())
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 message += "Device is required." + Environment.NewLine;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 if (MinFreeSpace <= 0)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 message += "Free space must be greater than 0." + Environment.NewLine;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 else if (FreeSpaceUnits == FreeSpaceUnits.percent && MinFreeSpace > 100)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 message += "Free space percent must be between 0 and 100." + Environment.NewLine;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 return message;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90 }
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
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 public enum FreeSpaceUnits { MB = 0, GB = 1, percent = 2 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 }