diff ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 3:96f0b028176d

File check
author Brad Greco <brad@bgreco.net>
date Fri, 11 Jan 2019 22:34:18 -0500
parents 453ecc1ed9ea
children 3142e52cbe69
line wrap: on
line diff
--- a/ServerMonitor/Objects/Checks/DiskSpaceCheck.cs	Sun Jan 06 20:49:08 2019 -0500
+++ b/ServerMonitor/Objects/Checks/DiskSpaceCheck.cs	Fri Jan 11 22:34:18 2019 -0500
@@ -10,6 +10,8 @@
     [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)]
     public class DiskSpaceCheck : SshCheck
     {
+        public override string Command { get => string.Format(base.Command, Device); }
+
         public string Device { get; set; }
 
         public double MinFreeSpace { get; set; }
@@ -18,30 +20,30 @@
 
         public DiskSpaceCheck()
         {
-            Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
+            //Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
+            Command = "df -P -k {0}";
             CheckExitCode = true;
             ExitCode = 0;
         }
 
-        protected override string GetCommand()
-        {
-            return string.Format(base.GetCommand(), Device);
-        }
-
         protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
         {
             List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
-            if (output.Split('\n').Length > 1)
+            if (results.Any(r => r.CheckStatus != CheckStatus.Success))
+                return results;
+
+            List<string> lines = output.Split('\n').ToList();
+            lines.RemoveAt(0);
+            if (lines.Count > 1)
             {
-                results.Add(Fail("df output was more than one line: " + output));
+                results.Add(Fail("df output was more than one line: " + string.Join("\n", lines)));
             }
             else
             {
-
-                string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
+                string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                 if (FreeSpaceUnits == FreeSpaceUnits.percent)
                 {
-                    if (int.TryParse(tokens[1].Replace("%", ""), out int percent))
+                    if (int.TryParse(tokens[4].Replace("%", ""), out int percent))
                     {
                         percent = 100 - percent;
                         string message = string.Format("Free disk space is {0}%", percent);
@@ -52,12 +54,12 @@
                     }
                     else
                     {
-                        results.Add(Fail("Unable to parse df output as integer: " + tokens[1].Replace("%", "")));
+                        results.Add(Fail("Unable to parse df output as integer: " + tokens[4].Replace("%", "")));
                     }
                 }
                 else
                 {
-                    if (int.TryParse(tokens[0], out int freeSpace))
+                    if (int.TryParse(tokens[3], out int freeSpace))
                     {
                         freeSpace /= 1024;
                         if (FreeSpaceUnits == FreeSpaceUnits.GB)
@@ -70,7 +72,7 @@
                     }
                     else
                     {
-                        results.Add(Fail("Unable to parse df output as integer: " + tokens[0]));
+                        results.Add(Fail("Unable to parse df output as integer: " + tokens[3]));
                     }
                 }
             }
@@ -91,4 +93,4 @@
     }
 
     public enum FreeSpaceUnits { MB = 0, GB = 1, percent = 2 }
-}
+}
\ No newline at end of file