view ServerMonitor/Objects/Checks/SshCheck.cs @ 19:b3128fe10d57

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

using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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)
        {
            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());
                    }
                }
                catch (Exception e)
                {
                    return Fail(e);
                }
            }, 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");
                if (result.Failed)
                    result.Message += ". Command output: " + output;
                results.Add(result);
            }
            return results;
        }

        /// <summary>Validates SSH check options.</summary>
        public override string Validate(bool saving = true)
        {
            string message = base.Validate();
            if (Server.Port <= 0)
                message += "Server SSH port is required." + Environment.NewLine;
            if (Server.Username.IsNullOrEmpty())
                message += "Server SSH username is required." + Environment.NewLine;
            if (Server.LoginType == LoginType.Password && Server.Password.IsNullOrEmpty())
                message += "Server SSH password is required." + Environment.NewLine;
            if (Server.LoginType == LoginType.PrivateKey && Server.KeyFile.IsNullOrEmpty())
                message += "Server SSH key is required." + Environment.NewLine;
            if (Command.IsNullOrEmpty())
                message += "Command is required." + Environment.NewLine;
            if (!CheckExitCode && !CheckCommandOutput)
                message += "At least one check must be enabled." + Environment.NewLine;
            if (CheckCommandOutput && CommandOutputUseRegex)
            {
                try
                {
                    Regex re = new Regex(CommandOutputPattern);
                }
                catch (ArgumentException)
                {
                    message += "Invalid regular expression for command output." + Environment.NewLine;
                }
            }
            return message;
        }
    }
}