-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_rbd_map.py
58 lines (49 loc) · 1.4 KB
/
check_rbd_map.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
#!/usr/bin/python
import os
import re
import sys
PATH_CONFIG_FILE = '/etc/ceph/rbdmap'
PATH_RBD_IMAGES = '/dev/rbd'
EXIT_OK = 0
EXIT_WARN = 1
EXIT_CRITICAL = 2
def create_pools(path):
pools = []
with open(path, "r") as file:
for line in file:
if "#" in line:
continue
else:
if re.search(r'\w', line):
image = re.split(r' ', line, maxsplit=1)
pools.append(image[0])
return pools
def check_files(pools):
is_ok = True
not_mapped_images = 'Not mapped: '
pointer_not_mapped_images = 0
for image in pools:
full_path_to_image = PATH_RBD_IMAGES + '/' + image
if not os.path.exists(full_path_to_image):
is_ok = False
pointer_not_mapped_images += 1
not_mapped_images += (str(pointer_not_mapped_images) + ') ' + image + ' ')
if is_ok:
print('OK. All images are mapped.')
sys.exit(EXIT_OK)
else:
print(not_mapped_images)
sys.exit(EXIT_CRITICAL)
def main():
try:
pools = create_pools(PATH_CONFIG_FILE)
except:
print("Warning! File cannot be opened or it doesn't exist")
sys.exit(EXIT_WARN)
if len(pools) == 0:
print('Warning! Not images in %s' % PATH_CONFIG_FILE)
sys.exit(EXIT_WARN)
else:
check_files(pools)
if __name__ == '__main__':
main()