Mercurial > servermonitor
view ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 15:23f2e0da1094
- Fix the last execution status being lost after a check is edited.
- Add comments.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Mon, 22 Apr 2019 21:11:27 -0400 |
parents | a36cc5c123f4 |
children | 68d7834dc28e |
line wrap: on
line source
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 override string Command => string.Format(DiskSpaceCommand, Device); protected string DiskSpaceCommand => "df -P -k {0}"; public string Device { get; set; } public double MinFreeSpace { get; set; } public FreeSpaceUnits FreeSpaceUnits { get; set; } public DiskSpaceCheck() { 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.Failed)) 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: " + string.Join("\n", lines))); } else { string[] tokens = lines[0].Split(new char[0], StringSplitOptions.RemoveEmptyEntries); if (FreeSpaceUnits == FreeSpaceUnits.percent) { if (int.TryParse(tokens[4].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[4].Replace("%", ""))); } } else { if (int.TryParse(tokens[3], 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[3])); } } } 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 } }