filtergamin.py

Go to the documentation of this file.
00001 # This file is part of Fail2Ban.
00002 #
00003 # Fail2Ban is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 2 of the License, or
00006 # (at your option) any later version.
00007 #
00008 # Fail2Ban is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with Fail2Ban; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 
00017 # Author: Cyril Jaquier
00018 # 
00019 # $Revision: 697 $
00020 
00021 __author__ = "Cyril Jaquier"
00022 __version__ = "$Revision: 697 $"
00023 __date__ = "$Date: 2008-05-19 23:08:36 +0200 (Mon, 19 May 2008) $"
00024 __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
00025 __license__ = "GPL"
00026 
00027 from failmanager import FailManagerEmpty
00028 from filter import FileFilter
00029 from mytime import MyTime
00030 
00031 import time, logging, gamin
00032 
00033 # Gets the instance of the logger.
00034 logSys = logging.getLogger("fail2ban.filter")
00035 
00036 ##
00037 # Log reader class.
00038 #
00039 # This class reads a log file and detects login failures or anything else
00040 # that matches a given regular expression. This class is instanciated by
00041 # a Jail object.
00042 
00043 class FilterGamin(FileFilter):
00044 
00045     ##
00046     # Constructor.
00047     #
00048     # Initialize the filter object with default values.
00049     # @param jail the jail object
00050     
00051     def __init__(self, jail):
00052         FileFilter.__init__(self, jail)
00053         self.__modified = False
00054         # Gamin monitor
00055         self.monitor = gamin.WatchMonitor()
00056         logSys.debug("Created FilterGamin")
00057 
00058 
00059     def callback(self, path, event):
00060         logSys.debug("Got event: " + `event` + " for " + path)
00061         if event in (gamin.GAMCreated, gamin.GAMChanged, gamin.GAMExists):
00062             logSys.debug("File changed: " + path)
00063             self.getFailures(path)
00064             self.__modified = True
00065 
00066 
00067     ##
00068     # Add a log file path
00069     #
00070     # @param path log file path
00071 
00072     def addLogPath(self, path, tail = True):
00073         if self.containsLogPath(path):
00074             logSys.error(path + " already exists")
00075         else:
00076             self.monitor.watch_file(path, self.callback)
00077             FileFilter.addLogPath(self, path, tail)
00078             logSys.info("Added logfile = %s" % path)            
00079     
00080     ##
00081     # Delete a log path
00082     #
00083     # @param path the log file to delete
00084     
00085     def delLogPath(self, path):
00086         if not self.containsLogPath(path):
00087             logSys.error(path + " is not monitored")
00088         else:
00089             self.monitor.stop_watch(path)
00090             FileFilter.delLogPath(self, path)
00091             logSys.info("Removed logfile = %s" % path)
00092         
00093     ##
00094     # Main loop.
00095     #
00096     # This function is the main loop of the thread. It checks if the
00097     # file has been modified and looks for failures.
00098     # @return True when the thread exits nicely
00099 
00100     def run(self):
00101         self.setActive(True)
00102         while self._isActive():
00103             if not self.getIdle():
00104                 # We cannot block here because we want to be able to
00105                 # exit.
00106                 if self.monitor.event_pending():
00107                     self.monitor.handle_events()
00108 
00109                 if self.__modified:
00110                     try:
00111                         while True:
00112                             ticket = self.failManager.toBan()
00113                             self.jail.putFailTicket(ticket)
00114                     except FailManagerEmpty:
00115                         self.failManager.cleanup(MyTime.time())
00116                     self.__modified = False
00117                 time.sleep(self.getSleepTime())
00118             else:
00119                 time.sleep(self.getSleepTime())
00120         # Cleanup Gamin
00121         self.__cleanup()
00122         logSys.debug(self.jail.getName() + ": filter terminated")
00123         return True
00124 
00125     ##
00126     # Desallocates the resources used by Gamin.
00127 
00128     def __cleanup(self):
00129         for path in self.getLogPath():
00130             self.monitor.stop_watch(path.getFileName())
00131         del self.monitor
Generated on Fri May 24 03:01:40 2013 for Fail2Ban by  doxygen 1.6.3