it works \o/ + the start of logwatcher.py
authorTero Marttila <terom@paivola.fi>
Thu, 20 Mar 2008 18:29:42 +0200
changeset 3 5ab150c4a328
parent 2 cc76980b8652
child 4 34d7897bd0f5
it works \o/ + the start of logwatcher.py

committer: Tero Marttila <terom@paivola.fi>
example.py
logwatcher.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example.py	Thu Mar 20 18:29:42 2008 +0200
@@ -0,0 +1,24 @@
+from twisted.internet import protocol, reactor
+from twisted.python import log
+import sys
+
+import api
+
+class ExampleModule (api.Module) :
+    name = "example"
+    version = 0x0001
+    
+    event_types = [
+        "example"
+    ]
+
+    def handleConnect (self) :
+        self.sendEvent("example", "this is an example event")
+        self.disconnect()
+
+if __name__ == '__main__' :
+    log.startLogging(sys.stderr)
+    
+    module = ExampleModule()
+    reactor.run()
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/logwatcher.py	Thu Mar 20 18:29:42 2008 +0200
@@ -0,0 +1,87 @@
+from twisted.internet import protocol, reactor
+from twisted.python import log
+import sys, re
+
+import api
+
+class TailProcessProtocol (protocol.ProcessProtocol) :
+    def __init__ (self, module, name, filters) :
+        self.module = module
+        self.name = name
+        self.filters = filters
+
+        self.buf = ""
+
+    def errReceived (self, data) :
+        self.module.error("tail for %s: %s" % (self.name, data))
+
+    def outReceived (self, data) :
+        data = buf + data
+        
+        while "\n" in data :
+            line, data = data.split("\n", 1)
+
+            for filter in self.filters :
+                out = filter.test(line)
+
+        self.buf = data
+
+class Filter (object) :
+    def __init__ (self, regexp, event_type) :
+        self.regexp = re.compile(regexp)
+        self.event_type = event_type
+
+    def test (self, line) :
+        match = self.regexp.search(line)
+        
+        if match :
+            return self._filter(match)
+        
+    def _filter (self, match) :
+        return match.string
+
+class SudoFilter (Filter) :
+    def __init__ (self) :
+        super(Filter, self).__init__("sudo: (?P<username>\w+) : TTY=(?P<tty>\w+) ; PWD=(?P<pwd>\w+) ; USER=(?P<target_user>\w+) ; COMMAND=(?P<command>\w+)", "sudo")
+
+    def _filter (self, match) :
+        return "%(username)s:%(tty)s:%(pwd)s - `%(command)s` as %(target_user)s"
+
+class ExampleModule (api.Module) :
+    name = "logs"
+    version = 0x0001
+    
+    event_types = [
+        "error",
+        "sudo"
+    ]
+
+    log_files = (
+        ("auth.log", "/var/log/auth.log", (
+            SudoFilter(),
+        )),
+    )
+
+    log_objs = None
+
+    def handleConnect (self) :
+        log.msg("Spawning tail processes...")
+        
+        self.log_objs = dict()
+
+        for name, file, filters in self.log_files :
+            log.msg("%s - %s..." % (name, file)))
+
+            p = self.log_objs[name] = TailProcessProtocol(filters)
+
+            reactor.spawnProcess(p, "/usr/bin/tail", ["tail", "--follow=name", file])
+    
+    def error (self, msg) :
+        self.sendEvent("error", msg)
+
+if __name__ == '__main__' :
+    log.startLogging(sys.stderr)
+    
+    module = ExampleModule()
+    reactor.run()
+