Mercurial > servermonitor
view ServerMonitor/Objects/Checks/SshCheck.cs @ 3:96f0b028176d
File check
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Fri, 11 Jan 2019 22:34:18 -0500 |
parents | 453ecc1ed9ea |
children | 3142e52cbe69 |
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 virtual 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 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 List<CheckResult> ProcessCommandResult(string output, int exitCode) { List<CheckResult> results = new List<CheckResult>(); if (CheckExitCode) { CheckResult result = GetIntResult(ExitCode, exitCode, "Exit code"); if (result.CheckStatus != CheckStatus.Success) result.Message += ". Command output: " + output; results.Add(result); } 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; } } }