diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServerMonitor/Objects/Checks/FileCheck.cs	Fri Jan 11 22:34:18 2019 -0500
@@ -0,0 +1,114 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace ServerMonitorApp
+{
+    [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)]
+    public class FileCheck : SshCheck
+    {
+        public override string Command { get => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME")); }
+
+        public string File { get; set; }
+
+        public bool CheckFileSize { get; set; }
+
+        public bool FileSizeLessThan { get; set; }
+
+        public double FileSize { get; set; }
+
+        public double FileSizeInSelectedUnits
+        {
+            get { return FileSize / Math.Pow(1024, (double)FileSizeUnits); }
+            set { FileSize = value * Math.Pow(1024, (double)FileSizeUnits); }
+        }
+
+        public SizeUnits FileSizeUnits { get; set; }
+
+        public bool CheckDateModified { get; set; }
+
+        public bool DateModifiedOlderThan { get; set; }
+
+        public double DateModified { get; set; }
+
+        public TimeUnits DateModifiedUnits { get; set; }
+
+        public FileCheck()
+        {
+            Command = "export TIME_STYLE=long-iso; ls -l \"{0}\"";
+            //Command = "export TIME_STYLE=long-iso; ls -l \"{0}\" | tr -s ' ' | cut -d ' ' -f 5,6,7";
+            CheckExitCode = true;
+            ExitCode = 0;
+        }
+
+        protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
+        {
+            List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
+            if (results.Any(r => r.CheckStatus != CheckStatus.Success))
+                return results;
+
+            if (output.Split('\n').Length > 1)
+            {
+                results.Add(Fail("ls output was more than one line: " + output));
+            }
+            else
+            {
+                string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
+                if (CheckFileSize)
+                {
+                    if (int.TryParse(tokens[4], out int bytes))
+                    {
+                        string message = string.Format("File size is {0} {1}", bytes / Math.Pow(1024, (double)FileSizeUnits), FileSizeUnits);
+                        if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan))
+                            results.Add(Pass(message));
+                        else
+                            results.Add(Fail(message));
+                    }
+                    else
+                    {
+                        results.Add(Fail("Unable to parse ls output as integer: " + tokens[4]));
+                    }
+                }
+                if (CheckDateModified)
+                {
+                    string dateString = tokens[5] + " " + tokens[6];
+                    if (DateTime.TryParse(dateString, out DateTime modified))
+                    {
+                        string message = string.Format("Last modified date is {0}", modified);
+                        TimeSpan age = DateTime.Now.Subtract(modified);
+                        double ageCompare = DateModifiedUnits == TimeUnits.Minute ? age.TotalMinutes :
+                                         DateModifiedUnits == TimeUnits.Hour ? age.TotalHours :
+                                         age.TotalDays;
+                        if ((ageCompare > DateModified && DateModifiedOlderThan) || (ageCompare < DateModified && !DateModifiedOlderThan))
+                            results.Add(Pass(message));
+                        else
+                            results.Add(Fail(message));
+                    }
+                    else
+                    {
+                        results.Add(Fail("Unable to parse ls output as date: " + dateString));
+                    }
+                }
+            }
+            return results;
+        }
+
+        public override string Validate(bool saving = true)
+        {
+            string message = base.Validate();
+            if (File.IsNullOrEmpty())
+                message += "File is required." + Environment.NewLine;
+            if (!CheckFileSize && !CheckDateModified)
+                message += "At least one check must be enabled." + Environment.NewLine;
+            if (CheckFileSize && FileSize <= 0)
+                message += "File size must be greater than 0." + Environment.NewLine;
+            if (CheckDateModified && DateModified <= 0)
+                message += "Date modified must be greater than 0." + Environment.NewLine;
+            return message;
+        }
+    }
+}
\ No newline at end of file