mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-22 02:51:03 +00:00
stratis: code cleanup and use perlpod format for documentation
This commit is contained in:
parent
7d31b4ce25
commit
a45257a64a
1 changed files with 46 additions and 27 deletions
|
@ -1,39 +1,58 @@
|
||||||
#!/usr/bin/python3 -tt
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""Munin plugin to monitor stratis pools and filesystems.
|
"""Munin plugin to monitor stratis pools and filesystems.
|
||||||
|
|
||||||
Copyright 2020 Kim B. Heino, Foobar Oy
|
=head1 NAME
|
||||||
License GPLv2+
|
|
||||||
|
stratis - monitor stratis pools and filesystems
|
||||||
|
|
||||||
|
=head1 APPLICABLE SYSTEMS
|
||||||
|
|
||||||
|
Linux systems with stratis filesystems.
|
||||||
|
|
||||||
|
=head1 CONFIGURATION
|
||||||
|
|
||||||
|
No configuration is required for this plugin.
|
||||||
|
|
||||||
|
=head1 AUTHOR
|
||||||
|
|
||||||
|
Kim B. Heino <b@bbbs.net>
|
||||||
|
|
||||||
|
=head1 LICENSE
|
||||||
|
|
||||||
|
GPLv2
|
||||||
|
|
||||||
|
=head1 MAGIC MARKERS
|
||||||
|
|
||||||
|
#%# family=auto
|
||||||
|
#%# capabilities=autoconf
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
#%# capabilities=autoconf
|
|
||||||
#%# family=auto
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
|
|
||||||
def safename(variable):
|
def safename(name):
|
||||||
"""Return safe variable name."""
|
"""Return safe variable name."""
|
||||||
ret = []
|
# Convert ä->a as isalpha('ä') is true
|
||||||
for letter in variable:
|
value = unicodedata.normalize('NFKD', name)
|
||||||
if letter.isalnum():
|
value = value.encode('ASCII', 'ignore').decode('utf-8')
|
||||||
ret.append(letter)
|
|
||||||
else:
|
# Remove non-alphanumeric chars
|
||||||
ret.append('_')
|
return ''.join(char.lower() if char.isalnum() else '_' for char in value)
|
||||||
return ''.join(ret)
|
|
||||||
|
|
||||||
|
|
||||||
def run_binary(arg):
|
def run_binary(arg):
|
||||||
"""Run binary and return output."""
|
"""Run binary and return output."""
|
||||||
try:
|
try:
|
||||||
cmd = subprocess.Popen(
|
return subprocess.run(arg, stdout=subprocess.PIPE, check=False,
|
||||||
arg, shell=False, close_fds=True, bufsize=-1,
|
encoding='utf-8', errors='ignore').stdout
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
except FileNotFoundError:
|
||||||
return cmd.communicate()[0].decode('utf-8', 'ignore')
|
|
||||||
except OSError:
|
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,7 +73,7 @@ def parse_unit(number, unit):
|
||||||
def find_pools():
|
def find_pools():
|
||||||
"""Return list of found pools and filesystems."""
|
"""Return list of found pools and filesystems."""
|
||||||
pool = []
|
pool = []
|
||||||
for line in run_binary(['/usr/bin/stratis', 'pool']).splitlines():
|
for line in run_binary(['stratis', 'pool']).splitlines():
|
||||||
if line.startswith('Name '):
|
if line.startswith('Name '):
|
||||||
continue
|
continue
|
||||||
line = line.split()
|
line = line.split()
|
||||||
|
@ -64,17 +83,17 @@ def find_pools():
|
||||||
pool.append((line[0], total, used, free))
|
pool.append((line[0], total, used, free))
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
dflist = run_binary(['/usr/bin/df']).splitlines()
|
dflist = run_binary(['df']).splitlines()
|
||||||
for line in run_binary(['/usr/bin/stratis', 'filesystem']).splitlines():
|
for line in run_binary(['stratis', 'filesystem']).splitlines():
|
||||||
if line.startswith('Pool Name ') or '-snap-' in line:
|
if line.startswith('Pool Name ') or '-snap-' in line:
|
||||||
continue
|
continue
|
||||||
line = line.split()
|
tokens = line.split()
|
||||||
df_used = used = parse_unit(line[2], line[3])
|
df_used = used = parse_unit(tokens[2], tokens[3])
|
||||||
for dfline in dflist:
|
for dfline in dflist:
|
||||||
if line[9] not in dfline: # match by uuid
|
if tokens[9] not in dfline: # match by uuid
|
||||||
continue
|
continue
|
||||||
df_used = int(dfline.split()[2]) * 1024
|
df_used = int(dfline.split()[2]) * 1024
|
||||||
files.append((line[0], line[1], used, df_used))
|
files.append((tokens[0], tokens[1], used, df_used))
|
||||||
return sorted(pool), sorted(files)
|
return sorted(pool), sorted(files)
|
||||||
|
|
||||||
|
|
||||||
|
@ -145,7 +164,7 @@ def fetch(pools, files):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) > 1 and sys.argv[1] == 'autoconf':
|
if len(sys.argv) > 1 and sys.argv[1] == 'autoconf':
|
||||||
print('yes' if find_pools()[0] else 'no')
|
print('yes' if find_pools()[0] else 'no (no stratis pools found)')
|
||||||
elif len(sys.argv) > 1 and sys.argv[1] == 'config':
|
elif len(sys.argv) > 1 and sys.argv[1] == 'config':
|
||||||
config(*find_pools())
|
config(*find_pools())
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue