-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
417 lines (335 loc) · 16.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using Microsoft.Extensions.Logging;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.Net.NetworkInformation;
public partial class Program
{
const string DEFAULT_PROCESS = "OpenVPNConnect";
const int DEFAULT_INTERVAL = 15;
const int DEFAULT_FAILURES = 5;
const int DEFAULT_TIMEOUTSECS = 3;
const int DEFAULT_AUXSKIPPERIODS = 3;
const int RESTART_DELAYSECS = 10;
static readonly List<string> DEFAULT_DESTINATIONS = new List<string>()
{
"1.0.0.1",
"8.8.4.4",
"www.bbc.co.uk",
};
static readonly string HEADER_ART = @" _____ _ __ __ _ _ " + Environment.NewLine +
@"| __ \ | | \ \ / / | | | | " + Environment.NewLine +
@"| |__) |___ _ __ ___ __ _| |_ ___ _ _\ \ /\ / /_ _| |_ ___| |__ ___ _ __ " + Environment.NewLine +
@"| _ // _ \ '_ \ / _ \/ _` | __/ _ \ '__\ \/ \/ / _` | __/ __| '_ \ / _ \ '__|" + Environment.NewLine +
@"| | \ \ __/ |_) | __/ (_| | || __/ | \ /\ / (_| | || (__| | | | __/ | " + Environment.NewLine +
@"|_| \_\___| .__/ \___|\__,_|\__\___|_| \/ \/ \__,_|\__\___|_| |_|\___|_| " + Environment.NewLine +
@" | | " + Environment.NewLine +
@" |_| ";
internal static ILogger? _logger = null;
internal static int _consecutiveFailures = 0;
internal static long _intervalsPassed = 0;
internal static int _intervalsSinceLastAuxRun = 0;
internal static async Task<int> Main(string[] args)
{
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("NonHostConsoleApp.Program", LogLevel.Debug)
.AddSimpleConsole(options =>
{
options.SingleLine = true;
options.TimestampFormat = "MM/dd/yyyy hh:mm:ss ";
})
.AddFile("RepeaterWatcher.log", fileLoggerOptions =>
{
fileLoggerOptions.MaxRollingFiles = 7;
fileLoggerOptions.FileSizeLimitBytes = 10485760; // 10 MB
});
});
_logger = loggerFactory.CreateLogger(Environment.MachineName);
int returnCode = 0;
Console.WriteLine(HEADER_ART);
_logger?.LogInformation("RepeaterWatcher by M0XDR");
RootCommand cmd = new RootCommand("Repeater watcher performs ICMP ping commands at set intervals and kills and restarts a process if failures reach a specified threshold.");
Option<string> processOption = new Option<string>(new[] { "-p", "--process" },
() => DEFAULT_PROCESS,
"The process to kill and restart.");
Option<IEnumerable<string>> destinationOption = new Option<IEnumerable<string>>(new[] { "-d", "--destinations" },
() => DEFAULT_DESTINATIONS,
"One or more destination IP or addresses to ping.") { AllowMultipleArgumentsPerToken = true };
Option<int> intervalOption = new Option<int>(new[] { "-i", "--interval" },
() => DEFAULT_INTERVAL,
"The number of seconds between tests.");
Option<int> failuresOption = new Option<int>(new[] { "-f", "--failures" },
() => DEFAULT_FAILURES,
"The number consecutive failures before restarting.");
Option<int> timeoutOption = new Option<int>(new[] { "-t", "--timeout" },
() => DEFAULT_TIMEOUTSECS,
"The ping timeout in seconds.");
Option<FileInfo> auxInfoOption = new Option<FileInfo>(new[] { "-a", "--aux" }, "The path to an auxillary process to run each period.");
Option<int> auxSkipPeriods = new Option<int>(new[] { "-s", "--skip" },
() => DEFAULT_AUXSKIPPERIODS,
"The number of intervals to skip before running the aux process.");
Option<string> auxArguments = new Option<string>(new[] { "-x", "--auxargs" },
"An argument string for the auxillary process.");
Argument<IEnumerable<string>> argumentsOption = new Argument<IEnumerable<string>>("restartArguments", "The arguments which will be passed to the restarted process.")
{
Arity = ArgumentArity.OneOrMore
};
cmd.AddArgument(argumentsOption);
cmd.AddOption(processOption);
cmd.AddOption(destinationOption);
cmd.AddOption(intervalOption);
cmd.AddOption(failuresOption);
cmd.AddOption(timeoutOption);
cmd.AddOption(auxInfoOption);
cmd.AddOption(auxSkipPeriods);
cmd.AddOption(auxArguments);
cmd.SetHandler(async (IEnumerable<string> restartArguments, string process, IEnumerable<string> destinations, int interval, int failures, int timeout, FileInfo auxInfo, int auxSkipPeriods, string auxArguments, CancellationToken token) =>
{
returnCode = await HandleCommandLineAsync(restartArguments, process, destinations, interval, failures, timeout, auxInfo, auxSkipPeriods, auxArguments, token);
}, argumentsOption, processOption, destinationOption, intervalOption, failuresOption, timeoutOption, auxInfoOption, auxSkipPeriods, auxArguments);
return await cmd.InvokeAsync(args);
}
private static async Task<int> HandleCommandLineAsync(
IEnumerable<string> restartArguments,
string process,
IEnumerable<string> destinations,
int interval,
int failures,
int timeout,
FileInfo auxInfo,
int auxSkipPeriods,
string auxArguments,
CancellationToken token)
{
List<string> testAddresses = destinations.ToList();
AuxInfo aux = new AuxInfo()
{
ProcessFileInfo = auxInfo,
SkipPeriods = auxSkipPeriods,
Arguments = auxArguments
};
_consecutiveFailures = 0;
while (!token.IsCancellationRequested)
{
try
{
await TestLoopAsync(restartArguments, process, interval, timeout, failures, testAddresses, aux, token);
}
catch (TaskCanceledException)
{
_logger?.LogError("Test loop has been cancelled.");
return 1;
}
catch (Exception ex)
{
_logger?.LogError("An exception {exception} occured. Sleeping for 30 seconds.", ex);
await Task.Delay(TimeSpan.FromSeconds(30), token);
}
_intervalsPassed++;
}
return 0;
}
/// <summary>
/// Perform a test and then any remedial action
/// </summary>
private static async Task TestLoopAsync(IEnumerable<string> restartArguments, string process, int interval, int timeout, int maxFailures, List<string> testAddresses, AuxInfo aux, CancellationToken token)
{
(int SuccessCount, int FailCount) pingResults = await PerformPingChecksAsync(timeout, testAddresses, token);
if (pingResults.SuccessCount == 0)
{
// We increment failures only if NONE of the tests succeeded
_consecutiveFailures++;
_logger?.LogWarning("Consecutive failures now stands at {failures}", _consecutiveFailures);
if (_consecutiveFailures >= maxFailures)
{
// We need to take some action as the maximum number of failures has been reached.
_consecutiveFailures = 0;
_logger?.LogError("Consecutive failures limit {maxFailures} has been reached.", maxFailures);
if (await RestartProcessAsync(process, restartArguments))
{
_logger?.LogInformation($"Giving process time to start up before monitoring is resumed...");
await Task.Delay(TimeSpan.FromSeconds(RESTART_DELAYSECS));
}
}
}
else
{
if (_consecutiveFailures > 0)
{
_logger?.LogInformation("At least one ping test succeeded. Resetting consecutive failure count.");
_consecutiveFailures = 0;
}
}
_logger?.LogInformation($"Sleeping for {interval} {(interval == 1 ? "second" : "seconds")}", interval);
// Deal with aux process
if (aux.ProcessFileInfo?.Exists == true)
{
if (_intervalsPassed % aux.SkipPeriods == 0)
{
_intervalsSinceLastAuxRun = 0;
try
{
_logger?.LogInformation("Auxilary process {auxillaryProcess} found and running this period.", aux.ProcessFileInfo.Name);
ProcessStartInfo auxProcStartInfo = new ProcessStartInfo(aux.ProcessFileInfo.FullName, aux?.Arguments ?? string.Empty);
Process parent = Process.GetCurrentProcess();
ProcessPriorityClass original = parent.PriorityClass;
try
{
// Start using a lower priority
parent.PriorityClass = ProcessPriorityClass.BelowNormal;
Process? auxProcess = Process.Start(auxProcStartInfo);
if (auxProcess != null)
{
_logger?.LogInformation("Auxilary started with ID {processId}", auxProcess.Id);
}
else
{
_logger?.LogError("Auxilary process at could not be started", aux?.ProcessFileInfo.Name ?? string.Empty);
}
}
finally
{
parent.PriorityClass = original;
}
}
catch (Exception ex)
{
_logger?.LogError("Auxilary process at {processFileInfo} could not be started. Exception {exception}", aux.ProcessFileInfo.Name, ex);
}
}
else
{
_intervalsSinceLastAuxRun++;
_logger?.LogInformation($"Running auxilary process in {aux.SkipPeriods - _intervalsSinceLastAuxRun} {(interval == 1 ? "interval" : "intervals")}.");
}
}
else
{
_logger?.LogWarning("Auxilary process specified {auxillaryProcess} can not be found", aux?.ProcessFileInfo?.FullName ?? "<unknown>");
}
await Task.Delay(interval * 1000, token);
}
/// <summary>
/// Restart a process
/// </summary>
/// <param name="process">process name e.g. openvpnconnect</param>
/// <param name="restartArguments">args to pass to process</param>
private static async Task<bool> RestartProcessAsync(string process, IEnumerable<string> restartArguments)
{
bool forceStart = false;
string? imagePath = null;
// Check if this is a fully qualified path and if it is, we will ensure the process is running
if (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(process)))
{
// We need to extract the image name from the full path as a directory name
// was resolved from the process variable, rather than just an image name.
imagePath = process;
process = Path.GetFileNameWithoutExtension(process);
forceStart = true;
}
Process[] managedProcesses = Process.GetProcessesByName(process);
if (managedProcesses.Length > 0)
{
_logger?.LogWarning($"There is {managedProcesses.Length} matching {(managedProcesses.Length == 1 ? "process" : "processes")} found", managedProcesses.Length);
// Find most likely image path
imagePath = managedProcesses.Select(p => p.MainModule?.FileName)
.GroupBy(p => p)
.OrderByDescending(group => group.Count())
.FirstOrDefault()?.Key;
if (string.IsNullOrWhiteSpace(imagePath))
{
_logger?.LogError("No image path found for processes matching name {process}. The process will not be killed or restarted.", process);
return false;
}
foreach (Process candidate in managedProcesses)
{
_logger?.LogWarning("Performing kill of process {candidate}", candidate.Id);
candidate.Kill();
}
_logger?.LogInformation("Restarting process at {imagePath} in {restartDelay} seconds", imagePath, RESTART_DELAYSECS);
await Task.Delay(TimeSpan.FromSeconds(RESTART_DELAYSECS));
return RunProcess(imagePath, restartArguments);
}
else
{
if (forceStart && !string.IsNullOrWhiteSpace(imagePath))
{
_logger?.LogWarning("No processes matching name {name} found. We will try and start it now by executing {imagePath}", process, imagePath);
return RunProcess(imagePath, restartArguments);
}
_logger?.LogWarning("No processes matching name {name} found. No processes will be closed or killed", process);
}
return true;
}
/// <summary>
/// Run a process
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
private static bool RunProcess(string imagePath, IEnumerable<string> restartArguments)
{
string commandLineArgs = string.Join(" ", restartArguments.Select(arg => arg));
try
{
Process? startedProcess = Process.Start(new ProcessStartInfo(imagePath, commandLineArgs));
if (startedProcess != null)
{
_logger?.LogInformation("Started process {imagePath} successfully. Process ID is {processId}", imagePath, startedProcess?.Id);
return true;
}
}
catch (Exception exception)
{
_logger?.LogError("Process could not be started using executable at {imagePath}. An exception occured {exception}", imagePath, exception);
return false;
}
_logger?.LogError("Process could not be started using executable at {imagePath}", imagePath);
return false;
}
/// <summary>
/// Ping a number of hosts and return the results
/// </summary>
private static async Task<(int SuccessCount, int FailCount)> PerformPingChecksAsync(int timeout, List<string> testAddresses, CancellationToken token)
{
_logger?.LogInformation($"Performing ping checks to {testAddresses.Count} {(testAddresses.Count == 1 ? "host" : "hosts")}...");
List<Task<PingReply>> pingChecks = new List<Task<PingReply>>();
foreach (string testAddress in testAddresses)
{
Task<PingReply> pingTest = Task.Run(async () =>
{
Ping pingSender = new Ping();
PingReply pingReply = await pingSender.SendPingAsync(testAddress, timeout * 1000);
_logger?.LogInformation($"{testAddress} - {pingReply.Status}{(pingReply.Status == IPStatus.Success ? " - Round Trip: " + pingReply.RoundtripTime + "ms" : string.Empty)}");
return pingReply;
}, token);
pingChecks.Add(pingTest);
}
PingReply[] pingResults = await Task.WhenAll(pingChecks);
int successCount = pingResults.Count(result => result.Status == IPStatus.Success);
int failCount = pingResults.Length - successCount;
if (failCount > 0 && successCount > 0)
{
_logger?.LogWarning($"{successCount} {(successCount == 1 ? "ping" : "pings")} were successful, but {failCount} {(failCount == 1 ? "ping" : "pings")} failed", successCount, failCount);
}
else if (failCount > 0 && successCount == 0)
{
_logger?.LogError($"{successCount} {(successCount == 1 ? "ping" : "pings")} were successful, but {failCount} {(failCount == 1 ? "ping" : "pings")} failed", successCount, failCount);
}
else
{
_logger?.LogInformation($"{successCount} {(successCount == 1 ? "ping" : "pings")} were successful", successCount);
}
return (successCount, failCount);
}
internal class AuxInfo
{
public FileInfo? ProcessFileInfo { get; set; }
public string? Arguments { get; set; }
public int SkipPeriods { get; set; }
}
}