diff ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 2:453ecc1ed9ea

Disk space check
author Brad Greco <brad@bgreco.net>
date Sun, 06 Jan 2019 20:49:08 -0500
parents
children 96f0b028176d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServerMonitor/Objects/Checks/DiskSpaceCheck.cs	Sun Jan 06 20:49:08 2019 -0500
@@ -0,0 +1,94 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ServerMonitorApp
+{
+    [DisplayName("Disk space check"), Description("Check the remaining free disk space"), DisplayWeight(11)]
+    public class DiskSpaceCheck : SshCheck
+    {
+        public string Device { get; set; }
+
+        public double MinFreeSpace { get; set; }
+
+        public FreeSpaceUnits FreeSpaceUnits { get; set; }
+
+        public DiskSpaceCheck()
+        {
+            Command = "df -P -k {0} | awk 'NR>1' | tr -s ' ' | cut -d ' ' -f 4,5";
+            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)
+            {
+                results.Add(Fail("df output was more than one line: " + output));
+            }
+            else
+            {
+
+                string[] tokens = output.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
+                if (FreeSpaceUnits == FreeSpaceUnits.percent)
+                {
+                    if (int.TryParse(tokens[1].Replace("%", ""), out int percent))
+                    {
+                        percent = 100 - percent;
+                        string message = string.Format("Free disk space is {0}%", percent);
+                        if (percent < MinFreeSpace)
+                            results.Add(Fail(message));
+                        else
+                            results.Add(Pass(message));
+                    }
+                    else
+                    {
+                        results.Add(Fail("Unable to parse df output as integer: " + tokens[1].Replace("%", "")));
+                    }
+                }
+                else
+                {
+                    if (int.TryParse(tokens[0], out int freeSpace))
+                    {
+                        freeSpace /= 1024;
+                        if (FreeSpaceUnits == FreeSpaceUnits.GB)
+                            freeSpace /= 1024;
+                        string message = string.Format("Free disk space is {0} {1}", freeSpace, FreeSpaceUnits);
+                        if (freeSpace < MinFreeSpace)
+                            results.Add(Fail(message));
+                        else
+                            results.Add(Pass(message));
+                    }
+                    else
+                    {
+                        results.Add(Fail("Unable to parse df output as integer: " + tokens[0]));
+                    }
+                }
+            }
+            return results;
+        }
+
+        public override string Validate(bool saving = true)
+        {
+            string message = base.Validate();
+            if (Device.IsNullOrEmpty())
+                message += "Device is required." + Environment.NewLine;
+            if (MinFreeSpace <= 0)
+                message += "Free space must be greater than 0." + Environment.NewLine;
+            else if (FreeSpaceUnits == FreeSpaceUnits.percent && MinFreeSpace > 100)
+                message += "Free space percent must be between 0 and 100." + Environment.NewLine;
+            return message;
+        }
+    }
+
+    public enum FreeSpaceUnits { MB = 0, GB = 1, percent = 2 }
+}