view ServerMonitor/Controls/SshCheckControl.cs @ 4:3142e52cbe69

Lots more progress
author Brad Greco <brad@bgreco.net>
date Sun, 10 Feb 2019 20:51:26 -0500
parents 9e92780ebc0f
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;
        }
    }
}