comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:3e1a2131f897
1 using Renci.SshNet;
2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Linq;
6 using System.Text;
7 using System.Text.RegularExpressions;
8 using System.Threading;
9 using System.Threading.Tasks;
10
11 namespace ServerMonitorApp
12 {
13 [DisplayName("SSH check"), Description("Check the result of a command run over SSH"), DisplayWeight(10)]
14 public class SshCheck : Check
15 {
16 public string Command { get; set; }
17
18 public bool CheckExitCode { get; set; }
19
20 public int ExitCode { get; set; }
21
22 public bool CheckCommandOutput { get; set; }
23
24 public MatchType CommandOutputMatchType { get; set; }
25
26 public string CommandOutputPattern { get; set; }
27
28 public bool CommandOutputUseRegex { get; set; }
29
30 protected override Task<CheckResult> ExecuteCheckAsync(CancellationToken token)
31 {
32 return Task.Run(() =>
33 {
34 try
35 {
36 if (!Server.SshClient.IsConnected)
37 Server.SshClient.Connect();
38 token.ThrowIfCancellationRequested();
39 using (SshCommand command = Server.SshClient.CreateCommand(Command))
40 {
41 token.Register(command.CancelAsync);
42 IAsyncResult ar = command.BeginExecute();
43 token.ThrowIfCancellationRequested();
44 string result = command.EndExecute(ar).Trim() + command.Error.Trim();
45
46 CheckResult exitCodeResult = null, commandOutputResult = null;
47 if (CheckExitCode)
48 exitCodeResult = GetIntResult(ExitCode, command.ExitStatus, "Exit code");
49 if (CheckCommandOutput)
50 commandOutputResult = GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, result, "Command output");
51 return MergeResults(exitCodeResult, commandOutputResult);
52 }
53 }
54 catch (Exception e)
55 {
56 return Fail(e);
57 }
58 }, token);
59 //TaskCompletionSource<CheckResult> tcs = new TaskCompletionSource<CheckResult>();
60
61 ////TODO timeout
62 //if (!Server.SshClient.IsConnected)
63 // Server.SshClient.Connect();
64 //using (SshCommand command = Server.SshClient.CreateCommand(Command))
65 //{
66 // token.Register(command.CancelAsync);
67 // command.BeginExecute(asyncResult =>
68 // {
69 // string result = command.EndExecute(asyncResult);
70 // tcs.SetResult(new CheckResult(this, CheckStatus.Success, result));
71 // });
72 //}
73
74 //return tcs.Task;
75 }
76
77 public override string Validate(bool saving = true)
78 {
79 string message = base.Validate();
80 if (Server.Port <= 0)
81 message += "Server SSH port is required." + Environment.NewLine;
82 if (Server.Username.IsNullOrEmpty())
83 message += "Server SSH username is required." + Environment.NewLine;
84 if (Server.LoginType == LoginType.Password && Server.Password.IsNullOrEmpty())
85 message += "Server SSH password is required." + Environment.NewLine;
86 if (Server.LoginType == LoginType.PrivateKey && Server.KeyFile.IsNullOrEmpty())
87 message += "Server SSH key is required." + Environment.NewLine;
88 if (Command.IsNullOrEmpty())
89 message += "Command is required." + Environment.NewLine;
90 if (!CheckExitCode && !CheckCommandOutput)
91 message += "At least one check must be enabled." + Environment.NewLine;
92 if (CheckCommandOutput && CommandOutputUseRegex)
93 {
94 try
95 {
96 Regex re = new Regex(CommandOutputPattern);
97 }
98 catch (ArgumentException)
99 {
100 message += "Invalid regular expression for command output." + Environment.NewLine;
101 }
102 }
103 return message;
104 }
105 }
106 }