-
Notifications
You must be signed in to change notification settings - Fork 5
/
testMozillaNagiosStatus.py
696 lines (625 loc) · 37.3 KB
/
testMozillaNagiosStatus.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#!/usr/bin/python
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is mozilla-nagios-bot
#
# The Initial Developer of the Original Code is Rob Tucker. Portions created
# by Rob Tucker are Copyright (C) Mozilla, Inc. All Rights Reserved.
#
# Alternatively, the contents of this file may be used under the terms of the
# GNU Public License, Version 2 (the "GPLv2 License"), in which case the
# provisions of GPLv2 License are applicable instead of those above. If you
# wish to allow use of your version of this file only under the terms of the
# GPLv2 License and not to allow others to use your version of this file under
# the MPL, indicate your decision by deleting the provisions above and replace
# them with the notice and other provisions required by the GPLv2 License. If
# you do not delete the provisions above, a recipient may use your version of
# this file under either the MPL or the GPLv2 License.
from mock import Mock
import unittest
from MozillaNagiosStatus import MozillaNagiosStatus
import re
from settings import *
from NagiosLogLine import NagiosLogLine
class MozillaNagiosStatusTest(unittest.TestCase):
tc = None
def setUp(self):
self.event = Mock()
self.event.source = 'rtucker'
self.event.target = '#sysadmins'
self.connection = Mock()
self.tc = MozillaNagiosStatus(self.connection, [])
self.my_nick = self.event.source
self.service_line = '[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5% inode=99%):'
self.ack_line = '[1318870432] SERVICE NOTIFICATION: socorroalertlist;fake-host.mozilla.org;Disk Space /;ACKNOWLEDGEMENT (WARNING);notify-by-email;DISK WARNING - free space: / 60658 MB (29% inode=97%):;ashish;bug 689547'
self.host_line = "[1313158996] HOST NOTIFICATION: sysalertslist;fake-host.mozilla.org;DOWN;host-notify-by-email;PING CRITICAL - Packet loss = 100%"
self.ack_host_line = "[1319720894] HOST NOTIFICATION: sysalertslist;fake-host.mozilla.org;ACKNOWLEDGEMENT (DOWN);host-notify-by-email;PING CRITICAL - Packet loss = 100%;rtucker;known"
def test_get_environment_vars(self):
self.assertEqual(self.tc.list_offset, 100)
self.assertEqual(self.tc.list_size, 100)
def test_ackable_list_bad_host(self):
self.assertEqual(len(self.tc.ackable_list), 100)
self.tc.ackable('Test Host-Not Found', 'Test Service', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
self.assertEqual(len(self.tc.ackable_list), 100)
self.assertEqual(self.tc.ackable_list[0]['host'], 'Test Host-Not Found')
self.assertEqual(self.tc.ackable_list[0]['service'], 'Test Service')
self.assertEqual(self.tc.ackable_list[0]['state'], 'CRITICAL')
self.assertEqual(self.tc.ackable_list[0]['message'], 'Test Message')
def test_downtime_by_index_bad_host(self):
self.tc.use_mklive_status = False
self.tc.ackable('Test Host-Not Found', 'Test Service', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 100 1m blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Host Not Found Test Host-Not Found' % self.my_nick)
def test_downtime_by_host_only_with_duration_preceeding(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'test-host.fake.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
'1324567',
]]
self.tc.ackable('test-host.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 1h test-host.fake.mozilla.com blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for host test-host.fake.mozilla.com scheduled for 1:00:00' % (self.my_nick) )
def test_downtime_by_index_host_only_duration_preceeding(self):
self.tc.use_mklive_status = False
self.tc.ackable('test-host.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 1m 100 blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for host test-host.fake.mozilla.com scheduled for 0:01:00' % (self.my_nick) )
def test_downtime_by_host_and_service(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'test-host-with-service.fake.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
'1324567',
]]
self.tc.ackable('test-host-with-service.fake.mozilla.com', 'Test Service', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime test-host-with-service.fake.mozilla.com:MySQL Uptime 1h test comment'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for service test-host-with-service.fake.mozilla.com:MySQL Uptime scheduled for 1:00:00' % (self.my_nick) )
def test_downtime_by_host_and_service_with_duration_preceeding(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'test-host.fake.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
'1324567',
]]
self.tc.ackable('test-host.fake.mozilla.com', 'Test Service', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 1h test-host.fake.mozilla.com:Test blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for service test-host.fake.mozilla.com:Test scheduled for 1:00:00' % (self.my_nick) )
def test_downtime_by_host_only(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'test-host-without-service.fake.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
'1324567',
]]
self.tc.ackable('test-host-without-service.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime test-host-without-service.fake.mozilla.com 1m blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for host test-host-without-service.fake.mozilla.com scheduled for 0:01:00' % (self.my_nick) )
#def test_downtime_by_host_only_cmd_return(self):
# self.tc.ackable('test-host.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
# self.assertEqual(self.tc.get_ack_number(), 100)
# message = 'downtime test-host.fake.mozilla.com 1m blah blah'
# m = re.search('^downtime\s+([^: ]+)(?::(.*))?\s+(\d+[dhms])\s+(.*)\s*$', message)
# cmd = self.tc.downtime(self.event, message, m, return_cmd=True)
# ### fixup timestamp vars in response
# ### Replace the execution timestamp
# cmd = re.sub('\[\d+\]', '[000000]', cmd)
# ### Replace the start and end timestamp
# cmd = re.sub('\.com;\d+;\d+;', '.com;1234;5678;', cmd)
# ### Confirm that the syntax is correct per:
# self.assertEqual(cmd, '[000000] SCHEDULE_HOST_DOWNTIME;test-host.fake.mozilla.com;1234;5678;2;0;60;%s;blah blah\n' % (self.my_nick ))
def test_downtime_by_index_host_only(self):
self.tc.use_mklive_status = False
self.tc.ackable('test-host.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 100 1m blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for host test-host.fake.mozilla.com scheduled for 0:01:00' % (self.my_nick) )
def test_downtime_by_index_host_only_interval_first(self):
self.tc.use_mklive_status = False
self.tc.ackable('test-host.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 1m 100 blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for host test-host.fake.mozilla.com scheduled for 0:01:00' % (self.my_nick) )
def test_downtime_by_index_with_service(self):
self.tc.use_mklive_status = False
self.tc.ackable('test-host.fake.mozilla.com', 'Test Service', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 100 1m blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for service test-host.fake.mozilla.com:Test Service scheduled for 0:01:00' % (self.my_nick) )
def test_increment_ackable_list_correctly(self):
self.tc.ackable('test-host.fake.mozilla.com', 'Test Service', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
self.assertEqual(len(self.tc.ackable_list), 100)
self.assertEqual(self.tc.ackable_list[0]['service'], 'Test Service')
self.tc.ackable('test-host.fake.mozilla.com', 'Test Service 2', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.ackable_list[0]['service'], 'Test Service')
self.assertEqual(self.tc.ackable_list[1]['service'], 'Test Service 2')
def test_downtime_by_hostname_with_service(self):
self.tc.use_mklive_status = False
self.tc.ackable('test-host.fake.mozilla.com', 'Test Service', 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime 100 1m blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for service test-host.fake.mozilla.com:Test Service scheduled for 0:01:00' % (self.my_nick) )
def test_downtime_by_hostname(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'test-host.fake.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
]]
self.tc.ackable('test-host.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
message = 'downtime test-host.fake.mozilla.com 1m blah blah'
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, '#sysadmins')
self.assertEqual(message, '%s: Downtime for host test-host.fake.mozilla.com scheduled for 0:01:00' % (self.my_nick) )
def test_mute(self):
message = "mute"
m = None
target, message = self.tc.mute(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: OK I'll mute" % (self.my_nick) )
def test_already_muted(self):
message = "mute"
m = None
target, message = self.tc.mute(self.event, message, m)
target, message = self.tc.mute(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: I'm already muted" % (self.my_nick) )
def test_already_muted_unmute(self):
message = "mute"
m = None
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
message = "unmute"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: OK I'll unmute" % (self.my_nick) )
def test_unmute_when_not_previously_muted(self):
m = None
message = "unmute"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: OK I'm not muted" % (self.my_nick) )
def test_oncall(self):
m = None
message = "whoisoncall"
target, message = self.tc.get_oncall(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: unknown currently has the pager" % (self.my_nick) )
def test_onduty(self):
m = None
message = "whoisonduty"
target, message = self.tc.get_oncall(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: unknown currently has the pager" % (self.my_nick) )
def test_unack_host(self):
message = "unack test-host.fake.mozilla.com"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: removed acknowledgment (if any) for host test-host.fake.mozilla.com" % (self.my_nick) )
def test_ack_host_with_service(self):
message = "ack test-host.fake.mozilla.com:asdf test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged service test-host.fake.mozilla.com:asdf test message" % (self.my_nick) )
def test_ack_host(self):
message = "ack test-host.fake.mozilla.com test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged host test-host.fake.mozilla.com" % (self.my_nick) )
def test_ack_host_by_index(self):
self.tc.process_line(self.host_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
message = "ack 100 test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged host fake-host.mozilla.org" % (self.my_nick) )
self.tc.process_line(self.ack_host_line, True)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged host fake-host.mozilla.org" % (self.my_nick) )
def test_ack_host_by_index_with_colon_in_message(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'fake-host.mozilla.org',
'0',
'Replication running. Lag time: 0 seconds',
'1324567',
]]
self.tc.process_line(self.host_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
message = "ack 100 test message :)"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged host fake-host.mozilla.org" % (self.my_nick) )
self.tc.process_line(self.ack_host_line, True)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged host fake-host.mozilla.org" % (self.my_nick) )
def test_ack_host_by_index_after_one_cycle(self):
self.tc.ackable_list = [None]*self.tc.list_size
self.assertEqual(len(self.tc.ackable_list), 100)
for i in range(0,101):
service_line = "[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition%s;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5 inode=99):" % i
self.tc.process_line(service_line, True)
message = "ack 100 test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged service fake-host.mozilla.org:root partition100" % (self.my_nick) )
self.tc.process_line(self.ack_host_line, True)
def test_ack_host_by_index_after_one_cycle_long_service_message(self):
self.tc.ackable_list = [None]*self.tc.list_size
self.assertEqual(len(self.tc.ackable_list), 100)
for i in range(0,12):
service_line = "[1334695172] SERVICE NOTIFICATION: sysalertslist;db1.foo.bar.mozilla.com;MySQL Replication;UNKNOWN;notify-by-sms;**ePN /usr/lib64/nagios/plugins/mozilla/check_mysql_replication: DBI connect(host=10.0.0.0:database=,username,...) failed: Cant connect to MySQL server on 10.0.0.0 (111) at (eval 6) line 30."
self.tc.process_line(service_line, True)
message = "ack 110 test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged service db1.foo.bar.mozilla.com:MySQL Replication" % (self.my_nick) )
self.tc.process_line(self.ack_host_line, True)
message = "status db1.foo.mozilla.com:MySQL replication"
m = re.search('^status ([^:]+):(.+)$', message)
target, message = self.tc.status_by_host_name(self.event, message, m)
self.assertEqual(target, "#sysadmins")
#self.assertEqual(message[0], "%s: db1.foo.mozilla.com:mysql Replication is \x033OK\x03\x03\x03 - uptime: 18346 threads: 199 questions: 12026601 slow queries: 1 opens: 496 flush tables: 1 open tables: 489 queries per second avg: 655.543" % (self.my_nick) )
def test_disallowed_ack_list(self):
self.assertEqual(self.tc.disallowed_ack[0], 'serverops_bugs')
def test_ack_service_by_index_disallowed(self):
self.tc.ackable_list = [None]*self.tc.list_size
self.assertEqual(len(self.tc.ackable_list), 100)
for i in range(0,12):
service_line = "[1334695172] SERVICE NOTIFICATION: sysalertslist;db1.foo.bar.mozilla.com;serverops_bugs;CRITICAL;notify-by-sms;some critical bug that I don't want to ack"
self.tc.process_line(service_line, True)
message = "ack 110 test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: I'm sorry but you're not allowed to ACK this alert here. Please visit the appropriate nagios webui to ACK it there." % (self.my_nick) )
self.tc.process_line(self.ack_host_line, True)
def test_ack_host_by_index_after_half_cycle(self):
self.tc.ackable_list = [None]*self.tc.list_size
self.assertEqual(len(self.tc.ackable_list), 100)
for i in range(0,51):
service_line = "[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition%s;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5 inode=99):" % i
self.tc.process_line(service_line, True)
message = "ack 150 test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged service fake-host.mozilla.org:root partition50" % (self.my_nick) )
self.assertEqual(self.tc.ackable_list[51], None)
def test_ack_host_by_index_after_five_cycles(self):
self.tc.ackable_list = [None]*self.tc.list_size
self.assertEqual(len(self.tc.ackable_list), 100)
for i in range(0,501):
service_line = "[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition%s;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5 inode=99):" % i
self.tc.process_line(service_line, True)
message = "ack 100 test message"
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged service fake-host.mozilla.org:root partition500" % (self.my_nick) )
self.tc.process_line(self.ack_host_line, True)
def test_ack_host_by_index_after_five_cycles_with_ack(self):
self.tc.ackable_list = [None]*self.tc.list_size
self.assertEqual(len(self.tc.ackable_list), 100)
for i in range(0,501):
service_line = "[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition%s;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5 inode=99):" % i
self.tc.process_line(service_line, True)
message = "ack %i test message" % (i % self.tc.list_size)
m, callback = self.get_regex_obj_and_callback(message)
target, message = callback(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged service fake-host.mozilla.org:root partition%i" % (self.my_nick, i) )
self.tc.process_line(self.ack_host_line, True)
def test_ack_host_by_index_after_five_cycles_with_ack_incorrect_syntax(self):
self.tc.ackable_list = [None]*self.tc.list_size
self.assertEqual(len(self.tc.ackable_list), 100)
for i in range(0,501):
service_line = "[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition%s;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5 inode=99):" % i
self.tc.process_line(service_line, True)
message = "%i ack test message" % (i % self.tc.list_size)
m = re.search('^(?:\s*ack\s*)?(\d+)(?:\s*ack\s*)?[:\s]+([^:]+)\s*$', message)
target, message = self.tc.ack(self.event, message, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: acknowledged service fake-host.mozilla.org:root partition%i" % (self.my_nick, i) )
self.tc.process_line(self.ack_host_line, True)
#def test_ack_host_by_index_after_hundred_cycles(self):
# self.tc.ackable_list = [None]*self.tc.list_size
# self.assertEqual(len(self.tc.ackable_list), 100)
# for i in range(0,100001):
# service_line = "[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition%s;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5 inode=99):" % i
# self.tc.process_line(service_line, True)
# message = "ack 100 test message"
# m = re.search('^(?:\s*ack\s*)?(\d+)(?:\s*ack\s*)?[:\s]+([^:]+)\s*$', message)
# target, message = self.tc.ack(self.event, message, m)
# self.assertEqual(target, "#sysadmins")
# self.assertEqual(message, "%s: acknowledged service fake-host.mozilla.org:root partition100000" % (self.my_nick) )
# self.tc.process_line(self.ack_host_line, True)
def test_unack_by_index(self):
cmd = "unack 100"
m, callback = self.get_regex_obj_and_callback(cmd)
target, message = callback(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: Sorry, but no alert exists at this index" % (self.my_nick) )
#Now add an alert to the list and try to unack
tmp = self.tc.ackable('test-host.fake.mozilla.com', None, 'CRITICAL', 'Test Message')
self.assertEqual(self.tc.get_ack_number(), 100)
m, callback = self.get_regex_obj_and_callback(cmd)
target, message = callback(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: removed acknowledgment (if any) for host test-host.fake.mozilla.com" % (self.my_nick) )
def test_process_line(self):
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
self.tc.process_line(self.host_line, True)
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 102)
def test_process_line_with_unknown_contacts(self):
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
self.tc.process_line(self.host_line, True)
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 102)
service_line = "[1318882274] SERVICE NOTIFICATION: unknowncontacthere;fake-host.mozilla.org;root partition;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5 inode=99):"
self.tc.process_line(service_line, True)
self.assertEqual(self.tc.get_ack_number(), 102)
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 103)
def test_page_by_index(self):
self.tc.page_with_alert_number = Mock()
self.tc.page_with_alert_number.return_value = "#sysadmins", "%s: %s has been paged" % (self.my_nick, self.event.source)
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
cmd = "page 100 %s" % (self.event.source)
m = re.search('^page\s+(\d+)\s+(\w+)\s*$', cmd)
target, message = self.tc.page_with_alert_number(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: %s has been paged" % (self.my_nick, self.event.source) )
def test_page_by_bad_index(self):
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
cmd = "page 101 %s" % (self.event.source)
m = re.search('^page\s+(\d+)\s+(\w+)\s*$', cmd)
target, message = self.tc.page_with_alert_number(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s: Sorry, but no alert exists at this index" % (self.my_nick) )
def test_get_channel_group(self):
self.assertEqual(self.tc.get_channel_group('sysalertslist'), '#sysadmins')
"""
This test is no longer valid, we did away with a default contact channel
"""
"""def test_get_channel_group_not_found(self):
self.assertEqual(self.tc.get_channel_group('thisshouldnevermatchblahblah'), '#default')
"""
def test_get_page_plugin(self):
plugins = self.tc.return_plugins()
self.assertEqual('^ack (\d+)\s+(.*)$', plugins[0]['regex'])
def get_regex_obj_and_callback(self, command_string):
regex_list = [r['regex'] for r in self.tc.message_commands]
for mc in self.tc.message_commands:
m = re.match(mc['regex'], command_string)
if m:
return m, mc['callback']
return None
class NagiosStatusTest(unittest.TestCase):
def setUp(self):
self.event = Mock()
self.event.source = 'rtucker'
self.event.target = '#sysadmins'
self.connection = Mock()
self.tc = MozillaNagiosStatus(self.connection, [])
self.my_nick = self.event.source
self.service_line = '[1318882274] SERVICE NOTIFICATION: sysalertslist;db2.foo.mozilla.com;mysql replication;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5% inode=99%):'
def test_empty_host_status(self):
cmd = "status db2.foo.mozilla.com"
m = re.search('^status ([^:]+)', cmd)
target, message = self.tc.status_by_host_name(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, '%s: db2.foo.mozilla.com is \x033OK\x03\x03\x03 - PING OK - Packet loss = 0%%, RTA = 0.91 ms' % self.event.source)
def test_empty_host_wildcard(self):
cmd = "status db*:PING"
m = re.search('^status ([^:]+)', cmd)
target, message = self.tc.status_by_host_name(self.event, cmd, m)
self.assertEqual(len(message), 2)
self.assertEqual(target, "#sysadmins")
self.assertTrue('%s: db1.foo.mozilla.com:PING is \x033OK\x03\x03\x03 - PING OK - Packet loss = 0%%, RTA = 0.80 ms' % self.event.source in message[0])
self.assertTrue('%s: db2.foo.mozilla.com:PING is \x033OK\x03\x03\x03 - PING OK - Packet loss = 0%%, RTA = 0.88 ms' % self.event.source in message[1])
def test_basic_host_status_wildcard_services(self):
cmd = "status db2.foo.mozilla.com:*"
m = re.search('^status ([^:]+):(.+)$', cmd)
target, message = self.tc.status_by_host_name(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(len(message), 11)
def test_basic_host_status_specific_service(self):
cmd = "status db2.foo.mozilla.com:swap"
m = re.search('^status ([^:]+):(.+)$', cmd)
target, message = self.tc.status_by_host_name(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(len(message), 1)
self.assertTrue("%s: db2.foo.mozilla.com:Swap is \x033OK\x03\x03\x03 - SWAP OK - 100%% free (2043 MB out of 2047 MB)" % self.event.source in message[0] )
def test_host_service_status_by_index(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'db2.foo.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
'1324567',
]]
self.tc.ackable_list = [None]*self.tc.list_size
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
cmd = "status 100"
m = re.search('^status (\d+)$', cmd)
target, message = self.tc.status_by_index(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertTrue('%s: db2.foo.mozilla.com Replication running. Lag time: 0 seconds' % (self.event.source) in message)
def test_host_service_status_multiple_entriesby_index(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'db2.foo.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
'123456'
]]
self.tc.ackable_list = [None]*self.tc.list_size
self.tc.process_line(self.service_line, True)
self.assertEqual(self.tc.get_ack_number(), 100)
cmd = "status 100"
m = re.search('^status (\d+)$', cmd)
target, message = self.tc.status_by_index(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertTrue('%s: db2.foo.mozilla.com Replication running. Lag time: 0 seconds' % (self.event.source) in message)
self.tc.process_line(self.service_line, True)
self.tc.process_line(self.service_line, True)
self.tc.process_line(self.service_line, True)
self.tc.process_line(self.service_line, True)
cmd = "status 103"
m = re.search('^status (\d+)$', cmd)
target, message = self.tc.status_by_index(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertTrue('%s: db2.foo.mozilla.com Replication running. Lag time: 0 seconds' % (self.event.source) in message)
cmd = "status 105"
m = re.search('^status (\d+)$', cmd)
target, message = self.tc.status_by_index(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertEqual(message, "%s Sorry, but I can't find any matching services" % self.event.source)
def test_host_only_status_by_index(self):
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'db1.foo.mozilla.com',
'0',
'SWAP OK - 99% free (2023 MB out of 2047 MB)',
'123456'
]]
self.tc.ackable_list = [None]*self.tc.list_size
custom_service_line = '[1318882274] SERVICE NOTIFICATION: sysalertslist;db1.foo.mozilla.com;SWAP;CRITICAL;notify-by-email;FAKE CRITICAL - free space: / 5294 MB (5% inode=99%):'
self.tc.process_line(custom_service_line, True)
self.tc.process_line(custom_service_line, True)
self.tc.process_line(custom_service_line, True)
self.tc.process_line(self.service_line, True)
cmd = "status 100"
m = re.search('^status (\d+)$', cmd)
target, message = self.tc.status_by_index(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertTrue("%s: db1.foo.mozilla.com SWAP OK - 99%% free (2023 MB out of 2047 MB)" % self.event.source in message)
cmd = "status 103"
m = re.search('^status (\d+)$', cmd)
self.tc.execute_query = Mock()
self.tc.execute_query.return_value = [[
'db2.foo.mozilla.com',
'0',
'Replication running. Lag time: 0 seconds',
'123456'
]]
target, message = self.tc.status_by_index(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertTrue("%s: db2.foo.mozilla.com Replication running. Lag time: 0 seconds" % self.event.source in message)
def test_basic_host_status_wildcard_service_from_start(self):
cmd = "status db2.foo.mozilla.com:Sw*"
m = re.search('^status ([^:]+):(.+)$', cmd)
target, message = self.tc.status_by_host_name(self.event, cmd, m)
self.assertEqual(target, "#sysadmins")
self.assertTrue("%s: db2.foo.mozilla.com:Swap is \x033OK\x03\x03\x03 - SWAP OK - 100%% free (2043 MB out of 2047 MB)" % self.event.source in message[0])
self.assertEqual(len(message), 1)
class NagiosLogLineTest(unittest.TestCase):
def setUp(self):
self.service_line = '[1318882274] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;root partition;CRITICAL;notify-by-email;DISK CRITICAL - free space: / 5294 MB (5% inode=99%):'
self.host_line = "[1313158996] HOST NOTIFICATION: sysalertslist;fake-host.mozilla.org;DOWN;host-notify-by-email;PING CRITICAL - Packet loss = 100%"
pass
def test_constructor(self):
l = NagiosLogLine('asdf')
self.assertEqual(l.line, 'asdf')
l = NagiosLogLine("asdf\r\n")
self.assertEqual(l.line, 'asdf')
def test_is_service_notification(self):
l = NagiosLogLine(self.service_line)
self.assertEqual(l.notification_type,'SERVICE')
def test_is_notification_recipient(self):
l = NagiosLogLine(self.service_line)
self.assertEqual(l.notification_recipient,'sysalertslist')
def test_is_host_notification(self):
l = NagiosLogLine(self.host_line)
self.assertEqual(l.notification_type,'HOST')
def test_get_service(self):
l = NagiosLogLine(self.service_line)
self.assertEqual(l.notification_type,'SERVICE')
self.assertEqual(l.service,'root partition')
def test_get_service_state(self):
l = NagiosLogLine(self.service_line)
self.assertEqual(l.state,'CRITICAL')
def test_get_host_state(self):
l = NagiosLogLine(self.host_line)
self.assertEqual(l.state,'DOWN')
def test_get_service_message(self):
l = NagiosLogLine(self.service_line)
self.assertEqual(l.message,'DISK CRITICAL - free space: / 5294 MB (5% inode=99%): (http://m.mozilla.org/root+partition)')
def test_get_service_message_acknowledged(self):
l = NagiosLogLine('[1318870432] SERVICE NOTIFICATION: sysalertslist;fake-host.mozilla.org;Disk Space /;ACKNOWLEDGEMENT (WARNING);notify-by-email;DISK WARNING - free space: / 60658 MB (29% inode=97%):;ashish;bug 689547')
self.assertEqual(l.is_service,True)
self.assertEqual(l.state,'ACKNOWLEDGEMENT (WARNING)')
def test_get_host_message(self):
l = NagiosLogLine(self.host_line)
self.assertEqual(l.message,'PING CRITICAL - Packet loss = 100%')
def test_get_host_name(self):
l = NagiosLogLine(self.host_line)
self.assertEqual(l.host,'fake-host.mozilla.org')