# HG changeset patch # User Brad Greco # Date 1554773394 14400 # Node ID 7127d5b5ac75732faf2b517de331cb4600e08b8d # Parent 052aa62cb42a2a4b79e33a6bc72a979e8643d455 Code cleanup and comments diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/CheckControl.cs --- a/ServerMonitor/Controls/CheckControl.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/CheckControl.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,18 +1,16 @@ using System; using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; using System.Data; using System.Linq; -using System.Text; using System.Windows.Forms; namespace ServerMonitorApp { - /// Base class for check controls + /// Base class for check controls. /// This control should never be used directly, but marking it abstract causes problems with the designer. public partial class CheckControl : UserControl { + /// Type of the check this control can edit. public Type CheckType => GetCheckType(GetType()); public CheckControl() @@ -24,6 +22,8 @@ { CheckGroupBox.Text = Helpers.GetDisplayName(CheckType); + // Bind change listeners to checkboxes to enable or disable a panel of controls. + // The leftmost checkbox is assumed to be the "master switch" for the panel. IEnumerable panels = CheckGroupBox.Controls.OfType(); foreach (Panel panel in panels) { @@ -41,6 +41,8 @@ DisablePanelByCheckBox((CheckBox)sender); } + /// Enables or disables all sibling controls of a checkbox. + /// The checkbox to disable or enable the sibling controls of. private void DisablePanelByCheckBox(CheckBox checkBox) { foreach (Control control in checkBox.Parent.Controls) @@ -50,16 +52,24 @@ } } + /// Sets the values of the controls from a check's properties. + /// Subclasses should implement this method. Marking as abstract causes problems with the designer. public virtual void LoadCheck(Check check) { } + /// Updates the properties of a check from user input. + /// Subclasses should implement this method. Marking as abstract causes problems with the designer. public virtual void UpdateCheck(Check check) { } + /// Creates an instance of a check control based on a check type. public static CheckControl Create(Type CheckType) { + // Use reflection to find a check control that declares it handles the given type. Type checkControlType = typeof(CheckControl).Assembly.GetTypes().FirstOrDefault(t => GetCheckType(t) == CheckType); + // If one was found, instantiate it. return checkControlType == null ? null : (CheckControl)Activator.CreateInstance(checkControlType); } + /// Gets the value of the CheckTypeAttribute for a type. private static Type GetCheckType(Type type) { return type.GetAttribute()?.CheckType; diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/DiskSpaceCheckControl.cs --- a/ServerMonitor/Controls/DiskSpaceCheckControl.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/DiskSpaceCheckControl.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,14 +1,8 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; -using System.Windows.Forms; namespace ServerMonitorApp { + /// Control for editing a disk space check. [CheckType(typeof(DiskSpaceCheck))] public partial class DiskSpaceCheckControl : CheckControl { @@ -19,9 +13,11 @@ private void DiskSpaceCheckControl_Load(object sender, EventArgs e) { + // Initialize the free space units to a non-empty value. FreeSpaceUnitsComboBox.SelectedIndex = 1; } + /// Sets the values of the controls from a check's properties. public override void LoadCheck(Check check1) { DiskSpaceCheck check = (DiskSpaceCheck)check1; @@ -30,6 +26,7 @@ FreeSpaceUnitsComboBox.SelectedIndex = (int)check.FreeSpaceUnits; } + /// Updates the properties of a check from user input. public override void UpdateCheck(Check check1) { DiskSpaceCheck check = (DiskSpaceCheck)check1; @@ -46,4 +43,4 @@ } } } -} \ No newline at end of file +} diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/FileCheckControl.cs --- a/ServerMonitor/Controls/FileCheckControl.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/FileCheckControl.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,14 +1,8 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; -using System.Windows.Forms; namespace ServerMonitorApp { + /// Control for editing a file check. [CheckType(typeof(FileCheck))] public partial class FileCheckControl : CheckControl { @@ -19,10 +13,12 @@ private void DiskSpaceCheckControl_Load(object sender, EventArgs e) { + // Initialize the combo boxes to non-empty values. FileSizeComparisonComboBox.SelectedIndex = 0; DateModifiedComparisonComboBox.SelectedIndex = 0; } + /// Sets the values of the controls from a check's properties. public override void LoadCheck(Check check1) { FileCheck check = (FileCheck)check1; @@ -36,12 +32,14 @@ DateModifiedTextBox.Text = check.DateModified.ToString(); DateModifiedUnitsComboBox.SelectedIndex = (int)check.DateModifiedUnits; - if (FileSizeTextBox.Text == "0") + // Blank out text boxes that are disabled and have a blank-looking value. + if (FileSizeTextBox.Text == "0" && !check.CheckFileSize) FileSizeTextBox.Clear(); - if (DateModifiedTextBox.Text == "0") + if (DateModifiedTextBox.Text == "0" && !check.CheckDateModified) DateModifiedTextBox.Clear(); } + /// Updates the properties of a check from user input. public override void UpdateCheck(Check check1) { FileCheck check = (FileCheck)check1; diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/HttpCheckControl.cs --- a/ServerMonitor/Controls/HttpCheckControl.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/HttpCheckControl.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,14 +1,6 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; -using System.Windows.Forms; - -namespace ServerMonitorApp +namespace ServerMonitorApp { + /// Control for editing an HTTP check. [CheckType(typeof(HttpCheck))] public partial class HttpCheckControl : CheckControl { @@ -17,6 +9,7 @@ InitializeComponent(); } + /// Sets the values of the controls from a check's properties. public override void LoadCheck(Check check1) { HttpCheck check = (HttpCheck)check1; @@ -32,6 +25,7 @@ ResponseBodyRegexCheckBox.Checked = check.ResponseBodyUseRegex; } + /// Updates the properties of a check from user input. public override void UpdateCheck(Check check1) { HttpCheck check = (HttpCheck)check1; diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/MatchComboBox.cs --- a/ServerMonitor/Controls/MatchComboBox.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/MatchComboBox.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,16 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; +using System.Windows.Forms; namespace ServerMonitorApp { + /// Combo box containing options for matching text against a response string. class MatchComboBox : ComboBox { protected override void OnCreateControl() { base.OnCreateControl(); + // Clear the items to prevent duplicates added at design time and run time. Items.Clear(); Items.Add("equals"); Items.Add("does not equal"); @@ -22,5 +20,6 @@ } } + /// Types of matches that can be run against a response string. public enum MatchType { Equals = 0, NotEquals = 1, Contains = 2, NotContains = 3, GreaterThan = 4, LessThan = 5 } } diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/ServerSummaryControl.cs --- a/ServerMonitor/Controls/ServerSummaryControl.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/ServerSummaryControl.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,10 +1,4 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; using System.Windows.Forms; namespace ServerMonitorApp diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/SizeUnitsComboBox.cs --- a/ServerMonitor/Controls/SizeUnitsComboBox.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/SizeUnitsComboBox.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,16 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; +using System.Windows.Forms; namespace ServerMonitorApp { + /// Combo box containing options for size units. class SizeUnitsComboBox : ComboBox { protected override void OnCreateControl() { base.OnCreateControl(); + // Clear the items to prevent duplicates added at design time and run time. Items.Clear(); Items.Add("B"); Items.Add("KB"); @@ -20,5 +18,6 @@ } } + /// Size units. public enum SizeUnits { B = 0, KB = 1, MB = 2, GB = 3 } } diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/SshCheckControl.cs --- a/ServerMonitor/Controls/SshCheckControl.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/SshCheckControl.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,14 +1,8 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; -using System.Windows.Forms; namespace ServerMonitorApp { + /// Control for editing an SSH space check. [CheckType(typeof(SshCheck))] public partial class SshCheckControl : CheckControl { @@ -17,6 +11,7 @@ InitializeComponent(); } + /// Sets the values of the controls from a check's properties. public override void LoadCheck(Check check1) { SshCheck check = (SshCheck)check1; @@ -29,6 +24,7 @@ CommandOutputRegexCheckBox.Checked = check.CommandOutputUseRegex; } + /// Updates the properties of a check from user input. public override void UpdateCheck(Check check1) { SshCheck check = (SshCheck)check1; @@ -55,6 +51,7 @@ check.CommandOutputUseRegex = CommandOutputRegexCheckBox.Checked; } + /// Disables the regex checkbox if a numeric comparison is selected. private void CommandOutputComboBox_SelectedIndexChanged(object sender, EventArgs e) { bool numeric = ((MatchType)CommandOutputComboBox.SelectedIndex).In(MatchType.GreaterThan, MatchType.LessThan); diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Controls/TimeUnitsComboBox.cs --- a/ServerMonitor/Controls/TimeUnitsComboBox.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Controls/TimeUnitsComboBox.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,16 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; +using System.Windows.Forms; namespace ServerMonitorApp { + /// Combo box containing options for time units. class TimeUnitsComboBox : ComboBox { protected override void OnCreateControl() { base.OnCreateControl(); + // Clear the items to prevent duplicates added at design time and run time. Items.Clear(); Items.Add("minutes"); Items.Add("hours"); @@ -19,5 +17,6 @@ } } + /// Time units. public enum TimeUnits { Minute = 0, Hour = 1, Day = 2 } } diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Forms/CheckBoxDialog.cs --- a/ServerMonitor/Forms/CheckBoxDialog.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Forms/CheckBoxDialog.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,18 +1,16 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; using System.Drawing; -using System.Linq; -using System.Text; using System.Windows.Forms; namespace ServerMonitorApp { + /// Message dialog with an additional checkbox. public partial class CheckBoxDialog : Form { + /// Message to show. public string Message { get; set; } + /// Check state of the checkbox. public bool Checked { get; private set; } public CheckBoxDialog() @@ -26,6 +24,7 @@ MessageLabel.Text = Message; } + /// Updates the public property with the checked state so it can be read by the dialog owner. private void PromptCheckBox_CheckedChanged(object sender, EventArgs e) { Checked = PromptCheckBox.Checked; diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Forms/CheckForm.cs --- a/ServerMonitor/Forms/CheckForm.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Forms/CheckForm.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,19 +1,17 @@ -using ServerMonitorApp.Properties; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; -using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; +using ServerMonitorApp.Properties; namespace ServerMonitorApp { public partial class CheckForm : Form { - //private static readonly Dictionary checkInfo = new Dictionary(); private readonly List checkControls = new List(); private bool helpShown; private CancellationTokenSource cancellationTokenSource; @@ -86,16 +84,6 @@ e.Value = Helpers.GetDisplayName((Type)e.ListItem); } - /*private void showGroupBox(GroupBox groupBox) - { - - }*/ - - /*private Type GetCheckType() - { - return (Type)CheckTypeComboBox.SelectedItem; - }*/ - private void ShowCheckControl() { IEnumerable checkControls = CheckSettingsPanel.Controls.OfType(); @@ -115,8 +103,6 @@ } if (checkControl != null) { - //CheckSettingsPanel.Height = checkControl.Height; - //Height = 230 + checkControl.Height; checkControl.Show(); } } @@ -210,30 +196,6 @@ localCancellationTokenSource = null; } - //private void RunButton_Click(object sender, EventArgs e) - //{ - // if (!Check.Server.SshClient.IsConnected) - // Check.Server.SshClient.Connect(); - // using (Renci.SshNet.SshCommand command = Check.Server.SshClient.CreateCommand("ls")) - // { - // //token.Register(command.CancelAsync); - // //command.BeginExecute(asyncResult => - // //{ - // // string result = command.EndExecute(asyncResult); - // // //tcs.SetResult(new CheckResult(this, CheckStatus.Success, result)); - // //}); - // IAsyncResult iar = command.BeginExecute(z); - // command.EndExecute(iar); - // //string result = command.Execute(); - // //System.Console.Write(result); - // } - //} - - //private void z(IAsyncResult ar) - //{ - // throw new NotImplementedException(); - //} - private void CancelRunButton_Click(object sender, EventArgs e) { cancellationTokenSource.Cancel(); @@ -250,25 +212,11 @@ if (result != null) { ResultLabel.Text = result.Message; - //ResultLabel.ForeColor = result.CheckStatus == CheckStatus.Success ? Color.Green : Color.Red; ResultIconPictureBox.Image = result.CheckStatus.GetImage(); ResultLabel.Visible = ResultIconPictureBox.Visible = true; } } - /*private class CheckInfo - { - public GroupBox GroupBox { get; private set; } - - public Func ValidateFunction { get; private set; } - - public CheckInfo(GroupBox groupBox, Func validateFunction) - { - GroupBox = groupBox; - ValidateFunction = validateFunction; - } - }*/ - private string BuildHelpText() { StringBuilder rtf = new StringBuilder(@"{\rtf1\ansi "); diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Forms/InputDialog.cs --- a/ServerMonitor/Forms/InputDialog.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Forms/InputDialog.cs Mon Apr 08 21:29:54 2019 -0400 @@ -1,21 +1,19 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; namespace ServerMonitorApp { + /// Message dialog with an input box. public partial class InputDialog : Form { + /// Message to show. public string Message { get; set; } + /// Icon to show. public Icon MessageIcon { get; set; } + /// The text entered by the user. public string Input { get; private set; } = ""; public InputDialog() @@ -29,6 +27,17 @@ MessageLabel.Text = Message; } + /// Updates the public property with the checked state so it can be read by the dialog owner. + private void InputTextBox_TextChanged(object sender, EventArgs e) + { + Input = InputTextBox.Text; + } + + /// Creates and shows an input box dialog. + /// The message to display. + /// The icon to display. + /// The dialog owner. + /// The string entered by the user, or null if the dialog was cancelled. public static string ShowDialog(string message, Icon icon = null, IWin32Window owner = null) { using (InputDialog dialog = new InputDialog() { Message = message, MessageIcon = icon }) @@ -36,10 +45,5 @@ return dialog.ShowDialog(owner) == DialogResult.OK ? dialog.Input : null; } } - - private void InputTextBox_TextChanged(object sender, EventArgs e) - { - Input = InputTextBox.Text; - } } } diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Objects/Checks/FileCheck.cs --- a/ServerMonitor/Objects/Checks/FileCheck.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Objects/Checks/FileCheck.cs Mon Apr 08 21:29:54 2019 -0400 @@ -75,6 +75,7 @@ } if (CheckDateModified) { + // TODO use the server time instead string dateString = tokens[5] + " " + tokens[6]; if (DateTime.TryParse(dateString, out DateTime modified)) { @@ -104,8 +105,8 @@ message += "File is required." + Environment.NewLine; if (!CheckFileSize && !CheckDateModified) message += "At least one check must be enabled." + Environment.NewLine; - if (CheckFileSize && FileSize <= 0) - message += "File size must be greater than 0." + Environment.NewLine; + if (CheckFileSize && FileSize < 0) + message += "File size must be at least 0." + Environment.NewLine; if (CheckDateModified && DateModified <= 0) message += "Date modified must be greater than 0." + Environment.NewLine; return message; diff -r 052aa62cb42a -r 7127d5b5ac75 ServerMonitor/Objects/Logger.cs --- a/ServerMonitor/Objects/Logger.cs Sat Mar 09 20:14:03 2019 -0500 +++ b/ServerMonitor/Objects/Logger.cs Mon Apr 08 21:29:54 2019 -0400 @@ -65,28 +65,35 @@ { DateTime maxAge = DateTime.Now.AddDays(-1 * Settings.Default.KeepLogDays); string tempFile = logFile + ".tmp"; - using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read)) - using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) - using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None)) - using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8)) + try { - while (true) + using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) + using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None)) + using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8)) { - try + while (true) { - string line = reader.ReadLine(); - if (line == null) - break; - if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge) + try { - await writer.WriteLineAsync(line); + string line = reader.ReadLine(); + if (line == null) + break; + if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge) + { + await writer.WriteLineAsync(line); + } } + catch (Exception) { } } - catch (Exception) { } } + File.Delete(logFile); + File.Move(tempFile, logFile); } - File.Delete(logFile); - File.Move(tempFile, logFile); + catch (FileNotFoundException) + { + // Do nothing if the log file does not exist yet. + } } } }