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 {
|
3
|
13 public override string Command { get => string.Format(base.Command, Device); }
|
|
14
|
2
|
15 public string Device { get; set; }
|
|
16
|
|
17 public double MinFreeSpace { get; set; }
|
|
18
|
|
19 public FreeSpaceUnits FreeSpaceUnits { get; set; }
|
|
20
|
|
21 public DiskSpaceCheck()
|
|
22 {
|
3
|
23 //Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
|
|
24 Command = "df -P -k {0}";
|
2
|
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);
|
3
|
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)
|
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 } |