logwatch_filters.py
author Tero Marttila <terom@paivola.fi>
Wed, 26 Mar 2008 01:16:54 +0200
changeset 16 521fec9bb663
parent 12 cf388baabf0a
child 18 6348bf9750bc
permissions -rw-r--r--
git is starting to confuse me...

Merge commit 'refs/remotes/publish/master'

Conflicts:

irc.py

committer: Tero Marttila <terom@paivola.fi>
import re

class Filter (object) :
    def __init__ (self, regexp, event_type) :
        self.regexp = re.compile(regexp)
        self.event_type = event_type

    def test (self, line) :
        match = self.regexp.search(line)
        
        if match :
            return self._filter(match)
        
    def _filter (self, match) :
        return match.string

class AutoFilter (Filter) :
    # your event type here, as a string
    EVENT = None

    # your regexp here, with named matchgroups
    REGEXP = None

    # your output format, with named interpolation params
    OUTPUT = None
    
    def __init__ (self) :
        super(AutoFilter, self).__init__(self.REGEXP, self.EVENT)
    
    def _filter (self, match) :
        return self.OUTPUT % match.groupdict()

class SudoFilter (AutoFilter) :
    EVENT   = "sudo"
    REGEXP  = "sudo:\s*(?P<username>\S+) : TTY=(?P<tty>\S+) ; PWD=(?P<pwd>.+?) ; USER=(?P<target_user>\S+) ; COMMAND=(?P<command>.*)"
    OUTPUT  = "%(username)s:%(tty)s - %(pwd)s - `%(command)s` as %(target_user)s"

class SSHFilter (AutoFilter) :
    EVENT   = "ssh"
    REGEXP  = "(?P<success>Accepted|Failed) password for (?P<username>\S+) from (?P<ip>\S+) port (?P<port>\S+) (?P<proto>\S+)"
    OUTPUT  = "%(success)s login for %(username)s from %(ip)s:%(port)s proto %(proto)s"