retour arriere sur parseStats.py

This commit is contained in:
David 2022-12-19 09:56:58 +01:00
parent a3e6532bd3
commit 2719c12ebf
2 changed files with 42 additions and 9 deletions

View File

@ -0,0 +1,32 @@
#!/usr/bin/env python3
import datetime, itertools, os, re
now = datetime.datetime.now(datetime.timezone.utc)
max_backup_delay = datetime.timedelta(1, 7200)
def get_name(match):
return match.group('name')
def check_backup(filename):
with open(filename) as f:
logs = f.read()
mixed_statuses = list(re.finditer(r'(?P<status>Succeeded|Failed) (?P<name>\w+) backup at (?P<date>\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\+\d\d:\d\d)$', logs, re.MULTILINE))
for name, statuses in itertools.groupby(sorted(mixed_statuses, key=get_name), key=get_name):
last = sorted(statuses, key=lambda x: x.group('date'))[-1]
print('{name}: {status} at {date}'.format(**last.groupdict()))
last_date = datetime.datetime.fromisoformat(last.group('date'))
last_status = last.group('status')
if last_status != 'Succeeded' or now - last_date > max_backup_delay:
failure.append(name)
failure = []
try:
check_backup ("/var/log/borgmatic.log")
except Exception:
check_backup ("/var/log/borgmatic.log.1")
if failure:
exit (1)
else:
exit (0)

View File

@ -205,16 +205,17 @@ def runMain():
year = currentDate.year year = currentDate.year
stats = Stats(year,mois) stats = Stats(year,mois)
res = stats.parse2() #res = stats.parse2()
for (k,v) in res.items():
print(f"{k}={v}")
#for (k,v) in res.items(): #for (k,v) in res.items():
# if k in STATS_TOT_FIELDS: # print(f"{k}={v}")
# chaine = STATS_FR_TOT_FIELDS[STATS_TOT_FIELDS.index(k)]
# elif k in STATS_AVG_FIELDS: res = stats.parse()
# chaine = STATS_FR_AVG_FIELDS[STATS_AVG_FIELDS.index(k)] for (k,v) in res.items():
# print(f"{chaine} : {v}") if k in STATS_TOT_FIELDS:
chaine = STATS_FR_TOT_FIELDS[STATS_TOT_FIELDS.index(k)]
elif k in STATS_AVG_FIELDS:
chaine = STATS_FR_AVG_FIELDS[STATS_AVG_FIELDS.index(k)]
print(f"{chaine} : {v}")
if __name__ == '__main__': if __name__ == '__main__':
runMain() runMain()