-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWinService_With_Restart_Service_Wait_For_Alert.xml
402 lines (380 loc) · 15.7 KB
/
WinService_With_Restart_Service_Wait_For_Alert.xml
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
<?xml version="1.0" encoding="UTF-8" ?>
<feed version="1.0" hasPendingRequests="false" >
<company></company>
<status>200</status>
<errmsg>OK</errmsg>
<interval>0</interval>
<entry type="predatasource">
<version>1578964490</version>
<name>WinService_With_Restart_Service</name>
<displayedas>Services RS</displayedas>
<description>Monitors state of Windows Services. Auto-restarts if service is detected down.</description>
<collector>wmi</collector>
<hasMultiInstances>true</hasMultiInstances>
<schedule>120</schedule>
<appliesTo>system.displayname == "se-la3-manage1"</appliesTo>
<wildcardauto>false</wildcardauto>
<wildcardpersist>false</wildcardpersist>
<wildcardlinuxscript></wildcardlinuxscript>
<wildcardlinuxcmdline></wildcardlinuxcmdline>
<wildcardwinscript></wildcardwinscript>
<wildcardwincmdline></wildcardwincmdline>
<wildcardgroovyscript></wildcardgroovyscript>
<wildcardschedule>1440</wildcardschedule>
<wildcarddisable>false</wildcarddisable>
<wildcarddeleteinactive>false</wildcarddeleteinactive>
<agdmethod>none</agdmethod>
<agdparams></agdparams>
<group></group>
<tags>os,windows</tags>
<technology>Collector service-account or wmi.user needs permissions to "start" services.</technology>
<adlist><![CDATA[{"agdmethod":"none","agdparams":"","id":0,"filters":[],"params":{}}]]></adlist>
<schemaVersion>2</schemaVersion>
<dataSourceType>1</dataSourceType>
<attributes>
<attribute>
<name>ip</name>
<value>dns</value>
<comment></comment>
</attribute>
<attribute>
<name>wmiNamespace</name>
<value>CIMV2</value>
<comment></comment>
</attribute>
<attribute>
<name>type</name>
<value>query</value>
<comment></comment>
</attribute>
<attribute>
<name>queryClass</name>
<value>Win32_Service</value>
<comment></comment>
</attribute>
<attribute>
<name>queryIndex</name>
<value>displayname</value>
<comment></comment>
</attribute>
<attribute>
<name>queryValue</name>
<value>##WILDVALUE##</value>
<comment></comment>
</attribute>
<attribute>
<name>methodName</name>
<value></value>
<comment></comment>
</attribute>
<attribute>
<name>targetPath</name>
<value></value>
<comment></comment>
</attribute>
<attribute>
<name>methodInputs</name>
<value></value>
<comment></comment>
</attribute>
</attributes>
<datapoints>
<datapoint>
<name>State</name>
<dataType>1</dataType>
<type>2</type>
<postprocessormethod>textmatch</postprocessormethod>
<postprocessorparam>Running</postprocessorparam>
<usevalue></usevalue>
<alertexpr>!= 1</alertexpr>
<alertmissing>1</alertmissing>
<alertsubject></alertsubject>
<alertbody>The Windows service ##INSTANCE## ##DSIDESCRIPTION## on ##HOST## is not running.
This started at ##START## - or ##DURATION## ago.</alertbody>
<enableanomalyalertsuppression></enableanomalyalertsuppression>
<description></description>
<maxvalue></maxvalue>
<minvalue>0</minvalue>
<userparam1>State</userparam1>
<userparam2></userparam2>
<userparam3></userparam3>
<iscomposite>false</iscomposite>
<rpn></rpn>
<alertTransitionIval>0</alertTransitionIval>
<alertClearTransitionIval>0</alertClearTransitionIval>
</datapoint>
<datapoint>
<name>Status</name>
<dataType>1</dataType>
<type>5</type>
<postprocessormethod>textmatch</postprocessormethod>
<postprocessorparam>OK</postprocessorparam>
<usevalue></usevalue>
<alertexpr></alertexpr>
<alertmissing>1</alertmissing>
<alertsubject></alertsubject>
<alertbody></alertbody>
<enableanomalyalertsuppression></enableanomalyalertsuppression>
<description></description>
<maxvalue></maxvalue>
<minvalue>0</minvalue>
<userparam1>Status</userparam1>
<userparam2></userparam2>
<userparam3></userparam3>
<iscomposite>false</iscomposite>
<rpn></rpn>
<alertTransitionIval>3</alertTransitionIval>
<alertClearTransitionIval>0</alertClearTransitionIval>
</datapoint>
<datapoint>
<name>Restart_Service</name>
<dataType>7</dataType>
<type>2</type>
<postprocessormethod>groovy</postprocessormethod>
<postprocessorparam>import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import com.santaba.agent.groovyapi.http.*;
import groovy.json.JsonSlurper;
deviceId = hostProps.get("system.deviceId")
instanceId = instanceProps.get("system.instanceId")
hostname = hostProps.get("system.ips").split(",")[0]
status = output["STATUS"]
state = output["STATE"]
service_name = output["NAME"]
dataPointName = 'State' //IN THIS EXAMPLE WE'RE FILTERING OUT ALERTS SEARCH TO THE SPECIFIC DATAPOINT. THIS CAN BE REMOVED, OR CHANGED IF NEED BE.
String[] start_service = ["cmd.exe", "/c", "sc", "\\\\${hostname}", "start", "${service_name}"];
String[] check_status = ["cmd.exe", "/c", "sc", "\\\\${hostname}", "query", "${service_name}"];
current_alert = lm_get_request("/device/devices/${deviceId}/alerts","?filter=instanceId:${instanceId},dataPointName:${dataPointName}") //Is there a current alert?
if( (state=="Running" && status == "OK") || current_alert.data.total == 0) {
return 0 //Service is running properly OR we haven't triggered an alert yet
} else if( current_alert.data.total != 0) { //An alert has triggered, let's trigger our automation
state = process_builder(check_status)
if ( !state.contains("RUNNING") ) {
service_restart = process_builder(start_service) //Let's restart the service
sleep(5000) //Let's give the service some time to start up
recheck_state = process_builder(check_status) //We recheck the service-status to make sure the remediation worked
if( recheck_state.contains("RUNNING")) {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("taskkill /F /IM cmd.exe"); //Let's quit the cmd.exe for this operation
return 2 //"Remediation executed successfully"
} else {
return 3 //"Remediation execution failed"
}
}
}
def process_builder(command) {
Process process = new ProcessBuilder(command).start();
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String output = bufferedReader.readLines();
return output
}
def lm_get_request(resource_path, query_params) {
accessId = hostProps.get('lmaccessid.key'); //This is your LM API Access ID
accessKey = hostProps.get('lmaccesskey.key'); //This is your LM API Access Key
account = '';
File file = new File('../conf/agent.conf');
file.text.eachLine
{
if (it =~ /company=/)
{
account = it.split('=')[1].trim();
}
}
url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" ;
httpClient = HTTP.open(account + ".logicmonitor.com", 443);
//Build the resource-path URL to retrieve stats from the "root" group of the device-tree
resourcePath = resource_path
queryParams = query_params
headers=calcSignature('GET',resourcePath,accessKey,accessId); // calcSignature defined below
response = httpClient.get(url + resourcePath + queryParams,headers)
responseBody = httpClient.getResponseBody();
allResponse = new JsonSlurper().parseText(responseBody);
responseCode = httpClient.getStatusCode();
if (responseCode == 200) {
return allResponse
} else {
return responseCode
};
}
def lm_post_request(resource_path, query_params, data) {
accessId = hostProps.get('lmaccessid.key'); //This is your LM API Access ID
accessKey = hostProps.get('lmaccesskey.key'); //This is your LM API Access Key
account = '';
File file = new File('../conf/agent.conf');
file.text.eachLine
{
if (it =~ /company=/)
{
account = it.split('=')[1].trim();
}
}
url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" ;
httpClient = HTTP.open(account + ".logicmonitor.com", 443);
//Build the resource-path URL to retrieve stats from the "root" group of the device-tree
resourcePath = resource_path
queryParams = query_params
data = data
headers=calcSignature('POST',resourcePath,accessKey,accessId,data); // calcSignature defined below
response = httpClient.post(url + resourcePath + queryParams,data,headers)
responseBody = httpClient.getResponseBody();
allResponse = new JsonSlurper().parseText(responseBody);
responseCode = httpClient.getStatusCode();
if (responseCode == 200) {
return allResponse
} else {
return responseCode
};
}
def calcSignature(verb,resourcePath,accessKey,accessId,data='') {
epoch = System.currentTimeMillis();
requestVars = verb + epoch + data + resourcePath;
hmac = Mac.getInstance("HmacSHA256");
secret = new SecretKeySpec(accessKey.getBytes(), "HmacSHA256");
hmac.init(secret);
hmac_signed = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes()));
signature = hmac_signed.bytes.encodeBase64();
headers = [:]
headers.put("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch)
return headers;
}</postprocessorparam>
<usevalue></usevalue>
<alertexpr></alertexpr>
<alertmissing>1</alertmissing>
<alertsubject></alertsubject>
<alertbody></alertbody>
<enableanomalyalertsuppression></enableanomalyalertsuppression>
<description>1=Running, 2=Restarted, 3=Restart-Failed</description>
<maxvalue></maxvalue>
<minvalue></minvalue>
<userparam1></userparam1>
<userparam2></userparam2>
<userparam3></userparam3>
<iscomposite>false</iscomposite>
<rpn></rpn>
<alertTransitionIval>0</alertTransitionIval>
<alertClearTransitionIval>0</alertClearTransitionIval>
</datapoint>
<datapoint>
<name>RunningStatus</name>
<dataType>1</dataType>
<type>2</type>
<postprocessormethod>expression</postprocessormethod>
<postprocessorparam>if(eq(State,1),Status,1)</postprocessorparam>
<usevalue></usevalue>
<alertexpr>!= 1</alertexpr>
<alertmissing>1</alertmissing>
<alertsubject></alertsubject>
<alertbody>The windows service ##INSTANCE## ##DSIDESCRIPTION## on ##HOST## is running, but not in the OK state.
It may be in error, degraded or starting/stoppingstate.
This started at ##START## - or ##DURATION## ago.</alertbody>
<enableanomalyalertsuppression></enableanomalyalertsuppression>
<description>If service is running, alert if Status is not OK.</description>
<maxvalue></maxvalue>
<minvalue></minvalue>
<userparam1>RunningStatus</userparam1>
<userparam2></userparam2>
<userparam3></userparam3>
<iscomposite>false</iscomposite>
<rpn></rpn>
<alertTransitionIval>3</alertTransitionIval>
<alertClearTransitionIval>0</alertClearTransitionIval>
</datapoint>
</datapoints>
<graphs>
<graph>
<name>Restart Status</name>
<title>Restart Status</title>
<verticallabel>1=Running, 2=Restarted, 3=Failed Restart</verticallabel>
<rigid>false</rigid>
<maxvalue>NaN</maxvalue>
<minvalue>0.0</minvalue>
<displayprio>1</displayprio>
<timescale>1hour</timescale>
<base1024>false</base1024>
<graphdatapoints>
<graphdatapoint>
<name>Restart_Service</name>
<datapointname>Restart_Service</datapointname>
<cf>1</cf>
</graphdatapoint>
</graphdatapoints>
<graphvirtualdatapoints>
</graphvirtualdatapoints>
<graphdatas>
<graphdata>
<type>1</type>
<legend>Restart Service</legend>
<color>silver</color>
<datapointname>Restart_Service</datapointname>
<isvirtualdatapoint>false</isvirtualdatapoint>
</graphdata>
</graphdatas>
</graph>
<graph>
<name>State</name>
<title>State</title>
<verticallabel>State</verticallabel>
<rigid>true</rigid>
<maxvalue>5.0</maxvalue>
<minvalue>0.0</minvalue>
<displayprio>1</displayprio>
<timescale>1hour</timescale>
<base1024>false</base1024>
<graphdatapoints>
<graphdatapoint>
<name>State</name>
<datapointname>State</datapointname>
<cf>1</cf>
</graphdatapoint>
</graphdatapoints>
<graphvirtualdatapoints>
</graphvirtualdatapoints>
<graphdatas>
<graphdata>
<type>1</type>
<legend>State 1 = Running</legend>
<color>blue</color>
<datapointname>State</datapointname>
<isvirtualdatapoint>false</isvirtualdatapoint>
</graphdata>
</graphdatas>
</graph>
<graph>
<name>Status</name>
<title>Status</title>
<verticallabel>Status</verticallabel>
<rigid>true</rigid>
<maxvalue>5.0</maxvalue>
<minvalue>0.0</minvalue>
<displayprio>1</displayprio>
<timescale>1hour</timescale>
<base1024>false</base1024>
<graphdatapoints>
<graphdatapoint>
<name>Status</name>
<datapointname>Status</datapointname>
<cf>1</cf>
</graphdatapoint>
</graphdatapoints>
<graphvirtualdatapoints>
</graphvirtualdatapoints>
<graphdatas>
<graphdata>
<type>1</type>
<legend>Status 1 = OK</legend>
<color>blue</color>
<datapointname>Status</datapointname>
<isvirtualdatapoint>false</isvirtualdatapoint>
</graphdata>
</graphdatas>
</graph>
</graphs>
<overviewgraphs>
</overviewgraphs>
<scripts>
</scripts>
</entry>
</feed>