view ServerMonitor/Controls/SshCheckControl.cs @ 19:b3128fe10d57

Add About form and license.
author Brad Greco <brad@bgreco.net>
date Wed, 29 May 2019 20:18:37 -0400
parents 7127d5b5ac75
children
line wrap: on
line source

using System;

namespace ServerMonitorApp
{
    /// <summary>Control for editing an SSH space check.</summary>
    [CheckType(typeof(SshCheck))]
    public partial class SshCheckControl : CheckControl
    {
        public SshCheckControl()
        {
            InitializeComponent();
        }

        /// <summary>Sets the values of the controls from a check's properties.</summary>
        public override void LoadCheck(Check check1)
        {
            SshCheck check = (SshCheck)check1;
            CommandTextBox.Text = check.Command;
            ExitCodeCheckBox.Checked = check.CheckExitCode;
            ExitCodeTextBox.Text = check.ExitCode.ToString();
            CommandOutputCheckBox.Checked = check.CheckCommandOutput;
            CommandOutputComboBox.SelectedIndex = (int)check.CommandOutputMatchType;
            CommandOutputTextBox.Text = check.CommandOutputPattern;
            CommandOutputRegexCheckBox.Checked = check.CommandOutputUseRegex;
        }

        /// <summary>Updates the properties of a check from user input.</summary>
        public override void UpdateCheck(Check check1)
        {
            SshCheck check = (SshCheck)check1;
            check.Command = CommandTextBox.Text.Trim();
            check.CheckExitCode = ExitCodeCheckBox.Checked;
            try
            {
                check.ExitCode = int.Parse(ExitCodeTextBox.Text);
            }
            catch
            {
                if (check.CheckExitCode)
                    throw new UpdateCheckException("Exit code must be an integer.");
                else
                    check.ExitCode = 0;
            }
            check.CheckCommandOutput = CommandOutputCheckBox.Checked;
            check.CommandOutputMatchType = (MatchType)CommandOutputComboBox.SelectedIndex;
            if (check.CheckCommandOutput && check.CommandOutputMatchType.In(MatchType.GreaterThan, MatchType.LessThan) && !decimal.TryParse(CommandOutputTextBox.Text, out decimal _))
            {
                throw new UpdateCheckException("Command output must be numeric if checking for greater than/less than.");
            }
            check.CommandOutputPattern = CommandOutputTextBox.Text;
            check.CommandOutputUseRegex = CommandOutputRegexCheckBox.Checked;
        }

        /// <summary>Disables the regex checkbox if a numeric comparison is selected.</summary>
        private void CommandOutputComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool numeric = ((MatchType)CommandOutputComboBox.SelectedIndex).In(MatchType.GreaterThan, MatchType.LessThan);
            CommandOutputRegexCheckBox.Enabled = !numeric;
            if (numeric)
                CommandOutputRegexCheckBox.Checked = false;
        }
    }
}