2020-08-29 19:10:59 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from re import match
|
|
|
|
from subprocess import check_output
|
|
|
|
from sys import exit
|
|
|
|
|
|
|
|
output = check_output(['zfs', 'get', 'creation', '-Hpr', '-t', 'snapshot'])
|
|
|
|
|
|
|
|
now = int(datetime.now().timestamp())
|
2020-12-18 07:23:42 +00:00
|
|
|
warn_age = now - (60 * 60 * 24 * 90)
|
2020-08-29 19:10:59 +00:00
|
|
|
|
|
|
|
warn_snapshots = set()
|
|
|
|
|
|
|
|
return_code = 0
|
|
|
|
|
|
|
|
for line in output.decode('utf-8').split("\n"):
|
|
|
|
if line.strip() == '':
|
|
|
|
continue
|
|
|
|
|
|
|
|
items = line.split("\t")
|
|
|
|
|
|
|
|
# If the snapshot name contains 'zfs-auto-snap', it's probably
|
|
|
|
# really an automated snapshot and will be cleaned up eventually.
|
|
|
|
# This check only cares about manually created snapshots, though.
|
|
|
|
if 'zfs-auto-snap' in items[0]:
|
|
|
|
continue
|
|
|
|
|
2020-12-18 07:23:42 +00:00
|
|
|
# SNAPSHOT_FOR_RESET is used by bundle automatic-reset-of-zfs-dataset
|
|
|
|
# to reset datasets to a known good state (used for test systems).
|
|
|
|
if 'SNAPSHOT_FOR_RESET' in items[0]:
|
|
|
|
continue
|
|
|
|
|
2020-08-29 19:10:59 +00:00
|
|
|
# These are docker-internal snapshots and should not be touched by
|
|
|
|
# us.
|
|
|
|
if match(r'^tank/docker/[a-z0-9]+(-init)?@[0-9]+', items[0]):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# line should be '<snapshot> creation <timestamp> -', separated by
|
|
|
|
# tabstops.
|
|
|
|
if len(items) < 3:
|
|
|
|
print('UNKNOWN - error while parsing ' + line)
|
|
|
|
exit(3)
|
|
|
|
|
|
|
|
creation_date = int(items[2])
|
|
|
|
|
2020-12-18 07:23:42 +00:00
|
|
|
if creation_date < warn_age:
|
2020-08-29 19:10:59 +00:00
|
|
|
warn_snapshots.add(items[0])
|
|
|
|
|
|
|
|
for snap in sorted(warn_snapshots):
|
2020-12-18 07:23:42 +00:00
|
|
|
print('WARN - {} is older than 90 days'.format(snap))
|
2020-08-29 19:10:59 +00:00
|
|
|
|
2020-12-18 07:23:42 +00:00
|
|
|
if len(warn_snapshots) > 0:
|
2020-08-29 19:10:59 +00:00
|
|
|
return_code = 1
|
|
|
|
else:
|
2020-12-18 07:23:42 +00:00
|
|
|
print('OK - no snapshots are older than 90 days')
|
2020-08-29 19:10:59 +00:00
|
|
|
|
|
|
|
exit(return_code)
|