1
- #!/usr/bin/env python
1
+ #!/usr/bin/python
2
2
3
3
# This program is free software: you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
version = "1.0"
17
17
18
18
import os , sys
19
+ import ast
19
20
from stat import *
20
21
from datetime import date , datetime
21
22
import re
@@ -31,8 +32,7 @@ logformat_list.append({'regexp':"(?P<MONTH_NAME>[a-zA-Z]{3}) (?P<DAY>[0-9]{1,2})
31
32
# NSCA Common
32
33
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]" })
33
34
# 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." })
36
36
37
37
# Allow to convert from month names to month number
38
38
MONTH_LOOKUP = {
@@ -202,6 +202,10 @@ parser.add_option("-D", "--debug", action = "store", dest = "debug",
202
202
default = 0 , type = "int" ,
203
203
help = "Output debugging information.\t \t \t \t \t None (default) = %default, Some = 1, More = 2" )
204
204
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
+
205
209
(options , args ) = parser .parse_args ()
206
210
207
211
if not 0 <= options .debug <= 2 :
@@ -230,6 +234,11 @@ if options.end_time:
230
234
else :
231
235
end_time = "23:59:59"
232
236
237
+ if options .logregexp :
238
+ regexpfile = options .logregexp
239
+ else :
240
+ regexpfile = ""
241
+
233
242
if len (args ) != 1 :
234
243
parser .error ("Invalid number of arguments" )
235
244
@@ -287,6 +296,16 @@ pos1 = pos2 = 0
287
296
debug_msg ("File: '{0}' Size: {1} Date: '{2}' Now: {3} Start: '{4}' End: '{5}'" .format (log_file , size , selectedday , now , searchstart , searchend ),0 )
288
297
debug_msg ("Timerange: '{0}' Endrange: {1} " .format (timerange ,endrange ),0 )
289
298
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
+
290
309
date_format = detect_date_format ()
291
310
if date_format is None :
292
311
print >> sys .stderr , "[ERROR]: Unable to detect date format or date format not supported"
0 commit comments