view ServerMonitor/Objects/SshCheck.cs @ 0:3e1a2131f897

Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
author Brad Greco <brad@bgreco.net>
date Mon, 31 Dec 2018 18:32:14 -0500
parents
children
line wrap: on
line source

using Renci.SshNet;
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
{
    [DisplayName("SSH check"), Description("Check the result of a command run over SSH"), DisplayWeight(10)]
    public class SshCheck : Check
    {
        public string Command { get; set; }

        public bool CheckExitCode { get; set; }

        public int ExitCode { get; set; }

        public bool CheckCommandOutput { get; set; }

        public MatchType CommandOutputMatchType { get; set; }

        public string CommandOutputPattern { get; set; }

        public bool CommandOutputUseRegex { get; set; }

        protected override Task<CheckResult> ExecuteCheckAsync(CancellationToken token)
        {
            return Task.Run(() =>
            {
                try
                {
                    if (!Server.SshClient.IsConnected)
                        Server.SshClient.Connect();
                    token.ThrowIfCancellationRequested();
                    using (SshCommand command = Server.SshClient.CreateCommand(Command))
                    {
                        token.Register(command.CancelAsync);
                        IAsyncResult ar = command.BeginExecute();
                        token.ThrowIfCancellationRequested();
                        string result = command.EndExecute(ar).Trim() + command.Error.Trim();

                        CheckResult exitCodeResult = null, commandOutputResult = null;
                        if (CheckExitCode)
                            exitCodeResult = GetIntResult(ExitCode, command.ExitStatus, "Exit code");
                        if (CheckCommandOutput)
                            commandOutputResult = GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, result, "Command output");
                        return MergeResults(exitCodeResult, commandOutputResult);
                    }
                }
                catch (Exception e)
                {
                    return Fail(e);
                }
            }, token);
            //TaskCompletionSource<CheckResult> tcs = new TaskCompletionSource<CheckResult>();

            ////TODO timeout
            //if (!Server.SshClient.IsConnected)
            //    Server.SshClient.Connect();
            //using (SshCommand command = Server.SshClient.CreateCommand(Command))
            //{
            //    token.Register(command.CancelAsync);
            //    command.BeginExecute(asyncResult =>
            //    {
            //        string result = command.EndExecute(asyncResult);
            //        tcs.SetResult(new CheckResult(this, CheckStatus.Success, result));
            //    });
            //}

            //return tcs.Task;
        }

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