annotate ServerMonitor/Objects/Checks/SshCheck.cs @ 17:68d7834dc28e

More comments.
author Brad Greco <brad@bgreco.net>
date Sat, 25 May 2019 15:14:26 -0400
parents a36cc5c123f4
children 7645122aa7a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
1 using Renci.SshNet;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2 using System;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.Collections.Generic;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.ComponentModel;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Text.RegularExpressions;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Threading;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 using System.Threading.Tasks;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
11 /// <summary>Executes an SSH command and checks the output or exit code.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 [DisplayName("SSH check"), Description("Check the result of a command run over SSH"), DisplayWeight(10)]
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 public class SshCheck : Check
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
15 /// <summary>The command to execute on the server.</summary>
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
16 public virtual string Command { get; set; }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
18 /// <summary>Whether the exit code of the command should be checked.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 public bool CheckExitCode { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
21 /// <summary>The required exit code of the command if CheckExitCode is true.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 public int ExitCode { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
24 /// <summary>Whether the text output of the command should be checked.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 public bool CheckCommandOutput { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
27 /// <summary>The method to use when checking the command output against the pattern.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 public MatchType CommandOutputMatchType { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
30 /// <summary>The string or pattern that the command output must match if CommandOutputMatchType is true.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 public string CommandOutputPattern { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
33 /// <summary>Whether the CommandOutputPattern should be interpreted as a regular expression.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 public bool CommandOutputUseRegex { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
36 /// <summary>Executes the SSH command on the server.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37 protected override Task<CheckResult> ExecuteCheckAsync(CancellationToken token)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 return Task.Run(() =>
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
43 // Exit now if the user cancelled the execution.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 token.ThrowIfCancellationRequested();
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
45
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
46 // Connect to the server if needed.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
47 if (!Server.SshClient.IsConnected)
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
48 {
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
49 // If the server private key file has not been opened, it is probably encrypted.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
50 // Report failure until the user enters the password.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
51 if (Server.LoginType == LoginType.PrivateKey && Server.PrivateKeyFile == null)
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
52 return Fail(string.Format("Private key '{0}' is locked or not accessible", Server.KeyFile));
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
53 Server.SshClient.Connect();
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
54 }
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
55
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
56 // Exit now if the user cancelled the execution.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
57 token.ThrowIfCancellationRequested();
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
58
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
59 using (SshCommand command = Server.SshClient.CreateCommand(Command))
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
61 // Execute the command.
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 token.Register(command.CancelAsync);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 IAsyncResult ar = command.BeginExecute();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 token.ThrowIfCancellationRequested();
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
65 // Store both the command output and the error streams so they can
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
66 // be logged and checked.
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
67 string output = (command.EndExecute(ar).Trim() + command.Error.Trim()).ConvertNewlines();
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
68 // Process the results (exit code and command output) and merge them into a single result.
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
69 return MergeResults(ProcessCommandResult(output, command.ExitStatus).ToArray());
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72 catch (Exception e)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 return Fail(e);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 }, token);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
79 /// <summary>Processes the command results and checks they match the expected values.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
80 /// <param name="output">The command output.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
81 /// <param name="exitCode">The command exit code.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
82 /// <returns>A list of check results according to user preferences.</returns>
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
83 protected virtual List<CheckResult> ProcessCommandResult(string output, int exitCode)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
84 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
85 List<CheckResult> results = new List<CheckResult>();
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
86 // Check the actual output against the expected output if command output checking is enabled.
13
a36cc5c123f4 Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
87 if (CheckCommandOutput)
a36cc5c123f4 Fix SSH command sublclasses holding on to old command strings after a program update.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
88 results.Add(GetStringResult(CommandOutputMatchType, CommandOutputPattern, CommandOutputUseRegex, output, "Command output"));
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
89 // Check the actual exit code against the expected exit code if exit code checking is enabled.
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
90 if (CheckExitCode)
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
91 {
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
92 CheckResult result = GetIntResult(ExitCode, exitCode, "Exit code");
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
93 if (result.Failed)
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
94 result.Message += ". Command output: " + output;
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
95 results.Add(result);
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
96 }
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
97 return results;
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
98 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
99
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 13
diff changeset
100 /// <summary>Validates SSH check options.</summary>
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101 public override string Validate(bool saving = true)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
102 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
103 string message = base.Validate();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 if (Server.Port <= 0)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 message += "Server SSH port is required." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106 if (Server.Username.IsNullOrEmpty())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107 message += "Server SSH username is required." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
108 if (Server.LoginType == LoginType.Password && Server.Password.IsNullOrEmpty())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
109 message += "Server SSH password is required." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 if (Server.LoginType == LoginType.PrivateKey && Server.KeyFile.IsNullOrEmpty())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 message += "Server SSH key is required." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 if (Command.IsNullOrEmpty())
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 message += "Command is required." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
114 if (!CheckExitCode && !CheckCommandOutput)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
115 message += "At least one check must be enabled." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
116 if (CheckCommandOutput && CommandOutputUseRegex)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
117 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
118 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
119 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
120 Regex re = new Regex(CommandOutputPattern);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
121 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
122 catch (ArgumentException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
123 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
124 message += "Invalid regular expression for command output." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
125 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
126 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
127 return message;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
128 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
129 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
130 }