Mercurial > servermonitor
annotate ServerMonitor/Objects/ServerMonitor.cs @ 36:10e60b05c7ec
Fix memory leak each time a check was executed.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sun, 15 Sep 2019 20:48:55 -0400 |
parents | 2ffb0bda7705 |
children | 8ab98a803d39 |
rev | line source |
---|---|
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
1 using Microsoft.Win32; |
29
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
2 using NAudio.Wave; |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
3 using Renci.SshNet; |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
4 using Renci.SshNet.Common; |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
5 using ServerMonitorApp.Properties; |
4 | 6 using System; |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
7 using System.Collections.Generic; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
8 using System.IO; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
9 using System.Linq; |
29
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
10 using System.Media; |
4 | 11 using System.Net.NetworkInformation; |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
12 using System.Threading; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
13 using System.Threading.Tasks; |
4 | 14 using System.Windows.Forms; |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
15 using System.Xml.Serialization; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
16 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
17 namespace ServerMonitorApp |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
18 { |
17 | 19 /// <summary>Central class for scheduling and executing checks against remote servers.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
20 public class ServerMonitor |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
21 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
22 private readonly string configFileDir; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
23 private readonly Logger logger; |
17 | 24 // Cancellation tokens for executing checks, keyed by check ID. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
25 private readonly Dictionary<int, CancellationTokenSource> tokens = new Dictionary<int, CancellationTokenSource>(); |
17 | 26 // SSH private keys, keyed by the path to the private key file. |
27 // A value of NULL indicates that the private key is inaccessible or encrypted. | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
28 private readonly Dictionary<string, PrivateKeyFile> privateKeys = new Dictionary<string, PrivateKeyFile>(); |
17 | 29 // IDs of all checks that have been paused due to network unavailability, |
30 // or due to the system being suspended. | |
31 // Not to be confused with checks that have been disabled by the user. | |
4 | 32 private readonly List<int> pausedChecks = new List<int>(); |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
33 private bool running, networkAvailable, suspend; |
17 | 34 // List of check execution tasks that have been started. |
35 // A check task begins by sleeping until the next scheduled execution time, | |
36 // then executes. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
37 private Dictionary<Task<CheckResult>, int> tasks; |
36
10e60b05c7ec
Fix memory leak each time a check was executed.
Brad Greco <brad@bgreco.net>
parents:
35
diff
changeset
|
38 // XML serializer that can handle servers and all check types. |
10e60b05c7ec
Fix memory leak each time a check was executed.
Brad Greco <brad@bgreco.net>
parents:
35
diff
changeset
|
39 private XmlSerializer xmlSerializer; |
4 | 40 private ServerSummaryForm mainForm; |
29
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
41 private WaveOut waveOut = new WaveOut(); |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
42 MediaFoundationReader mediaReader; |
4 | 43 |
17 | 44 /// <summary>Fires when the status of a check changes.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
45 public event EventHandler<CheckStatusChangedEventArgs> CheckStatusChanged; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
46 |
17 | 47 /// <summary>The collection of registered servers.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
48 public List<Server> Servers { get; private set; } = new List<Server>(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
49 |
17 | 50 /// <summary>A collection of all checks belonging to all registerd servers.</summary> |
4 | 51 public IEnumerable<Check> Checks => Servers.SelectMany(s => s.Checks); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
52 |
17 | 53 /// <summary>Path to the file that stores server and check configuration.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
54 public string ConfigFile { get; private set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
55 |
17 | 56 /// <summary>Path to the file that stores server and check configuration.</summary> |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
57 public IEnumerable<string> LockedKeys { get { return privateKeys.Where(kvp => kvp.Value == null).Select(kvp => kvp.Key); } } |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
58 |
17 | 59 /// <summary>ServerMonitor constructor.</summary> |
60 /// <param name="mainForm">A reference to the main form.</param> | |
4 | 61 public ServerMonitor(ServerSummaryForm mainForm) |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
62 { |
4 | 63 this.mainForm = mainForm; |
17 | 64 // Store configuration in %appdata%\ServerMonitor |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
65 configFileDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ServerMonitor"); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
66 ConfigFile = Path.Combine(configFileDir, "servers.xml"); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
67 logger = new Logger(Path.Combine(configFileDir, "monitor.log")); |
36
10e60b05c7ec
Fix memory leak each time a check was executed.
Brad Greco <brad@bgreco.net>
parents:
35
diff
changeset
|
68 xmlSerializer = new XmlSerializer(typeof(List<Server>), Check.CheckTypes); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
69 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
70 |
17 | 71 /// <summary>Registers a new server with the server monitor.</summary> |
72 /// <param name="server">The server to be added.</param> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
73 public void AddServer(Server server) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
74 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
75 Servers.Add(server); |
4 | 76 SaveServers(); |
28
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
77 server.CheckModified += Server_CheckModified; |
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
78 server.EnabledChanged += Server_EnabledChanged; |
4 | 79 } |
80 | |
17 | 81 /// <summary>Deletes a server from the server monitor.</summary> |
82 /// <param name="server">The server to be deleted.</param> | |
4 | 83 public void DeleteServer(Server server) |
84 { | |
85 Servers.Remove(server); | |
28
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
86 // Cancel all queued and executing checks belonging to a |
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
87 // server that was deleted. |
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
88 foreach (Check check in server.Checks) |
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
89 { |
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
90 CancelCheck(check); |
437442cd8090
Fix checks still running after a server is deleted and checks not running immediately after a server is created.
Brad Greco <brad@bgreco.net>
parents:
25
diff
changeset
|
91 } |
4 | 92 SaveServers(); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
93 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
94 |
17 | 95 /// <summary>Loads all servers and checks from the config file.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
96 public void LoadServers() |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
97 { |
17 | 98 bool triedBackup = false; |
99 | |
29
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
100 Read: |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
101 TextReader reader = null; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
102 try |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
103 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
104 reader = new StreamReader(ConfigFile); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
105 Servers.Clear(); |
36
10e60b05c7ec
Fix memory leak each time a check was executed.
Brad Greco <brad@bgreco.net>
parents:
35
diff
changeset
|
106 Servers.AddRange((List<Server>)xmlSerializer.Deserialize(reader)); |
17 | 107 // Do some more set-up now that the servers and checks have been loaded. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
108 foreach (Server server in Servers) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
109 { |
17 | 110 // Read private keys into memory if they are accessible and not encrypted. |
111 // If PrivateKeyFile != null, it means same the key has already been loaded for | |
112 // a different server and nothing more needs to be done. | |
6
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
113 if (server.LoginType == LoginType.PrivateKey && server.PrivateKeyFile == null) |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
114 OpenPrivateKey(server.KeyFile); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
115 foreach (Check check in server.Checks) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
116 { |
17 | 117 // Update the checks so they know what server they belong to. |
118 // Would rather do this in the Server object on deserialization, but | |
119 // that doesn't work when using the XML serializer for some reason. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
120 check.Server = server; |
17 | 121 // If the program last exited while the check was running, change its status |
122 // to the result of its last execution (since, at this point, the check is | |
123 // not running). | |
4 | 124 if (check.Status == CheckStatus.Running) |
125 check.Status = check.LastRunStatus; | |
0
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 server.CheckModified += Server_CheckModified; |
4 | 128 server.EnabledChanged += Server_EnabledChanged; |
0
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 // If the file doesn't exist, no special handling is needed. It will be created later. |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
132 catch (FileNotFoundException) { } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
133 catch (DirectoryNotFoundException) { } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
134 catch (InvalidOperationException) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
135 { |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
136 reader?.Close(); |
17 | 137 // If there was an error parsing the XML, try again with the backup config file. |
138 if (!triedBackup) | |
139 { | |
140 File.Copy(ConfigFile, ConfigFile + ".error", true); | |
141 string backupConfig = ConfigFile + ".bak"; | |
142 if (File.Exists(backupConfig)) | |
143 { | |
144 File.Copy(backupConfig, ConfigFile, true); | |
145 } | |
146 triedBackup = true; | |
147 goto Read; | |
148 } | |
149 else | |
150 { | |
151 // If there was an error reading the backup file too, give up. | |
152 throw; | |
153 } | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
154 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
155 finally |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
156 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
157 reader?.Close(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
158 } |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
159 Application.ApplicationExit += Application_ApplicationExit; |
4 | 160 NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
161 SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; |
17 | 162 |
163 // Remove old entries from the log file according to user preferences. | |
6
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
164 logger.TrimLog(); |
17 | 165 |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
166 Run(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
167 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
168 |
17 | 169 /// <summary>Saves all servers and checks to the config file.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
170 public void SaveServers() |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
171 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
172 GenerateIds(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
173 TextWriter writer = null; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
174 try |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
175 { |
17 | 176 // Make a backup first in case something goes wrong in the middle of writing. |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
177 File.Copy(ConfigFile, ConfigFile + ".bak", true); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
178 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
179 catch { } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
180 try |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
181 { |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
182 writer = new StreamWriter(ConfigFile); |
36
10e60b05c7ec
Fix memory leak each time a check was executed.
Brad Greco <brad@bgreco.net>
parents:
35
diff
changeset
|
183 xmlSerializer.Serialize(writer, Servers); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
184 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
185 catch (DirectoryNotFoundException) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
186 { |
17 | 187 // If the directory does not exist, create it and try again. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
188 Directory.CreateDirectory(configFileDir); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
189 writer = new StreamWriter(ConfigFile); |
36
10e60b05c7ec
Fix memory leak each time a check was executed.
Brad Greco <brad@bgreco.net>
parents:
35
diff
changeset
|
190 xmlSerializer.Serialize(writer, Servers); |
0
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 finally |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
193 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
194 writer?.Close(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
195 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
196 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
197 |
17 | 198 /// <summary>Main server monitor loop. Schedules and executes checks.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
199 private async void Run() |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
200 { |
17 | 201 // Do not run again if already running or if the system is suspending or resuming. |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
202 if (running || suspend) |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
203 return; |
17 | 204 |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
205 running = true; |
17 | 206 |
207 // If the network is available, immediately execute checks that were supposed to run | |
208 // earlier but could not due to network unavailability or the system being suspended. | |
4 | 209 networkAvailable = Helpers.IsNetworkAvailable(); |
210 if (networkAvailable) | |
211 { | |
212 foreach (int id in pausedChecks) | |
213 { | |
214 await ExecuteCheckAsync(Checks.FirstOrDefault(c => c.Id == id)); | |
215 } | |
216 pausedChecks.Clear(); | |
217 } | |
17 | 218 |
219 // Schedule all checks to run according to their schedules. | |
220 // Each check will sleep until it is scheduled to run, then execute. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
221 tasks = Checks.ToDictionary(c => ScheduleExecuteCheckAsync(c), c => c.Id); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
222 while (tasks.Count > 0) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
223 { |
17 | 224 // When any check is done sleeping and executing, remove the completed |
225 // task and queue a new task to schedule it again. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
226 Task<CheckResult> task = await Task.WhenAny(tasks.Keys); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
227 tasks.Remove(task); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
228 try |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
229 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
230 CheckResult result = await task; |
17 | 231 // Do not schedule the task again if it is now disabled. |
232 // Result will be null if a scheduled check was disabled. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
233 if (result != null && result.CheckStatus != CheckStatus.Disabled) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
234 tasks.Add(ScheduleExecuteCheckAsync(result.Check), result.Check.Id); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
235 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
236 catch (OperationCanceledException) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
237 { |
17 | 238 // When a server's state changes to Disabled, any checks that are executing |
239 // are immediately cancelled. Silently catch these expected exceptions. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
240 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
241 } |
17 | 242 // If there are no enabled checks scheduled, exit the main loop. |
243 // It will be restarted when a check or server is enabled. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
244 running = false; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
245 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
246 |
17 | 247 /// <summary>Schedules a check to be run on its schedule.</summary> |
248 /// <param name="check">The check to execute.</param> | |
249 /// <returns>The async check result.</returns> | |
250 private async Task<CheckResult> ScheduleExecuteCheckAsync(Check check) | |
251 { | |
252 // Do not schedule or execute the check if it or its server is disabled. | |
253 if (!check.Enabled || !check.Server.Enabled) | |
254 return await Task.FromResult(new CheckResult(check, CheckStatus.Disabled, null)); | |
255 | |
256 // Create a cancellation token that will be used to cancel the check if it or | |
257 // its server is disabled while it is executing. | |
258 CancellationTokenSource cts = new CancellationTokenSource(); | |
259 tokens[check.Id] = cts; | |
260 | |
261 // Sleep until next time the check is supposed to be executed. | |
262 // Use the LastScheduledRunTime so manual executions by the user do not | |
263 // interfere with the schedule. | |
264 check.NextRunTime = check.Schedule.GetNextTime(check.LastScheduledRunTime); | |
265 int delay = Math.Max(0, (int)(check.NextRunTime - DateTime.Now).TotalMilliseconds); | |
266 await Task.Delay(delay, cts.Token); | |
267 check.LastScheduledRunTime = check.NextRunTime; | |
268 | |
269 // Execute the check if not cancelled. | |
270 if (!cts.IsCancellationRequested) | |
271 { | |
272 // If the network is available, execute the check. | |
273 // Otherwise, add it to the list of paused checks to be executed | |
274 // when the network becomes available again. | |
275 if (networkAvailable) | |
276 { | |
277 return await ExecuteCheckAsync(check, cts.Token); | |
278 } | |
279 else | |
280 { | |
281 if (!pausedChecks.Contains(check.Id)) | |
282 pausedChecks.Add(check.Id); | |
283 } | |
284 } | |
285 return await Task.FromResult(new CheckResult(check, CheckStatus.Disabled, null)); | |
286 } | |
287 | |
288 /// <summary>Executes a check asynchronously.</summary> | |
289 /// <param name="check">The check to execute.</param> | |
290 /// <param name="token">A chancellation token that may be used to cancel the check execution.</param> | |
291 /// <returns>The async check result.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
292 public async Task<CheckResult> ExecuteCheckAsync(Check check, CancellationToken token = default(CancellationToken)) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
293 { |
17 | 294 // Update the status. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
295 check.Status = CheckStatus.Running; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
296 OnCheckStatusChanged(check); |
17 | 297 |
298 // Execute the check. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
299 CheckResult result = await check.ExecuteAsync(token); |
17 | 300 |
301 // Increment the consecutive failure counter on failue, or reset | |
302 // the counter on success. | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
303 if (result.Failed) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
304 check.ConsecutiveFailures++; |
17 | 305 else |
306 check.ConsecutiveFailures = 0; | |
307 | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
308 OnCheckStatusChanged(check, result); |
4 | 309 HandleResultAsync(result); |
310 return result; | |
311 } | |
312 | |
17 | 313 /// <summary>Handles the result of a check execution.</summary> |
314 /// <param name="result">The result.</param> | |
4 | 315 private void HandleResultAsync(CheckResult result) |
316 { | |
17 | 317 // Log the result. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
318 logger.Log(result); |
17 | 319 |
320 // Notify the user of failure according to user preferences. | |
321 // If the check succeeded, result.FailAction will be None. | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
322 if (result.Check.ConsecutiveFailures >= result.Check.MaxConsecutiveFailures) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
323 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
324 if (result.FailAction == FailAction.FlashTaskbar) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
325 mainForm.AlertServerForm(result.Check); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
326 if (result.FailAction.In(FailAction.FlashTaskbar, FailAction.NotificationBalloon)) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
327 mainForm.ShowBalloon(result); |
29
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
328 PlaySound(result.FailSound); |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
329 } |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
330 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
331 |
29
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
332 /// <summary>Plays a sound.</summary> |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
333 /// <param name="sound"> |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
334 /// If null, no sound is played. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
335 /// If string.Empty, the Windows default error sound is played. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
336 /// Otherwise, plays the sound at the given path. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
337 /// </param> |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
338 private void PlaySound(string sound) |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
339 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
340 if (sound == string.Empty) |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
341 SystemSounds.Asterisk.Play(); |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
342 else if (sound != null) |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
343 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
344 try |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
345 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
346 mediaReader = new MediaFoundationReader(sound); |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
347 waveOut.Init(mediaReader); |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
348 waveOut.PlaybackStopped += WaveOut_PlaybackStopped; |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
349 waveOut.Play(); |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
350 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
351 catch |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
352 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
353 // Play the default sound if something went wrong. |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
354 SystemSounds.Asterisk.Play(); |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
355 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
356 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
357 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
358 |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
359 /// <summary>Disposes the media reader after sound playback.</summary> |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
360 private void WaveOut_PlaybackStopped(object sender, StoppedEventArgs e) |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
361 { |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
362 mediaReader.Dispose(); |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
363 } |
f6235dc0a8ec
Add ability to play a sound on check failure.
Brad Greco <brad@bgreco.net>
parents:
28
diff
changeset
|
364 |
17 | 365 /// <summary>Reads all check results from the log for a server.</summary> |
366 /// <param name="server">The server whose check results should be read.</param> | |
367 /// <returns>A list of all check results found in the log file for the given server.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
368 public IList<CheckResult> GetLog(Server server) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
369 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
370 return logger.Read(server); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
371 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
372 |
17 | 373 /// <summary>Saves the check settings and notifies event subscribers when the status of a check changes.</summary> |
374 /// <param name="check">The check whose status has changed.</param> | |
375 /// <param name="result">The check result that caused the status to change, if any.</param> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
376 private void OnCheckStatusChanged(Check check, CheckResult result = null) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
377 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
378 SaveServers(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
379 CheckStatusChanged?.Invoke(check, new CheckStatusChangedEventArgs(check, result)); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
380 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
381 |
17 | 382 /// <summary>Handles user modifications to a check's settings.</summary> |
383 /// <param name="sender">The check that was modified.</param> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
384 private void Server_CheckModified(object sender, EventArgs e) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
385 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
386 Check check = (Check)sender; |
17 | 387 |
388 // No need to mess with the task queue if not currently running. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
389 if (running) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
390 { |
17 | 391 Task<CheckResult> task = tasks.FirstOrDefault(kvp => kvp.Value == check.Id).Key; |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
392 if (task == null) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
393 { |
17 | 394 // No tasks associated with the check, so schedule a new one. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
395 tasks.Add(ScheduleExecuteCheckAsync(check), check.Id); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
396 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
397 else |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
398 { |
17 | 399 // Check was modified or deleted, so remove any waiting tasks. |
4 | 400 CancelCheck(check); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
401 if (check.Server != null) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
402 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
403 // If the check was not deleted, schedule the new check. |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
404 // But only if it's still running, otherwise restarting the monitor below |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
405 // will create a duplicate run. |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
406 if (running) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
407 tasks.Add(ScheduleExecuteCheckAsync(check), check.Id); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
408 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
409 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
410 } |
17 | 411 // Run again in case removing a task above caused it to stop. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
412 Run(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
413 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
414 |
17 | 415 /// <summary>Handles the enabled state of a server changing.</summary> |
416 /// <param name="sender">The server that was enabled or disabled.</param> | |
4 | 417 private void Server_EnabledChanged(object sender, EventArgs e) |
418 { | |
419 Server server = (Server)sender; | |
17 | 420 |
421 // Make sure the monitor is running. If no servers were enabled before this | |
422 // one was enabled, it is not running. | |
4 | 423 if (server.Enabled) |
424 { | |
425 Run(); | |
25
781d8b980be1
Fix checks not getting scheduled when a server is enabled.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
426 // Schedule all checks to run. |
781d8b980be1
Fix checks not getting scheduled when a server is enabled.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
427 foreach (Check check in server.Checks) |
781d8b980be1
Fix checks not getting scheduled when a server is enabled.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
428 { |
781d8b980be1
Fix checks not getting scheduled when a server is enabled.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
429 Server_CheckModified(check, new EventArgs()); |
781d8b980be1
Fix checks not getting scheduled when a server is enabled.
Brad Greco <brad@bgreco.net>
parents:
17
diff
changeset
|
430 } |
4 | 431 } |
432 else | |
433 { | |
17 | 434 // Cancel all queued and executing checks belonging to a |
435 // server that was disabled. | |
4 | 436 foreach (Check check in server.Checks) |
437 { | |
438 CancelCheck(check); | |
439 } | |
440 } | |
441 } | |
442 | |
17 | 443 /// <summary>Cancels a check that may be executing.</summary> |
444 /// <param name="check">The check to cancel.</param> | |
4 | 445 private void CancelCheck(Check check) |
446 { | |
6
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
447 if (tasks == null) |
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
448 return; |
17 | 449 |
450 // Find the waiting or executing task for the check and remove it. | |
4 | 451 Task<CheckResult> task = tasks.FirstOrDefault(kvp => kvp.Value == check.Id).Key; |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
452 if (task != null) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
453 tasks.Remove(task); |
17 | 454 |
455 // Remove it from the list of paused checks so it doesn't get restarted later. | |
4 | 456 pausedChecks.RemoveAll(id => id == check.Id); |
17 | 457 |
458 // Cancel the current execution. | |
4 | 459 if (tokens.TryGetValue(check.Id, out CancellationTokenSource cts)) |
460 cts.Cancel(); | |
461 } | |
462 | |
17 | 463 /// <summary>Handles network state changing.</summary> |
4 | 464 private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e) |
465 { | |
466 networkAvailable = Helpers.IsNetworkAvailable(); | |
17 | 467 // If the network is available, it might not have been before. |
468 // This method is not called from the correct thread, so special | |
469 // handling is needed to start it on the UI thread again. | |
4 | 470 if (networkAvailable) |
471 mainForm.Invoke((MethodInvoker)(() => Run())); | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
472 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
473 |
17 | 474 /// <summary>Handles system power mode changes.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
475 private async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
476 { |
17 | 477 // If the system is being suspended, cancel all waiting and executing checks. |
478 // Once all the checks are removed, the main loop will exit. | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
479 if (e.Mode == PowerModes.Suspend) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
480 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
481 foreach (Check check in Checks) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
482 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
483 CancelCheck(check); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
484 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
485 suspend = true; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
486 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
487 else if (e.Mode == PowerModes.Resume) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
488 { |
17 | 489 // When resuming from suspend, examine each check to find out if it was |
490 // scheduled to be executed during the time period when the systems was | |
491 // suspended. Add them to the paused checks list, to be executed almost | |
492 // immediately. | |
493 // Make sure the list is empty to start. | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
494 pausedChecks.Clear(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
495 foreach (Check check in Checks) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
496 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
497 if (check.Enabled && check.Server.Enabled && check.NextRunTime < DateTime.Now) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
498 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
499 pausedChecks.Add(check.Id); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
500 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
501 } |
35
2ffb0bda7705
Increase wait time after system resume to begin running checks to prevent false positives.
Brad Greco <brad@bgreco.net>
parents:
29
diff
changeset
|
502 // Wait 20 seconds to give things time to quiet down after resuming. |
2ffb0bda7705
Increase wait time after system resume to begin running checks to prevent false positives.
Brad Greco <brad@bgreco.net>
parents:
29
diff
changeset
|
503 await Task.Delay(20000); |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
504 suspend = false; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
505 Run(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
506 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
507 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
508 |
17 | 509 /// <summary>Unregister system events when exiting.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
510 private void Application_ApplicationExit(object sender, EventArgs e) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
511 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
512 NetworkChange.NetworkAddressChanged -= NetworkChange_NetworkAddressChanged; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
513 SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
514 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
515 |
17 | 516 /// <summary>Attempts to read a private file.</summary> |
517 /// <param name="path">The path to the private key file.</param> | |
518 /// <param name="password">The password used to encrypt the key.</param> | |
519 /// <returns>A status indicating the result of the attempt.</returns> | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
520 public KeyStatus OpenPrivateKey(string path, string password = null) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
521 { |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
522 KeyStatus keyStatus; |
17 | 523 |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
524 if (path == null) |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
525 keyStatus = KeyStatus.NotAccessible; |
17 | 526 |
527 // Check if the key has already been open and read. | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
528 if (privateKeys.TryGetValue(path, out PrivateKeyFile key) && key != null) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
529 keyStatus = KeyStatus.Open; |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
530 else |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
531 { |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
532 try |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
533 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
534 key = new PrivateKeyFile(path, password); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
535 keyStatus = KeyStatus.Open; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
536 } |
17 | 537 // If the key is encrypted and the password is empty or incorrect, |
538 // return the NeedPassword status. | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
539 catch (Exception e) when (e is SshPassPhraseNullOrEmptyException || e is InvalidOperationException) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
540 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
541 keyStatus = KeyStatus.NeedPassword; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
542 } |
17 | 543 // For any other failure reason, return the NotAccessible status. |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
544 catch (Exception) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
545 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
546 keyStatus = KeyStatus.NotAccessible; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
547 } |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
548 } |
17 | 549 // A single private key may be used by multiple servers. Update all servers |
550 // that use this private key with the results of the above operations. | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
551 foreach (Server server in Servers) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
552 { |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
553 if (server.KeyFile == path) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
554 { |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
555 server.PrivateKeyFile = key; |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
556 server.KeyStatus = keyStatus; |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
557 } |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
558 } |
17 | 559 // Keep a reference to this private key so we don't have to re-open |
560 // it later if the same key is used on a different server. | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
561 privateKeys[path] = key; |
17 | 562 |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
563 return keyStatus; |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
564 } |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
565 |
17 | 566 /// <summary>Generates internal IDs for servers and checks.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
567 private void GenerateIds() |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
568 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
569 if (Servers.Any()) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
570 { |
17 | 571 // Start at the maximum ID to make sure IDs are not reused |
572 // if a server was deleted so old log entries do not get associated | |
573 // with a new server. | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
574 int id = Servers.Max(s => s.Id); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
575 foreach (Server server in Servers) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
576 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
577 if (server.Id == 0) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
578 server.Id = ++id; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
579 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
580 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
581 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
582 if (Checks.Any()) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
583 { |
17 | 584 // Start with the max check ID, same reasons as above. |
585 // Is there a reason this is stored in a setting? | |
4 | 586 int id = Math.Max(Settings.Default.MaxCheckId, Checks.Max(c => c.Id)); |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
587 foreach (Check check in Checks) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
588 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
589 if (check.Id == 0) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
590 check.Id = ++id; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
591 } |
4 | 592 Settings.Default.MaxCheckId = id; |
593 Settings.Default.Save(); | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
594 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
595 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
596 |
17 | 597 /// <summary>Creates an XML serializer that can handle servers and all check types.</summary> |
598 /// <returns>An XML serializer that can handle servers and all check types.</returns> | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
599 private XmlSerializer CreateXmlSerializer() |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
600 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
601 return new XmlSerializer(typeof(List<Server>), Check.CheckTypes); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
602 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
603 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
604 |
17 | 605 /// <summary>Event arguments for when a check status changes.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
606 public class CheckStatusChangedEventArgs : EventArgs |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
607 { |
17 | 608 /// <summary>The check whose status changed.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
609 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
|
610 |
17 | 611 /// <summary>The check result that caused the status to change, if any.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
612 public CheckResult CheckResult { get; private set; } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
613 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
614 public CheckStatusChangedEventArgs(Check check, CheckResult result) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
615 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
616 Check = check; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
617 CheckResult = result; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
618 } |
4 | 619 } |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
620 |
17 | 621 /// <summary>Possible actions that may be taken when a check fails.</summary> |
622 public enum FailAction | |
623 { | |
624 /// <summary>Flashes the Server Monitor tasbar program icon.</summary> | |
625 FlashTaskbar = 0, | |
626 /// <summary>Shows a balloon in the notification area.</summary> | |
627 NotificationBalloon = 1, | |
628 /// <summary>Take no action.</summary> | |
629 None = 10 | |
630 } | |
631 | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
632 } |