pyruse/pyruse/main.py

54 lines
1.8 KiB
Python
Raw Normal View History

# pyruse is intended as a replacement to both fail2ban and epylog
2018-01-31 12:04:21 +01:00
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
2017-12-15 19:36:50 +01:00
import os
import sys
from systemd import journal
2018-01-31 12:04:21 +01:00
from pyruse import config, module, workflow
2017-12-15 19:36:50 +01:00
PYRUSE_ENVVAR = "PYRUSE_EXTRA"
PYRUSE_PATHS = []
def _setPyrusePaths():
global PYRUSE_ENVVAR, PYRUSE_PATHS
for p in "/etc/pyruse", os.environ.get(PYRUSE_ENVVAR):
2018-01-31 13:00:21 +01:00
if p and os.path.isdir(p):
2017-12-15 19:36:50 +01:00
PYRUSE_PATHS.insert(0, p)
sys.path.insert(1, p)
PYRUSE_PATHS.insert(0, os.curdir)
def _doForEachJournalEntry(workflow):
enc8b = config.Config().asMap().get("8bit-message-encoding", "iso-8859-1")
2017-12-15 19:36:50 +01:00
j = journal.Reader(journal.SYSTEM_ONLY)
j.seek_tail()
j.get_previous()
while True:
2018-01-31 13:00:21 +01:00
event = j.wait(None)
2017-12-15 19:36:50 +01:00
if event == journal.APPEND:
for entry in j:
m = entry['MESSAGE']
if not isinstance(m, str):
entry['MESSAGE'] = m.decode(enc8b)
step = workflow.firstStep
while step is not None:
step = step.run(entry)
2017-12-16 20:26:59 +01:00
def boot(modName):
_setPyrusePaths()
conf = config.Config(PYRUSE_PATHS)
2017-12-16 20:26:59 +01:00
if "action_" in modName:
module.get({"action": modName, "args": None}).module.boot()
2017-12-16 20:26:59 +01:00
elif "filter_" in modName:
module.get({"filter": modName, "args": None}).module.boot()
2017-12-16 20:26:59 +01:00
else:
raise ValueError("Neither “action_” nor “filter_” found in the module name; the `boot` feature cannot work for %s\n" % modName)
2017-12-15 19:36:50 +01:00
def main():
_setPyrusePaths()
2018-01-31 12:04:21 +01:00
conf = config.Config(PYRUSE_PATHS).asMap().get("actions", {})
wf = workflow.Workflow(conf)
_doForEachJournalEntry(wf)
2017-12-15 19:36:50 +01:00
if __name__ == '__main__':
main()