-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamilog.py
executable file
·84 lines (69 loc) · 2.35 KB
/
amilog.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
#!/usr/bin/env python3
import sys
from logwriter.TemplateBuilder import TemplateBuilder
from datetime import datetime
import pyami
import os
import shutil
import time
tb = TemplateBuilder()
logdir = ""
logs_fp = {}
current_time = time.time()
event_time = 0
def make_logsdir(dirname, force_mkdir=False):
try:
os.makedirs(dirname)
except FileExistsError:
if force_mkdir:
shutil.rmtree(dirname)
os.makedirs(dirname)
else:
print("Error: Cannot make directory %s: Directory already exists!" % dirname)
return False
except:
print("Error: Cannot make directory %s" % dirname)
return False
return True
def action_handler(action, userdata):
if action.Exec() != "Controller":
return
# raise ValueError("Action exec must be Controller")
action_time = event_time + action.GetSleepCursor()
variables = action.Variables()
field_actions = action.FieldActions()
kvdict = {}
for field, atype in field_actions.items():
for at, aval in atype.items():
if at == "set":
for k, v in aval.items():
kvdict[field] = k
try:
event = tb.get_event(variables["$event_type"], variables["$event_template"], kvdict)
except:
print("Could not extract the right variables: $event_type and $event_template from action %s" % action.Name())
return
et = datetime.fromtimestamp(action_time)
event = et.strftime(event)
log_filename = os.path.join(logdir, variables["$event_type"].replace(".", "_") + ".log")
try:
logs_fp[variables["$event_type"]].write(event)
except:
logs_fp[variables["$event_type"]] = open(log_filename, "w")
logs_fp[variables["$event_type"]].write(event)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Syntax: %s file.ami output_directory" % sys.argv[0])
sys.exit(1)
ami_file = sys.argv[1]
logdir = sys.argv[2]
make_logsdir(logdir, "-f" in sys.argv)
ami = pyami.Ami()
ami.Parse(ami_file)
start_time = ami.GetStartTime()
if ami.GetStartTime() > 0:
event_time = start_time
print("Events Start Time:%s" % time.ctime(event_time))
else:
event_time = current_time - ami.GetSleepCursor()
ami.Run(action_handler, None)