2017-12-16 18:59:33 +01:00
|
|
|
|
# pyruse is intended as a replacement to both fail2ban and epylog
|
2018-01-31 12:04:21 +01:00
|
|
|
|
# Copyright © 2017–2018 Y. Gablin
|
2017-12-16 18:59:33 +01:00
|
|
|
|
# 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(fct):
|
|
|
|
|
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:
|
2018-01-31 13:00:21 +01:00
|
|
|
|
fct(entry)
|
2017-12-16 20:26:59 +01:00
|
|
|
|
|
|
|
|
|
def boot(modName):
|
|
|
|
|
if "action_" in modName:
|
|
|
|
|
module.get({"action": modName}).module.boot()
|
|
|
|
|
elif "filter_" in modName:
|
|
|
|
|
module.get({"filter": modName}).module.boot()
|
|
|
|
|
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)
|
2017-12-15 19:36:50 +01:00
|
|
|
|
_doForEachJournalEntry(wf.run)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|