comparison 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
comparison
equal deleted inserted replaced
1:9e92780ebc0f 2:453ecc1ed9ea
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(GetCommand()))
40 {
41 token.Register(command.CancelAsync);
42 IAsyncResult ar = command.BeginExecute();
43 token.ThrowIfCancellationRequested();
44 string output = (command.EndExecute(ar).Trim() + command.Error.Trim()).ConvertNewlines();
45 return MergeResults(ProcessCommandResult(output, command.ExitStatus).ToArray());
46 }
47 }
48 catch (Exception e)
49 {
50 return Fail(e);
51 }
52 }, token);
53 //TaskCompletionSource<CheckResult> tcs = new TaskCompletionSource<CheckResult>();
54
55 ////TODO timeout
56 //if (!Server.SshClient.IsConnected)
57 // Server.SshClient.Connect();
58 //using (SshCommand command = Server.SshClient.CreateCommand(Command))
59 //{
60 // token.Register(command.CancelAsync);
61 // command.BeginExecute(asyncResult =>
62 // {
63 // string result = command.EndExecute(asyncResult);
64 // tcs.SetResult(new CheckResult(this, CheckStatus.Success, result));
65 // });
66 //}
67
68 //return tcs.Task;
69 }
70
71 protected virtual string GetCommand()
72 {
73 return Command;
74 }
75
76 protected virtual List<CheckResult> ProcessCommandResult(string output, int exitCode)
77 {
78 List<CheckResult> results = new List<CheckResult>();
79 if (CheckExitCode)
80 results.Add(GetIntResult(ExitCode, exitCode, "Exit code"));
81 if (CheckCommandOutput)
82 results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output"));
83 return results;
84 }
85
86 public override string Validate(bool saving = true)
87 {
88 string message = base.Validate();
89 if (Server.Port <= 0)
90 message += "Server SSH port is required." + Environment.NewLine;
91 if (Server.Username.IsNullOrEmpty())
92 message += "Server SSH username is required." + Environment.NewLine;
93 if (Server.LoginType == LoginType.Password && Server.Password.IsNullOrEmpty())
94 message += "Server SSH password is required." + Environment.NewLine;
95 if (Server.LoginType == LoginType.PrivateKey && Server.KeyFile.IsNullOrEmpty())
96 message += "Server SSH key is required." + Environment.NewLine;
97 if (Command.IsNullOrEmpty())
98 message += "Command is required." + Environment.NewLine;
99 if (!CheckExitCode && !CheckCommandOutput)
100 message += "At least one check must be enabled." + Environment.NewLine;
101 if (CheckCommandOutput && CommandOutputUseRegex)
102 {
103 try
104 {
105 Regex re = new Regex(CommandOutputPattern);
106 }
107 catch (ArgumentException)
108 {
109 message += "Invalid regular expression for command output." + Environment.NewLine;
110 }
111 }
112 return message;
113 }
114 }
115 }