Example #1
0
    def __init__(self, conf):
        super(FUCore, self).__init__()

        self._conf = conf
        self._logger = logging.getLogger(self.__class__.__name__)
        if self._conf.log_facility == 'file':
            handler = TimedRotatingFileHandler(self._conf.log_filename,
                                               self._conf.log_when,
                                               self._conf.log_interval,
                                               self._conf.log_keep)
            if os.path.exists(self._conf.log_filename):
                # try to fixup rollover time
                stat = os.stat(self._conf.log_filename)
                ctime = int(stat.st_ctime)
                # work around python 2.6.2 deficiencies.. (not 100% accurate)
                if sys.hexversion < 0x20603f0:
                    handler.rolloverAt = ctime + handler.interval
                else:
                    handler.rolloverAt = handler.computeRollover(ctime)

            format = '%(asctime)s [%(process)d]: %(levelname)s: %(message)s'
        else:
            handler = SysLogHandler('/dev/log', SysLogHandler.LOG_NEWS)
            format = 'SYNFU[%(process)d] %(message)s'

        formatter = logging.Formatter(format)
        handler.setFormatter(formatter)
        self._logger.setLevel(logging.DEBUG)
        self._logger.addHandler(handler)
        
        self._blacklist = {}
        if self._conf.blacklist_filename:
            try:
                blacklist_file = open(self._conf.blacklist_filename)
                for (lno, line) in enumerate(blacklist_file.readlines()):
                    line = line.strip()
                    if line.startswith('#'):
                        continue
                    fields = [x.strip() for x in line.split(';') if x]
                    if len(fields) < 2 or len(fields) > 3:
                        self._log("!!! {0}:{1}: invalid field count {2} expected 2 or 3",
                                  self._conf.blacklist_filename, lno + 1, len(fields))
                        continue
                    elif fields[1].lower() in ['e','ne','en'] and not len(fields) == 3:
                        self._log('!!! {0}:{1}: invalid field count {2} for rule "{3}"',
                                  self._conf.blacklist_filename, lno + 1, len(fields), fields[1])
                        continue
                    elif not fields[1].lower() in ['d', 'n', 'e', 'ne', 'en']:
                        self._log('!!! {0}:{1}: invalid rule "{2}"',
                                  self._conf.blacklist_filename, lno + 1, fields[1])
                        continue
                    
                    if len(fields) == 2:
                        fields.append(None)
                    
                    self._blacklist[fields[0]] = { 'addr' : fields[0],
                                                   'action' : fields[1],
                                                   'param' : fields[2] }
                    self._log('--- blacklist: <addr: {0}>; <action: {1}>; <param: {2}>',
                              fields[0], fields[1], fields[2], verbosity=3)
            except IOError, e:
                self._log('!!! failed to open blacklist file "{0}": {1}',
                          self._conf.blacklist_filename, str(e))
Example #2
0
    def __init__(self, conf):
        super(FUCore, self).__init__()

        self._conf = conf
        self._logger = logging.getLogger(self.__class__.__name__)
        if self._conf.log_facility == 'file':
            handler = TimedRotatingFileHandler(self._conf.log_filename,
                                               self._conf.log_when,
                                               self._conf.log_interval,
                                               self._conf.log_keep)
            if os.path.exists(self._conf.log_filename):
                # try to fixup rollover time
                stat = os.stat(self._conf.log_filename)
                ctime = int(stat.st_ctime)
                # work around python 2.6.2 deficiencies.. (not 100% accurate)
                if sys.hexversion < 0x20603f0:
                    handler.rolloverAt = ctime + handler.interval
                else:
                    handler.rolloverAt = handler.computeRollover(ctime)

            format = '%(asctime)s [%(process)d]: %(levelname)s: %(message)s'
        else:
            handler = SysLogHandler('/dev/log', SysLogHandler.LOG_NEWS)
            format = 'SYNFU[%(process)d] %(message)s'

        formatter = logging.Formatter(format)
        handler.setFormatter(formatter)
        self._logger.setLevel(logging.DEBUG)
        self._logger.addHandler(handler)

        self._blacklist = {}
        if self._conf.blacklist_filename:
            try:
                blacklist_file = open(self._conf.blacklist_filename)
                for (lno, line) in enumerate(blacklist_file.readlines()):
                    line = line.strip()
                    if line.startswith('#'):
                        continue
                    fields = [x.strip() for x in line.split(';') if x]
                    if len(fields) < 2 or len(fields) > 3:
                        self._log(
                            "!!! {0}:{1}: invalid field count {2} expected 2 or 3",
                            self._conf.blacklist_filename, lno + 1,
                            len(fields))
                        continue
                    elif fields[1].lower() in ['e', 'ne', 'en'
                                               ] and not len(fields) == 3:
                        self._log(
                            '!!! {0}:{1}: invalid field count {2} for rule "{3}"',
                            self._conf.blacklist_filename, lno + 1,
                            len(fields), fields[1])
                        continue
                    elif not fields[1].lower() in ['d', 'n', 'e', 'ne', 'en']:
                        self._log('!!! {0}:{1}: invalid rule "{2}"',
                                  self._conf.blacklist_filename, lno + 1,
                                  fields[1])
                        continue

                    if len(fields) == 2:
                        fields.append(None)

                    self._blacklist[fields[0]] = {
                        'addr': fields[0],
                        'action': fields[1],
                        'param': fields[2]
                    }
                    self._log(
                        '--- blacklist: <addr: {0}>; <action: {1}>; <param: {2}>',
                        fields[0],
                        fields[1],
                        fields[2],
                        verbosity=3)
            except IOError, e:
                self._log('!!! failed to open blacklist file "{0}": {1}',
                          self._conf.blacklist_filename, str(e))