view ServerMonitor/Objects/Checks/SshCheck.cs @ 2:453ecc1ed9ea

Disk space check
author Brad Greco <brad@bgreco.net>
date Sun, 06 Jan 2019 20:49:08 -0500
parents ServerMonitor/Objects/SshCheck.cs@3e1a2131f897
children 96f0b028176d
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(GetCommand()))
                    {
                        token.Register(command.CancelAsync);
                        IAsyncResult ar = command.BeginExecute();
                        token.ThrowIfCancellationRequested();
                        string output = (command.EndExecute(ar).Trim() + command.Error.Trim()).ConvertNewlines();
                        return MergeResults(ProcessCommandResult(output, command.ExitStatus).ToArray());
                    }
                }
                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;
        }

        protected virtual string GetCommand()
        {
            return Command;
        }

        protected virtual List<CheckResult> ProcessCommandResult(string output, int exitCode)
        {
            List<CheckResult> results = new List<CheckResult>();
            if (CheckExitCode)
                results.Add(GetIntResult(ExitCode, exitCode, "Exit code"));
            if (CheckCommandOutput)
                results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output"));
            return results;
        }

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