Mercurial > servermonitor
comparison ServerMonitor/Objects/Checks/FileCheck.cs @ 4:3142e52cbe69
Lots more progress
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sun, 10 Feb 2019 20:51:26 -0500 |
parents | 96f0b028176d |
children | 7127d5b5ac75 |
comparison
equal
deleted
inserted
replaced
3:96f0b028176d | 4:3142e52cbe69 |
---|---|
9 namespace ServerMonitorApp | 9 namespace ServerMonitorApp |
10 { | 10 { |
11 [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)] | 11 [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)] |
12 public class FileCheck : SshCheck | 12 public class FileCheck : SshCheck |
13 { | 13 { |
14 public override string Command { get => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME")); } | 14 public override string Command => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME")); |
15 | 15 |
16 public string File { get; set; } | 16 public string File { get; set; } |
17 | 17 |
18 public bool CheckFileSize { get; set; } | 18 public bool CheckFileSize { get; set; } |
19 | 19 |
20 public bool FileSizeLessThan { get; set; } | 20 public bool FileSizeLessThan { get; set; } |
21 | 21 |
22 public double FileSize { get; set; } | 22 public int FileSize { get; set; } |
23 | 23 |
24 public double FileSizeInSelectedUnits | 24 public double FileSizeInSelectedUnits |
25 { | 25 { |
26 get { return FileSize / Math.Pow(1024, (double)FileSizeUnits); } | 26 get => Math.Round(ConvertBytesToSelectedUnits(FileSize), 1); |
27 set { FileSize = value * Math.Pow(1024, (double)FileSizeUnits); } | 27 set => FileSize = ConvertSelectedUnitsToBytes(value); |
28 } | 28 } |
29 | 29 |
30 public SizeUnits FileSizeUnits { get; set; } | 30 public SizeUnits FileSizeUnits { get; set; } |
31 | 31 |
32 public bool CheckDateModified { get; set; } | 32 public bool CheckDateModified { get; set; } |
46 } | 46 } |
47 | 47 |
48 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) | 48 protected override List<CheckResult> ProcessCommandResult(string output, int exitCode) |
49 { | 49 { |
50 List<CheckResult> results = base.ProcessCommandResult(output, exitCode); | 50 List<CheckResult> results = base.ProcessCommandResult(output, exitCode); |
51 if (results.Any(r => r.CheckStatus != CheckStatus.Success)) | 51 if (results.Any(r => r.Failed)) |
52 return results; | 52 return results; |
53 | 53 |
54 if (output.Split('\n').Length > 1) | 54 if (output.Split('\n').Length > 1) |
55 { | 55 { |
56 results.Add(Fail("ls output was more than one line: " + output)); | 56 results.Add(Fail("ls output was more than one line: " + output)); |
60 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); | 60 string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); |
61 if (CheckFileSize) | 61 if (CheckFileSize) |
62 { | 62 { |
63 if (int.TryParse(tokens[4], out int bytes)) | 63 if (int.TryParse(tokens[4], out int bytes)) |
64 { | 64 { |
65 string message = string.Format("File size is {0} {1}", bytes / Math.Pow(1024, (double)FileSizeUnits), FileSizeUnits); | 65 string message = string.Format("File size is {0} {1}", Math.Round(ConvertBytesToSelectedUnits(bytes), 1), FileSizeUnits); |
66 if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan)) | 66 if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan)) |
67 results.Add(Pass(message)); | 67 results.Add(Pass(message)); |
68 else | 68 else |
69 results.Add(Fail(message)); | 69 results.Add(Fail(message)); |
70 } | 70 } |
108 message += "File size must be greater than 0." + Environment.NewLine; | 108 message += "File size must be greater than 0." + Environment.NewLine; |
109 if (CheckDateModified && DateModified <= 0) | 109 if (CheckDateModified && DateModified <= 0) |
110 message += "Date modified must be greater than 0." + Environment.NewLine; | 110 message += "Date modified must be greater than 0." + Environment.NewLine; |
111 return message; | 111 return message; |
112 } | 112 } |
113 | |
114 private double ConvertBytesToSelectedUnits(int sizeInBytes) | |
115 { | |
116 return sizeInBytes / Math.Pow(1024, (double)FileSizeUnits); | |
117 } | |
118 | |
119 private int ConvertSelectedUnitsToBytes(double sizeInSelectedUnits) | |
120 { | |
121 return (int)(sizeInSelectedUnits * Math.Pow(1024, (int)FileSizeUnits)); | |
122 } | |
113 } | 123 } |
114 } | 124 } |