fixbot/logwatch_sources.py
author terom@fixme.fi
Mon, 15 Sep 2008 00:27:05 +0300
changeset 21 aa6df8f9c44a
permissions -rw-r--r--
add initial code back under fixbot/, the git-convert somehow broke
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     1
from twisted.internet import reactor, protocol
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     2
from twisted.python import log
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     3
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     4
import fifo
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     5
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     6
class LogSource (object) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     7
    def __init__ (self, name, filters) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     8
        # set later on
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     9
        self.module = None
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    10
        
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    11
        # what filters to apply
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    12
        self.filters = filters
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    13
        
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    14
        # name, for display purposes
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    15
        self.name = name
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    16
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    17
        # used to gather data together into lines
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    18
        self.buf = ""
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    19
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    20
    def setModule (self, module) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    21
        self.module = module
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    22
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    23
    def handleError (self, msg) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    24
        log.err(msg)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    25
        self.module.error(msg)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    26
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    27
    def handleData (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    28
        data = self.buf + data
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    29
        
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    30
        while "\n" in data :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    31
            line, data = data.split("\n", 1)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    32
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    33
            self.handleLine(line)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    34
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    35
        self.buf = data
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    36
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    37
    def handleLine (self, line) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    38
        log.msg("Matching line `%s'..." % line)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    39
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    40
        for filter in self.filters :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    41
            out = filter.test(line)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    42
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    43
            if out :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    44
                log.msg("\t%s: %s" % (filter.event_type, out))
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    45
                self.module.sendEvent(filter.event_type, out)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    46
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    47
                break
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    48
            elif out is False :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    49
                return
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    50
            else :  # None
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    51
                continue
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    52
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    53
class File (LogSource, protocol.ProcessProtocol) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    54
    def __init__ (self, name, path, filters) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    55
        super(File, self).__init__(name, filters)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    56
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    57
        self.path = path
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    58
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    59
        log.msg("spawning tail process for %s:%s" % (name, path))
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    60
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    61
        reactor.spawnProcess(self, "/usr/bin/tail", ["tail", "-n0", "--follow=name", path])
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    62
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    63
    def errReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    64
        self.handleError("tail for %s: %s" % (self.name, data))
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    65
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    66
    def outReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    67
        self.handleData(data)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    68
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    69
    def processEnded (self, reason) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    70
        self.handleError("tail process for %s quit: %s" % (self.name, reason.getErrorMessage()))
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    71
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    72
class Fifo (LogSource, fifo.Fifo) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    73
    def __init__ (self, name, path, filters) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    74
        LogSource.__init__(self, name, filters)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    75
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    76
        log.msg("opening fifo for %s:%s" % (name, path))
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    77
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    78
        fifo.Fifo.__init__(self, path)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    79
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    80
    def dataReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    81
        self.handleData(data)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    82
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    83
    def handleEOF (self) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    84
        self.handleError("!!! EOF on fifo %s, re-opening" % self.name)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    85
        self.reopen()
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    86
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    87
    def connectionLost (self, reason) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    88
        super(Fifo, self).connectionLost(reason)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    89
        self.handleError("lost fifo for %s: %s" % (self.name, reason.getErrorMessage()))
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    90
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    91