timetemplate.py
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 __author__ = "Cyril Jaquier"
00023 __version__ = "$Revision: 645 $"
00024 __date__ = "$Date: 2008-01-16 23:55:04 +0100 (Wed, 16 Jan 2008) $"
00025 __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
00026 __license__ = "GPL"
00027
00028 import time, logging
00029
00030 from template import Template, Templates
00031 from mytime import MyTime
00032
00033
00034 import iso8601
00035
00036
00037 logSys = logging.getLogger("fail2ban.timetemplate")
00038
00039 class TimeTemplate(Template):
00040
00041 def __init__(self):
00042 Template.__init__(self, Template.TEMPLATE_TIME, "<TIME>")
00043
00044 def setRegex(self, regex):
00045 Template.setRegex(self, "(?P<%s>%s)" % (self.getName(), regex))
00046
00047 def getTime(self, line):
00048 raise Exception("getTime() is abstract")
00049
00050
00051 class TimeISO8601(TimeTemplate):
00052
00053 def __init__(self):
00054 TimeTemplate.__init__(self)
00055 date_re = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}" \
00056 ".[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?" \
00057 "(Z|(([-+])([0-9]{2}):([0-9]{2})))?"
00058 self.setRegex(date_re)
00059
00060 def getTime(self, line):
00061
00062 return list(iso8601.parse_date(line).utctimetuple())
00063
00064
00065 class TimeEpoch(TimeTemplate):
00066
00067 def __init__(self):
00068 TimeTemplate.__init__(self)
00069
00070 self.setRegex("\d{10}(\.\d{6})?")
00071
00072 def getTime(self, line):
00073
00074 return list(time.localtime(float(line)))
00075
00076
00077
00078
00079
00080
00081
00082 class TimeStrptime(TimeTemplate):
00083
00084 TABLE = dict()
00085 TABLE["Jan"] = []
00086 TABLE["Feb"] = [u"Fév"]
00087 TABLE["Mar"] = [u"Mär"]
00088 TABLE["Apr"] = ["Avr"]
00089 TABLE["May"] = ["Mai"]
00090 TABLE["Jun"] = []
00091 TABLE["Jul"] = []
00092 TABLE["Aug"] = ["Aou"]
00093 TABLE["Sep"] = []
00094 TABLE["Oct"] = ["Okt"]
00095 TABLE["Nov"] = []
00096 TABLE["Dec"] = [u"Déc", "Dez"]
00097
00098 def __init__(self):
00099 TimeTemplate.__init__(self)
00100 self.__pattern = ""
00101
00102 def setPattern(self, pattern):
00103 self.__pattern = pattern.strip()
00104
00105 def getPattern(self):
00106 return self.__pattern
00107
00108
00109 def convertLocale(date):
00110 for t in TimeStrptime.TABLE:
00111 for m in TimeStrptime.TABLE[t]:
00112 if date.find(m) >= 0:
00113 return date.replace(m, t)
00114 return date
00115 convertLocale = staticmethod(convertLocale)
00116
00117 def getTime(self, line):
00118 try:
00119
00120 date = list(time.strptime(line, self.getPattern()))
00121 except ValueError:
00122
00123 conv = self.convertLocale(line)
00124 try:
00125 date = list(time.strptime(conv, self.getPattern()))
00126 except ValueError:
00127
00128
00129 conv += " %s" % MyTime.gmtime()[0]
00130 pattern = "%s %%Y" % self.getPattern()
00131 date = list(time.strptime(conv, pattern))
00132 if date[0] < 2000:
00133
00134 date[0] = MyTime.gmtime()[0]
00135
00136
00137
00138 if time.mktime(date) > MyTime.time():
00139 date[0] -= 1
00140 return date
00141
00142
00143 class TimeTai64n(TimeTemplate):
00144
00145 def __init__(self):
00146 TimeTemplate.__init__(self)
00147
00148 self.setRegex("@[0-9a-f]{24}")
00149
00150 def getTime(self, line):
00151
00152 seconds_since_epoch = line[2:17]
00153 return list(time.gmtime(int(seconds_since_epoch, 16)))
00154
00155
00156 class TimeTemplates(Templates):
00157
00158 def __init__(self):
00159 Templates.__init__(self)
00160
00161 template = TimeStrptime()
00162 template.setDescription("Month Day Hour:Minute:Second")
00163 template.setRegex("\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}")
00164 template.setPattern("%b %d %H:%M:%S")
00165 self.templates.append(template)
00166
00167 template = TimeStrptime()
00168 template.setDescription("Weekday Month Day Hour:Minute:Second Year")
00169 template.setRegex("\S{3} \S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2} \d{4}")
00170 template.setPattern("%a %b %d %H:%M:%S %Y")
00171 self.templates.append(template)
00172
00173 template = TimeStrptime()
00174 template.setDescription("Weekday Month Day Hour:Minute:Second")
00175 template.setRegex("\S{3} \S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}")
00176 template.setPattern("%a %b %d %H:%M:%S")
00177 self.templates.append(template)
00178
00179 template = TimeStrptime()
00180 template.setDescription("Year/Month/Day Hour:Minute:Second")
00181 template.setRegex("\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}")
00182 template.setPattern("%Y/%m/%d %H:%M:%S")
00183 self.templates.append(template)
00184
00185 template = TimeStrptime()
00186 template.setDescription("Day/Month/Year Hour:Minute:Second")
00187 template.setRegex("\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}")
00188 template.setPattern("%d/%m/%Y %H:%M:%S")
00189 self.templates.append(template)
00190
00191 template = TimeStrptime()
00192 template.setDescription("Day/Month/Year:Hour:Minute:Second")
00193 template.setRegex("\d{2}/\S{3}/\d{4}:\d{2}:\d{2}:\d{2}")
00194 template.setPattern("%d/%b/%Y:%H:%M:%S")
00195 self.templates.append(template)
00196
00197 template = TimeStrptime()
00198 template.setDescription("Year-Month-Day Hour:Minute:Second")
00199 template.setRegex("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}")
00200 template.setPattern("%Y-%m-%d %H:%M:%S")
00201 self.templates.append(template)
00202
00203 template = TimeStrptime()
00204 template.setDescription("Day-Month-Year Hour:Minute:Second[.Millisecond]")
00205 template.setRegex("\d{2}-\S{3}-\d{4} \d{2}:\d{2}:\d{2}")
00206 template.setPattern("%d-%b-%Y %H:%M:%S")
00207 self.templates.append(template)
00208
00209 template = TimeTai64n()
00210 template.setDescription("TAI64N")
00211 self.templates.append(template)
00212
00213 template = TimeEpoch()
00214 template.setDescription("Epoch")
00215 self.templates.append(template)
00216
00217 template = TimeISO8601()
00218 template.setDescription("ISO 8601")
00219 self.templates.append(template)