filtergamin.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 __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
00034 logSys = logging.getLogger("fail2ban.filter")
00035
00036
00037
00038
00039
00040
00041
00042
00043 class FilterGamin(FileFilter):
00044
00045
00046
00047
00048
00049
00050
00051 def __init__(self, jail):
00052 FileFilter.__init__(self, jail)
00053 self.__modified = False
00054
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
00069
00070
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
00082
00083
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
00095
00096
00097
00098
00099
00100 def run(self):
00101 self.setActive(True)
00102 while self._isActive():
00103 if not self.getIdle():
00104
00105
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
00121 self.__cleanup()
00122 logSys.debug(self.jail.getName() + ": filter terminated")
00123 return True
00124
00125
00126
00127
00128 def __cleanup(self):
00129 for path in self.getLogPath():
00130 self.monitor.stop_watch(path.getFileName())
00131 del self.monitor