changeset 13:a36cc5c123f4

Fix SSH command sublclasses holding on to old command strings after a program update.
author Brad Greco <brad@bgreco.net>
date Mon, 15 Apr 2019 19:25:27 -0400
parents d92176c5398a
children 2db36ab759de
files ServerMonitor/Objects/Checks/DiskSpaceCheck.cs ServerMonitor/Objects/Checks/FileCheck.cs ServerMonitor/Objects/Checks/SshCheck.cs
diffstat 3 files changed, 16 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ServerMonitor/Objects/Checks/DiskSpaceCheck.cs	Mon Apr 15 19:24:55 2019 -0400
+++ b/ServerMonitor/Objects/Checks/DiskSpaceCheck.cs	Mon Apr 15 19:25:27 2019 -0400
@@ -10,7 +10,9 @@
     [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)]
     public class DiskSpaceCheck : SshCheck
     {
-        public override string Command => string.Format(base.Command, Device);
+        public override string Command => string.Format(DiskSpaceCommand, Device);
+
+        protected string DiskSpaceCommand => "df -P -k {0}";
 
         public string Device { get; set; }
 
@@ -20,8 +22,6 @@
 
         public DiskSpaceCheck()
         {
-            //Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
-            Command = "df -P -k {0}";
             CheckExitCode = true;
             ExitCode = 0;
         }
--- a/ServerMonitor/Objects/Checks/FileCheck.cs	Mon Apr 15 19:24:55 2019 -0400
+++ b/ServerMonitor/Objects/Checks/FileCheck.cs	Mon Apr 15 19:25:27 2019 -0400
@@ -5,13 +5,23 @@
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
+using System.Xml.Serialization;
 
 namespace ServerMonitorApp
 {
     [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)]
     public class FileCheck : SshCheck
     {
-        public override string Command => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME"));
+        public override string Command => string.Format(FileCommand, Regex.Replace(File, "^~", "$HOME"));
+
+        protected string FileCommand
+        {
+            get
+            {
+                int timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now).Hours * -1; // Invert because POSIX says so.
+                return "export TIME_STYLE=long-iso; export TZ=UTC" + timeZoneOffset + "; ls -l \"{0}\"";
+            }
+        }
 
         public string File { get; set; }
 
@@ -39,8 +49,6 @@
 
         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;
         }
@@ -75,7 +83,6 @@
                 }
                 if (CheckDateModified)
                 {
-                    // TODO use the server time instead
                     string dateString = tokens[5] + " " + tokens[6];
                     if (DateTime.TryParse(dateString, out DateTime modified))
                     {
--- a/ServerMonitor/Objects/Checks/SshCheck.cs	Mon Apr 15 19:24:55 2019 -0400
+++ b/ServerMonitor/Objects/Checks/SshCheck.cs	Mon Apr 15 19:25:27 2019 -0400
@@ -66,6 +66,8 @@
         protected virtual List<CheckResult> ProcessCommandResult(string output, int exitCode)
         {
             List<CheckResult> results = new List<CheckResult>();
+            if (CheckCommandOutput)
+                results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output"));
             if (CheckExitCode)
             {
                 CheckResult result = GetIntResult(ExitCode, exitCode, "Exit code");
@@ -73,8 +75,6 @@
                     result.Message += ". Command output: " + output;
                 results.Add(result);
             }
-            if (CheckCommandOutput)
-                results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output"));
             return results;
         }