Mercurial > servermonitor
annotate ServerMonitor/Objects/CheckResult.cs @ 32:2342e9459444
Fix schedule times not being saved.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sun, 16 Jun 2019 16:55:14 -0400 |
parents | f6235dc0a8ec |
children |
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 ServerMonitorApp.Properties; |
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 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
4 namespace ServerMonitorApp |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
5 { |
17 | 6 /// <summary> |
7 /// The result of an executed check. | |
8 /// Contains data about the check's last execution including status, time, and log message. | |
9 /// </summary> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
10 public class CheckResult |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
11 { |
17 | 12 // The date format to use in log files. |
13 private const string dateFormat = "yyyy-MM-dd HH:mm:ss.fff"; | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
14 |
17 | 15 /// <summary>The originating check of this check result.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
16 public Check Check { get; private set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
17 |
17 | 18 /// <summary>The result status of the check execution.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
19 public CheckStatus CheckStatus { get; private set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
20 |
17 | 21 /// <summary>The message generated by the check execution.</summary> |
3 | 22 public string Message { get; set; } |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
23 |
17 | 24 /// <summary>The time the check execution began.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
25 public DateTime StartTime { get; set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
26 |
17 | 27 /// <summary>The time the check execution ended.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
28 public DateTime EndTime { get; set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
29 |
17 | 30 /// <summary>Whether the check execution resulted in success or failure.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
31 public bool Failed => CheckStatus.In(CheckStatus.Error, CheckStatus.Warning, CheckStatus.Information); |
4 | 32 |
17 | 33 /// <summary>Action to perform when the check fails.</summary> |
4 | 34 public FailAction FailAction |
35 { | |
36 get | |
37 { | |
17 | 38 // Use the global preferences for each status to determine the action to take. |
4 | 39 switch (CheckStatus) |
40 { | |
41 case CheckStatus.Error: return Settings.Default.ErrorAction; | |
42 case CheckStatus.Warning: return Settings.Default.WarningAction; | |
43 case CheckStatus.Information: return Settings.Default.InformationAction; | |
17 | 44 // On success (or any other status), do nothing. |
4 | 45 default: return FailAction.None; |
46 } | |
47 } | |
48 } | |
49 | |
29
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
50 /// <summary>Sound to play when the check fails.</summary> |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
51 /// <remarks> |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
52 /// Returns null if no sound should be played. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
53 /// Returns string.Empty if the Windows default error sound should be played. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
54 /// Otherwise, returns the path to the custom sound file that should be played. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
55 /// </remarks> |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
56 public string FailSound |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
57 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
58 get |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
59 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
60 string setting = null; |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
61 // Use the global preferences for each status to determine the action to take. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
62 switch (CheckStatus) |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
63 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
64 case CheckStatus.Error: setting = Settings.Default.ErrorSound; break; |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
65 case CheckStatus.Warning: setting = Settings.Default.WarningSound; break; |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
66 case CheckStatus.Information: setting = Settings.Default.InformationSound; break; |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
67 // On success (or any other status), do nothing. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
68 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
69 if (int.TryParse(setting, out int index)) { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
70 return index == 0 ? null : string.Empty; |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
71 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
72 return setting; |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
73 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
74 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
75 |
17 | 76 /// <summary>CheckResult constructor.</summary> |
77 /// <param name="check">The originating check of this check result.</param> | |
78 /// <param name="status">The result status of the check execution.</param> | |
79 /// <param name="message">The message generated by the check execution.</param> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
80 public CheckResult(Check check, CheckStatus status, string message) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
81 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
82 Check = check; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
83 CheckStatus = status; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
84 Message = message; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
85 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
86 |
17 | 87 /// <summary>Generates a string representation of the check result that can be logged.</summary> |
88 /// <returns>A string representation of the check result that can be logged.</returns> | |
89 /// <remarks> | |
90 /// The log string is in the format: | |
91 /// [Check ID] [Start time] [End time] [Check status] [Check output] | |
92 /// | |
93 /// The check ID is left-padded with zeros to simplify log parsing and filtering by check ID. | |
94 /// Dates are formatted according to the dateFormat defined in this class. | |
95 /// Newlines in check output are escaped so the log string contains no literal newline characters. | |
96 /// </remarks> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
97 public string ToLogString() |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
98 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
99 return string.Format("{0:00000} {1} {2} {3} {4}", |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
100 Check.Id, |
17 | 101 StartTime.ToString(dateFormat), |
102 EndTime.ToString(dateFormat), | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
103 CheckStatus, |
2 | 104 Message.ConvertNewlines().Replace("\n", "\\n")); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
105 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
106 |
17 | 107 /// <summary>Parses a log string to create a check result object.</summary> |
108 /// <param name="check">The originating check for the check result.</param> | |
109 /// <param name="logString">The log string to parse.</param> | |
110 /// <returns>A check result object.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
111 public static CheckResult FromLogString(Check check, string logString) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
112 { |
17 | 113 // The check ID, start time, and end time are fixed in length, so no pattern matching is needed. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
114 DateTime startTime = DateTime.Parse(logString.Substring(6, 23)); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
115 DateTime endTime = DateTime.Parse(logString.Substring(30, 23)); |
17 | 116 // The check status is not fixed in length, but will not contain any spaces. |
117 // So, the first space following the beginning of the checks status will | |
118 // mark the start of the result message. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
119 int messageStartPos = logString.IndexOf(' ', 54); |
17 | 120 // Now we know the length of the status token, so we can extract and parse it. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
121 Enum.TryParse(logString.Substring(54, messageStartPos - 54), out CheckStatus status); |
17 | 122 // Put it all together. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
123 return new CheckResult(check, status, logString.Substring(messageStartPos + 1)) { StartTime = startTime, EndTime = endTime }; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
124 } |
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 } |