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
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 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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
6 /// <summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
7 /// The result of an executed check.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
8 /// Contains data about the check's last execution including status, time, and log message.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
12 // The date format to use in log files.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
21 /// <summary>The message generated by the check execution.</summary>
3
96f0b028176d File check
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
32
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
33 /// <summary>Action to perform when the check fails.</summary>
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
34 public FailAction FailAction
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
35 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
36 get
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
37 {
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
38 // Use the global preferences for each status to determine the action to take.
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
39 switch (CheckStatus)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
40 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
41 case CheckStatus.Error: return Settings.Default.ErrorAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
42 case CheckStatus.Warning: return Settings.Default.WarningAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
43 case CheckStatus.Information: return Settings.Default.InformationAction;
17
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
44 // On success (or any other status), do nothing.
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
45 default: return FailAction.None;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
46 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
47 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
48 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 3
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
76 /// <summary>CheckResult constructor.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
77 /// <param name="check">The originating check of this check result.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
78 /// <param name="status">The result status of the check execution.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
87 /// <summary>Generates a string representation of the check result that can be logged.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
88 /// <returns>A string representation of the check result that can be logged.</returns>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
89 /// <remarks>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
90 /// The log string is in the format:
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
91 /// [Check ID] [Start time] [End time] [Check status] [Check output]
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
92 ///
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
93 /// The check ID is left-padded with zeros to simplify log parsing and filtering by check ID.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
94 /// Dates are formatted according to the dateFormat defined in this class.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
95 /// Newlines in check output are escaped so the log string contains no literal newline characters.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
101 StartTime.ToString(dateFormat),
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
107 /// <summary>Parses a log string to create a check result object.</summary>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
108 /// <param name="check">The originating check for the check result.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
109 /// <param name="logString">The log string to parse.</param>
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
116 // The check status is not fixed in length, but will not contain any spaces.
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
117 // So, the first space following the beginning of the checks status will
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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
68d7834dc28e More comments.
Brad Greco <brad@bgreco.net>
parents: 8
diff changeset
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 }