comparison 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
comparison
equal deleted inserted replaced
2:453ecc1ed9ea 3:96f0b028176d
11 namespace ServerMonitorApp 11 namespace ServerMonitorApp
12 { 12 {
13 [DisplayName("SSH check"), Description("Check the result of a command run over SSH"), DisplayWeight(10)] 13 [DisplayName("SSH check"), Description("Check the result of a command run over SSH"), DisplayWeight(10)]
14 public class SshCheck : Check 14 public class SshCheck : Check
15 { 15 {
16 public string Command { get; set; } 16 public virtual string Command { get; set; }
17 17
18 public bool CheckExitCode { get; set; } 18 public bool CheckExitCode { get; set; }
19 19
20 public int ExitCode { get; set; } 20 public int ExitCode { get; set; }
21 21
34 try 34 try
35 { 35 {
36 if (!Server.SshClient.IsConnected) 36 if (!Server.SshClient.IsConnected)
37 Server.SshClient.Connect(); 37 Server.SshClient.Connect();
38 token.ThrowIfCancellationRequested(); 38 token.ThrowIfCancellationRequested();
39 using (SshCommand command = Server.SshClient.CreateCommand(GetCommand())) 39 using (SshCommand command = Server.SshClient.CreateCommand(Command))
40 { 40 {
41 token.Register(command.CancelAsync); 41 token.Register(command.CancelAsync);
42 IAsyncResult ar = command.BeginExecute(); 42 IAsyncResult ar = command.BeginExecute();
43 token.ThrowIfCancellationRequested(); 43 token.ThrowIfCancellationRequested();
44 string output = (command.EndExecute(ar).Trim() + command.Error.Trim()).ConvertNewlines(); 44 string output = (command.EndExecute(ar).Trim() + command.Error.Trim()).ConvertNewlines();
66 //} 66 //}
67 67
68 //return tcs.Task; 68 //return tcs.Task;
69 } 69 }
70 70
71 protected virtual string GetCommand()
72 {
73 return Command;
74 }
75
76 protected virtual List<CheckResult> ProcessCommandResult(string output, int exitCode) 71 protected virtual List<CheckResult> ProcessCommandResult(string output, int exitCode)
77 { 72 {
78 List<CheckResult> results = new List<CheckResult>(); 73 List<CheckResult> results = new List<CheckResult>();
79 if (CheckExitCode) 74 if (CheckExitCode)
80 results.Add(GetIntResult(ExitCode, exitCode, "Exit code")); 75 {
76 CheckResult result = GetIntResult(ExitCode, exitCode, "Exit code");
77 if (result.CheckStatus != CheckStatus.Success)
78 result.Message += ". Command output: " + output;
79 results.Add(result);
80 }
81 if (CheckCommandOutput) 81 if (CheckCommandOutput)
82 results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output")); 82 results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output"));
83 return results; 83 return results;
84 } 84 }
85 85