Skip to content

Commit 9cbdd78

Browse files
committed
Merge pull request #2 from claque2000/master
Support of importing format from file added
2 parents 3d7aa44 + 782e94d commit 9cbdd78

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

timegrep

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/python
22

33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -16,6 +16,7 @@
1616
version = "1.0"
1717

1818
import os, sys
19+
import ast
1920
from stat import *
2021
from datetime import date, datetime
2122
import re
@@ -31,8 +32,7 @@ logformat_list.append({'regexp':"(?P<MONTH_NAME>[a-zA-Z]{3}) (?P<DAY>[0-9]{1,2})
3132
# NSCA Common
3233
logformat_list.append({'regexp':"(?P<origin>\d+\.\d+\.\d+\.\d+) (?P<identd>-|\w*) (?P<auth>-|\w*) \[(?P<DAY>[0-9]{1,2})/(?P<MONTH_NAME>[a-zA-Z]{3})/(?P<YEAR>[0-9]{4}):(?P<HOURS>[0-9]{2}):(?P<MINUTES>[0-9]{2}):(?P<SECONDS>[0-9]{2}) (?P<tz>[\-\+]?\d\d\d\d)\]", 'name':"NSCA Common", 'description':"host rfc931 username [%d/%b/%Y:%H:%M:%S +TZ]"})
3334
# Bind8
34-
logformat_list.append({'regexp':"(?P<DAY>[0-9]{1,2})\-(?P<MONTH_NAME>[a-zA-Z]{3})\-(?P<YEAR>[0-9]{4}) (?P<HOURS>[0-9]{2}):(?P<MINUTES>[0-9]{2}):(?P<SECONDS>[0-9]{2})\.", 'name':"Bind8", 'description':"%d-%b-%Y %H:%M:%S."})
35-
35+
logformat_list.append({'regexp':"(?P<DAY>[0-9]{2})\-(?P<MONTH_NAME>[a-zA-Z]{3})\-(?P<YEAR>[0-9]{4}) (?P<HOURS>[0-9]{2}):(?P<MINUTES>[0-9]{2}):(?P<SECONDS>[0-9]{2})\.", 'name':"Bind8", 'description':"%d-%b-%Y %H:%M:%S."})
3636

3737
# Allow to convert from month names to month number
3838
MONTH_LOOKUP = {
@@ -202,6 +202,10 @@ parser.add_option("-D", "--debug", action = "store", dest = "debug",
202202
default = 0, type = "int",
203203
help = "Output debugging information.\t\t\t\t\tNone (default) = %default, Some = 1, More = 2")
204204

205+
parser.add_option("-L", "--log-regexp", action = "store", dest = "logregexp",
206+
default = None,
207+
help = "Add one or more log formats from a file.")
208+
205209
(options, args) = parser.parse_args()
206210

207211
if not 0 <= options.debug <= 2:
@@ -230,6 +234,11 @@ if options.end_time:
230234
else:
231235
end_time = "23:59:59"
232236

237+
if options.logregexp:
238+
regexpfile = options.logregexp
239+
else:
240+
regexpfile = ""
241+
233242
if len(args) != 1:
234243
parser.error("Invalid number of arguments")
235244

@@ -287,6 +296,16 @@ pos1 = pos2 = 0
287296
debug_msg("File: '{0}' Size: {1} Date: '{2}' Now: {3} Start: '{4}' End: '{5}'".format(log_file, size, selectedday, now, searchstart, searchend),0)
288297
debug_msg("Timerange: '{0}' Endrange: {1} ".format(timerange,endrange),0)
289298

299+
if regexpfile != "":
300+
try:
301+
format_handle = open(regexpfile,'r')
302+
except:
303+
print >> sys.stderr, "[ERROR] Error while opening file {0}".format(regexpfile)
304+
exit(1)
305+
for format_line in format_handle:
306+
logformat_list.append(ast.literal_eval(format_line))
307+
format_handle.close()
308+
290309
date_format = detect_date_format()
291310
if date_format is None:
292311
print >> sys.stderr, "[ERROR]: Unable to detect date format or date format not supported"

0 commit comments

Comments
 (0)