From 50075efcfbe66eb3b4b889a283e4165c6fea0f22 Mon Sep 17 00:00:00 2001 From: Yves G Date: Wed, 31 Jan 2018 13:04:23 +0100 Subject: [PATCH] make the dailyReport able to cope with restarts --- pyruse/actions/action_dailyReport.py | 34 ++++++++++++++++------------ 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pyruse/actions/action_dailyReport.py b/pyruse/actions/action_dailyReport.py index 5458393..7e69fcc 100644 --- a/pyruse/actions/action_dailyReport.py +++ b/pyruse/actions/action_dailyReport.py @@ -1,5 +1,5 @@ # pyruse is intended as a replacement to both fail2ban and epylog -# Copyright © 2017 Y. Gablin +# Copyright © 2017–2018 Y. Gablin # Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing. import json import os @@ -11,10 +11,8 @@ from pyruse import base, config, email class Action(base.Action): _storage = config.Config().asMap().get("storage", "/var/lib/pyruse") \ + "/" + os.path.basename(__file__) + ".journal" - + _out = None _hour = 0 - _out = open(_storage, "w", 1) - _out.write("[\n") _txtDocStart = '= Pyruse Report\n\n' _txtHeadWarn = '== WARNING Messages\n\n' @@ -34,6 +32,19 @@ class Action(base.Action): _htmPreStart = '
'
     _htmPreStop = '
\n' + def _closeJournal(): + Action._out.write("{}]") + Action._out.close() + Action._out = None + + def _openJournal(): + if Action._out is None: + if os.path.exists(Action._storage): + Action._out = open(Action._storage, "a", 1) + else: + Action._out = open(Action._storage, "w", 1) + Action._out.write("[\n") + def __init__(self, args): super().__init__() l = args["level"] @@ -61,19 +72,11 @@ class Action(base.Action): Action._out.write(",\n") thisHour = datetime.today().hour if thisHour < Action._hour: - self._closeJournal() + Action._closeJournal() self._sendReport() - self._openJournal() + Action._openJournal() Action._hour = thisHour - def _closeJournal(self): - Action._out.write("{}]") - Action._out.close() - - def _openJournal(self): - Action._out = open(Action._storage, "w", 1) - Action._out.write("[\n") - def _encode(self, text): return text.replace('&', '&').replace('<', '<').replace('>', '>') @@ -99,6 +102,7 @@ class Action(base.Action): messages[L][M].append(T) else: messages[L][M] = [T] + os.remove(Action._storage) html = Action._htmDocStart + Action._htmHeadWarn text = Action._txtDocStart + Action._txtHeadWarn @@ -136,3 +140,5 @@ class Action(base.Action): html += Action._htmDocStop email.Mail(text, html).send() + +Action._openJournal()