-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppConfiguration.cs
164 lines (150 loc) · 6.99 KB
/
AppConfiguration.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.ServiceProcess;
using System.Xml;
namespace ServiceMonitor
{
public static class AppConfiguration
{
public static List<Service> GetServiceListConfiguration()
{
List<Service> output = new List<Service>();
XmlNode serviceMonitorConfig = GetConfigXML();
foreach (XmlNode service in serviceMonitorConfig["Services"].SelectNodes("Service"))
{
try
{
if (service.Attributes["Name"] != null)
{
Service currentService = new Service { Name = service.Attributes["Name"].Value };
currentService.serviceController = new ServiceController(currentService.Name);
try
{
currentService.serviceController.Status.ToString();//This will throw an exception if the service does not exist
}
catch (InvalidOperationException ex)
{
if (ex.InnerException.Message == "The specified service does not exist as an installed service")
{
Messages.LogMessage($"The specified service {currentService.Name} does not exist as an installed service", EventLogEntryType.Warning, EventID.ServiceDoesNotExist);
break;
}
else
{
throw ex;
}
}
Messages.LogMessage($"Found service {currentService.Name}, retrieving configuration");
if (service["MemoryLimitMB"] != null)
{
if (int.TryParse(service["MemoryLimitMB"].FirstChild.Value, out int memoryLimitMB))
{
currentService.MemoryLimitMB = memoryLimitMB;
}
}
if (service["AutoRestartTime"] != null)
{
currentService.AutoRestartTime = service["AutoRestartTime"].FirstChild.Value;
}
if (service["AutoRestartFrequency"] != null)
{
switch (service["AutoRestartFrequency"].FirstChild.Value)
{
case "Hourly":
currentService.AutoRestartFrequency = Service.Frequency.Hourly;
break;
case "Daily":
currentService.AutoRestartFrequency = Service.Frequency.Daily;
break;
case "Weekly":
currentService.AutoRestartFrequency = Service.Frequency.Weekly;
break;
case "Monthly":
currentService.AutoRestartFrequency = Service.Frequency.Monthly;
break;
default:
currentService.AutoRestartFrequency = Service.Frequency.Never;
break;
}
}
if (service["ClearMSMQ"] != null)
{
currentService.ClearMSMQ = bool.TryParse(service["ClearMSMQ"].FirstChild.Value, out bool result);
}
currentService.Process = GetServicePath(currentService.Name);
if (currentService.Process == null)
{
throw new Exception($"Unable to locate path to executable for service {currentService.Name}");
}
output.Add(currentService);
}
}
catch (Exception ex)
{
Messages.LogMessage(ex.Message, EventLogEntryType.Error, EventID.Generic);
}
}
return output;
}
private static XmlNode GetConfigXML()
{
try
{
XmlDocument config = new XmlDocument();
string applicationName = Process.GetCurrentProcess().ProcessName;
Messages.LogMessage($"Loading configuration file.");
config.Load($"{applicationName}.config");
XmlNode serviceMonitorConfig = config["ServiceMonitor"];
return serviceMonitorConfig;
}
catch (Exception ex)
{
Messages.LogMessage(ex.Message, EventLogEntryType.Error, EventID.FailedToLoadConfiguration);
throw ex;
}
}
private static string GetServicePath(string serviceName)
{
WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName));
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery);
ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
foreach (ManagementObject managementObject in managementObjectCollection)
{
string[] exeSeparator = { ".exe" };
return managementObject.GetPropertyValue("PathName").ToString().Split('\\').Last().Split(exeSeparator, StringSplitOptions.RemoveEmptyEntries).First();
}
return null;
}
public static SMTPConfig GetSMTPConfiguration()
{
XmlNode serviceMonitorConfig = GetConfigXML();
SMTPConfig output = new SMTPConfig();
foreach (XmlNode smtpConfig in serviceMonitorConfig.SelectNodes("SMTP"))
{
try
{
if (smtpConfig["Server"] != null)
{
output.Server = smtpConfig["Server"].FirstChild.Value;
}
if (smtpConfig["ToEmail"] != null)
{
output.ToEmail = smtpConfig["ToEmail"].FirstChild.Value;
}
if (smtpConfig["FromEmail"] != null)
{
output.FromEmail = smtpConfig["FromEmail"].FirstChild.Value;
}
}
catch (Exception ex)
{
Messages.LogMessage(ex.Message, EventLogEntryType.Error, EventID.SMTPConfigurationFailed);
}
}
return output;
}
}
}