-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranker.py
432 lines (358 loc) · 17.1 KB
/
ranker.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
import numpy as np
from difference import *
from collections import OrderedDict
log_path = os.getcwd() + '/log/' + str(datetime.datetime.now().strftime(
'%Y-%m-%d')) + '_changerca.log'
logger = Logger(log_path, logging.DEBUG, __name__).getlog()
class Ranker:
def __init__(self,
case_path,
case_name,
rca="gied",
gray_the=0.7,
p_threshold=0.15
):
"""
func init
:input
- alert_time: alert time from idkey
- alert_module: alert module from idkey
- root_module: moduel with fualty change
- gray_the: threshold for determine gray change failure
- p_threshold: threshold for p-vaule to determine sginificant different
"""
self.base_path = os.path.join(case_path, rca)
self.case_file = os.path.join(self.base_path, case_name)
self.case_name = case_name
with open(self.case_file, 'r') as file:
print(self.case_file)
self.data = json.load(file)
self.alert_time = self.data["detect_time"]
self.rca = rca
self.alert_time_stamp = self.data["detect_timestamp"]
if rca == "microrca":
self.alert_module = self.data["Microrca_result"]
elif rca == "microscope":
self.alert_module = self.data["Microscope_result"]
else:
self.alert_module = self.data["GIED_result"]
self.root_module = self.data["fault_release_service"]
self.day = self.data["detect_time"].split(" ")[0]
self.gray_the = gray_the
self.p_threshold = p_threshold
def root_cause_change_identifycation(self):
"""
func root_cause_change_identifycation: get root cause ranking result
:return
- final result dict {"module_name": {"score": 1.0, "platform": "platform name", "task_id": "task id of change ticket", "fault_type": "change"}}
"""
gray_change_result = None
final_score = {}
final_score[self.alert_module] = {}
logger.info("-------------------------------------------------------")
logger.info("---------------------Case Information------------------")
logger.info("%s, %s, rca: %s, label: %s", self.case_name, self.alert_time,
self.alert_module, self.root_module)
if self.alert_module in self.data["plan"]:
file = self.alert_module + ".json"
file = os.path.join(self.base_path, file)
if os.path.exists(file):
print(file)
with open(file, "r+") as f:
change_data = json.load(f)
else:
logger.error("file %s not exist", file)
differentiator = Differentiator(self.base_path,
self.case_file, self.rca, self.p_threshold)
if len(change_data["host_list_change"]) > 0 and len(change_data["host_list_old"]) > 0:
# alert module gray change
gray_change_result = differentiator.difference_method(
self.alert_module)
if gray_change_result[1] > self.gray_the:
logger.info("gray_change_result %s", gray_change_result)
final_score[gray_change_result[0]] = {}
final_score[gray_change_result[0]
]["score"] = gray_change_result[1]
final_score[gray_change_result[0]
]["fault_type"] = "change"
logger.info(
"-------------gray change fault %s ----------------", final_score)
return final_score
old_difference_score = differentiator.difference_method(
self.alert_module, "old")
# difference_score_item = (
# gray_change_result[3] + old_difference_score[3]) / (gray_change_result[4] + old_difference_score[4])
# logger.info("gray_change_result %s, %s",
# gray_change_result, old_difference_score)
if gray_change_result[2] + old_difference_score[2] == 1:
# one point fault
final_score[self.alert_module]["fault_type"] = "other"
logger.info(
"-------------one point fault %s ----------------", final_score)
return final_score
anomaly_resource = self.determine_resource_fault()
for item in anomaly_resource:
if len(anomaly_resource[item]) > 0:
final_score[self.alert_module][item] = anomaly_resource[item]
final_score[self.alert_module]["fault_type"] = "other"
logger.info(
"-------------resource fault %s ----------------", final_score)
return final_score
final_score = self.suspicious_change_ranker()
final_score = dict(sorted(final_score.items(), key=lambda x: (
x[1]['score'], random.random()), reverse=True))
# final_score = OrderedDict(
# sorted(final_score.items(), key=lambda x: (-x[1]['score'])))
logger.info("-------------------final score------------------")
logger.info("%s, %s, rca: %s, label: %s", self.case_name, self.alert_time,
self.alert_module, self.root_module)
return final_score
def root_cause_change_identifycation_wodep(self):
"""
func root_cause_change_identifycation: get root cause ranking result
:return
- final result dict {"module_name": {"score": 1.0, "platform": "platform name", "task_id": "task id of change ticket", "fault_type": "change"}}
"""
gray_change_result = None
final_score = {}
final_score[self.alert_module] = {}
logger.info("-------------------------------------------------------")
logger.info("---------------------Case Information------------------")
logger.info("%s, %s, rca: %s, label: %s", self.case_name, self.alert_time,
self.alert_module, self.root_module)
for svc in self.data["plan"]:
file = svc + ".json"
file = os.path.join(self.base_path, file)
if os.path.exists(file):
print(file)
with open(file, "r+") as f:
change_data = json.load(f)
else:
logger.error("file %s not exist", file)
differentiator = Differentiator(self.base_path,
self.case_file, self.rca, self.p_threshold)
if len(change_data["host_list_change"]) > 0 and len(change_data["host_list_old"]) > 0:
# alert module gray change
gray_change_result = differentiator.difference_method(
svc)
if gray_change_result[1] is not None and gray_change_result[1] > self.gray_the:
logger.info("gray_change_result %s", gray_change_result)
final_score[gray_change_result[0]] = {}
final_score[gray_change_result[0]
]["score"] = gray_change_result[1]
final_score[gray_change_result[0]
]["fault_type"] = "change"
logger.info(
"-------------gray change fault %s ----------------", final_score)
return final_score
# final_score = self.suspicious_change_ranker()
# final_score = dict(sorted(final_score.items(), key=lambda x: (x[1]['score'], random.random()), reverse=True))
# final_score = OrderedDict(
# sorted(final_score.items(), key=lambda x: (-x[1]['score'])))
logger.info("-------------------final score------------------")
logger.info("%s, %s, rca: %s, label: %s", self.case_name, self.alert_time,
self.alert_module, self.root_module)
return final_score
def suspicious_change_ranker(self):
"""
func suspicious_change_ranker: get suspicious change score if it is not gray change and other fault
:return
- final result dict {"module_name": {"score": 1.0, "platform": "platform name", "task_id": "task id of change ticket", "fault_type": "suspicious"}}
"""
final_score = {}
differentiator = Differentiator(self.base_path,
self.case_file, self.rca, self.p_threshold)
deepth_list = self.get_deepth()
deep_score = self.dependency_ranker(deepth_list)
change_time_list = self.get_last_change_time()
time_score = self.time_ranker(change_time_list)
difference_score = differentiator.get_all_difference_result()
old_difference_score = differentiator.get_all_difference_result("old")
logger.info("difference_score: %s", difference_score)
for item in difference_score:
if difference_score[item][1] == "gray":
if difference_score[item][0] - old_difference_score[item][0] > self.gray_the:
final_score[item] = {}
final_score[item]["score"] = 3 + difference_score[item][0]
final_score[item]["fault_type"] = "change"
for item in time_score:
if item not in final_score and item in difference_score and time_score[item] != 0:
# logger.info("%s, %s, %s, %s", difference_score[item][2], old_difference_score[item] [2], difference_score[item][3], old_difference_score[item][3])
# if difference_score[item][1] == "gray":
final_score[item] = {}
final_score[item]["score"] = time_score[item] + \
difference_score[item][0] + deep_score[item]
final_score[item]["fault_type"] = "suspicious"
return final_score
def determine_resource_fault(self):
"""
func determine_resource_fault: determine whether the alert is caused by the resource fault
return:
- anomaly_result: dict of anomaly resource
"""
file = self.base_path + "/" + self.alert_module + ".json"
if os.path.exists(file):
with open(file, "r+") as f:
change_data = json.load(f)
else:
logger.error("file %s not exist", file)
anomaly_result = {
"cpu": [],
"memory": []
}
for instance in change_data["host_list_change"]:
if "resource" in instance:
if "cpu" in instance["resource"] and len(instance["resource"]["cpu"]) > 0:
i = 0
for item in instance["resource"]["cpu"]:
if item > 80:
i = i + 1
if i > 3:
anomaly_result["cpu"].append(instance["ip_addr"])
if "memory" in instance["resource"] and len(instance["resource"]["memory"]) > 0:
i = 0
for item in instance["resource"]["memory"]:
if item > 80:
i = i + 1
if i > 3:
anomaly_result["memory"].append(instance["ip_addr"])
for instance in change_data["host_list_old"]:
if "resource" in instance:
if "cpu" in instance["resource"] and len(instance["resource"]["cpu"]) > 0:
i = 0
for item in instance["resource"]["cpu"]:
if item > 80:
i = i + 1
if i > 3:
anomaly_result["cpu"].append(instance["ip_addr"])
if "memory" in instance["resource"] and len(instance["resource"]["memory"]) > 0:
i = 0
for item in instance["resource"]["memory"]:
if item > 80:
i = i + 1
if i > 3:
anomaly_result["memory"].append(instance["ip_addr"])
return anomaly_result
def get_deepth(self):
"""
func get_deepth: get deepth of all service
return:
- deepth_dict: dict of deepth {"servicea": 0, "serviceb": 1}
"""
deepth_dict = {}
# logger.info(self.data)
spicious_set_name = "spicious_set_" + self.rca
for item in self.data["plan"]:
if item in self.data[spicious_set_name]:
file = self.base_path + "/" + item + ".json"
with open(file, 'r') as file:
data = json.load(file)
deepth_dict[item] = int(data["deepth"])
# logger.info(deepth_list)
return deepth_dict
def get_last_change_time(self):
"""
get_last_change_time: get last change time of all service
:return
- change_time_dict: dict of time {"servicea": 60, "serviceb": 1200}
"""
change_time_dict = {}
spicious_set_name = "spicious_set_" + self.rca
for module in self.data["plan"]:
if module in self.data[spicious_set_name]:
processes = self.data["plan"][module]["process"]
last_time = 1
for process in processes:
# logger.info("process: %s, %s",module, process)
if last_time < int(process) and int(process) < self.alert_time_stamp:
last_time = int(process)
change_time_dict[module] = last_time
if last_time == 1:
change_time_dict[module] = 0
# logger.info(change_time_list)
return change_time_dict
def dependency_ranker(self, deepth_list):
"""
func dependency_ranker: produce a dependency score based on the change time
: input
- deepth_list
:return
- scores: dependency score, {"servicea": 0.7, "serviceb": 0.8}
"""
max_deepth = 2
score_list = {}
# logger.info(deepth_list)
# for item in deepth_list:
# if deepth_list[item] > max_deepth:
# max_deepth = deepth_list[item]
for item in deepth_list:
if deepth_list[item] + max_deepth > 0:
score_list[item] = 1.0 * max_deepth / \
(deepth_list[item] + max_deepth)
else:
score_list[item] = 0
logger.info("-------------------deep score------------------")
logger.info(score_list)
return score_list
def time_ranker(self, change_time_list):
"""
func time_ranker: produce a time score based on the change time
: input
- aler_time:
- change_time_list:
:return
- scores: time score, {"servicea": 0.7, "serviceb": 0.8}
"""
score_list = {}
for item in change_time_list:
# print(item, change_time_list[item], change_time_list[item] == 0)
if change_time_list[item] == 0:
score_list[item] = 0
logger.info("time score: %s, %s", item, change_time_list[item])
else:
delta = abs(int(self.alert_time_stamp) -
change_time_list[item])
# logger.info("time: %s, %s", item,delta)
# if 1920 * 60 < delta:
# score_list[item] = 1
# elif 960 * 60 < delta <= 1920 * 60:
# score_list[item] = 2
# elif 480 * 60 < delta <= 960 * 60:
# score_list[item] = 3
# elif 240 * 60 < delta <= 480 * 60:
# score_list[item] = 4
# elif 120 * 60 < delta <= 240 * 60:
# score_list[item] = 5
# elif 60 * 60 < delta <= 120 * 60:
# score_list[item] = 6
# elif 30 * 60 < delta <= 60 * 60:
# score_list[item] = 7
# elif delta <= 30 * 60:
# score_list[item] = 8
if 960 < delta <= 1920:
score_list[item] = 1
elif 480 < delta <= 960:
score_list[item] = 2
elif 240 < delta <= 480:
score_list[item] = 3
elif 120 < delta <= 240:
score_list[item] = 4
elif 60 < delta <= 120:
score_list[item] = 5
elif delta <= 60:
score_list[item] = 6
score_list[item] = score_list[item] / 6.0
logger.info("-------------------time score------------------")
logger.info(score_list)
return score_list
def get_change(self):
if "task_id" in self.data[self.alert_module]["change_ticket"]:
return True
if __name__ == '__main__':
base_path = "/mnt/multidata/2023-09-23/change"
case_path_list = os.listdir(base_path)
for case_file in case_path_list:
case_name = case_file + ".json"
case_file = os.path.join(base_path, case_file)
ranker = Ranker(case_file, case_name, "gied")
result = ranker.root_cause_change_identifycation()