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