annotate ServerMonitor/Objects/Checks/FileCheck.cs @ 3:96f0b028176d

File check
author Brad Greco <brad@bgreco.net>
date Fri, 11 Jan 2019 22:34:18 -0500
parents
children 3142e52cbe69
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 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 public override string Command { get => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME")); }
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
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 public double FileSize { get; set; }
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 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 get { return FileSize / Math.Pow(1024, (double)FileSizeUnits); }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 set { FileSize = value * Math.Pow(1024, (double)FileSizeUnits); }
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);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 if (results.Any(r => r.CheckStatus != CheckStatus.Success))
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 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 string message = string.Format("File size is {0} {1}", bytes / Math.Pow(1024, (double)FileSizeUnits), FileSizeUnits);
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 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 string dateString = tokens[5] + " " + tokens[6];
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 if (DateTime.TryParse(dateString, out DateTime modified))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 string message = string.Format("Last modified date is {0}", modified);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 TimeSpan age = DateTime.Now.Subtract(modified);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 double ageCompare = DateModifiedUnits == TimeUnits.Minute ? age.TotalMinutes :
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84 DateModifiedUnits == TimeUnits.Hour ? age.TotalHours :
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 age.TotalDays;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 if ((ageCompare > DateModified && DateModifiedOlderThan) || (ageCompare < DateModified && !DateModifiedOlderThan))
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 results.Add(Pass(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 results.Add(Fail(message));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
91 else
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
92 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 results.Add(Fail("Unable to parse ls output as date: " + dateString));
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 }
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 return results;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
100 public override string Validate(bool saving = true)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
102 string message = base.Validate();
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
103 if (File.IsNullOrEmpty())
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 message += "File is required." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 if (!CheckFileSize && !CheckDateModified)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106 message += "At least one check must be enabled." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107 if (CheckFileSize && FileSize <= 0)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
108 message += "File size must be greater than 0." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
109 if (CheckDateModified && DateModified <= 0)
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 message += "Date modified must be greater than 0." + Environment.NewLine;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 return message;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 }
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents:
diff changeset
114 }