Mercurial > servermonitor
annotate ServerMonitor/Objects/Checks/FileCheck.cs @ 13:a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Mon, 15 Apr 2019 19:25:27 -0400 |
parents | 7127d5b5ac75 |
children | 68d7834dc28e |
rev | line source |
---|---|
3 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.ComponentModel; | |
4 using System.Linq; | |
5 using System.Text; | |
6 using System.Text.RegularExpressions; | |
7 using System.Threading.Tasks; | |
13
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
8 using System.Xml.Serialization; |
3 | 9 |
10 namespace ServerMonitorApp | |
11 { | |
12 [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)] | |
13 public class FileCheck : SshCheck | |
14 { | |
13
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
15 public override string Command => string.Format(FileCommand, Regex.Replace(File, "^~", "$HOME")); |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
16 |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
17 protected string FileCommand |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
18 { |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
19 get |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
20 { |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
21 int timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now).Hours * -1; // Invert because POSIX says so. |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
22 return "export TIME_STYLE=long-iso; export TZ=UTC" + timeZoneOffset + "; ls -l \"{0}\""; |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
23 } |
a36cc5c123f4
Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents:
9
diff
changeset
|
24 } |
3 | 25 |
26 public string File { get; set; } | |
27 | |
28 public bool CheckFileSize { get; set; } | |
29 | |
30 public bool FileSizeLessThan { get; set; } | |
31 | |
4 | 32 public int FileSize { get; set; } |
3 | 33 |
34 public double FileSizeInSelectedUnits | |
35 { | |
4 | 36 get => Math.Round(ConvertBytesToSelectedUnits(FileSize), 1); |
37 set => FileSize = ConvertSelectedUnitsToBytes(value); | |
3 | 38 } |
39 | |
40 public SizeUnits FileSizeUnits { get; set; } | |
41 | |
42 public bool CheckDateModified { get; set; } | |
43 | |
44 public bool DateModifiedOlderThan { get; set; } | |
45 | |
46 public double DateModified { get; set; } | |
47 | |
48 public TimeUnits DateModifiedUnits { get; set; } | |
49 | |
50 public FileCheck() | |
51 { | |
52 CheckExitCode = true; | |
53 ExitCode = 0; | |
54 } | |
55 | |
56 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) | |
57 { | |
58 List<CheckResult> results = base.ProcessCommandResult(output, exitCode); | |
4 | 59 if (results.Any(r => r.Failed)) |
3 | 60 return results; |
61 | |
62 if (output.Split('\n').Length > 1) | |
63 { | |
64 results.Add(Fail("ls output was more than one line: " + output)); | |
65 } | |
66 else | |
67 { | |
68 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); | |
69 if (CheckFileSize) | |
70 { | |
71 if (int.TryParse(tokens[4], out int bytes)) | |
72 { | |
4 | 73 string message = string.Format("File size is {0} {1}", Math.Round(ConvertBytesToSelectedUnits(bytes), 1), FileSizeUnits); |
3 | 74 if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan)) |
75 results.Add(Pass(message)); | |
76 else | |
77 results.Add(Fail(message)); | |
78 } | |
79 else | |
80 { | |
81 results.Add(Fail("Unable to parse ls output as integer: " + tokens[4])); | |
82 } | |
83 } | |
84 if (CheckDateModified) | |
85 { | |
86 string dateString = tokens[5] + " " + tokens[6]; | |
87 if (DateTime.TryParse(dateString, out DateTime modified)) | |
88 { | |
89 string message = string.Format("Last modified date is {0}", modified); | |
90 TimeSpan age = DateTime.Now.Subtract(modified); | |
91 double ageCompare = DateModifiedUnits == TimeUnits.Minute ? age.TotalMinutes : | |
92 DateModifiedUnits == TimeUnits.Hour ? age.TotalHours : | |
93 age.TotalDays; | |
94 if ((ageCompare > DateModified && DateModifiedOlderThan) || (ageCompare < DateModified && !DateModifiedOlderThan)) | |
95 results.Add(Pass(message)); | |
96 else | |
97 results.Add(Fail(message)); | |
98 } | |
99 else | |
100 { | |
101 results.Add(Fail("Unable to parse ls output as date: " + dateString)); | |
102 } | |
103 } | |
104 } | |
105 return results; | |
106 } | |
107 | |
108 public override string Validate(bool saving = true) | |
109 { | |
110 string message = base.Validate(); | |
111 if (File.IsNullOrEmpty()) | |
112 message += "File is required." + Environment.NewLine; | |
113 if (!CheckFileSize && !CheckDateModified) | |
114 message += "At least one check must be enabled." + Environment.NewLine; | |
9 | 115 if (CheckFileSize && FileSize < 0) |
116 message += "File size must be at least 0." + Environment.NewLine; | |
3 | 117 if (CheckDateModified && DateModified <= 0) |
118 message += "Date modified must be greater than 0." + Environment.NewLine; | |
119 return message; | |
120 } | |
4 | 121 |
122 private double ConvertBytesToSelectedUnits(int sizeInBytes) | |
123 { | |
124 return sizeInBytes / Math.Pow(1024, (double)FileSizeUnits); | |
125 } | |
126 | |
127 private int ConvertSelectedUnitsToBytes(double sizeInSelectedUnits) | |
128 { | |
129 return (int)(sizeInSelectedUnits * Math.Pow(1024, (int)FileSizeUnits)); | |
130 } | |
3 | 131 } |
132 } |