Skip to content

Commit c8e4f31

Browse files
committed
Connecting the pieces
1 parent 2fa5bb6 commit c8e4f31

File tree

6 files changed

+63
-104
lines changed

6 files changed

+63
-104
lines changed

logdissect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
from logdissect.core import LogDissectCore
2626

2727
dissect = LogDissectCore()
28-
dissect.run_parse()
28+
dissect.run_job()

logdissect/core.py

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
from logdissect import __version__
3636
from optparse import OptionParser
3737
from optparse import OptionGroup
38+
import gettext
39+
gettext.install('licins')
3840

3941
class LogDissectCore:
4042

@@ -60,108 +62,63 @@ def __init__(self):
6062
_("Output options"))
6163

6264
# run_job does the actual job using the other functions.
63-
def run_job
65+
def run_job(self):
6466
"""Execute a logdissect job"""
6567
# To Do: execute the other functions here
66-
# load_parsers()
67-
# load_outputs()
68-
# config_options()
69-
# load_inputs
70-
# run_parse()
68+
self.load_parsers()
69+
self.load_outputs()
70+
self.config_options()
71+
self.load_inputs()
72+
self.run_parse()
7173
# Note to self: leave morphing out until the rest works.
72-
# run_morph() run_morph should look like run_parse
74+
# self.run_morph() run_morph should look like run_parse
7375
# We'll need to split self.options.morphers_list and then split
7476
# the list into range x y, grep z, etc.
7577
# Probably do that in load_morphers
76-
# run_merge()
77-
# run_output()
78+
self.run_merge()
79+
self.run_output()
7880
# This will have to parse outputs_list into a list, etc.
7981

8082
def run_parse(self):
8183
"""Parse one or more log files"""
8284
# Data set already has source file names from load_inputs
8385
parsedset = self.data_set
8486
for l in self.data_set.data_set:
85-
parsemodule = self.parse_modules[self.options.parser]()
87+
parsemodule = self.parse_modules[self.options.parser]
8688
parsemodule.data = l
8789
parsedlog = parsemodule.run_parse()
8890
parsedset.data_set.append(parsedlog)
8991
self.data_set = parsedset
9092

9193
def run_merge(self):
9294
"""Merge all our data sets together"""
93-
if self.data_set.data_set < 2:
94-
self.data_set.finalized_data = self.data_set.data_set[0]
95-
return 0
96-
else:
97-
ourdataset = self.data_set
98-
ourfinallog = self.data_set.finalized_data
99-
key_log = ourdataset.data_set.pop(0)
100-
while ourdataset.data_set[0]:
101-
second_log = ourdataset.data_set.pop(0)
102-
for x, y in map(None, key_log, second_log):
103-
if not x:
104-
ourfinallog.append(y)
105-
elif not y:
106-
ourfinallog.append(x)
107-
else:
108-
if x.date_stamp_year <= y.date_stamp_year:
109-
ourfinallog.append(x)
110-
ourfinallog.append(y)
111-
else:
112-
ourfinallog.append(y)
113-
ourfinallog.append(x)
114-
key_log = ourfinallog
115-
self.data_set.finalized_data = key_log
116-
# for otherlog in ourdataset.data_set:
117-
# for trial_entry in key_log:
118-
# if trial_entry.date_stamp_year < \
119-
# otherlog[0].date_stamp_year:
120-
# ourfinallog.append(trial_entry)
121-
# else:
122-
# ourfinallog.append(otherlog.pop(
123-
124-
# To Do: replace this stuff above inside the else loop:
125-
# for log in self.data_set.data_set:
126-
# key_log = self.data_set.data_set.pop(0)
127-
# for entry in key_log:
128-
# if self.data_set.data_set > 0:
129-
# this_try = self.data_set.data_set[0].entries.pop(0)
130-
# for log in self.data_set.data_set:
131-
# for other_entry in log:
132-
# if entry.date_stamp_year < \
133-
# this_try.date_stamp_year:
134-
# self.data_set.finalized_data.append(other_entry.raw_text)
135-
# self.data_set.finalized_data.append(entry.raw_text)
136-
137-
# def run_morph(self, options):
138-
# """Morph the data set using the selected modules"""
139-
# pass
95+
#Note: just add the logs together then sort the final list.
96+
for l in self.data_set.data_set:
97+
self.data_set.finalized_data += l
98+
self.data_set.finalized_data.sort(key=lambda x: x.date_stamp_year)
14099

