view ServerMonitor/Controls/SshCheckControl.cs @ 1:9e92780ebc0f

Additional validation for SSH check
author Brad Greco <brad@bgreco.net>
date Tue, 01 Jan 2019 21:14:47 -0500
parents 3e1a2131f897
children 7127d5b5ac75
line wrap: on
line source

´╗┐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
{
    [CheckType(typeof(SshCheck))]
    public partial class SshCheckControl : CheckControl
    {
        public SshCheckControl()
        {
            InitializeComponent();
        }

        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;
        }

        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;
        }

        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;
        }
    }
}