make the dailyReport able to cope with restarts

master
Yves G 2018-01-31 13:04:23 +01:00
parent 4b42b69b49
commit 50075efcfb
1 changed files with 20 additions and 14 deletions

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog # pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin # Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing. # Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
import json import json
import os import os
@ -11,10 +11,8 @@ from pyruse import base, config, email
class Action(base.Action): class Action(base.Action):
_storage = config.Config().asMap().get("storage", "/var/lib/pyruse") \ _storage = config.Config().asMap().get("storage", "/var/lib/pyruse") \
+ "/" + os.path.basename(__file__) + ".journal" + "/" + os.path.basename(__file__) + ".journal"
_out = None
_hour = 0 _hour = 0
_out = open(_storage, "w", 1)
_out.write("[\n")
_txtDocStart = '= Pyruse Report\n\n' _txtDocStart = '= Pyruse Report\n\n'
_txtHeadWarn = '== WARNING Messages\n\n' _txtHeadWarn = '== WARNING Messages\n\n'
@ -34,6 +32,19 @@ class Action(base.Action):
_htmPreStart = '<pre>' _htmPreStart = '<pre>'
_htmPreStop = '</pre>\n' _htmPreStop = '</pre>\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): def __init__(self, args):
super().__init__() super().__init__()
l = args["level"] l = args["level"]
@ -61,19 +72,11 @@ class Action(base.Action):
Action._out.write(",\n") Action._out.write(",\n")
thisHour = datetime.today().hour thisHour = datetime.today().hour
if thisHour < Action._hour: if thisHour < Action._hour:
self._closeJournal() Action._closeJournal()
self._sendReport() self._sendReport()
self._openJournal() Action._openJournal()
Action._hour = thisHour 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): def _encode(self, text):
return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;') return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
@ -99,6 +102,7 @@ class Action(base.Action):
messages[L][M].append(T) messages[L][M].append(T)
else: else:
messages[L][M] = [T] messages[L][M] = [T]
os.remove(Action._storage)
html = Action._htmDocStart + Action._htmHeadWarn html = Action._htmDocStart + Action._htmHeadWarn
text = Action._txtDocStart + Action._txtHeadWarn text = Action._txtDocStart + Action._txtHeadWarn
@ -136,3 +140,5 @@ class Action(base.Action):
html += Action._htmDocStop html += Action._htmDocStop
email.Mail(text, html).send() email.Mail(text, html).send()
Action._openJournal()