Skip to content

Instantly share code, notes, and snippets.

@lmacken
Created January 9, 2015 15:51
Show Gist options
  • Save lmacken/21db0e848beedb901bc3 to your computer and use it in GitHub Desktop.
Save lmacken/21db0e848beedb901bc3 to your computer and use it in GitHub Desktop.
Monitoring the systemd journal with Twisted
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