141-
def do_output(self):
100+
def run_output(self):
142101
"""Output finalized data"""
143102
for o in self.options.output_list.split(','):
144103
if o in logdissect.output.__all__:
145104
self.output_list[o](data=self.data_set.finalized_data)
146105

147-
148-
149106
def config_options(self):
150107
"""Set config options"""
151108
# Input option:
152109
self.option_parser.add_option("-i", "--input",
153110
action="store",
154-
dest="input_list",
111+
dest="inputs_list",
155112
help=_("specifies input files"))
156113
# Module list options:
157114
self.option_parser.add_option("--list-parsers",
158115
action="callback",
159116
callback=self.list_parsers,
160117
help=_("returns a list of available parsers"))
161-
self.option_parser.add_option("--list-morphers",
162-
action="callback",
163-
callback=self.list_morphers,
164-
help=_("returns a list of available morphers"))
118+
# self.option_parser.add_option("--list-morphers",
119+
# action="callback",
120+
# callback=self.list_morphers,
121+
# help=_("returns a list of available morphers"))
165122
self.option_parser.add_option("--list-outputs",
166123
action="callback",
167124
callback=self.list_outputs,
@@ -180,24 +137,24 @@ def config_options(self):
180137
dest="output_list",
181138
help=_("specifies output formats to use"))
182139

183-
self.option_parser.add_option_group(self.input_options)
184-
self.option_parser.add_option_group(self.parse_options)
185-
self.option_parser.add_option_group(self.morph_options)
186-
self.option_parser.add_option_group(self.output_options)
140+
# self.option_parser.add_option_group(self.input_options)
141+
# self.option_parser.add_option_group(self.parse_options)
142+
# self.option_parser.add_option_group(self.morph_options)
143+
# self.option_parser.add_option_group(self.output_options)
187144
self.options, args = self.option_parser.parse_args(sys.argv[1:])
188145

189146
# To Do: finish input file loading
190147
# Load input files:
191148
def load_inputs(self):
192149
"""Load the specified inputs"""
193-
for f in self.inputs_list:
150+
for f in self.options.inputs_list.split(' '):
194151
fullpath = os.path.abspath(f)
195152
log = LogData()
196153
log.source_full_path = fullpath
197154
self.data_set.data_set.append(log)
198155

199156
# Parsing modules:
200-
def list_parsers(self):
157+
def list_parsers(self, *args):
201158
"""Return a list of available parsing modules"""
202159
print '==== Available parsing modules: ===='
203160
print
@@ -208,7 +165,7 @@ def list_parsers(self):
208165

209166
def load_parsers(self):
210167
"""Load parsing module(s)"""
211-
for parser in sorted(logdissect.parsers.__parsers__):
168+
for parser in sorted(logdissect.parsers.__all__):
212169
self.parse_modules[parser] = \
213170
__import__('logdissect.parsers.' + parser, globals(), \
214171
locals(), [logdissect]).ParseModule(self.parse_options)
@@ -231,13 +188,13 @@ def load_parsers(self):
231188
# locals(), [logdissect]).MorphModule(self.morph_options)
232189

233190
# Output modules (log file, csv, html, etc)
234-
def list_outputs(self):
191+
def list_outputs(self, *args):
235192
"""Return a list of available output modules"""
236193
print '==== Available output modules ===='
237194
print
238195
for output in sorted(self.output_modules):
239196
print string.ljust(self.output_modules[output].name, 16) + \
240-
': ' + self.format_module[format].desc
197+
': ' + self.output_modules[output].desc
241198
sys.exit(0)
242199

243200
def load_outputs(self):
@@ -249,4 +206,4 @@ def load_outputs(self):
249206

250207
if __name__ == "__main__":
251208
dissect = LogDissectCore()
252-
dissect.run_parse()
209+
dissect.run_job()

logdissect/data/data.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
class LogEntry:
27-
def __init__(self, options):
27+
def __init__(self):
2828
"""Initialize a log entry"""
2929
self.date_stamp = None
3030
# For datestamps that include a year:
@@ -106,13 +106,14 @@ def __init__(self):
106106
self.creation_date = None
107107
# To Do: update options for finalized data
108108

