annotate ServerMonitor/Objects/Check.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 9e92780ebc0f
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 System;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
2 using System.Linq;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.Text.RegularExpressions;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Threading;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Threading.Tasks;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 using System.Xml.Serialization;
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 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 /*public enum CheckType
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 Command
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 }*/
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 public enum CheckStatus
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 Success,
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 Information,
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 Warning,
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 Error,
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 Running,
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 Disabled,
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 public abstract class Check
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 private static Type[] _checkTypes;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 public static Type[] CheckTypes
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32 get
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 return _checkTypes ?? (_checkTypes = typeof(Check).Assembly.GetTypes()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 .Where(t => t.IsSubclassOf(typeof(Check)))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 .OrderBy(t => t.GetAttribute<DisplayWeightAttribute>()?.DisplayWeight).ToArray());
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37 }
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
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40 public int Id { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
41
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 public string Name { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 /*public CheckType Type { get; set; }*/
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46 public int Timeout { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 public bool Enabled { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 public Schedule Schedule { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 public DateTime LastRunTime { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 public DateTime LastScheduledRunTime { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
56 public DateTime NextRunTime { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 public string LastMessage { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 public CheckStatus Status { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 public CheckStatus FailStatus { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
64 [XmlIgnore]
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 public Server Server { get; set; }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 public Check()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
69 FailStatus = CheckStatus.Error;
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 public override string ToString()
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 Name;
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
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 public virtual 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
78 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
79 string message = string.Empty;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
80 if (Name.IsNullOrEmpty() && saving)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
81 message += "Name cannot be blank." + Environment.NewLine;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
82 return message;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
83 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
84
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
85 //public virtual CheckStatus Execute()
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
86 //{
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
87 // //TODO
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
88 // throw new NotImplementedException();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
89 //}
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
90
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
91 public async Task<CheckResult> ExecuteAsync(CancellationToken token = default(CancellationToken), bool update = true)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
92 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
93 //TODO check cancellation token before proceeding
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
94 CheckResult result;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
95 DateTime startTime = DateTime.Now;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
96 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
97 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
98 Task<CheckResult> checkTask = ExecuteCheckAsync(token);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99 try
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
100 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
101 if (await Task.WhenAny(checkTask, Task.Delay(Timeout, token)) == checkTask)
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 result = await checkTask;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
104 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
105 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
106 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
107 result = Fail("Timed out.");
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
108 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
109 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
110 catch (TaskCanceledException)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
111 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
112 return null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
113 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
114 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
115 catch (Exception e)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
116 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
117 result = Fail(e.GetBaseException().Message);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
118 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
119 result.StartTime = startTime;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
120 result.EndTime = DateTime.Now;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
121 // If a check is executed from the CheckForm, we don't want to update the status or log the event.
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
122 if (update)
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 Status = result.CheckStatus;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
125 LastMessage = result.Message;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
126 LastRunTime = result.EndTime;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
127 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
128 return result;
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
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
131 protected CheckResult Pass(string message)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
132 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
133 return new CheckResult(this, CheckStatus.Success, message);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
134 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
135
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
136 protected CheckResult Fail(string message)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
137 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
138 return new CheckResult(this, FailStatus, message);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
139 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
140
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
141 protected CheckResult Fail(Exception e)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
142 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
143 return new CheckResult(this, FailStatus, e.GetBaseException().Message);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
144 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
145
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
146 protected virtual CheckResult GetIntResult(int expectedValue, int resultValue, string description)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
147 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
148 if (expectedValue == resultValue)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
149 return Pass(string.Format("{0}: {1}", description, resultValue));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
150 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
151 return Fail(string.Format("{0}: {1} (expected: {2})", description, resultValue, expectedValue));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
152 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
153
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
154 protected virtual CheckResult GetStringResult(MatchType matchType, string expectedPattern, bool useRegex, string resultValue, string description)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
155 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
156 bool match;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
157 if (useRegex)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
158 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
159 if (matchType.In(MatchType.Equals, MatchType.NotEquals))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
160 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
161 if (!expectedPattern.StartsWith("^"))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
162 expectedPattern = "^" + expectedPattern;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
163 if (!expectedPattern.EndsWith("$"))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
164 expectedPattern += "$";
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
165 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
166 Regex re = new Regex(expectedPattern, RegexOptions.Singleline);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
167 match = re.IsMatch(resultValue);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
168 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
169 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
170 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
171 if (matchType.In(MatchType.Equals, MatchType.NotEquals))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
172 match = expectedPattern == resultValue;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
173 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
174 match = resultValue.Contains(expectedPattern);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
175 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
176
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
177 if (matchType.In(MatchType.Equals, MatchType.Contains))
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
178 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
179 if (match)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
180 return Pass(string.Format("{0} {1} the pattern: {2}", description, matchType.ToString().ToLower(), expectedPattern));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
181 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
182 return Fail(string.Format("{0} does not {1} the pattern: {2} ({0}: {3})", description, matchType.ToString().ToLower().TrimEnd('s'), expectedPattern, resultValue));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
183 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
184 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
185 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
186 if (match)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
187 return Fail(string.Format("{0} {1} the pattern: {2} ({0}: {3})", description, matchType.ToString().ToLower().Replace("not", ""), expectedPattern, resultValue));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
188 else
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
189 return Pass(string.Format("{0} does not {1} the pattern: {2}", description, matchType.ToString().ToLower().TrimEnd('s').Replace("not", ""), expectedPattern));
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
190 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
191 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
192
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
193 protected CheckResult MergeResults(params CheckResult[] results)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
194 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
195 StringBuilder message = new StringBuilder();
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
196 bool failed = false;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
197 foreach (CheckResult result in results)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
198 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
199 if (result == null)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
200 continue;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
201 if (result.CheckStatus != CheckStatus.Success)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
202 failed = true;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
203 message.AppendLine(result.Message);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
204 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
205 return failed ? Fail(message.ToString().Trim()) : Pass(message.ToString().Trim());
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
206 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
207
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
208 protected abstract 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
209 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
210 }