annotate ServerMonitor/Objects/Checks/FileCheck.cs @ 9:7127d5b5ac75

Code cleanup and comments
author Brad Greco <brad@bgreco.net>
date Mon, 08 Apr 2019 21:29:54 -0400
parents 3142e52cbe69
children a36cc5c123f4
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;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 namespace ServerMonitorApp
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)]
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 public class FileCheck : SshCheck
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
14 public override string Command => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME"));
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 public string File { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 public bool CheckFileSize { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 public bool FileSizeLessThan { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
22 public int FileSize { get; set; }
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 public double FileSizeInSelectedUnits
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
26 get => Math.Round(ConvertBytesToSelectedUnits(FileSize), 1);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
27 set => FileSize = ConvertSelectedUnitsToBytes(value);
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 }
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 SizeUnits FileSizeUnits { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32 public bool CheckDateModified { get; set; }
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 bool DateModifiedOlderThan { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 public double DateModified { get; set; }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 public TimeUnits DateModifiedUnits { get; set; }
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 FileCheck()
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 Command = "export TIME_STYLE=long-iso; ls -l \"{0}\"";
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 //Command = "export TIME_STYLE=long-iso; ls -l \"{0}\" | tr -s ' ' | cut -d ' ' -f 5,6,7";
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 CheckExitCode = true;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 ExitCode = 0;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
51 if (results.Any(r => r.Failed))
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 return results;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 if (output.Split('\n').Length > 1)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 results.Add(Fail("ls output was more than one line: " + output));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 if (CheckFileSize)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 if (int.TryParse(tokens[4], out int bytes))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 {
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
65 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
66 if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 results.Add(Pass(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 results.Add(Fail(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 results.Add(Fail("Unable to parse ls output as integer: " + tokens[4]));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 if (CheckDateModified)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 {
9
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
78 // TODO use the server time instead
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 string dateString = tokens[5] + " " + tokens[6];
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 if (DateTime.TryParse(dateString, out DateTime modified))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 string message = string.Format("Last modified date is {0}", modified);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 TimeSpan age = DateTime.Now.Subtract(modified);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 double ageCompare = DateModifiedUnits == TimeUnits.Minute ? age.TotalMinutes :
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 DateModifiedUnits == TimeUnits.Hour ? age.TotalHours :
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 age.TotalDays;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 if ((ageCompare > DateModified && DateModifiedOlderThan) || (ageCompare < DateModified && !DateModifiedOlderThan))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 results.Add(Pass(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90 results.Add(Fail(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
91 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
92 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 results.Add(Fail("Unable to parse ls output as date: " + dateString));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
95 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
96 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
97 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 return results;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
100
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101 public override string Validate(bool saving = true)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
102 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
103 string message = base.Validate();
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 if (File.IsNullOrEmpty())
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 message += "File is required." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106 if (!CheckFileSize && !CheckDateModified)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107 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
108 if (CheckFileSize && FileSize < 0)
7127d5b5ac75 Code cleanup and comments
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
109 message += "File size must be at least 0." + Environment.NewLine;
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 if (CheckDateModified && DateModified <= 0)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 message += "Date modified must be greater than 0." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 return message;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
114
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
115 private double ConvertBytesToSelectedUnits(int sizeInBytes)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
116 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
117 return sizeInBytes / Math.Pow(1024, (double)FileSizeUnits);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
118 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
119
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
120 private int ConvertSelectedUnitsToBytes(double sizeInSelectedUnits)
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 return (int)(sizeInSelectedUnits * Math.Pow(1024, (int)FileSizeUnits));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
123 }
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
124 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
125 }