109-
def read_logs(self):
110-
"""Read in a log file"""
111-
for logfile in self.source_full_paths:
112-
newlog = LogData(source_full_path=logfile, parser=self.parser)
113-
newlog.parse_log()
114-
self.data_set.append(newlog)
109+
# To Do: Think about moving these back here:
110+
# def read_logs(self):
111+
# """Read in a log file"""
112+
# for logfile in self.source_full_paths:
113+
# newlog = LogData(source_full_path=logfile, parser=self.parser)
114+
# newlog.parse_log()
115+
# self.data_set.append(newlog)
115116

116-
def merge(self):
117-
"""Merge set of log data into one sorted instance"""
118-
pass
117+
# def merge(self):
118+
# """Merge set of log data into one sorted instance"""
119+
# pass

logdissect/output/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23-
__all__ = []
23+
__formats__ = ['log']

logdissect/output/log.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23-
from LogDissect.output.type import OutputModule as OurModule
23+
from logdissect.output.type import OutputModule as OurModule
24+
from logdissect.data.data import LogData
2425

2526
class OutputModule(OurModule):
26-
def __init__(self):
27+
def __init__(self, options):
2728
self.name = 'log'
2829
self.desc = 'Output module for standard log file'
2930
self.data = LogData()
3031
self.output_path = ''
3132

3233
def write_output(self):
33-
with open(str(self.output_path), 'w') as output_file
34-
for entry in self.data.entries:
35-
f.write(str(entry) + '\n')
36-
return 0
34+
with open(str(self.output_path), 'w') as output_file:
35+
for entry in self.data.entries:
36+
f.write(str(entry) + '\n')

logdissect/parsers/syslog.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@
2222

2323
import os
2424
import re
25-
from LogDissect.parsers.type import ParseModule as OurModule
26-
from LogDissect.data.data import LogEntry
27-
from LogDissect.data.data import LogData
25+
import datetime
26+
from logdissect.parsers.type import ParseModule as OurModule
27+
from logdissect.data.data import LogEntry
28+
from logdissect.data.data import LogData
2829

2930
class ParseModule(OurModule):
30-
def __init__(self):
31+
def __init__(self, options):
3132
self.name = 'syslog'
3233
self.desc = 'Syslog parsing module'
3334
self.data = LogData()
34-
self.date_format = re.compile(r"^([A-Z][a-z]{2} \d{1,2} \d{2} \d{2}:\d{2}:\d{2})"
35+
self.date_format = re.compile(r"^([A-Z][a-z]{2} \d{1,2} \d{2} \d{2}:\d{2}:\d{2})")
3536

3637
def run_parse(self):
3738
current_entry = LogEntry()
3839
self.data.source_file_mtime = \
39-
os.path.getmtime(str(self.data.source_fullpath))
40-
time_list = datetime.fromtimestamp(self.data.source_file_mtime)
41-
self.data.source_file_time = str(time_list[0]) + \
42-
str(time_list[1]).zfill(2) + str(time_list[2]).zfill(2) + \
43-
str(time_list[3]) + str(time_list[4]) + str(time_list[5])
40+
os.path.getmtime(self.data.source_full_path)
41+
time_list = datetime.datetime.fromtimestamp(self.data.source_file_mtime)
42+
# self.data.source_file_time = str(time_list[0]) + \
43+
# str(time_list[1]).zfill(2) + str(time_list[2]).zfill(2) + \
44+
# str(time_list[3]) + str(time_list[4]) + str(time_list[5])
4445
self.data.source_file_year = time_list[0]
4546
#To Do: strip year out of mtime
4647
entry_year = self.data.source_file_year
@@ -64,7 +65,7 @@ def run_parse(self):
6465
'Jul':'07', 'Aug':'08', 'Sep':'09', \
6566
'Oce':'10', 'Nov':'11', 'Dec':'12'}
6667
if date in months:
67-
int_month = months{date}
68+
int_month = months[date]
6869
date = str(date).zfill(2)
6970
time_list = str(date_list[2].split(':'))
7071
date_stamp = str(int_month) + str(date) + \

0 commit comments

Comments
 (0)