fail2ban/logreader/parser.py

67 lines
2.1 KiB
Python

# This file is part of Fail2Ban.
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Author: Cyril Jaquier
#
# $Revision$
__author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
import time, re
class Parser:
""" This class is the main log parser class. It should be inherited
by all the service specific classes.
"""
def getLogMatch(self, pattern, line):
""" Returns a match if pattern is found in line.
"""
return re.search(pattern, line)
def getLogIPv4(self, line):
""" Returns IP if one is found in line. Match IPv4 string.
"""
matchIP = re.search("(?:\d{1,3}\.){3}\d{1,3}", line)
if matchIP:
return matchIP.group()
else:
return None
def getLogIP(self, line):
""" Returns IP if one is found in line.
"""
return self.getLogIPv4(line)
def getLogTimeStandard(self, line):
""" Returns the log time of line using a standard log format.
Format: Oct 14 11:47:08
"""
date = list(time.strptime(line[0:15], "%b %d %H:%M:%S"))
date[0] = time.gmtime()[0]
unixTime = time.mktime(date)
return unixTime
def getLogTime(self, line):
""" Returns the log time of line.
"""
return self.getLogTimeStandard(line)