-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMDiagnostor.py
1311 lines (1057 loc) · 51.9 KB
/
CMDiagnostor.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
# -*- coding: UTF-8 -*-
from __future__ import print_function
import numpy as np
import pandas as pd
import os
from scipy.stats import pearsonr
import datetime
import threading
from functools import cmp_to_key
import math
import sys
from scipy.optimize import dual_annealing
import pickle
import csv
class RT_Detection:
"""This class is designed for RT type anomaly detection
Attributes:
current: detection window before the alarm
comparison_today: today's comparison window
comparison_day_before: yesterday's comparison window
comparison_7days_before: comparison window 7 days before
"""
def __init__(self, current, comparison_today, comparison_day_before, comparison_7days_before):
self.current = current
self.comparison_today = comparison_today
self.comparison_day_before = comparison_day_before
self.comparison_7days_before = comparison_7days_before
def detection(self):
self.number_of_over_max_values0 = 0
self.delta_of_maximum_values0 = 0
self.number_of_over_average_values0 = 0
self.ratio_of_average_values0 = 0
self.number_of_over_max_values1 = 0
self.delta_of_maximum_values1 = 0
self.number_of_over_average_values1 = 0
self.ratio_of_average_values1 = 0
self.number_of_over_max_values7 = 0
self.delta_of_maximum_values7 = 0
self.number_of_over_average_values7 = 0
self.ratio_of_average_values7 = 0
maximum_value_in_comparison_window0 = -1
maximum_value_in_comparison_window1 = -1
maximum_value_in_comparison_window7 = -1
valid_count = 0
comparison_sum = 0
for point in self.comparison_today:
if (not np.isnan(point)):
maximum_value_in_comparison_window0 = max(maximum_value_in_comparison_window0, point)
valid_count = valid_count+1
comparison_sum = comparison_sum+point
if valid_count != 0:
average_value_in_comparison_window0 = comparison_sum/valid_count
else:
average_value_in_comparison_window0 = 0
comparison_sum = 0
valid_count = 0
for point in self.comparison_day_before:
if not np.isnan(point):
maximum_value_in_comparison_window1 = max(maximum_value_in_comparison_window1, point)
valid_count = valid_count+1
comparison_sum = comparison_sum+point
if valid_count != 0:
average_value_in_comparison_window1 = comparison_sum/valid_count
else:
average_value_in_comparison_window1 = 0
comparison_sum = 0
valid_count = 0
for point in self.comparison_7days_before:
if not np.isnan(point):
maximum_value_in_comparison_window7 = max(maximum_value_in_comparison_window7, point)
valid_count = valid_count+1
comparison_sum = comparison_sum+point
if valid_count != 0:
average_value_in_comparison_window7 = comparison_sum/valid_count
else:
average_value_in_comparison_window7 = 0
valid_count = 0
detection_sum = 0
for point in self.current:
if not np.isnan(point):
valid_count = valid_count+1
detection_sum = detection_sum+point
if valid_count != 0:
average_value_in_detection_window = detection_sum/valid_count
else:
average_value_in_detection_window = 0
maximum_value_in_detection_window = -1
for point in self.current:
if (not np.isnan(point)) and point > maximum_value_in_comparison_window0:
self.number_of_over_max_values0 = self.number_of_over_max_values0+1
if (not np.isnan(point)) and point > maximum_value_in_comparison_window1:
self.number_of_over_max_values1 = self.number_of_over_max_values1+1
if (not np.isnan(point)) and point > maximum_value_in_comparison_window7:
self.number_of_over_max_values7 = self.number_of_over_max_values7+1
if not np.isnan(point):
maximum_value_in_detection_window = max(maximum_value_in_detection_window, point)
if (not np.isnan(point)) and point > average_value_in_comparison_window0:
self.number_of_over_average_values0 = self.number_of_over_average_values0 + 1
if (not np.isnan(point)) and point > average_value_in_comparison_window1:
self.number_of_over_average_values1 = self.number_of_over_average_values1 + 1
if (not np.isnan(point)) and point > average_value_in_comparison_window7:
self.number_of_over_average_values7 = self.number_of_over_average_values7 + 1
self.delta_of_maximum_values0 = maximum_value_in_detection_window - maximum_value_in_comparison_window0
self.delta_of_maximum_values1 = maximum_value_in_detection_window - maximum_value_in_comparison_window1
self.delta_of_maximum_values7 = maximum_value_in_detection_window - maximum_value_in_comparison_window7
if average_value_in_comparison_window0 != 0:
self.ratio_of_average_values0 = average_value_in_detection_window/average_value_in_comparison_window0
else:
self.ratio_of_average_values0 = 0
if average_value_in_comparison_window1 != 0:
self.ratio_of_average_values1 = average_value_in_detection_window/average_value_in_comparison_window1
else:
self.ratio_of_average_values1 = 0
if average_value_in_comparison_window7 != 0:
self.ratio_of_average_values7 = average_value_in_detection_window/average_value_in_comparison_window7
else:
self.ratio_of_average_values7 = 0
return [self.number_of_over_max_values0, self.delta_of_maximum_values0, self.number_of_over_average_values0, self.ratio_of_average_values0,
self.number_of_over_max_values1, self.delta_of_maximum_values1, self.number_of_over_average_values1, self.ratio_of_average_values1,
self.number_of_over_max_values7, self.delta_of_maximum_values7, self.number_of_over_average_values7, self.ratio_of_average_values7]
class Span:
"""This class represents 'call' in our framework
Attributes:
caller: the caller node of this call
callee: the callee node of this call
metric: time series data of this call
entry_span: the entry call related to this call whose caller or callee violates the SLO requirements
is_entry: a boolean variable that indicates whether this call is an entry
"""
def __init__(self, caller, callee, metric, entry_span=None, is_entry=False):
self.caller = caller
self.callee = callee
self.metric = metric
self.entry_span = entry_span
self.callgraph_index = -1
self.qpm_anomaly = False
self.ec_anomaly = False
self.rt_anomaly = False
self.ec_beta = 0.0
self.rt_beta = 0.0
self.is_entry = is_entry
if str(type(self.caller)) == "<class '__main__.Node'>":
if self.caller.server == item_server or self.callee.server == item_server:
self.is_entry = True
else:
if self.caller[0] == item_server or self.callee[0] == item_server:
self.is_entry=True
self.extra_span = False
def __eq__(self, span):
if span == None:
return False
return self.caller == span.caller and self.callee == span.callee
def __hash__(self):
return hash(self.caller)+hash(self.callee)
def __str__(self):
return str(self.caller)+' \n'+str(self.callee)
def get_caller(self):
return self.caller
def get_callee(self):
return self.callee
def normalize_metric(self):
span_str = str(self.get_caller())+' '+str(self.get_callee())
try:
request_list = split_data(self.metric[0])
except:
request_list = [np.nan]*1440
try:
duration_list = split_data(self.metric[1])
except:
duration_list = [np.nan]*1440
try:
exception_list = split_data(self.metric[2])
except:
exception_list = [np.nan]*1440
try:
timeout_list = split_data(self.metric[3])
except:
timeout_list = [np.nan]*1440
self.qpm = request_list
self.ec = [np.nan]*1440
self.rt = [np.nan]*1440
for exception, timeout, index in zip(exception_list, timeout_list, range(0,1440)):
if (not np.isnan(exception)) and (not np.isnan(timeout)):
self.ec[index] = exception + timeout
for duration, request, index in zip(duration_list, request_list, range(0, 1440)):
if (not np.isnan(duration)) and (not np.isnan(duration)):
self.rt[index] = duration/request
def compute_pearsonr_to_entry_span(self):
if self.is_entry == True:
self.entry_span = self
compare_ec = self.entry_span.ec
compare_rt = self.entry_span.rt
data1_ec = []
data2_ec = []
data1_rt = []
data2_rt = []
for data_point, compare_point in zip(self.ec, compare_ec):
if (not np.isnan(data_point)) and (not np.isnan(compare_point)):
data1_ec.append(data_point)
data2_ec.append(compare_point)
for data_point, compare_point in zip(self.rt, compare_rt):
if (not np.isnan(data_point)) and (not np.isnan(compare_point)):
data1_rt.append(data_point)
data2_rt.append(compare_point)
try:
if len(data1_ec) > 10:
ec_similarity, _ = pearsonr(data1_ec, data2_ec)
self.ec_similarity = abs(ec_similarity)
else:
self.ec_similarity = 0.0
if len(data1_rt) > 10:
rt_similarity, _ = pearsonr(data1_rt, data2_rt)
self.rt_similarity = abs(rt_similarity)
else:
self.rt_similarity = 0.0
except:
pass
def metric_valid(self):
qpm_count = 0
ec_count = 0
rt_count = 0
for i, j, k in zip(self.qpm, self.ec, self.rt):
if not np.isnan(i):
qpm_count = qpm_count + 1
if not np.isnan(j):
ec_count = ec_count + 1
if not np.isnan(k):
rt_count = rt_count + 1
return qpm_count > 20 and ec_count > 20 and rt_count > 20
def metric_valid_30_mins_before_alarm(self, alarm_time):
if alarm_time > 30:
qpm_count = 0
ec_count = 0
rt_count = 0
for i, j, k in zip(self.qpm[alarm_time-30:alarm_time], self.ec[alarm_time-30:alarm_time], self.rt[alarm_time-30:alarm_time]):
if not np.isnan(i):
qpm_count = qpm_count + 1
if not np.isnan(j):
ec_count = ec_count + 1
if not np.isnan(k):
rt_count = rt_count + 1
return qpm_count>=5 and ec_count>=5 and rt_count>=5
else:
return False
class Node:
"""This class represents one end of a call(caller or callee)
Attributes:
server: name of the service
service: name of the port
method: method name
set: custom tag
"""
def __init__(self, server, service, method, set):
self.server = server
self.service = service
self.method = method
self.set = set
self.depth = 0
self.list_index = -1
self.callgraph_index = -1
def __eq__(self, node):
if node == None:
return False
return self.server == node.server and self.service == node.service and self.method == node.method and self.set == node.set
def __hash__(self):
return hash(self.server+self.service+self.method+self.set)
def __str__(self):
return '('+self.server+','+self.service+','+self.method+','+self.set+')'
def __add__(self, node):
return self.get_turple() + node.get_turple()
def get_turple(self):
return (self.server, self.service, self.method, self.set)
class Root_Cause:
"""This class represents the root cause founded during the exploration stage
"""
def __init__(self, span, turple, root_score):
self.turple = turple
self.root_score = root_score
self.span = span
ec_sum = 0
request_sum = 0
for point in self.span.qpm[alarm_time-10:alarm_time]:
if (not np.isnan(point)):
request_sum = request_sum + point
for point in self.span.ec[alarm_time-10:alarm_time]:
if (not np.isnan(point)):
ec_sum = ec_sum + point
if request_sum != 0:
self.error_rate = ec_sum / request_sum
else:
self.error_rate = 0
def __eq__(self, root_cause):
if root_cause == None:
return False
else:
return self.turple == root_cause.turple
class Root_Cause_Server:
"""This class represents the root cause aggregate to the server scale
"""
def __init__(self, server, root_cause_lsit):
self.server = server
self.root_cause_list = root_cause_lsit
self.root_cause_number = len(root_cause_lsit)
self.max_root_score = 0.0
self.max_error_rate = 0.0
sum_error_rate = 0.0
sum = 0.0
for root_cause in self.root_cause_list:
sum = sum + root_cause.root_score
self.max_root_score = max(self.max_root_score, root_cause.root_score)
self.max_error_rate = max(self.max_error_rate, root_cause.error_rate)
sum_error_rate = sum_error_rate + root_cause.error_rate
self.average_root_score = sum / self.root_cause_number
self.average_error_rate = sum_error_rate/self.root_cause_number
def __eq__(self, target):
if target == None:
return False
else:
return self.server == target.server and self.root_cause_list == target.root_cause_list
def __str__(self):
self_str = self.server + '\n'
return self_str
class Regression_Pruning:
"""This class works during thr regression pruning stage
"""
def __init__(self, downstream_span, compare_span_list):
self.downstream_span = downstream_span
self.compare_span_list = compare_span_list
self.lack_data=False
self.compute_regression()
def compute_regression(self):
temp_turple = (self.downstream_span, )
for span in self.compare_span_list:
temp_turple = temp_turple + (span, )
global similarity_array_dict
if str(type(similarity_array_dict.get(temp_turple))) != "<class 'NoneType'>":
similarity_result = similarity_array_dict[temp_turple]
self.real_upstream_span_list = []
for index, similarity in enumerate(similarity_result):
if similarity > regression_threshold:
self.real_upstream_span_list.append(self.compare_span_list[index])
else:
array_u = list()
array_d = list()
for span in self.compare_span_list:
array_u.append(list())
for index in range(0, 1440):
flag = True
for span in self.compare_span_list:
if np.isnan(span.qpm[index]):
flag = False
if np.isnan(self.downstream_span.qpm[index]):
flag = False
if flag:
for index_2, span in enumerate(self.compare_span_list):
array_u[index_2].append(span.qpm[index])
array_d.append(self.downstream_span.qpm[index])
flag = False
if len(array_d) == 0:
flag = True
array_d = np.array(array_d)
array_us = np.array(array_u)
X = np.transpose(array_us)
if not flag:
similarity_result = linear_model_func(X, array_d)
similarity_array_dict[temp_turple] = similarity_result
self.real_upstream_span_list = []
for index, similarity in enumerate(similarity_result):
if similarity > regression_threshold:
self.real_upstream_span_list.append(self.compare_span_list[index])
else:
self.real_upstream_span_list = self.compare_span_list
def linear_model_func(A1,b1):
num_x = np.shape(A1)[1]
global regression_param
theta = []
for i in range(0, num_x):
theta.append(1/num_x)
def my_func(x):
ls = (b1-np.dot(A1,x))**2
result = np.sum(ls)
return result
bnds = [(0,regression_param)]
for i in range(num_x-1):
bnds.append((0,regression_param))
res1 = dual_annealing(my_func, bounds = bnds, maxiter=1000)
return res1.x
class Callgraph:
"""This class represents the call graph in our framework
"""
def __init__(self, all_spans, entry_spans):
self.all_spans = all_spans
self.entry_spans = entry_spans
self.all_nodes = []
self.ec_anomaly_entry_spans = []
self.rt_anomaly_entry_spans = []
self.adjancy_matrix = []
self.generate_all_nodes()
self.normalize_all_spans()
self.generate_adjacency_matrix()
def normalize_all_spans(self):
for span in self.all_spans:
span.normalize_metric()
if span.caller.server == item_server or span.callee.server == item_server:
span.is_entry = True
anomaly_detection_for_one_span(span)
if span.is_entry and span.ec_anomaly:
self.ec_anomaly_entry_spans.append(span)
if span.is_entry and span.rt_anomaly:
self.rt_anomaly_entry_spans.append(span)
def generate_all_nodes(self):
for span in self.all_spans:
if str(type(span.get_caller())) == "<class '__main__.Node'>":
caller_node = Node(*span.get_caller().get_turple())
callee_node = Node(*span.get_callee().get_turple())
else:
caller_node = Node(*span.get_caller())
callee_node = Node(*span.get_callee())
if caller_node not in self.all_nodes:
self.all_nodes.append(caller_node)
span.caller = caller_node
else:
span.caller = self.all_nodes[self.all_nodes.index(caller_node)]
if callee_node not in self.all_nodes:
self.all_nodes.append(callee_node)
span.callee = callee_node
else:
span.callee = self.all_nodes[self.all_nodes.index(callee_node)]
print('call graph span number:', len(self.all_spans), ' call graph node number:', len(self.all_nodes))
for index, node in enumerate(self.all_nodes):
node.callgraph_index = index
def generate_adjacency_matrix(self):
nodes_number = len(self.all_nodes)
for i in range(0, nodes_number):
temp_list = [None]*nodes_number
self.adjancy_matrix.append(temp_list)
for span in self.all_spans:
temp_caller_node = span.get_caller()
temp_callee_node = span.get_callee()
caller_node_index = temp_caller_node.callgraph_index
callee_node_index = temp_callee_node.callgraph_index
self.adjancy_matrix[caller_node_index][callee_node_index] = span
count = 0
for row in range(0, nodes_number):
for col in range(0, nodes_number):
if self.adjancy_matrix[row][col]!=None:
count = count + 1
def get_root_cause_server_list(self, root_cause_list):
root_cause_list = sorted(root_cause_list, key=lambda x: x.root_score, reverse=True)
root_cause_server_dict = {}
for root_cause in root_cause_list:
if root_cause_server_dict.get(root_cause.turple[0]) == None:
root_cause_server_dict[root_cause.turple[0]] = []
root_cause_server_dict[root_cause.turple[0]].append(root_cause)
root_cause_server_list = []
for server, temp_root_cause_list in root_cause_server_dict.items():
root_cause_server = Root_Cause_Server(server, temp_root_cause_list)
root_cause_server_list.append(root_cause_server)
def cmp(root_cause_server1, root_cause_server2):
if root_cause_server1.root_cause_number < root_cause_server2.root_cause_number:
return 1
elif root_cause_server1.root_cause_number == root_cause_server2.root_cause_number:
if root_cause_server1.average_error_rate < root_cause_server2.average_error_rate:
return 1
elif root_cause_server1.average_error_rate == root_cause_server2.average_error_rate:
if root_cause_server1.max_root_score < root_cause_server2.max_root_score:
return 1
else:
return -1
else:
return -1
else:
return -1
root_cause_server_list = sorted(root_cause_server_list, key=cmp_to_key(cmp), reverse=False)
return root_cause_server_list
def compute_pearsonr_between_two_spans(span1, span2, option):
if option == 'qpm':
qpm1 = span1.qpm[alarm_time-30:alarm_time]
qpm2 = span2.qpm[alarm_time-30:alarm_time]
data1 = []
data2 = []
for point1, point2 in zip(qpm1, qpm2):
if (not np.isnan(point1)) and (not np.isnan(point2)):
data1.append(point1)
data2.append(point2)
if len(data1) <= 2:
return 0.0
similarity, _ = pearsonr(data1, data2)
return abs(similarity)
elif option == 'ec':
ec1 = span1.ec[alarm_time-30:alarm_time]
ec2 = span2.ec[alarm_time-30:alarm_time]
data1 = []
data2 = []
for point1, point2 in zip(ec1, ec2):
if (not np.isnan(point1)) and (not np.isnan(point2)):
data1.append(point1)
data2.append(point2)
if len(data1) <= 2:
return 0.0
similarity, _ = pearsonr(data1, data2)
return abs(similarity)
elif option == 'rt':
rt1 = span1.rt[alarm_time-30:alarm_time]
rt2 = span2.rt[alarm_time-30:alarm_time]
data1 = []
data2 = []
for point1, point2 in zip(rt1, rt2):
if (not np.isnan(point1)) and (not np.isnan(point2)):
data1.append(point1)
data2.append(point2)
if len(data1) <= 2:
return 0.0
similarity, _ = pearsonr(data1, data2)
return abs(similarity)
def anomaly_detection_for_one_span(span):
caller_turple = span.get_caller().get_turple()
callee_turple = span.get_callee().get_turple()
metric1 = None
metric7 = None
caller_callee_turple = caller_turple + callee_turple
if caller_callee1.get(caller_callee_turple) != None:
metric1 = caller_callee1[caller_callee_turple]
if caller_callee7.get(caller_callee_turple) != None:
metric7 = caller_callee7[caller_callee_turple]
if metric1 == None or metric7 == None:
return
span_day_before = Span(caller_turple, callee_turple, metric1)
span_7days_before = Span(caller_turple, callee_turple, metric7)
span_day_before.normalize_metric()
span_7days_before.normalize_metric()
current_rt = span.rt[alarm_time-10: alarm_time]
comparison_rt_today = span.rt[alarm_time-70: alarm_time-10]
comparison_rt_day_before = span_day_before.rt[alarm_time-60: alarm_time]
comparison_rt_7days_before = span_7days_before.rt[alarm_time-60: alarm_time]
rt_detection = RT_Detection(current_rt, comparison_rt_today, comparison_rt_day_before, comparison_rt_7days_before)
y = iso_forest.predict([rt_detection.detection()])[0]
if y==-1:
span.rt_anomaly = True
span.rt_beta = 1.0
else:
span.rt_anomaly = False
span.rt_beta=0.0
current_ec = span.ec[alarm_time-10: alarm_time]
comparison_ec_today = span.ec[alarm_time-70:alarm_time-10]
comparison_ec_day_before = span_day_before.ec[alarm_time-60: alarm_time]
comparison_ec_7days_before = span_7days_before.ec[alarm_time-60: alarm_time]
exceptions_ec = [-1] * 10
anomaly_detection_compute_95(exceptions_ec, comparison_ec_today, current_ec, 1)
anomaly_detection_compute_95(exceptions_ec, comparison_ec_day_before, current_ec, 1)
anomaly_detection_compute_95(exceptions_ec, comparison_ec_7days_before, current_ec, 1)
ec_valid = 10 - exceptions_ec.count(-1)
if ec_valid != 0:
if 1.0*exceptions_ec.count(1) / (10-exceptions_ec.count(-1)) > threshold:
span.ec_anomaly = True
span.ec_beta = 1.0
else:
span.ec_beta = 0.0
def ninty_five(comparison):
data_list = []
for i in comparison:
if not np.isnan(i):
data_list.append(i)
data_list = sorted(data_list, reverse=True)
length = len(data_list)
min_index = math.ceil(0.95 * length)
max_index = math.ceil(0.05 * length)
if min_index >= len(data_list):
min_index = len(data_list) - 1
return (data_list[min_index], data_list[max_index])
def data_valid(data_list):
count = 0
for point in data_list:
if not np.isnan(point):
count = count + 1
return count > 5
def anomaly_detection_compute_95(exceptions, comparison, current, kind):
if (not data_valid(comparison)) or (not data_valid(current)):
return
min, max = ninty_five(comparison)
for index, date_point in enumerate(current):
if not np.isnan(current[index]):
exceptions[index] = 0
if kind == 0:
if current[index]< min or current[index] > max:
exceptions[index] = 1
else:
if current[index] > max:
exceptions[index] = 1
def split_data(data):
time_list = [np.nan]*1440
data_points = data.split(',')
for data_point in data_points:
time_list[int(data_point.split(':')[0])] = float(data_point.split(':')[1])
return time_list
def flex_similarity_pruning(downstream_span, upstream_spans):
pruning_list = Regression_Pruning(downstream_span, upstream_spans)
return pruning_list.real_upstream_span_list
def merge_dict(dic1, dic2):
for key, value in dic2.items():
if key in dic1:
dic1[key].extend(value)
else:
dic1[key] = value
def read_one_file(lock, path, file, caller_data_local, callee_data_local, caller_callee_local):
thread_caller_data = {}
thread_callee_data = {}
thread_caller_callee = {}
if file.startswith('part'):
file_path = path+file
temp_file = pd.read_csv(file_path, sep='|')
for i, line in temp_file.iterrows():
cur_node = (line[1], line[2], line[3], line[4],
line[5], line[6], line[7], line[8])
if thread_caller_data.get((line[1], line[2], line[3], line[4])) == None:
thread_caller_data[(
line[1], line[2], line[3], line[4])] = list()
thread_caller_data[(line[1], line[2], line[3],
line[4])].append(cur_node)
if thread_callee_data.get((line[5], line[6], line[7], line[8])) == None:
thread_callee_data[(
line[5], line[6], line[7], line[8])] = list()
thread_callee_data[(line[5], line[6], line[7],
line[8])].append(cur_node)
thread_caller_callee[cur_node] = [
line[9], line[10], line[12], line[13]]
lock.acquire()
merge_dict(caller_data_local, thread_caller_data)
merge_dict(callee_data_local, thread_callee_data)
merge_dict(caller_callee_local, thread_caller_callee)
lock.release()
def read_files(path):
temp_caller_data = {}
temp_callee_data = {}
temp_caller_callee = {}
files = os.listdir(path)
threads_local = []
lock = threading.Lock()
for file in files:
thread = threading.Thread(target=read_one_file, args=(
lock, path, file, temp_caller_data, temp_callee_data, temp_caller_callee))
threads_local.append(thread)
thread.start()
for thread in threads_local:
thread.join()
return (temp_caller_data, temp_callee_data, temp_caller_callee)
def read_one_file_past(lock, path, file, caller_callee_local):
thread_caller_callee = {}
if file.startswith('part'):
file_path = path+file
temp_file = pd.read_csv(file_path, sep='|')
for i, line in temp_file.iterrows():
cur_node = (line[1], line[2], line[3], line[4],
line[5], line[6], line[7], line[8])
thread_caller_callee[cur_node] = [
line[9], line[10], line[12], line[13]]
lock.acquire()
merge_dict(caller_callee_local, thread_caller_callee)
lock.release()
def read_files_past(path):
temp_caller_callee = {}
files = os.listdir(path)
threads_local = []
lock = threading.Lock()
for file in files:
thread = threading.Thread(target=read_one_file_past, args=(
lock, path, file, temp_caller_callee))
threads_local.append(thread)
thread.start()
for thread in threads_local:
thread.join()
return temp_caller_callee
def get_uplink_spans(span, entry_span):
temp_spans = []
if callee_data.get(span.get_caller()) != None:
for caller_callee_turple in callee_data[span.get_caller()]:
caller = (caller_callee_turple[0], caller_callee_turple[1],
caller_callee_turple[2], caller_callee_turple[3])
callee = (caller_callee_turple[4], caller_callee_turple[5],
caller_callee_turple[6], caller_callee_turple[7])
metric = caller_callee[caller_callee_turple]
new_span = Span(caller, callee, metric, entry_span=entry_span)
new_span.normalize_metric()
# if (new_span not in temp_spans) and (new_span not in uplink_cache_spans):
if new_span.metric_valid_30_mins_before_alarm(alarm_time):
temp_spans.append(new_span)
return temp_spans
def get_downlink_spans(span, entry_span):
temp_spans = []
if caller_data.get(span.get_callee()) != None:
for caller_callee_turple in caller_data[span.get_callee()]:
caller = (caller_callee_turple[0], caller_callee_turple[1],
caller_callee_turple[2], caller_callee_turple[3])
callee = (caller_callee_turple[4], caller_callee_turple[5],
caller_callee_turple[6], caller_callee_turple[7])
metric = caller_callee[caller_callee_turple]
new_span = Span(caller, callee, metric, entry_span=entry_span)
new_span.normalize_metric()
if new_span.metric_valid_30_mins_before_alarm(alarm_time):
if (caller, callee) not in cache_span_dict.keys():
temp_spans.append(new_span)
temp_upstream_spans = get_uplink_spans(span, entry_span)
for temp_up_span in temp_upstream_spans:
temp_up_span.extra_span = True
global extra_span_list
extra_span_list.extend(temp_upstream_spans)
cache_span_dict[(caller, callee)] = 0
return temp_spans
def extend_entry_span(caller_spans, callee_spans):
downlink_cache_spans = []
for span in caller_spans:
downlink_cache_spans.extend(get_downlink_spans(span, span))
while len(downlink_cache_spans) != 0:
top_span = downlink_cache_spans[0]
downlink_cache_spans.pop(0)
downlink_spans.append(top_span)
downlink_cache_spans.extend(get_downlink_spans(top_span, top_span.entry_span))
all_related_spans = []
entry_spans = []
entry_spans.extend(caller_spans)
entry_spans.extend(callee_spans)
all_related_spans.extend(entry_spans)
all_related_spans.extend(uplink_spans)
all_related_spans.extend(downlink_spans)
return (entry_spans, list(set(all_related_spans)))
def get_downstream_spans(cg, span):
callee_index = span.callee.callgraph_index
temp_span_list = []
for col in range(0, len(cg.all_nodes)):
if cg.adjancy_matrix[callee_index][col] != None and cg.temp_cache_span_dict.get(cg.adjancy_matrix[callee_index][col]) == None:
cg.adjancy_matrix[callee_index][col].temp_exploration_upstream_span = span
temp_span_list.append(cg.adjancy_matrix[callee_index][col])
cg.temp_cache_span_dict[cg.adjancy_matrix[callee_index][col]] = 1
return temp_span_list
def get_upstream_spans(cg, span):
caller_index = span.caller.callgraph_index
temp_span_list = []
for row in range(0, len(cg.all_nodes)):
if cg.adjancy_matrix[row][caller_index] != None:
temp_span_list.append(cg.adjancy_matrix[row][caller_index])
return temp_span_list
def root_cause_exploration(cg):
cg.qpm_root_cause_list = []
cg.ec_root_cause_list = []
cg.rt_root_cause_list = []
global explored_ec_anomaly_entry_spans
global explored_rt_anomaly_entry_spans
for span in cg.all_spans:
span.temp_exploration_upstream_span = None
span.compute_pearsonr_to_entry_span()
ec_anomaly_entry_spans = cg.ec_anomaly_entry_spans
rt_anomaly_entry_spans = cg.rt_anomaly_entry_spans
for entry_span in ec_anomaly_entry_spans:
if entry_span not in explored_ec_anomaly_entry_spans:
explored_ec_anomaly_entry_spans.append(entry_span)
else:
continue
cache_span_list = []
cg.temp_cache_span_dict = {}
temp_span_list = get_downstream_spans(cg, entry_span)
flag = False
for temp_span in temp_span_list:
if temp_span.ec_anomaly:
temp_similarity = compute_pearsonr_between_two_spans(entry_span, temp_span, 'ec')
if temp_similarity > microhecl_threshold:
cache_span_list.append(temp_span)
else:
root_cause = Root_Cause(entry_span, entry_span.callee.get_turple(), entry_span.ec_similarity)
cg.ec_root_cause_list.append(root_cause)
flag = True
if not flag:
root_cause = Root_Cause(entry_span, entry_span.callee.get_turple(), entry_span.ec_similarity)
cg.ec_root_cause_list.append(root_cause)
while len(cache_span_list)>0:
top_span = cache_span_list[0]
cache_span_list.pop(0)
if top_span.is_entry and (top_span in explored_ec_anomaly_entry_spans):
continue
if top_span.is_entry and (top_span not in explored_ec_anomaly_entry_spans) and (top_span.temp_exploration_upstream_span == None):
explored_ec_anomaly_entry_spans.append(top_span)
temp_span_list = get_downstream_spans(cg, top_span)
flag = False
for temp_span in temp_span_list:
if temp_span.ec_anomaly:
flag = True
temp_similarity = compute_pearsonr_between_two_spans(top_span, temp_span, 'ec')
if temp_similarity > microhecl_threshold:
cache_span_list.append(temp_span)
else:
root_cause = Root_Cause(top_span, top_span.callee.get_turple(), top_span.ec_similarity)
cg.ec_root_cause_list.append(root_cause)
if not flag:
root_cause = Root_Cause(top_span, top_span.callee.get_turple(), top_span.ec_similarity)
cg.ec_root_cause_list.append(root_cause)
if top_span.is_entry and (top_span not in explored_ec_anomaly_entry_spans) and (top_span.temp_exploration_upstream_span != None):
explored_ec_anomaly_entry_spans.append(top_span)
temp_upstream_span_list = get_upstream_spans(cg, top_span)
if len(temp_upstream_span_list) > 1:
real_upstream_span_list = flex_similarity_pruning(top_span, temp_upstream_span_list)
if top_span.temp_exploration_upstream_span in real_upstream_span_list:
temp_span_list = get_downstream_spans(cg, top_span)
flag = False
for temp_span in temp_span_list:
if temp_span.ec_anomaly:
temp_similarity = compute_pearsonr_between_two_spans(top_span, temp_span, 'ec')