Skip to content

Commit a45257a

Browse files
kimheinosumpfralle
authored andcommitted
stratis: code cleanup and use perlpod format for documentation
1 parent 7d31b4c commit a45257a

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

plugins/disk/stratis

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,58 @@
1-
#!/usr/bin/python3 -tt
2-
# -*- coding: utf-8 -*-
1+
#!/usr/bin/env python3
32

43
"""Munin plugin to monitor stratis pools and filesystems.
54
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 <[email protected]>
20+
21+
=head1 LICENSE
22+
23+
GPLv2
24+
25+
=head1 MAGIC MARKERS
26+
27+
#%# family=auto
28+
#%# capabilities=autoconf
29+
30+
=cut
831
9-
#%# capabilities=autoconf
10-
#%# family=auto
1132
"""
1233

1334
import os
1435
import subprocess
1536
import sys
37+
import unicodedata
1638

1739

18-
def safename(variable):
40+
def safename(name):
1941
"""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)
2748

2849

2950
def run_binary(arg):
3051
"""Run binary and return output."""
3152
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:
3756
return ''
3857

3958

@@ -54,7 +73,7 @@ def parse_unit(number, unit):
5473
def find_pools():
5574
"""Return list of found pools and filesystems."""
5675
pool = []
57-
for line in run_binary(['/usr/bin/stratis', 'pool']).splitlines():
76+
for line in run_binary(['stratis', 'pool']).splitlines():
5877
if line.startswith('Name '):
5978
continue
6079
line = line.split()
@@ -64,17 +83,17 @@ def find_pools():
6483
pool.append((line[0], total, used, free))
6584

6685
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():
6988
if line.startswith('Pool Name ') or '-snap-' in line:
7089
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])
7392
for dfline in dflist:
74-
if line[9] not in dfline: # match by uuid
93+
if tokens[9] not in dfline: # match by uuid
7594
continue
7695
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))
7897
return sorted(pool), sorted(files)
7998

8099

@@ -145,7 +164,7 @@ def fetch(pools, files):
145164

146165
if __name__ == '__main__':
147166
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)')
149168
elif len(sys.argv) > 1 and sys.argv[1] == 'config':
150169
config(*find_pools())
151170
else:

0 commit comments

Comments
 (0)