author | Tero Marttila <terom@fixme.fi> |
Thu, 04 Feb 2010 20:39:53 +0200 | |
changeset 48 | ba101beeb062 |
parent 40 | b9fdb7710768 |
child 51 | 342850300d6a |
permissions | -rw-r--r-- |
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 |
|
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
5 |
from twisted.internet import protocol |
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 |
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 |
""" |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
42 |
Buffer the given chunk of data, passing any full lines to handleLine |
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) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
49 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
50 |
self.handleLine(line) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
51 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
52 |
self.buf = data |
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 |
def handleLine (self, line) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
55 |
log.msg("Matching line `%s'..." % line) |
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 |
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
|
58 |
# let the filter process the line |
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
59 |
out = filter.test(line) |
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 |
if out : |
48
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
62 |
# unpack |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
63 |
type, msg = out |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
64 |
|
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
|
65 |
# positive match, send |
48
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
66 |
log.msg("\t%s: %s" % (type, msg)) |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
67 |
|
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
68 |
# drop until we have a module |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
69 |
if self.module : |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
70 |
self.module.sendEvent(type, msg) |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
71 |
|
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
72 |
# ok, first hit does it |
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
73 |
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
|
74 |
|
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
75 |
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
|
76 |
# negative match, stop processing |
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
77 |
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
|
78 |
|
48
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
79 |
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
|
80 |
# no match |
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
81 |
continue |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
82 |
|
48
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
83 |
else : |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
84 |
raise ValueError(out) |
ba101beeb062
work on logwatch docs, small tweaks
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
85 |
|
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
86 |
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
|
87 |
""" |
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
|
88 |
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
|
89 |
""" |
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
|
90 |
|
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
91 |
def __init__ (self, name, path, filters) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
92 |
super(File, self).__init__(name, filters) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
93 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
94 |
self.path = path |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
95 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
96 |
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
|
97 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
98 |
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
|
99 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
100 |
def errReceived (self, data) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
101 |
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
|
102 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
103 |
def outReceived (self, data) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
104 |
self.handleData(data) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
105 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
106 |
def processEnded (self, reason) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
107 |
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
|
108 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
109 |
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
|
110 |
""" |
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 |
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
|
112 |
""" |
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 |
|
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
114 |
def __init__ (self, name, path, filters) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
115 |
LogSource.__init__(self, name, filters) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
116 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
117 |
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
|
118 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
119 |
fifo.Fifo.__init__(self, path) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
120 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
121 |
def dataReceived (self, data) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
122 |
self.handleData(data) |
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 handleEOF (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
125 |
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
|
126 |
self.reopen() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
127 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
128 |
def connectionLost (self, reason) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
129 |
super(Fifo, self).connectionLost(reason) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
130 |
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
|
131 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
132 |