annotate ServerMonitor/Objects/Checks/FileCheck.cs @ 16:7626b099aefd

More comments.
author Brad Greco <brad@bgreco.net>
date Tue, 30 Apr 2019 20:40:58 -0400
parents a36cc5c123f4
children 68d7834dc28e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
1 using System;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2 using System.Collections.Generic;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.ComponentModel;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.Linq;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Text;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Text.RegularExpressions;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
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
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 namespace ServerMonitorApp
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)]
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 public class FileCheck : SshCheck
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
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
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 public string File { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 public bool CheckFileSize { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 public bool FileSizeLessThan { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
32 public int FileSize { get; set; }
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 public double FileSizeInSelectedUnits
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
36 get => Math.Round(ConvertBytesToSelectedUnits(FileSize), 1);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
37 set => FileSize = ConvertSelectedUnitsToBytes(value);
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40 public SizeUnits FileSizeUnits { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 public bool CheckDateModified { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 public bool DateModifiedOlderThan { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 public double DateModified { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 public TimeUnits DateModifiedUnits { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 public FileCheck()
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 CheckExitCode = true;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 ExitCode = 0;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
59 if (results.Any(r => r.Failed))
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 return results;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 if (output.Split('\n').Length > 1)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 results.Add(Fail("ls output was more than one line: " + output));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 if (CheckFileSize)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 if (int.TryParse(tokens[4], out int bytes))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
73 string message = string.Format("File size is {0} {1}", Math.Round(ConvertBytesToSelectedUnits(bytes), 1), FileSizeUnits);
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75 results.Add(Pass(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 results.Add(Fail(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 results.Add(Fail("Unable to parse ls output as integer: " + tokens[4]));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 if (CheckDateModified)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 string dateString = tokens[5] + " " + tokens[6];
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 if (DateTime.TryParse(dateString, out DateTime modified))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 string message = string.Format("Last modified date is {0}", modified);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90 TimeSpan age = DateTime.Now.Subtract(modified);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
91 double ageCompare = DateModifiedUnits == TimeUnits.Minute ? age.TotalMinutes :
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
92 DateModifiedUnits == TimeUnits.Hour ? age.TotalHours :
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 age.TotalDays;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 if ((ageCompare > DateModified && DateModifiedOlderThan) || (ageCompare < DateModified && !DateModifiedOlderThan))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
95 results.Add(Pass(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
96 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
97 results.Add(Fail(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
100 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101 results.Add(Fail("Unable to parse ls output as date: " + dateString));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
102 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
103 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 return results;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
108 public override string Validate(bool saving = true)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
109 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 string message = base.Validate();
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 if (File.IsNullOrEmpty())
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 message += "File is required." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 if (!CheckFileSize && !CheckDateModified)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
114 message += "At least one check must be enabled." + Environment.NewLine;
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
115 if (CheckFileSize && FileSize < 0)
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
116 message += "File size must be at least 0." + Environment.NewLine;
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
117 if (CheckDateModified && DateModified <= 0)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
118 message += "Date modified must be greater than 0." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
119 return message;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
120 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
121
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
122 private double ConvertBytesToSelectedUnits(int sizeInBytes)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
123 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
124 return sizeInBytes / Math.Pow(1024, (double)FileSizeUnits);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
125 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
126
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
127 private int ConvertSelectedUnitsToBytes(double sizeInSelectedUnits)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
128 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
129 return (int)(sizeInSelectedUnits * Math.Pow(1024, (int)FileSizeUnits));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
130 }
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
131 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
132 }