This repository was archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlvm.py
executable file
·111 lines (89 loc) · 2.93 KB
/
lvm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""Description: Allows Zabbix to monitor lvm values
Author: Olivier van der Toorn <[email protected]>
"""
import sys
import subprocess
import json
import re
import pdb
def filter_digits(string):
"""Grabs the digits from the input string.
:param string: input string
:type string: str
:return: digits from the input string
"""
value = re.sub(r'[^0-9a-z,.]', '', string)
if value == '':
value = 0
return value
def run_command(lvm, option):
"""Runs an lvm command with the specified options.
"""
option = option
command = [
'sudo', lvm, '--unbuffered', '--noheadings', '--nosuffix',
'--units', 'b', '--reportformat', 'json', '-o', option]
process = subprocess.run(
command,
stdout=subprocess.PIPE)
# output = filter_digits(str(process.stdout, 'utf-8'))
output = str(process.stdout, 'utf-8')
return output
def physical_volume():
"""Gathers the information for all physical volumes and
spits it out as a json. Can be used as the source for dependent
items.
"""
options = ("pv_name,pv_free,pv_size")
output = run_command('pvs', options)
json_data = json.loads(output)
data = {}
for row in json_data['report'][0]['pv']:
name = row['pv_name']
del row['pv_name']
data[name] = {k: v or 0 for k, v in row.items()}
return json.dumps(data, sort_keys=True, indent=4)
def volume_group():
"""Gathers the information for all volume groups and spits it
out as a json.
"""
options = ("vg_name,lv_count,pv_count,vg_free,vg_size")
output = run_command('vgs', options)
json_data = json.loads(output)
data = {}
for row in json_data['report'][0]['vg']:
name = row['vg_name']
del row['vg_name']
data[name] = {k: v or 0 for k, v in row.items()}
return json.dumps(data, sort_keys=True, indent=4)
def logical_volume():
"""Gathers the information for all logical volumes and spits it out
as a json.
"""
options = ("lv_name,cache_dirty_blocks,cache_read_hits,"
"cache_read_misses,cache_total_blocks,cache_write_hits,"
"cache_write_misses,copy_percent,"
"data_percent,lv_layout,lv_size")
output = run_command('lvs', options)
json_data = json.loads(output)
data = {}
for row in json_data['report'][0]['lv']:
name = row['lv_name']
del row['lv_name']
data[name] = {k: v or 0 for k, v in row.items()}
return json.dumps(data, sort_keys=True, indent=4)
def main(*switch):
"""Switches between the requested type.
"""
output = ''
if len(switch) == 1:
if switch[0] == 'lv':
output = logical_volume()
elif switch[0] == 'vg':
output = volume_group()
elif switch[0] == 'pv':
output = physical_volume()
return output
if __name__ == '__main__':
print(main(*sys.argv[1:]))