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 pathdiscover_lvm.py
executable file
·139 lines (115 loc) · 3.53 KB
/
discover_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
""" Description: Discovers lvm related things.
Author: Olivier van der Toorn <[email protected]>"""
import json
import re
import subprocess
import sys
import pdb
def check_lvm():
"""Checks if the necessary tools are available.
"""
command = ['sudo', 'lvm', 'version']
try:
subprocess.check_call(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
passed = True
except subprocess.CalledProcessError:
passed = False
return passed
def physical_volumes():
"""Returns the discovered physical volumes.
"""
volumes = []
if check_lvm() is True:
command = [
'sudo', 'pvs', '--unbuffered', '--noheading',
'--nosuffix', '--reportformat', 'json',
'-o', 'pv_name'
]
process = subprocess.run(
command,
stdout=subprocess.PIPE)
output = str(process.stdout, 'utf-8')
if output:
parsed = json.loads(output)['report'][0]['pv']
for physical_volume in parsed:
volume = physical_volume['pv_name']
shortname = volume.split('/')[-1]
volumes.append(
{
'{#SHORTNAME}': shortname,
'{#VOLUME}': volume,
}
)
return volumes
def volume_groups():
"""Returns the discovered volume groups.
"""
volumes = []
if check_lvm() is True:
command = [
'sudo', 'vgs', '--unbuffered', '--noheading',
'--nosuffix', '--reportformat', 'json',
'-o', 'vg_name'
]
process = subprocess.run(
command,
stdout=subprocess.PIPE)
output = str(process.stdout, 'utf-8')
if output:
parsed = json.loads(output)['report'][0]['vg']
for volume_group in parsed:
volumes.append(
{
'{#SHORTNAME}': volume_group['vg_name']
}
)
return volumes
def logical_volumes():
"""Uses `lvs` to discover the logical volumes and their paths.
"""
volumes = []
if check_lvm() is True:
command = [
'sudo', 'lvs', '--unbuffered', '--noheading',
'--nosuffix', '--reportformat', 'json',
'-o', 'lv_name,lv_path'
]
process = subprocess.run(
command,
stdout=subprocess.PIPE)
output = str(process.stdout, 'utf-8')
if output:
parsed = json.loads(output)['report'][0]['lv']
for logical_volume in parsed:
name = logical_volume['lv_name']
if logical_volume['lv_path'] == '':
path = name
else:
path = logical_volume['lv_path']
volumes.append(
{
'{#SHORTNAME}': name,
'{#VOLUME}': path,
}
)
return volumes
def main(switch=None):
"""Reads /etc/zabbix/urls.txt for URLs to monitor.
"""
if switch == 'pv':
volumes = physical_volumes()
elif switch == 'vg':
volumes = volume_groups()
elif switch == 'lv':
volumes = logical_volumes()
else:
volumes = []
data = {'data': volumes}
return json.dumps(data, sort_keys=True, indent=4)
if __name__ == '__main__':
print(main(*sys.argv[1:])) # pragma: no cover