diff ServerMonitor/Objects/Checks/SshCheck.cs @ 17:68d7834dc28e

More comments.
author Brad Greco <brad@bgreco.net>
date Sat, 25 May 2019 15:14:26 -0400
parents a36cc5c123f4
children 7645122aa7a9
line wrap: on
line diff
--- a/ServerMonitor/Objects/Checks/SshCheck.cs	Tue Apr 30 20:40:58 2019 -0400
+++ b/ServerMonitor/Objects/Checks/SshCheck.cs	Sat May 25 15:14:26 2019 -0400
@@ -2,57 +2,70 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
-using System.Linq;
-using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading.Tasks;
 
 namespace ServerMonitorApp
 {
+    /// <summary>Executes an SSH command and checks the output or exit code.</summary>
     [DisplayName("SSH check"), Description("Check the result of a command run over SSH"), DisplayWeight(10)]
     public class SshCheck : Check
     {
+        /// <summary>The command to execute on the server.</summary>
         public virtual string Command { get; set; }
 
+        /// <summary>Whether the exit code of the command should be checked.</summary>
         public bool CheckExitCode { get; set; }
 
+        /// <summary>The required exit code of the command if CheckExitCode is true.</summary>
         public int ExitCode { get; set; }
 
+        /// <summary>Whether the text output of the command should be checked.</summary>
         public bool CheckCommandOutput { get; set; }
 
+        /// <summary>The method to use when checking the command output against the pattern.</summary>
         public MatchType CommandOutputMatchType { get; set; }
 
+        /// <summary>The string or pattern that the command output must match if CommandOutputMatchType is true.</summary>
         public string CommandOutputPattern { get; set; }
 
+        /// <summary>Whether the CommandOutputPattern should be interpreted as a regular expression.</summary>
         public bool CommandOutputUseRegex { get; set; }
 
+        /// <summary>Executes the SSH command on the server.</summary>
         protected override Task<CheckResult> ExecuteCheckAsync(CancellationToken token)
         {
-            try
-            {
-                if (!Server.SshClient.IsConnected)
-                {
-                    if (Server.LoginType == LoginType.PrivateKey && Server.PrivateKeyFile == null)
-                        return Task.FromResult(Fail(string.Format("Private key '{0}' is locked or not accessible", Server.KeyFile)));
-                    Server.SshClient.Connect();
-                }
-            }
-            catch (Exception e)
-            {
-                return Task.FromResult(Fail(e));
-            }
             return Task.Run(() =>
             {
                 try
                 {
+                    // Exit now if the user cancelled the execution.
                     token.ThrowIfCancellationRequested();
+
+                    // Connect to the server if needed.
+                    if (!Server.SshClient.IsConnected)
+                    {
+                        // If the server private key file has not been opened, it is probably encrypted.
+                        // Report failure until the user enters the password.
+                        if (Server.LoginType == LoginType.PrivateKey && Server.PrivateKeyFile == null)
+                            return Fail(string.Format("Private key '{0}' is locked or not accessible", Server.KeyFile));
+                        Server.SshClient.Connect();
+                    }
+
+                    // Exit now if the user cancelled the execution.
+                    token.ThrowIfCancellationRequested();
+
                     using (SshCommand command = Server.SshClient.CreateCommand(Command))
                     {
+                        // Execute the command.
                         token.Register(command.CancelAsync);
                         IAsyncResult ar = command.BeginExecute();
                         token.ThrowIfCancellationRequested();
+                        // Store both the command output and the error streams so they can
+                        // be logged and checked.
                         string output = (command.EndExecute(ar).Trim() + command.Error.Trim()).ConvertNewlines();
+                        // Process the results (exit code and command output) and merge them into a single result.
                         return MergeResults(ProcessCommandResult(output, command.ExitStatus).ToArray());
                     }
                 }
@@ -63,11 +76,17 @@
             }, token);
         }
 
+        /// <summary>Processes the command results and checks they match the expected values.</summary>
+        /// <param name="output">The command output.</param>
+        /// <param name="exitCode">The command exit code.</param>
+        /// <returns>A list of check results according to user preferences.</returns>
         protected virtual List<CheckResult> ProcessCommandResult(string output, int exitCode)
         {
             List<CheckResult> results = new List<CheckResult>();
+            // Check the actual output against the expected output if command output checking is enabled.
             if (CheckCommandOutput)
                 results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output"));
+            // Check the actual exit code against the expected exit code if exit code checking is enabled.
             if (CheckExitCode)
             {
                 CheckResult result = GetIntResult(ExitCode, exitCode, "Exit code");
@@ -78,6 +97,7 @@
             return results;
         }
 
+        /// <summary>Validates SSH check options.</summary>
         public override string Validate(bool saving = true)
         {
             string message = base.Validate();