Repository navigation
Expand file tree
/
Copy pathopen_process_file.py
More file actions
70 lines (49 loc) · 2 KB
/
Copy pathopen_process_file.py
File metadata and controls
70 lines (49 loc) · 2 KB
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
from datetime import datetime , date
import os
import os.path
import json
import math
#class for open and process files
class ProcessFile:
# function for open json an return dict wich data
# input -> None
# output -> dict
def open_file(self):
with open('/root/exercising_python/simple-input.json',) as f:
data = json.load(f)
return data
# function that recive dict and transform for patterns simple_output
# input -> dict
# output -> dict
def processing_json(self,data):
jsonList = list()
for x in data:
data_dict = {
"server": x['server'],
}
if "process" in x:
data_dict['process'] = x['process']
else:
data_dict['process'] = x['source']
if type(x['date']) is str:
data_dict['date']=x['date']
#print(data_dict['date'])
elif int(math.log10(x['date']))+1 == 13:
data_dict['date'] = datetime.fromtimestamp(int(x['date'])/1000).isoformat(timespec='microseconds')
elif int(math.log10(x['date']))+1 == 10:
data_dict['date'] = datetime.fromtimestamp(int(x['date'])).isoformat(timespec='microseconds')
if "severity" in x:
data_dict['severity'] = x['severity']
data_dict['message'] = x['message']
jsonList.append(data_dict)
else:
data_dict['severity'] = "WARN"
for event in x['events']:
data_dict['message'] = event['indicator-type']+" "+event['indicator-value']
jsonList.append(data_dict)
return jsonList
# function for save json in file
def save_json(self,data):
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
return print("ok")