# HG changeset patch # User Brad Greco # Date 1546395287 18000 # Node ID 9e92780ebc0fe868a04a9a24408f3bf399a421c0 # Parent 3e1a2131f89766250564bf13af0bcf54cda82d4e Additional validation for SSH check diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Controls/HttpCheckControl.cs --- a/ServerMonitor/Controls/HttpCheckControl.cs Mon Dec 31 18:32:14 2018 -0500 +++ b/ServerMonitor/Controls/HttpCheckControl.cs Tue Jan 01 21:14:47 2019 -0500 @@ -37,7 +37,14 @@ HttpCheck check = (HttpCheck)check1; check.Url = UrlTextBox.Text.Trim(); check.CheckResponseCode = ResponseCodeCheckBox.Checked; - check.ResponseCode = int.Parse(ResponseCodeTextBox.Text); + try + { + check.ResponseCode = int.Parse(ResponseCodeTextBox.Text); + } + catch + { + throw new UpdateCheckException("Response code must be an integer."); + } check.CheckResponseLength = ResponseLengthCheckbox.Checked; check.ResponseLengthMin = ResponseLengthMinTextBox.Text; check.ResponseLengthMax = ResponseLengthMaxTextBox.Text; diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Controls/IncludesComboBox.cs --- a/ServerMonitor/Controls/IncludesComboBox.cs Mon Dec 31 18:32:14 2018 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; - -namespace ServerMonitorApp -{ - class MatchComboBox : ComboBox - { - protected override void OnCreateControl() - { - base.OnCreateControl(); - Items.Clear(); - Items.Add("equals"); - Items.Add("does not equal"); - Items.Add("contains"); - Items.Add("does not contain"); - } - } - - public enum MatchType { Equals = 0, NotEquals = 1, Contains = 2, NotContains = 3 } -} diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Controls/MatchComboBox.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ServerMonitor/Controls/MatchComboBox.cs Tue Jan 01 21:14:47 2019 -0500 @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Forms; + +namespace ServerMonitorApp +{ + class MatchComboBox : ComboBox + { + protected override void OnCreateControl() + { + base.OnCreateControl(); + Items.Clear(); + Items.Add("equals"); + Items.Add("does not equal"); + Items.Add("contains"); + Items.Add("does not contain"); + Items.Add("is greater than"); + Items.Add("is less than"); + SelectedIndex = 0; + } + } + + public enum MatchType { Equals = 0, NotEquals = 1, Contains = 2, NotContains = 3, GreaterThan = 4, LessThan = 5 } +} diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Controls/SshCheckControl.Designer.cs --- a/ServerMonitor/Controls/SshCheckControl.Designer.cs Mon Dec 31 18:32:14 2018 -0500 +++ b/ServerMonitor/Controls/SshCheckControl.Designer.cs Tue Jan 01 21:14:47 2019 -0500 @@ -80,10 +80,18 @@ // this.CommandOutputComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.CommandOutputComboBox.FormattingEnabled = true; + this.CommandOutputComboBox.Items.AddRange(new object[] { + "equals", + "does not equal", + "contains", + "does not contain", + "is greater than", + "is less than"}); this.CommandOutputComboBox.Location = new System.Drawing.Point(106, 4); this.CommandOutputComboBox.Name = "CommandOutputComboBox"; this.CommandOutputComboBox.Size = new System.Drawing.Size(110, 21); this.CommandOutputComboBox.TabIndex = 8; + this.CommandOutputComboBox.SelectedIndexChanged += new System.EventHandler(this.CommandOutputComboBox_SelectedIndexChanged); // // CommandOutputCheckBox // diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Controls/SshCheckControl.cs --- a/ServerMonitor/Controls/SshCheckControl.cs Mon Dec 31 18:32:14 2018 -0500 +++ b/ServerMonitor/Controls/SshCheckControl.cs Tue Jan 01 21:14:47 2019 -0500 @@ -34,11 +34,33 @@ SshCheck check = (SshCheck)check1; check.Command = CommandTextBox.Text.Trim(); check.CheckExitCode = ExitCodeCheckBox.Checked; - check.ExitCode = int.Parse(ExitCodeTextBox.Text); + 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; + } } } diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Forms/CheckForm.cs --- a/ServerMonitor/Forms/CheckForm.cs Mon Dec 31 18:32:14 2018 -0500 +++ b/ServerMonitor/Forms/CheckForm.cs Tue Jan 01 21:14:47 2019 -0500 @@ -137,8 +137,15 @@ check.Enabled = EnabledCheckBox.Checked; check.Timeout = (int)TimeoutInput.Value; check.Schedule = new Schedule((FrequencyUnits)FrequencyUnitsComboBox.SelectedItem, (int)FrequencyUpDown.Value, StartTimePicker.Value.TimeOfDay, EndTimePicker.Value.TimeOfDay); - checkControl?.UpdateCheck(check); - result = check.Validate(saving); + try + { + checkControl?.UpdateCheck(check); + result = check.Validate(saving); + } + catch (UpdateCheckException e) + { + result = e.Message; + } } if (!result.IsNullOrEmpty()) { diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Objects/Check.cs --- a/ServerMonitor/Objects/Check.cs Mon Dec 31 18:32:14 2018 -0500 +++ b/ServerMonitor/Objects/Check.cs Tue Jan 01 21:14:47 2019 -0500 @@ -169,9 +169,26 @@ else { if (matchType.In(MatchType.Equals, MatchType.NotEquals)) + { match = expectedPattern == resultValue; + } + else if (matchType.In(MatchType.Contains, MatchType.NotContains)) + { + match = resultValue.Contains(expectedPattern); + } else - match = resultValue.Contains(expectedPattern); + { + if (decimal.TryParse(expectedPattern, out decimal expectedNumeric) && + decimal.TryParse(resultValue, out decimal resultNumeric)) + { + match = (matchType == MatchType.GreaterThan && resultNumeric > expectedNumeric) || + (matchType == MatchType.LessThan && resultNumeric < expectedNumeric); + } + else + { + return Fail(string.Format("{0} is not numeric: {1}", description, resultValue)); + } + } } if (matchType.In(MatchType.Equals, MatchType.Contains)) @@ -181,13 +198,20 @@ else return Fail(string.Format("{0} does not {1} the pattern: {2} ({0}: {3})", description, matchType.ToString().ToLower().TrimEnd('s'), expectedPattern, resultValue)); } - else + else if (matchType.In(MatchType.NotEquals, MatchType.NotContains)) { if (match) return Fail(string.Format("{0} {1} the pattern: {2} ({0}: {3})", description, matchType.ToString().ToLower().Replace("not", ""), expectedPattern, resultValue)); else return Pass(string.Format("{0} does not {1} the pattern: {2}", description, matchType.ToString().ToLower().TrimEnd('s').Replace("not", ""), expectedPattern)); } + else + { + if (match) + return Pass(string.Format("{0} ({1}) is {2} {3}", description, resultValue, matchType.ToString().ToLower().Replace("than", " than"), expectedPattern)); + else + return Fail(string.Format("{0} ({1}) is not {2} {3}", description, resultValue, matchType.ToString().ToLower().Replace("than", " than"), expectedPattern)); + } } protected CheckResult MergeResults(params CheckResult[] results) diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/Objects/UpdateCheckException.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ServerMonitor/Objects/UpdateCheckException.cs Tue Jan 01 21:14:47 2019 -0500 @@ -0,0 +1,12 @@ +using System; +using System.Runtime.Serialization; + +namespace ServerMonitorApp +{ + public class UpdateCheckException : Exception + { + public UpdateCheckException(string message) : base(message) + { + } + } +} \ No newline at end of file diff -r 3e1a2131f897 -r 9e92780ebc0f ServerMonitor/ServerMonitor.csproj --- a/ServerMonitor/ServerMonitor.csproj Mon Dec 31 18:32:14 2018 -0500 +++ b/ServerMonitor/ServerMonitor.csproj Tue Jan 01 21:14:47 2019 -0500 @@ -74,9 +74,10 @@ HttpCheckControl.cs - + Component + Form