Révision a45257a6
stratis: code cleanup and use perlpod format for documentation
| plugins/disk/stratis | ||
|---|---|---|
| 1 |
#!/usr/bin/python3 -tt |
|
| 2 |
# -*- coding: utf-8 -*- |
|
| 1 |
#!/usr/bin/env python3 |
|
| 3 | 2 |
|
| 4 | 3 |
"""Munin plugin to monitor stratis pools and filesystems. |
| 5 | 4 |
|
| 6 |
Copyright 2020 Kim B. Heino, Foobar Oy |
|
| 7 |
License GPLv2+ |
|
| 5 |
=head1 NAME |
|
| 6 |
|
|
| 7 |
stratis - monitor stratis pools and filesystems |
|
| 8 |
|
|
| 9 |
=head1 APPLICABLE SYSTEMS |
|
| 10 |
|
|
| 11 |
Linux systems with stratis filesystems. |
|
| 12 |
|
|
| 13 |
=head1 CONFIGURATION |
|
| 14 |
|
|
| 15 |
No configuration is required for this plugin. |
|
| 16 |
|
|
| 17 |
=head1 AUTHOR |
|
| 18 |
|
|
| 19 |
Kim B. Heino <b@bbbs.net> |
|
| 20 |
|
|
| 21 |
=head1 LICENSE |
|
| 22 |
|
|
| 23 |
GPLv2 |
|
| 24 |
|
|
| 25 |
=head1 MAGIC MARKERS |
|
| 26 |
|
|
| 27 |
#%# family=auto |
|
| 28 |
#%# capabilities=autoconf |
|
| 29 |
|
|
| 30 |
=cut |
|
| 8 | 31 |
|
| 9 |
#%# capabilities=autoconf |
|
| 10 |
#%# family=auto |
|
| 11 | 32 |
""" |
| 12 | 33 |
|
| 13 | 34 |
import os |
| 14 | 35 |
import subprocess |
| 15 | 36 |
import sys |
| 37 |
import unicodedata |
|
| 16 | 38 |
|
| 17 | 39 |
|
| 18 |
def safename(variable):
|
|
| 40 |
def safename(name):
|
|
| 19 | 41 |
"""Return safe variable name.""" |
| 20 |
ret = [] |
|
| 21 |
for letter in variable: |
|
| 22 |
if letter.isalnum(): |
|
| 23 |
ret.append(letter) |
|
| 24 |
else: |
|
| 25 |
ret.append('_')
|
|
| 26 |
return ''.join(ret) |
|
| 42 |
# Convert ä->a as isalpha('ä') is true
|
|
| 43 |
value = unicodedata.normalize('NFKD', name)
|
|
| 44 |
value = value.encode('ASCII', 'ignore').decode('utf-8')
|
|
| 45 |
|
|
| 46 |
# Remove non-alphanumeric chars |
|
| 47 |
return ''.join(char.lower() if char.isalnum() else '_' for char in value) |
|
| 27 | 48 |
|
| 28 | 49 |
|
| 29 | 50 |
def run_binary(arg): |
| 30 | 51 |
"""Run binary and return output.""" |
| 31 | 52 |
try: |
| 32 |
cmd = subprocess.Popen( |
|
| 33 |
arg, shell=False, close_fds=True, bufsize=-1, |
|
| 34 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
| 35 |
return cmd.communicate()[0].decode('utf-8', 'ignore')
|
|
| 36 |
except OSError: |
|
| 53 |
return subprocess.run(arg, stdout=subprocess.PIPE, check=False, |
|
| 54 |
encoding='utf-8', errors='ignore').stdout |
|
| 55 |
except FileNotFoundError: |
|
| 37 | 56 |
return '' |
| 38 | 57 |
|
| 39 | 58 |
|
| ... | ... | |
| 54 | 73 |
def find_pools(): |
| 55 | 74 |
"""Return list of found pools and filesystems.""" |
| 56 | 75 |
pool = [] |
| 57 |
for line in run_binary(['/usr/bin/stratis', 'pool']).splitlines():
|
|
| 76 |
for line in run_binary(['stratis', 'pool']).splitlines(): |
|
| 58 | 77 |
if line.startswith('Name '):
|
| 59 | 78 |
continue |
| 60 | 79 |
line = line.split() |
| ... | ... | |
| 64 | 83 |
pool.append((line[0], total, used, free)) |
| 65 | 84 |
|
| 66 | 85 |
files = [] |
| 67 |
dflist = run_binary(['/usr/bin/df']).splitlines()
|
|
| 68 |
for line in run_binary(['/usr/bin/stratis', 'filesystem']).splitlines():
|
|
| 86 |
dflist = run_binary(['df']).splitlines() |
|
| 87 |
for line in run_binary(['stratis', 'filesystem']).splitlines(): |
|
| 69 | 88 |
if line.startswith('Pool Name ') or '-snap-' in line:
|
| 70 | 89 |
continue |
| 71 |
line = line.split()
|
|
| 72 |
df_used = used = parse_unit(line[2], line[3])
|
|
| 90 |
tokens = line.split()
|
|
| 91 |
df_used = used = parse_unit(tokens[2], tokens[3])
|
|
| 73 | 92 |
for dfline in dflist: |
| 74 |
if line[9] not in dfline: # match by uuid
|
|
| 93 |
if tokens[9] not in dfline: # match by uuid
|
|
| 75 | 94 |
continue |
| 76 | 95 |
df_used = int(dfline.split()[2]) * 1024 |
| 77 |
files.append((line[0], line[1], used, df_used))
|
|
| 96 |
files.append((tokens[0], tokens[1], used, df_used))
|
|
| 78 | 97 |
return sorted(pool), sorted(files) |
| 79 | 98 |
|
| 80 | 99 |
|
| ... | ... | |
| 145 | 164 |
|
| 146 | 165 |
if __name__ == '__main__': |
| 147 | 166 |
if len(sys.argv) > 1 and sys.argv[1] == 'autoconf': |
| 148 |
print('yes' if find_pools()[0] else 'no')
|
|
| 167 |
print('yes' if find_pools()[0] else 'no (no stratis pools found)')
|
|
| 149 | 168 |
elif len(sys.argv) > 1 and sys.argv[1] == 'config': |
| 150 | 169 |
config(*find_pools()) |
| 151 | 170 |
else: |
Formats disponibles : Unified diff