visio.chapril.org-tools/monitoring/check_borgmatic

32 lines
1.1 KiB
Python

#!/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)