Created
January 9, 2015 15:51
-
-
Save lmacken/21db0e848beedb901bc3 to your computer and use it in GitHub Desktop.
Monitoring the systemd journal with Twisted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from systemd import journal | |
from twisted.internet import reactor | |
UNITS = ['suricata.service'] | |
class JournalMonitor(object): | |
def __init__(self): | |
self.journal = journal.Reader() | |
for unit in UNITS: | |
self.journal.add_match(_SYSTEMD_UNIT=unit) | |
self.journal.seek_tail() | |
self.journal.get_previous() | |
reactor.addReader(self) | |
def fileno(self): | |
return self.journal.fileno() | |
def doRead(self): | |
if self.journal.process() != journal.APPEND: | |
return | |
for entry in self.journal: | |
print(entry) | |
def connectionLost(self, reason): | |
print(reason) | |
if reactor.running: | |
reactor.stop() | |
def logPrefix(self): | |
return self.__class__.__name__ | |
JournalMonitor() | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment