bundlewrap/bundles/zfs/files/check_zpool_space
Franziska Kunsmann f8bbe00d47
All checks were successful
bundlewrap/pipeline/head This commit looks good
overall better handling and usage of exceptions
2021-04-02 18:57:13 +02:00

41 lines
1 KiB
Python

#!/usr/bin/env python3
from subprocess import check_output
from sys import argv, exit
import re
def to_bytes(size):
suffixes = ['', 'K', 'M', 'G', 'T', 'P']
number, suffix = re.match(r'([0-9\.]+)([A-Z]?)', size).groups()
if suffix not in suffixes:
print('Unexpected suffix "{}" in size "{}"'.format(suffix, size)
return float(number) * 1024**suffixes.index(suffix)
pool = argv[1]
critical_perc = float(argv[2])
try:
output = check_output(['zpool', 'list', '-Ho', 'size,alloc', pool])
except:
print('CRITICAL - "zpool" failed')
exit(2)
size, alloc = output.decode('UTF-8').strip().split()
try:
size_b = to_bytes(size)
alloc_b = to_bytes(alloc)
except:
print('CRITICAL - Could not process output of "zpool list": {}'.format(output))
exit(2)
percentage = alloc_b / size_b * 100
if percentage > critical_perc:
print('CRITICAL - Pool "{}" uses {:.2f}% of its space'.format(pool, percentage))
exit(2)
print('OK - Pool "{}" uses {:.2f}% of its space'.format(pool, percentage))
exit(0)