Mercurial > servermonitor
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:453ecc1ed9ea | 3:96f0b028176d |
---|---|
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; | |
8 | |
9 namespace ServerMonitorApp | |
10 { | |
11 [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)] | |
12 public class FileCheck : SshCheck | |
13 { | |
14 public override string Command { get => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME")); } | |
15 | |
16 public string File { get; set; } | |
17 | |
18 public bool CheckFileSize { get; set; } | |
19 | |
20 public bool FileSizeLessThan { get; set; } | |
21 | |
22 public double FileSize { get; set; } | |
23 | |
24 public double FileSizeInSelectedUnits | |
25 { | |
26 get { return FileSize / Math.Pow(1024, (double)FileSizeUnits); } | |
27 set { FileSize = value * Math.Pow(1024, (double)FileSizeUnits); } | |
28 } | |
29 | |
30 public SizeUnits FileSizeUnits { get; set; } | |
31 | |
32 public bool CheckDateModified { get; set; } | |
33 | |
34 public bool DateModifiedOlderThan { get; set; } | |
35 | |
36 public double DateModified { get; set; } | |
37 | |
38 public TimeUnits DateModifiedUnits { get; set; } | |
39 | |
40 public FileCheck() | |
41 { | |
42 Command = "export TIME_STYLE=long-iso; ls -l \"{0}\""; | |
43 //Command = "export TIME_STYLE=long-iso; ls -l \"{0}\" | tr -s ' ' | cut -d ' ' -f 5,6,7"; | |
44 CheckExitCode = true; | |
45 ExitCode = 0; | |
46 } | |
47 | |
48 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) | |
49 { | |
50 List<CheckResult> results = base.ProcessCommandResult(output, exitCode); | |
51 if (results.Any(r => r.CheckStatus != CheckStatus.Success)) | |
52 return results; | |
53 | |
54 if (output.Split('\n').Length > 1) | |
55 { | |
56 results.Add(Fail("ls output was more than one line: " + output)); | |
57 } | |
58 else | |
59 { | |
60 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); | |
61 if (CheckFileSize) | |
62 { | |
63 if (int.TryParse(tokens[4], out int bytes)) | |
64 { | |
65 string message = string.Format("File size is {0} {1}", bytes / Math.Pow(1024, (double)FileSizeUnits), FileSizeUnits); | |
66 if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan)) | |
67 results.Add(Pass(message)); | |
68 else | |
69 results.Add(Fail(message)); | |
70 } | |
71 else | |
72 { | |
73 results.Add(Fail("Unable to parse ls output as integer: " + tokens[4])); | |
74 } | |
75 } | |
76 if (CheckDateModified) | |
77 { | |
78 string dateString = tokens[5] + " " + tokens[6]; | |
79 if (DateTime.TryParse(dateString, out DateTime modified)) | |
80 { | |
81 string message = string.Format("Last modified date is {0}", modified); | |
82 TimeSpan age = DateTime.Now.Subtract(modified); | |
83 double ageCompare = DateModifiedUnits == TimeUnits.Minute ? age.TotalMinutes : | |
84 DateModifiedUnits == TimeUnits.Hour ? age.TotalHours : | |
85 age.TotalDays; | |
86 if ((ageCompare > DateModified && DateModifiedOlderThan) || (ageCompare < DateModified && !DateModifiedOlderThan)) | |
87 results.Add(Pass(message)); | |
88 else | |
89 results.Add(Fail(message)); | |
90 } | |
91 else | |
92 { | |
93 results.Add(Fail("Unable to parse ls output as date: " + dateString)); | |
94 } | |
95 } | |
96 } | |
97 return results; | |
98 } | |
99 | |
100 public override string Validate(bool saving = true) | |
101 { | |
102 string message = base.Validate(); | |
103 if (File.IsNullOrEmpty()) | |
104 message += "File is required." + Environment.NewLine; | |
105 if (!CheckFileSize && !CheckDateModified) | |
106 message += "At least one check must be enabled." + Environment.NewLine; | |
107 if (CheckFileSize && FileSize <= 0) | |
108 message += "File size must be greater than 0." + Environment.NewLine; | |
109 if (CheckDateModified && DateModified <= 0) | |
110 message += "Date modified must be greater than 0." + Environment.NewLine; | |
111 return message; | |
112 } | |
113 } | |
114 } |