-
Notifications
You must be signed in to change notification settings - Fork 0
/
tamandua_parser.py
executable file
·157 lines (136 loc) · 4.52 KB
/
tamandua_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
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
#!/usr/bin/env python3
"""
Tamandua. Aggregate information from logfiles.
This is the main (entry point) of the parser.
"""
import os
import sys
import argparse
BASEDIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(BASEDIR)
# JSONDecodeError exists from python 3.5 and onwards
# As Debian Jessie only has python 3.4 we need this workaround
if sys.version_info[1] < 5:
JSONDecodeError = ValueError
else:
from json import JSONDecodeError
from src.plugins.plugin_manager import PluginManager
from src.config import Config
from src.constants import CONFIGFILE
from src.exceptions import print_exception
from src.repository.factory import RepositoryFactory
class DefaultArgs():
logfile = os.path.join('mock_logs', 'extern-intern_to_intern.log')
printdata = False
printmsgs = False
configfile = os.path.join(BASEDIR, CONFIGFILE)
def main(args: DefaultArgs):
"""Entry point of the application."""
try:
Config().setup(
args.configfile,
BASEDIR,
vars(args)
)
except FileNotFoundError as e:
print_exception(
e,
"Trying to read the config",
"Exiting application",
description="The configfile was not found",
fatal=True)
sys.exit(6)
except JSONDecodeError as e:
print_exception(
e,
"Trying to parse the config",
"Exiting application",
description="Syntax error in the configfile",
fatal=True)
print(e)
sys.exit(7)
except Exception as e:
print_exception(e, "Trying to read the config", "Exiting application", fatal=True)
sys.exit(8)
try:
pluginManager = PluginManager(
absPluginsPath=os.path.join(BASEDIR, 'plugins-enabled'))
except Exception as e:
print_exception(
e,
"Trying to create an instance of PluginManager",
"Exiting Application",
fatal=True)
sys.exit(1)
repository = RepositoryFactory.create_repository()
currByte = repository.get_position_of_last_read_byte()
logfilehandle = open(args.logfile, 'r', errors='replace')
linecounter = 0
try:
for line in logfilehandle:
pluginManager.process_line(line)
if args.printmsgs:
linecounter += 1
sys.stdout.write('\rProcessed %d lines' % linecounter)
sys.stdout.flush()
except UnicodeDecodeError as e:
print_exception(
e,
"Trying to read a line from the given logfile.",
"Aborting")
except Exception as e:
print_exception(
e,
"Trying to read a line from the given logfile",
"Quit application",
fatal=True)
sys.exit(9)
if args.printmsgs:
print('')
# save the byte position for the next run
diffByte = os.fstat(logfilehandle.fileno()).st_size
newByte = max(0, currByte + diffByte - 1000)
repository.save_position_of_last_read_byte(newByte)
# aggregate fragments to objects
for container in pluginManager.dataReceiver.containers:
try:
container.build_final()
except Exception as e:
print_exception(
e,
'Aggregating the data',
'Discarding aggregation and exiting application',
fatal=True)
sys.exit(10)
"""We only start with the executation if we are the main."""
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Tamandua parser aggregates from logfile data")
parser.add_argument(
'logfile',
metavar='LOGFILE',
type=str,
help='Logfile to be parsed')
parser.add_argument(
'--config',
'-c',
dest='configfile',
default=os.path.join(BASEDIR, CONFIGFILE),
type=str,
help='Path to the configfile')
parser.add_argument(
'--print-data',
dest='printdata',
default=False,
action='store_true',
help='Print data after processing it, eg. print aggregated mail objects')
parser.add_argument(
'--print-msgs',
dest='printmsgs',
default=False,
action='store_true',
help='Print system messages, eg.: currently processed loglines, number of processed objects')
# https://docs.python.org/3/library/argparse.html#argparse.Namespace
args = DefaultArgs()
parser.parse_args(namespace=args)
main(args)