2020-08-29 19:10:59 +00:00
|
|
|
#!/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()
|
2021-04-02 16:57:13 +00:00
|
|
|
if suffix not in suffixes:
|
|
|
|
print('Unexpected suffix "{}" in size "{}"'.format(suffix, size)
|
2020-08-29 19:10:59 +00:00
|
|
|
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)
|