fixbot/logwatch/sources.py
author Tero Marttila <terom@fixme.fi>
Fri, 05 Feb 2010 21:30:06 +0200
changeset 54 395182b7ea0f
parent 53 21ab25ffa1e8
child 59 43806d334fb4
permissions -rw-r--r--
logPrefix magic for LogSource, cleanup handleMessage
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
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
     8
from fixbot import fifo
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
     9
from fixbot.logwatch import message
21
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
class LogSource (object) :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    12
    """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    13
        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
    14
    """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    15
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    16
    def __init__ (self, name, filters) :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    17
        """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    18
            name            - label lines read from this source
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    19
            filters         - LogFilter chain to pass lines through
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    20
        """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    21
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    22
        # set later on
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    23
        self.module = None
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    24
        
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    25
        # what filters to apply
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    26
        self.filters = filters
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    27
        
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    28
        # name, for display purposes
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    29
        self.name = name
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    30
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    31
        # 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
    32
        self.buf = ""
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    33
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    34
    def setModule (self, module) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    35
        self.module = module
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 handleError (self, msg) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    38
        log.err(msg)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    39
        self.module.error(msg)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    40
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    41
    def handleData (self, data) :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    42
        """
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    43
            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
    44
        """
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    45
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    46
        data = self.buf + data
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    47
        
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    48
        while "\n" in data :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    49
            line, data = data.split("\n", 1)
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    50
            
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    51
            # full line
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    52
            self.handleLine(line)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    53
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    54
        self.buf = data
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    55
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    56
    def handleLine (self, line) :
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    57
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    58
            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
    59
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    60
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    61
        # parse
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    62
        try :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    63
            msg = message.SyslogMessage(line)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    64
    
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    65
        except Exception, e :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    66
            # log and ignore
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    67
            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
    68
    
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    69
        else :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    70
            # handle the structured message
54
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    71
            log.callWithLogger(self, self.handleMessage, msg)
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    72
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    73
    def handleMessage (self, msg) :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    74
        """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    75
            Process the given SyslogMessage
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    76
        """
54
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    77
        
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    78
        # Log incoming lines
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    79
        log.msg(repr(msg))
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    80
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    81
        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
    82
            # let the filter process the line
53
21ab25ffa1e8 implement SyslogFilter
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    83
            out = filter.match(msg)
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    84
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    85
            if out :
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    86
                # unpack
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    87
                type, msg = out
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    88
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
    89
                # positive match, send
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    90
                log.msg("\t%s: %s" % (type, msg))
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    91
                
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    92
                # drop until we have a module
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    93
                if self.module :
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    94
                    self.module.sendEvent(type, msg)
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    95
                
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    96
                # ok, first hit does it
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    97
                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
    98
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    99
            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
   100
                # negative match, stop processing
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   101
                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
   102
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   103
            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
   104
                # no match
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   105
                continue
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   106
48
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   107
            else :
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   108
                raise ValueError(out)
ba101beeb062 work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   109
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   110
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
   111
    """
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
   112
        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
   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
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   115
    def __init__ (self, name, path, filters) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   116
        super(File, self).__init__(name, filters)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   117
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   118
        self.path = path
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
        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
   121
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   122
        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
   123
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   124
    def errReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   125
        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
   126
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   127
    def outReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   128
        self.handleData(data)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   129
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   130
    def processEnded (self, reason) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   131
        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
   132
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   133
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
   134
    """
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
   135
        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
   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
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   138
    def __init__ (self, name, path, filters) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   139
        LogSource.__init__(self, name, filters)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   140
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   141
        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
   142
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   143
        fifo.Fifo.__init__(self, 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
    def dataReceived (self, data) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   146
        self.handleData(data)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   147
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   148
    def handleEOF (self) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   149
        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
   150
        self.reopen()
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   151
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   152
    def connectionLost (self, reason) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   153
        super(Fifo, self).connectionLost(reason)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   154
        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
   155
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   156
class UnixDatagramSocket (LogSource, protocol.DatagramProtocol) :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   157
    """
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   158
        Stream messages from a UNIX datagram socket
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
    
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   161
    # maximum size of the recieved messages
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   162
    # 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
   163
    MAX_PACKET_SIZE = 4096
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
   164
51
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   165
    def __init__ (self, name, path, filters) :
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   166
        LogSource.__init__(self, name, filters)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   167
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   168
        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
   169
        
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   170
        # open UNIX socket
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   171
        reactor.listenUNIXDatagram(path, self, self.MAX_PACKET_SIZE)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   172
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   173
    def datagramReceived (self, data, addr) :
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
            Got message from syslog-ng
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
        
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   178
        # handle it as a line of data
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   179
        self.handleLine(data)
342850300d6a implement UnixDatagramSocket and LogSource.handleMessage
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
   180
54
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   181
    def logPrefix (self) :
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   182
        return "LogSource(%s)" % (self.name, )
395182b7ea0f logPrefix magic for LogSource, cleanup handleMessage
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   183