/
test_business_correlator.py
1555 lines (1298 loc) · 63.1 KB
/
test_business_correlator.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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright (C) 2009-2014:
# 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 reading and processing of config files
#
import re
from shinken_test import *
class TestBusinesscorrel(ShinkenTest):
def setUp(self):
self.setup_with_file('etc/shinken_business_correlator.cfg')
# We will try a simple bd1 OR db2
def test_simple_or_business_correlator(self):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the hosts and services"
now = time.time()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.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
svc_bd1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db1")
self.assertEqual(False, svc_bd1.got_business_rule)
self.assertIs(None, svc_bd1.business_rule)
svc_bd2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db2")
self.assertEqual(False, svc_bd2.got_business_rule)
self.assertIs(None, svc_bd2.business_rule)
svc_cor = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Simple_Or")
self.assertEqual(True, svc_cor.got_business_rule)
self.assertIsNot(svc_cor.business_rule, None)
bp_rule = svc_cor.business_rule
self.assertEqual('|', bp_rule.operand)
# We check for good parent/childs links
# So svc_cor should be a son of svc_bd1 and svc_bd2
# and bd1 and bd2 should be parents of svc_cor
self.assertIn(svc_cor, svc_bd1.child_dependencies)
self.assertIn(svc_cor, svc_bd2.child_dependencies)
self.assertIn(svc_bd1, svc_cor.parent_dependencies)
self.assertIn(svc_bd2, svc_cor.parent_dependencies)
sons = bp_rule.sons
print "Sons,", sons
# We've got 2 sons, 2 services nodes
self.assertEqual(2, len(sons))
self.assertEqual('service', sons[0].operand)
self.assertEqual(svc_bd1, sons[0].sons[0])
self.assertEqual('service', sons[1].operand)
self.assertEqual(svc_bd2, sons[1].sons[0])
# Now state working on the states
self.scheduler_loop(1, [[svc_bd2, 0, 'OK | value1=1 value2=2'], [svc_bd1, 0, 'OK | rtt=10']])
self.assertEqual('OK', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual('OK', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we set the bd1 as soft/CRITICAL
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('SOFT', svc_bd1.state_type)
self.assertEqual(0, svc_bd1.last_hard_state_id)
# The business rule must still be 0
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we get bd1 CRITICAL/HARD
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual(2, svc_bd1.last_hard_state_id)
# The rule must still be a 0 (or inside)
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we also set bd2 as CRITICAL/HARD... byebye 0 :)
self.scheduler_loop(2, [[svc_bd2, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(2, svc_bd2.last_hard_state_id)
# And now the state of the rule must be 2
state = bp_rule.get_state()
self.assertEqual(2, state)
# And If we set one WARNING?
self.scheduler_loop(2, [[svc_bd2, 1, 'WARNING | value1=1 value2=2']])
self.assertEqual('WARNING', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(1, svc_bd2.last_hard_state_id)
# Must be WARNING (better no 0 value)
state = bp_rule.get_state()
self.assertEqual(1, state)
# We will try a simple bd1 AND db2
def test_simple_and_business_correlator(self):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the hosts and services"
now = time.time()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.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
svc_bd1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db1")
self.assertEqual(False, svc_bd1.got_business_rule)
self.assertIs(None, svc_bd1.business_rule)
svc_bd2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db2")
self.assertEqual(False, svc_bd2.got_business_rule)
self.assertIs(None, svc_bd2.business_rule)
svc_cor = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Simple_And")
self.assertEqual(True, svc_cor.got_business_rule)
self.assertIsNot(svc_cor.business_rule, None)
bp_rule = svc_cor.business_rule
self.assertEqual('&', bp_rule.operand)
sons = bp_rule.sons
print "Sons,", sons
# We've got 2 sons, 2 services nodes
self.assertEqual(2, len(sons))
self.assertEqual('service', sons[0].operand)
self.assertEqual(svc_bd1, sons[0].sons[0])
self.assertEqual('service', sons[1].operand)
self.assertEqual(svc_bd2, sons[1].sons[0])
# Now state working on the states
self.scheduler_loop(1, [[svc_bd2, 0, 'OK | value1=1 value2=2'], [svc_bd1, 0, 'OK | rtt=10']])
self.assertEqual('OK', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual('OK', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we set the bd1 as soft/CRITICAL
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('SOFT', svc_bd1.state_type)
self.assertEqual(0, svc_bd1.last_hard_state_id)
# The business rule must still be 0
# becase we want HARD states
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we get bd1 CRITICAL/HARD
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual(2, svc_bd1.last_hard_state_id)
# The rule must go CRITICAL
state = bp_rule.get_state()
self.assertEqual(2, state)
# Now we also set bd2 as WARNING/HARD...
self.scheduler_loop(2, [[svc_bd2, 1, 'WARNING | value1=1 value2=2']])
self.assertEqual('WARNING', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(1, svc_bd2.last_hard_state_id)
# And now the state of the rule must be 2
state = bp_rule.get_state()
self.assertEqual(2, state)
# And If we set one WARNING too?
self.scheduler_loop(2, [[svc_bd1, 1, 'WARNING | value1=1 value2=2']])
self.assertEqual('WARNING', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual(1, svc_bd1.last_hard_state_id)
# Must be WARNING (worse no 0 value for both)
state = bp_rule.get_state()
self.assertEqual(1, state)
# We will try a simple 1of: bd1 OR/AND db2
def test_simple_1of_business_correlator(self):
self.run_simple_1of_business_correlator()
# We will try a simple -1of: bd1 OR/AND db2
def test_simple_1of_neg_business_correlator(self):
self.run_simple_1of_business_correlator(with_neg=True)
# We will try a simple 50%of: bd1 OR/AND db2
def test_simple_1of_pct_business_correlator(self):
self.run_simple_1of_business_correlator(with_pct=True)
# We will try a simple -50%of: bd1 OR/AND db2
def test_simple_1of_pct_neg_business_correlator(self):
self.run_simple_1of_business_correlator(with_pct=True, with_neg=True)
def run_simple_1of_business_correlator(self, with_pct=False, with_neg=False):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the hosts and services"
now = time.time()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.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
svc_bd1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db1")
self.assertEqual(False, svc_bd1.got_business_rule)
self.assertIs(None, svc_bd1.business_rule)
svc_bd2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db2")
self.assertEqual(False, svc_bd2.got_business_rule)
self.assertIs(None, svc_bd2.business_rule)
if with_pct is True:
if with_neg is True:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of_pct_neg")
else:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of_pct")
else:
if with_neg is True:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of_neg")
else:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of")
self.assertEqual(True, svc_cor.got_business_rule)
self.assertIsNot(svc_cor.business_rule, None)
bp_rule = svc_cor.business_rule
self.assertEqual('of:', bp_rule.operand)
# Simple 1of: so in fact a triple ('1','2','2') (1of and MAX,MAX
if with_pct is True:
if with_neg is True:
self.assertEqual(('-50%', '2', '2'), bp_rule.of_values)
else:
self.assertEqual(('50%', '2', '2'), bp_rule.of_values)
else:
if with_neg is True:
self.assertEqual(('-1', '2', '2'), bp_rule.of_values)
else:
self.assertEqual(('1', '2', '2'), bp_rule.of_values)
sons = bp_rule.sons
print "Sons,", sons
# We've got 2 sons, 2 services nodes
self.assertEqual(2, len(sons))
self.assertEqual('service', sons[0].operand)
self.assertEqual(svc_bd1, sons[0].sons[0])
self.assertEqual('service', sons[1].operand)
self.assertEqual(svc_bd2, sons[1].sons[0])
# Now state working on the states
self.scheduler_loop(1, [[svc_bd2, 0, 'OK | value1=1 value2=2'], [svc_bd1, 0, 'OK | rtt=10']])
self.assertEqual('OK', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual('OK', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we set the bd1 as soft/CRITICAL
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('SOFT', svc_bd1.state_type)
self.assertEqual(0, svc_bd1.last_hard_state_id)
# The business rule must still be 0
# becase we want HARD states
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we get bd1 CRITICAL/HARD
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual(2, svc_bd1.last_hard_state_id)
# The rule still be OK
state = bp_rule.get_state()
self.assertEqual(0, state)
# Now we also set bd2 as CRITICAL/HARD...
self.scheduler_loop(2, [[svc_bd2, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(2, svc_bd2.last_hard_state_id)
# And now the state of the rule must be 2 now
state = bp_rule.get_state()
self.assertEqual(2, state)
# And If we set one WARNING now?
self.scheduler_loop(2, [[svc_bd1, 1, 'WARNING | value1=1 value2=2']])
self.assertEqual('WARNING', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual(1, svc_bd1.last_hard_state_id)
# Must be WARNING (worse no 0 value for both, like for AND rule)
state = bp_rule.get_state()
self.assertEqual(1, state)
# We will try a simple 1of: test_router_0 OR/AND test_host_0
def test_simple_1of_business_correlator_with_hosts(self):
self.run_simple_1of_business_correlator_with_hosts()
# We will try a simple -1of: test_router_0 OR/AND test_host_0
def test_simple_1of_neg_business_correlator_with_hosts(self):
self.run_simple_1of_business_correlator_with_hosts(with_neg=True)
# We will try a simple 50%of: test_router_0 OR/AND test_host_0
def test_simple_1of_pct_business_correlator_with_hosts(self):
self.run_simple_1of_business_correlator_with_hosts(with_pct=True)
# We will try a simple -50%of: test_router_0 OR/AND test_host_0
def test_simple_1of_pct_neg_business_correlator_with_hosts(self):
self.run_simple_1of_business_correlator_with_hosts(with_pct=True, with_neg=True)
def run_simple_1of_business_correlator_with_hosts(self, with_pct=False, with_neg=False):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the hosts and services"
now = time.time()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.act_depend_of = [] # ignore the router
if with_pct is True:
if with_neg is True:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of_with_host_pct_neg")
else:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of_with_host_pct")
else:
if with_neg is True:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of_with_host_neg")
else:
svc_cor = self.sched.services.find_srv_by_name_and_hostname(
"test_host_0", "Simple_1Of_with_host")
self.assertEqual(True, svc_cor.got_business_rule)
self.assertIsNot(svc_cor.business_rule, None)
bp_rule = svc_cor.business_rule
self.assertEqual('of:', bp_rule.operand)
# Simple 1of: so in fact a triple ('1','2','2') (1of and MAX,MAX
if with_pct is True:
if with_neg is True:
self.assertEqual(('-50%', '2', '2'), bp_rule.of_values)
else:
self.assertEqual(('50%', '2', '2'), bp_rule.of_values)
else:
if with_neg is True:
self.assertEqual(('-1', '2', '2'), bp_rule.of_values)
else:
self.assertEqual(('1', '2', '2'), bp_rule.of_values)
sons = bp_rule.sons
print "Sons,", sons
# We've got 2 sons, 2 services nodes
self.assertEqual(2, len(sons))
self.assertEqual('host', sons[0].operand)
self.assertEqual(host, sons[0].sons[0])
self.assertEqual('host', sons[1].operand)
self.assertEqual(router, sons[1].sons[0])
# We will try a simple bd1 OR db2, but this time we will
# schedule a real check and see if it's good
def test_simple_or_business_correlator_with_schedule(self):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the hosts and services"
now = time.time()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.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
svc_bd1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db1")
self.assertEqual(False, svc_bd1.got_business_rule)
self.assertIs(None, svc_bd1.business_rule)
svc_bd2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db2")
self.assertEqual(False, svc_bd2.got_business_rule)
self.assertIs(None, svc_bd2.business_rule)
svc_cor = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Simple_Or")
self.assertEqual(True, svc_cor.got_business_rule)
self.assertIsNot(svc_cor.business_rule, None)
bp_rule = svc_cor.business_rule
self.assertEqual('|', bp_rule.operand)
sons = bp_rule.sons
print "Sons,", sons
# We've got 2 sons, 2 services nodes
self.assertEqual(2, len(sons))
self.assertEqual('service', sons[0].operand)
self.assertEqual(svc_bd1, sons[0].sons[0])
self.assertEqual('service', sons[1].operand)
self.assertEqual(svc_bd2, sons[1].sons[0])
# Now state working on the states
self.scheduler_loop(1, [[svc_bd2, 0, 'OK | value1=1 value2=2'], [svc_bd1, 0, 'OK | rtt=10']])
self.assertEqual('OK', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual('OK', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
state = bp_rule.get_state()
self.assertEqual(0, state)
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# Now we set the bd1 as soft/CRITICAL
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('SOFT', svc_bd1.state_type)
self.assertEqual(0, svc_bd1.last_hard_state_id)
# The business rule must still be 0
state = bp_rule.get_state()
self.assertEqual(0, state)
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# Now we get bd1 CRITICAL/HARD
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual(2, svc_bd1.last_hard_state_id)
# The rule must still be a 0 (or inside)
state = bp_rule.get_state()
self.assertEqual(0, state)
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# Now we also set bd2 as CRITICAL/HARD... byebye 0 :)
self.scheduler_loop(2, [[svc_bd2, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(2, svc_bd2.last_hard_state_id)
# And now the state of the rule must be 2
state = bp_rule.get_state()
self.assertEqual(2, state)
# And now we must be CRITICAL/SOFT!
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('CRITICAL', svc_cor.state)
self.assertEqual('SOFT', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# OK, re recheck again, GO HARD!
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('CRITICAL', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(2, svc_cor.last_hard_state_id)
# And If we set one WARNING?
self.scheduler_loop(2, [[svc_bd2, 1, 'WARNING | value1=1 value2=2']])
self.assertEqual('WARNING', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(1, svc_bd2.last_hard_state_id)
# Must be WARNING (better no 0 value)
state = bp_rule.get_state()
self.assertEqual(1, state)
# And in a HARD
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('WARNING', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(1, svc_cor.last_hard_state_id)
print "All elements", bp_rule.list_all_elements()
print "IMPACT:", svc_bd2.impacts
for i in svc_bd2.impacts:
print i.get_name()
# Assert that Simple_Or Is an impact of the problem bd2
self.assertIn(svc_cor, svc_bd2.impacts)
# and bd1 too
self.assertIn(svc_cor, svc_bd1.impacts)
def test_dep_node_list_elements(self):
svc_bd1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db1")
self.assertEqual(False, svc_bd1.got_business_rule)
self.assertIs(None, svc_bd1.business_rule)
svc_bd2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db2")
self.assertEqual(False, svc_bd2.got_business_rule)
self.assertIs(None, svc_bd2.business_rule)
svc_cor = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Simple_Or")
self.assertEqual(True, svc_cor.got_business_rule)
self.assertIsNot(svc_cor.business_rule, None)
bp_rule = svc_cor.business_rule
self.assertEqual('|', bp_rule.operand)
print "All elements", bp_rule.list_all_elements()
all_elt = bp_rule.list_all_elements()
self.assertIn(svc_bd2, all_elt)
self.assertIn(svc_bd1, all_elt)
print "DBG: bd2 depend_on_me", svc_bd2.act_depend_of_me
# We will try a full ERP rule and
# schedule a real check and see if it's good
def test_full_erp_rule_with_schedule(self):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the hosts and services"
now = time.time()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.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
svc_bd1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db1")
self.assertEqual(False, svc_bd1.got_business_rule)
self.assertIs(None, svc_bd1.business_rule)
svc_bd2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "db2")
self.assertEqual(False, svc_bd2.got_business_rule)
self.assertIs(None, svc_bd2.business_rule)
svc_web1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "web1")
self.assertEqual(False, svc_web1.got_business_rule)
self.assertIs(None, svc_web1.business_rule)
svc_web2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "web2")
self.assertEqual(False, svc_web2.got_business_rule)
self.assertIs(None, svc_web2.business_rule)
svc_lvs1 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "lvs1")
self.assertEqual(False, svc_lvs1.got_business_rule)
self.assertIs(None, svc_lvs1.business_rule)
svc_lvs2 = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "lvs2")
self.assertEqual(False, svc_lvs2.got_business_rule)
self.assertIs(None, svc_lvs2.business_rule)
svc_cor = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "ERP")
self.assertEqual(True, svc_cor.got_business_rule)
self.assertIsNot(svc_cor.business_rule, None)
bp_rule = svc_cor.business_rule
self.assertEqual('&', bp_rule.operand)
sons = bp_rule.sons
print "Sons,", sons
# We've got 3 sons, each 3 rules
self.assertEqual(3, len(sons))
bd_node = sons[0]
self.assertEqual('|', bd_node.operand)
self.assertEqual(svc_bd1, bd_node.sons[0].sons[0])
self.assertEqual(svc_bd2, bd_node.sons[1].sons[0])
# Now state working on the states
self.scheduler_loop(1, [[svc_bd2, 0, 'OK | value1=1 value2=2'], [svc_bd1, 0, 'OK | rtt=10']])
self.assertEqual('OK', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual('OK', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
state = bp_rule.get_state()
self.assertEqual(0, state)
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# Now we set the bd1 as soft/CRITICAL
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('SOFT', svc_bd1.state_type)
self.assertEqual(0, svc_bd1.last_hard_state_id)
# The business rule must still be 0
state = bp_rule.get_state()
self.assertEqual(0, state)
print "Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "ERP: Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# Now we get bd1 CRITICAL/HARD
self.scheduler_loop(1, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd1.state)
self.assertEqual('HARD', svc_bd1.state_type)
self.assertEqual(2, svc_bd1.last_hard_state_id)
# The rule must still be a 0 (or inside)
state = bp_rule.get_state()
self.assertEqual(0, state)
print "ERP: Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "ERP: Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# Now we also set bd2 as CRITICAL/HARD... byebye 0 :)
self.scheduler_loop(2, [[svc_bd2, 2, 'CRITICAL | value1=1 value2=2']])
self.assertEqual('CRITICAL', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(2, svc_bd2.last_hard_state_id)
# And now the state of the rule must be 2
state = bp_rule.get_state()
self.assertEqual(2, state)
# And now we must be CRITICAL/SOFT!
print "ERP: Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "ERP: Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('CRITICAL', svc_cor.state)
self.assertEqual('SOFT', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# OK, re recheck again, GO HARD!
print "ERP: Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "ERP: Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('CRITICAL', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(2, svc_cor.last_hard_state_id)
# And If we set one WARNING?
self.scheduler_loop(2, [[svc_bd2, 1, 'WARNING | value1=1 value2=2']])
self.assertEqual('WARNING', svc_bd2.state)
self.assertEqual('HARD', svc_bd2.state_type)
self.assertEqual(1, svc_bd2.last_hard_state_id)
# Must be WARNING (better no 0 value)
state = bp_rule.get_state()
self.assertEqual(1, state)
# And in a HARD
print "ERP: Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "ERP: Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('WARNING', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(1, svc_cor.last_hard_state_id)
print "All elements", bp_rule.list_all_elements()
print "IMPACT:", svc_bd2.impacts
for i in svc_bd2.impacts:
print i.get_name()
# Assert that Simple_Or Is an impact of the problem bd2
self.assertIn(svc_cor, svc_bd2.impacts)
# and bd1 too
self.assertIn(svc_cor, svc_bd1.impacts)
# And now all is green :)
self.scheduler_loop(2, [[svc_bd2, 0, 'OK | value1=1 value2=2'], [svc_bd1, 0, 'OK | value1=1 value2=2']])
print "ERP: Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "ERP: Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# And no more in impact
self.assertNotIn(svc_cor, svc_bd2.impacts)
self.assertNotIn(svc_cor, svc_bd1.impacts)
# And what if we set 2 service from distant rule CRITICAL?
# ERP should be still OK
# And now all is green :)
self.scheduler_loop(2, [[svc_bd1, 2, 'CRITICAL | value1=1 value2=2'], [svc_web1, 2, 'CRITICAL | value1=1 value2=2']])
print "ERP: Launch internal check"
svc_cor.launch_check(now-1)
c = svc_cor.actions[0]
self.assertEqual(True, c.internal)
self.assertTrue(c.is_launchable(now))
# ask the scheduler to launch this check
# and ask 2 loops: one for launch the check
# and another to integer the result
self.scheduler_loop(2, [])
# We should have no more the check
self.assertEqual(0, len(svc_cor.actions))
print "ERP: Look at svc_cor state", svc_cor.state
# What is the svc_cor state now?
self.assertEqual('OK', svc_cor.state)
self.assertEqual('HARD', svc_cor.state_type)
self.assertEqual(0, svc_cor.last_hard_state_id)
# We will try a simple 1of: bd1 OR/AND db2
def test_complex_ABCof_business_correlator(self):
self.run_complex_ABCof_business_correlator(with_pct=False)
# We will try a simple 1of: bd1 OR/AND db2
def test_complex_ABCof_pct_business_correlator(self):
self.run_complex_ABCof_business_correlator(with_pct=True)
def run_complex_ABCof_business_correlator(self, with_pct=False):
#
# Config is not correct because of a wrong relative path
# in the main config file
#
print "Get the hosts and services"
now = time.time()
host = self.sched.hosts.find_by_name("test_host_0")
host.checks_in_progress = []
host.act_depend_of = [] # ignore the router
router = self.sched.hosts.find_by_name("test_router_0")
router.checks_in_progress = []
router.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
A = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "A")
self.assertEqual(False, A.got_business_rule)
self.assertIs(None, A.business_rule)
B = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "B")
self.assertEqual(False, B.got_business_rule)
self.assertIs(None, B.business_rule)
C = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "C")
self.assertEqual(False, C.got_business_rule)
self.assertIs(None, C.business_rule)
D = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "D")
self.assertEqual(False, D.got_business_rule)
self.assertIs(None, D.business_rule)
E = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "E")
self.assertEqual(False, E.got_business_rule)
self.assertIs(None, E.business_rule)
if with_pct == False:
svc_cor = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Complex_ABCOf")
else:
svc_cor = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "Complex_ABCOf_pct")
self.assertEqual(True, svc_cor.got_business_rule)