fixbot/logwatch/sources.py
author Tero Marttila <terom@fixme.fi>
Sat, 20 Feb 2010 22:32:18 +0200
changeset 63 a849c00b63f8
parent 59 43806d334fb4
child 64 8574aeff9b36
permissions -rw-r--r--
move fixbot.fifo to fixbot.logwatch.fifo
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     1
"""
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     2
    Implementations of the various sources of log data
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     3
"""
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     4
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
     5
from twisted.internet import protocol, reactor
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     6
from twisted.python import log
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     7
63
a849c00b63f8 move fixbot.fifo to fixbot.logwatch.fifo
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
     8
from fixbot.logwatch import fifo, message
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     9
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    10
class LogSource (object) :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    11
    """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    12
        Reads lines of log data from some file or other source.
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    13
    """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    14
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    15
    def __init__ (self, name, filters) :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    16
        """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    17
            name            - label lines read from this source
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    18
            filters         - LogFilter chain to pass lines through
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    19
        """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    20
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    21
        # set later on
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    22
        self.module = None
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    23
        
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    24
        # what filters to apply
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    25
        self.filters = filters
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
        # name, for display purposes
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    28
        self.name = name
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
        # 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
    31
        self.buf = ""
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
    def setModule (self, module) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    34
        self.module = module
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    35
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    36
    def handleError (self, msg) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    37
        log.err(msg)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    38
        self.module.error(msg)
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
    def handleData (self, data) :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    41
        """
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    42
            Feed binary data into the buffer, processing all lines via handleLine()
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    43
        """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    44
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    45
        data = self.buf + data
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
        while "\n" in data :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    48
            line, data = data.split("\n", 1)
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    49
            
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    50
            # full line
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    51
            self.handleLine(line)
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
        self.buf = data
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    54
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    55
    def handleLine (self, line) :
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    56
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    57
            Parse given line into a SyslogMessage, and pass it to handleMessage
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    58
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    59
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    60
        # parse
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    61
        try :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    62
            msg = message.SyslogMessage(line)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    63
    
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    64
        except Exception, e :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    65
            # log and ignore
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    66
            log.err(e, "Invalid syslog message from source %s: %s" % (self.name, line))
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    67
    
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    68
        else :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    69
            # handle the structured message
54
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    70
            log.callWithLogger(self, self.handleMessage, msg)
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    71
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    72
    def handleMessage (self, msg) :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    73
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    74
            Process the given SyslogMessage
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    75
        """
54
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    76
        
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    77
        # Log incoming lines
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    78
        log.msg(repr(msg))
21
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
        for filter in self.filters :
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
    81
            # let the filter process the line
53
21ab25ffa1e8 implement SyslogFilter
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    82
            out = filter.match(msg)
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    83
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    84
            if out :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    85
                # unpack
59
43806d334fb4 prepend source name to output type
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    86
                label, msg = out
43806d334fb4 prepend source name to output type
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    87
43806d334fb4 prepend source name to output type
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    88
                # output tag/type
43806d334fb4 prepend source name to output type
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    89
                type = "%s:%s" % (self.name, label)
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    90
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
    91
                # positive match, send
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    92
                log.msg("\t%s: %s" % (type, msg))
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    93
                
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    94
                # drop until we have a module
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    95
                if self.module :
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    96
                    self.module.sendEvent(type, msg)
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    97
                
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    98
                # ok, first hit does it
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    99
                break
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   100
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   101
            elif out is False :
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   102
                # negative match, stop processing
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   103
                return
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   104
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   105
            elif out is None :
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   106
                # no match
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   107
                continue
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   108
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   109
            else :
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   110
                raise ValueError(out)
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   111
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   112
class File (LogSource, protocol.ProcessProtocol) :
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   113
    """
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   114
        Stream lines from a regular file using /usr/bin/tail -f
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   115
    """
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   116
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   117
    def __init__ (self, name, path, filters) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   118
        super(File, self).__init__(name, filters)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   119
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   120
        self.path = path
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   121
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   122
        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
   123
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   124
        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
   125
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   126
    def errReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   127
        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
   128
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   129
    def outReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   130
        self.handleData(data)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   131
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   132
    def processEnded (self, reason) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   133
        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
   134
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   135
class Fifo (LogSource, fifo.Fifo) :
40
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   136
    """
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   137
        Stream lines from a fifo object.
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   138
    """
b9fdb7710768 move logwatch*.py to separate fixbot.logwatch package, and move logwatch_sources to etc/fixbot-logwatch.py
Tero Marttila <terom@fixme.fi>
parents: 21
diff changeset
   139
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   140
    def __init__ (self, name, path, filters) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   141
        LogSource.__init__(self, name, filters)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   142
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   143
        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
   144
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   145
        fifo.Fifo.__init__(self, path)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   146
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   147
    def dataReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   148
        self.handleData(data)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   149
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   150
    def handleEOF (self) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   151
        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
   152
        self.reopen()
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   153
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   154
    def connectionLost (self, reason) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   155
        super(Fifo, self).connectionLost(reason)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   156
        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
   157
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   158
class UnixDatagramSocket (LogSource, protocol.DatagramProtocol) :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   159
    """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   160
        Stream messages from a UNIX datagram socket
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   161
    """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   162
    
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   163
    # maximum size of the recieved messages
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   164
    # native syslog is 1024, but syslog-ng does better... so 4k
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   165
    MAX_PACKET_SIZE = 4096
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   166
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   167
    def __init__ (self, name, path, filters) :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   168
        LogSource.__init__(self, name, filters)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   169
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   170
        log.msg("opening unix socket for %s at: %s" % (name, path))
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   171
        
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   172
        # open UNIX socket
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   173
        reactor.listenUNIXDatagram(path, self, self.MAX_PACKET_SIZE)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   174
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   175
    def datagramReceived (self, data, addr) :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   176
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   177
            Got message from syslog-ng
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   178
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   179
        
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   180
        # handle it as a line of data
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   181
        self.handleLine(data)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   182
54
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   183
    def logPrefix (self) :
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   184
        return "LogSource(%s)" % (self.name, )
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   185