3 import sys |
3 import sys |
4 |
4 |
5 import api |
5 import api |
6 import logwatch_config as config |
6 import logwatch_config as config |
7 |
7 |
8 class TailProcessProtocol (protocol.ProcessProtocol) : |
|
9 def __init__ (self, module, name, filters) : |
|
10 self.module = module |
|
11 self.name = name |
|
12 self.filters = filters |
|
13 |
|
14 self.buf = "" |
|
15 |
|
16 def errReceived (self, data) : |
|
17 self.module.error("tail for %s: %s" % (self.name, data)) |
|
18 |
|
19 def outReceived (self, data) : |
|
20 data = self.buf + data |
|
21 |
|
22 while "\n" in data : |
|
23 line, data = data.split("\n", 1) |
|
24 |
|
25 log.msg("Matching line `%s'..." % line) |
|
26 |
|
27 for filter in self.filters : |
|
28 out = filter.test(line) |
|
29 |
|
30 if out : |
|
31 log.msg("\t%s: %s" % (filter.event_type, out)) |
|
32 self.module.sendEvent(filter.event_type, out) |
|
33 |
|
34 self.buf = data |
|
35 |
|
36 def processEnded (self, reason) : |
|
37 msg = "tail process for %s quit: %s" % (self.name, reason.getErrorMessage()) |
|
38 |
|
39 log.err(msg) |
|
40 self.module.error(msg) |
|
41 |
|
42 class LogWatchModule (api.Module) : |
8 class LogWatchModule (api.Module) : |
43 name = "logs" |
9 name = "logs" |
44 version = 0x0001 |
10 version = 0x0005 |
45 |
11 |
46 event_types = [ |
12 event_types = [ |
47 "error", |
13 "error", |
48 "sudo", |
14 "sudo", |
49 "ssh", |
15 "ssh", |
|
16 "all" |
50 ] |
17 ] |
51 |
18 |
52 log_files = config.log_files |
|
53 |
|
54 log_objs = None |
|
55 |
|
56 def handleConnect (self) : |
19 def handleConnect (self) : |
57 log.msg("Spawning tail processes...") |
20 for source in config.sources() : |
58 |
21 source.setModule(self) |
59 self.log_objs = dict() |
|
60 |
|
61 for name, file, filters in self.log_files : |
|
62 log.msg("\t%s - %s..." % (name, file)) |
|
63 |
|
64 p = self.log_objs[name] = TailProcessProtocol(self, name, filters) |
|
65 |
|
66 reactor.spawnProcess(p, "/usr/bin/tail", ["tail", "-n0", "--follow=name", file]) |
|
67 |
22 |
68 def error (self, msg) : |
23 def error (self, msg) : |
69 self.sendEvent("error", msg) |
24 self.sendEvent("error", msg) |
70 |
25 |
71 if __name__ == '__main__' : |
26 if __name__ == '__main__' : |