-
Notifications
You must be signed in to change notification settings - Fork 0
/
rados.py
82 lines (71 loc) · 3.08 KB
/
rados.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
#!/usr/bin/env python3
#
# Replacement for librados which is not reachable for Checkmk plugin if Ceph runs in Kubernetes Cluster
#
# ©2022 henri.wahl@ukdd.de
from json import dumps, loads
from subprocess import run
# command to get into rook ceph tools container and run 'ceph'
CEPH_COMMAND = 'kubectl -n rook-ceph exec -it deploy/rook-ceph-tools -- ceph'
# hostname template for Ceph nodes and their appearance in Checkmk
CEPH_HOSTNAME_TEMPLATE = 'ceph-cluster-'
class Rados:
"""
Dummy class for Checkmk ceph plugin
"""
def __init__(self, **kwargs):
"""
Dummy init - no need for initialization
"""
def run_cli_command(self, command_json, timeout=5):
"""
Run command in Ceph CLI
"""
# every commmand comes in key 'prefix' - sometimes contains multiple keywords
command = loads(command_json)['prefix'].split(' ')
# some command comes extra keyword
if loads(command_json).get('detail'):
command += [loads(command_json).get('detail')]
command_as_list = CEPH_COMMAND.split(' ') +\
['-f', 'json', '--connect-timeout', str(timeout)] + command
result = run(command_as_list, capture_output=True)
return_code = result.returncode
# kick off newlines
result_json = result.stdout.strip()
# because 'quorum_names' key in JSON does not contain actual hostnames but 'a', 'b', 'c'
# this has to be fixed to make Checkmk happy
# comes from Ceph Rook and might have to be adjusted
if b'"quorum_names":' in result_json:
quorum_names_hostnames = []
result_dict = loads(result_json)
for quorum_name in result_dict['quorum_names']:
#
# Here some local adjustment and fantasy is required.
# Depending on the naming scheme in your environment you might need to adjust this to
# get real hostnames instead of 'a', 'b' and 'c' etc.
#
# derive host number from quorum name - 97 is the ascii number of 'a'
quorum_names_hostnames.append(f'{CEPH_HOSTNAME_TEMPLATE}{ord(quorum_name)-96}')
result_dict['quorum_names'] = quorum_names_hostnames
result_json = dumps(result_dict)
return return_code, result_json
def connect(self):
"""
Dummy connect method - just to be compatible with Checkmk ceph plugin
No connect needed because using kubectl + rook
"""
def mon_command(self, command, inbuf=b'', timeout=5):
"""
https://docs.ceph.com/en/latest/rados/api/python/#rados.Rados.mon_command
"""
return self.run_cli_command(command)
def mgr_command(self, command, inbuf=b'', timeout=5):
"""
https://docs.ceph.com/en/latest/rados/api/python/#rados.Rados.mgr_command
"""
return self.run_cli_command(command)
# Debugging
if __name__ == '__main__':
rados = Rados()
rados.connect()
print(rados.mon_command(dumps({"prefix": "status", "format": "json"})))