Mercurial > servermonitor
view ServerMonitor/Objects/Checks/DiskSpaceCheck.cs @ 4:3142e52cbe69
Lots more progress
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sun, 10 Feb 2019 20:51:26 -0500 |
parents | 96f0b028176d |
children | a36cc5c123f4 |
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(base.Command, Device); 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"; Command = "df -P -k {0}"; 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 } }