-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert_parser.py
96 lines (87 loc) · 2.91 KB
/
alert_parser.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
from copy import deepcopy
import pdb
class AlertParser:
def __init__(self, entries):
self.entries = deepcopy(entries)
self.alerts = []
for entry in self.entries:
alert = deepcopy(entry["_source"])
alert["uid"]=entry["_id"]
self.alerts.append(alert)
def get_alerts(self, offset=0,limit=0,ids=[]):
if ids:
alerts = deepcopy([alert for alert in self.alerts if int(alert["id"]) in ids])
else:
alerts = deepcopy(self.alerts)
if (limit == 0):
data = alerts[offset:]
else:
data = alerts[offset:offset+limit]
return {
"total_items": len(alerts),
"data": data
}
def get_agents(self, offset=0,limit=0):
agents = {}
for alert in self.alerts:
agentID = alert["agent"]["id"]
if agentID in agents:
agents[agentID]["total_alerts"] += 1
else:
agents[agentID]=deepcopy(alert["agent"])
agents[agentID]["total_alerts"] = 1
if (limit == 0):
data = list(agents.values())[offset:]
else:
data = list(agents.values())[offset:offset+limit]
return {
"total_items": len(agents),
"data" : data
}
def get_agent_by(self, id):
agent = None
for alert in self.alerts:
agentID = int(alert["agent"]["id"])
if agentID == id:
if agent is None:
agent = deepcopy(alert["agent"])
agent["alerts"]=[alert]
else:
agent["alerts"].append(alert)
if agent is not None:
agent["total_alerts"]=len(agent["alerts"])
return {
"data" : agent
}
def get_rules(self, offset=0,limit=0):
rules = {}
for alert in self.alerts:
ruleID = alert["rule"]["id"]
if ruleID in rules:
rules[ruleID]["total_alerts"] += 1
else:
rules[ruleID]=deepcopy(alert["rule"])
rules[ruleID]["total_alerts"] = 1
if (limit == 0):
data = list(rules.values())[offset:]
else:
data = list(rules.values())[offset:offset+limit]
return {
"total_items": len(rules),
"data" : data
}
def get_rule_by(self, id):
rule = None
for alert in self.alerts:
ruleID = int(alert["rule"]["id"])
if ruleID == id:
if rule is None:
rule = deepcopy(alert["rule"])
rule["alerts"]=[alert]
else:
rule["alerts"].append(alert)
if rule is not None:
rule["total_alerts"] = len(rule["alerts"])
return {
"data" : rule
}