/
test_downtimes.py
executable file
·440 lines (418 loc) · 20.6 KB
/
test_downtimes.py
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python
# Copyright (C) 2009-2010:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
#
# This file is used to test host- and service-downtimes.
#
from shinken_test import *
#time.time = original_time_time
#time.sleep = original_time_sleep
class TestDowntime(ShinkenTest):
def test_schedule_fixed_svc_downtime(self):
self.print_header()
# schedule a 2-minute downtime
# downtime must be active
# consume a good result, sleep for a minute
# downtime must be active
# consume a bad result
# downtime must be active
# no notification must be found in broks
duration = 600
now = time.time()
# downtime valid for the next 2 minutes
cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
self.sched.run_external_command(cmd)
svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
svc.checks_in_progress = []
svc.act_depend_of = [] # no hostchecks on critical checkresults
time.sleep(20)
self.scheduler_loop(1, [[svc, 0, 'OK']])
print "downtime was scheduled. check its activity and the comment"
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(svc.downtimes[0].fixed)
self.assert_(svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
self.assert_(len(self.sched.comments) == 1)
self.assert_(len(svc.comments) == 1)
self.assert_(svc.comments[0] in self.sched.comments.values())
self.assert_(svc.downtimes[0].comment_id == svc.comments[0].id)
self.scheduler_loop(1, [[svc, 0, 'OK']])
print "good check was launched, downtime must be active"
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(svc.in_scheduled_downtime)
self.assert_(svc.downtimes[0].fixed)
self.assert_(svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
self.scheduler_loop(1, [[svc, 2, 'BAD']])
print "bad check was launched (SOFT;1), downtime must be active"
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(svc.in_scheduled_downtime)
self.assert_(svc.downtimes[0].fixed)
self.assert_(svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
# now the state changes to hard
self.scheduler_loop(1, [[svc, 2, 'BAD']])
print "bad check was launched (HARD;2), downtime must be active"
print svc.downtimes[0]
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(svc.in_scheduled_downtime)
self.assert_(svc.downtimes[0].fixed)
self.assert_(svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
scheduled_downtime_depth = svc.scheduled_downtime_depth
cmd = "[%lu] DEL_SVC_DOWNTIME;%d" % (now, svc.downtimes[0].id)
self.sched.run_external_command(cmd)
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(not svc.in_scheduled_downtime)
self.assert_(svc.scheduled_downtime_depth < scheduled_downtime_depth)
self.assert_(svc.downtimes[0].fixed)
self.assert_(not svc.downtimes[0].is_in_effect)
self.assert_(svc.downtimes[0].can_be_deleted)
self.assert_(len(self.sched.comments) == 1)
self.assert_(len(svc.comments) == 1)
# now a notification must be sent
self.scheduler_loop(1, [[svc, 2, 'BAD']])
# downtimes must have been deleted now
self.assert_(len(self.sched.downtimes) == 0)
self.assert_(len(svc.downtimes) == 0)
self.assert_(len(self.sched.comments) == 0)
self.assert_(len(svc.comments) == 0)
def test_schedule_flexible_svc_downtime(self):
self.print_header()
#----------------------------------------------------------------
# schedule a flexible downtime of 3 minutes for the host
#----------------------------------------------------------------
duration = 180
now = time.time()
cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;0;0;%d;lausser;blablub" % (now, now, now + duration, duration)
self.sched.run_external_command(cmd)
svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
svc.checks_in_progress = []
svc.act_depend_of = [] # no hostchecks on critical checkresults
time.sleep(20)
#----------------------------------------------------------------
# check if a downtime object exists (scheduler and service)
# check if the downtime is still inactive
#----------------------------------------------------------------
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(not svc.downtimes[0].fixed)
self.assert_(not svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
self.assert_(len(self.sched.comments) == 1)
self.assert_(len(svc.comments) == 1)
self.assert_(svc.comments[0] in self.sched.comments.values())
self.assert_(svc.downtimes[0].comment_id == svc.comments[0].id)
#----------------------------------------------------------------
# run the service and return an OK status
# check if the downtime is still inactive
#----------------------------------------------------------------
self.scheduler_loop(1, [[svc, 0, 'OK']])
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(not svc.in_scheduled_downtime)
self.assert_(not svc.downtimes[0].fixed)
self.assert_(not svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
time.sleep(61)
#----------------------------------------------------------------
# run the service twice to get a soft critical status
# check if the downtime is still inactive
#----------------------------------------------------------------
self.scheduler_loop(1, [[svc, 2, 'BAD']])
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(not svc.in_scheduled_downtime)
self.assert_(not svc.downtimes[0].fixed)
self.assert_(not svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
time.sleep(61)
#----------------------------------------------------------------
# run the service again to get a hard critical status
# check if the downtime is active now
#----------------------------------------------------------------
self.scheduler_loop(1, [[svc, 2, 'BAD']])
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
self.assert_(svc.in_scheduled_downtime)
self.assert_(not svc.downtimes[0].fixed)
self.assert_(svc.downtimes[0].is_in_effect)
self.assert_(not svc.downtimes[0].can_be_deleted)
#----------------------------------------------------------------
# cancel the downtime
# check if the downtime is inactive now and can be deleted
#----------------------------------------------------------------
scheduled_downtime_depth = svc.scheduled_downtime_depth
cmd = "[%lu] DEL_SVC_DOWNTIME;%d" % (now, svc.downtimes[0].id)
self.sched.run_external_command(cmd)
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 1)
self.assert_(not svc.in_scheduled_downtime)
self.assert_(svc.scheduled_downtime_depth < scheduled_downtime_depth)
self.assert_(not svc.downtimes[0].fixed)
self.assert_(not svc.downtimes[0].is_in_effect)
self.assert_(svc.downtimes[0].can_be_deleted)
self.assert_(len(self.sched.comments) == 1)
self.assert_(len(svc.comments) == 1)
time.sleep(61)
#----------------------------------------------------------------
# run the service again with a critical status
# the downtime must have disappeared
# a notification must be sent
#----------------------------------------------------------------
self.scheduler_loop(1, [[svc, 2, 'BAD']])
self.assert_(len(self.sched.downtimes) == 0)
self.assert_(len(svc.downtimes) == 0)
self.assert_(len(self.sched.comments) == 0)
self.assert_(len(svc.comments) == 0)
self.show_logs()
self.show_actions()
def test_schedule_fixed_host_downtime(self):
self.print_header()
# schedule a 2-minute downtime
# downtime must be active
# consume a good result, sleep for a minute
# downtime must be active
# consume a bad result
# downtime must be active
# no notification must be found in broks
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
svc.checks_in_progress = []
svc.act_depend_of = [] # no hostchecks on critical checkresults
print "test_schedule_fixed_host_downtime initialized"
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 0)
self.assert_(self.count_actions() == 0)
#----------------------------------------------------------------
# schedule a downtime of 10 minutes for the host
#----------------------------------------------------------------
duration = 600
now = time.time()
# fixed downtime valid for the next 10 minutes
cmd = "[%lu] SCHEDULE_HOST_DOWNTIME;test_host_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
self.sched.run_external_command(cmd)
self.sched.update_downtimes_and_comments()
print "Launch scheduler loop"
self.scheduler_loop(1, [], do_sleep=False) # push the downtime notification
self.show_actions()
print "Launch worker loop"
#self.worker_loop()
self.show_actions()
print "After both launchs"
time.sleep(20)
#----------------------------------------------------------------
# check if a downtime object exists (scheduler and host)
#----------------------------------------------------------------
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(host.downtimes) == 1)
self.assert_(host.downtimes[0] in self.sched.downtimes.values())
self.assert_(host.downtimes[0].fixed)
self.assert_(host.downtimes[0].is_in_effect)
self.assert_(not host.downtimes[0].can_be_deleted)
self.assert_(len(self.sched.comments) == 1)
self.assert_(len(host.comments) == 1)
self.assert_(host.comments[0] in self.sched.comments.values())
self.assert_(host.downtimes[0].comment_id == host.comments[0].id)
self.show_logs()
self.show_actions()
print "*****************************************************************************************************************************************************************Log matching:", self.get_log_match("STARTED*")
self.assert_(self.count_logs() == 2) # start downt, notif downt
#sys.exit(1)
self.assert_(self.count_actions() == 2) # notif" down
self.clear_logs()
self.clear_actions()
#----------------------------------------------------------------
# send the host to a hard DOWN state
# check log messages, (no) notifications and eventhandlers
#----------------------------------------------------------------
self.scheduler_loop(1, [[host, 2, 'DOWN']])
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 2) # soft1, evt1
self.assert_(self.count_actions() == 1) # evt1
self.clear_logs()
#--
self.scheduler_loop(1, [[host, 2, 'DOWN']])
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 2) # soft2, evt2
self.assert_(self.count_actions() == 1) # evt2
self.clear_logs()
#--
self.scheduler_loop(1, [[host, 2, 'DOWN']])
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 2) # hard3, evt3
self.assert_(self.count_actions() == 2) # evt3, notif"
self.clear_logs()
#--
# we have a notification, but this is blocked. it will stay in
# the actions queue because we have a notification_interval.
# it's called notif" because it is a master notification
print "DBG: host", host.state, host.state_type
self.scheduler_loop(1, [[host, 2, 'DOWN']], do_sleep=True)
print "DBG2: host", host.state, host.state_type
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 0) #
self.assert_(self.count_actions() == 1) # notif"
self.clear_logs()
#----------------------------------------------------------------
# the host comes UP again
# check log messages, (no) notifications and eventhandlers
# a (recovery) notification was created, but has been blocked.
# we see it in the actions as a zombie
#----------------------------------------------------------------
self.scheduler_loop(1, [[host, 0, 'UP']], do_sleep=True)
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 2) # hard3ok, evtok
self.assert_(self.count_actions() == 2) # evtok, notif"
self.clear_logs()
self.clear_actions()
def test_schedule_fixed_host_downtime_with_service(self):
self.print_header()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
svc.checks_in_progress = []
svc.act_depend_of = [] # no hostchecks on critical checkresults
host.notification_interval = 0
svc.notification_interval = 0
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 0)
self.assert_(self.count_actions() == 0)
#----------------------------------------------------------------
# schedule a downtime of 10 minutes for the host
#----------------------------------------------------------------
duration = 600
now = time.time()
cmd = "[%lu] SCHEDULE_HOST_DOWNTIME;test_host_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
self.sched.run_external_command(cmd)
self.sched.update_downtimes_and_comments()
self.scheduler_loop(1, [], do_sleep=False) # push the downtime notification
#self.worker_loop() # push the downtime notification
time.sleep(10)
#----------------------------------------------------------------
# check if a downtime object exists (scheduler and host)
# check the start downtime notification
#----------------------------------------------------------------
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(host.downtimes) == 1)
self.assert_(host.in_scheduled_downtime)
self.assert_(host.downtimes[0] in self.sched.downtimes.values())
self.assert_(host.downtimes[0].fixed)
self.assert_(host.downtimes[0].is_in_effect)
self.assert_(not host.downtimes[0].can_be_deleted)
self.assert_(len(self.sched.comments) == 1)
self.assert_(len(host.comments) == 1)
self.assert_(host.comments[0] in self.sched.comments.values())
self.assert_(host.downtimes[0].comment_id == host.comments[0].id)
self.scheduler_loop(4, [[host, 2, 'DOWN']], do_sleep=True)
self.show_logs()
self.show_actions()
self.assert_(self.count_logs() == 8) # start downt, notif downt, soft1, evt1, soft 2, evt2, hard 3, evt3
self.clear_logs()
self.clear_actions()
#----------------------------------------------------------------
# now the service becomes critical
# check that the host has a downtime, _not_ the service
# check logs, (no) notifications and eventhandlers
#----------------------------------------------------------------
print "now the service goes critical"
self.scheduler_loop(4, [[svc, 2, 'CRITICAL']], do_sleep=True)
self.assert_(len(self.sched.downtimes) == 1)
self.assert_(len(svc.downtimes) == 0)
self.assert_(not svc.in_scheduled_downtime)
self.assert_(svc.host.in_scheduled_downtime)
self.show_logs()
self.show_actions()
# soft 1, evt1, hard 2, evt2
self.assert_(self.count_logs() == 4)
self.clear_logs()
self.clear_actions()
#----------------------------------------------------------------
# the host comes UP again
# check log messages, (no) notifications and eventhandlers
#----------------------------------------------------------------
print "now the host comes up"
self.scheduler_loop(2, [[host, 0, 'UP']], do_sleep=True)
self.show_logs()
self.show_actions()
# hard 3, eventhandler
self.assert_(self.count_logs() == 2) # up, evt
self.clear_logs()
self.clear_actions()
#----------------------------------------------------------------
# the service becomes OK again
# check log messages, (no) notifications and eventhandlers
# check if the stop downtime notification is the only one
#----------------------------------------------------------------
self.scheduler_loop(2, [[host, 0, 'UP']], do_sleep=True)
self.assert_(len(self.sched.downtimes) == 0)
self.assert_(len(host.downtimes) == 0)
self.assert_(not host.in_scheduled_downtime)
self.show_logs()
self.show_actions()
self.assert_(self.log_match(1, 'HOST DOWNTIME ALERT.*STOPPED'))
self.clear_logs()
self.clear_actions()
# todo
# checks return 1=warn. this means normally up
# set use_aggressive_host_checking which treats warn as down
# send host into downtime
# run service checks with result critical
# host exits downtime
# does the service send a notification like when it exts a svc dt?
# check for notifications
# host is down and in downtime. what about service eventhandlers?
def test_notification_after_cancel_flexible_svc_downtime(self):
# schedule flexible downtime
# good check
# bad check -> SOFT;1
# eventhandler SOFT;1
# bad check -> HARD;2
# downtime alert
# eventhandler HARD;2
# cancel downtime
# bad check -> HARD;2
# notification critical
#
pass
if __name__ == '__main__':
unittest.main()