blob: 54bc97ee5c48c1a9c7aae7234b3964c30a1c4bb2 [file] [log] [blame]
Jiri Broulikc2be93b2017-10-03 14:20:00 +02001#!/usr/bin/env python
2
3
4def main():
5
6 from subprocess import check_output
Machi Hoshinobf4a6382018-04-05 10:04:55 +09007 from subprocess import CalledProcessError
Jiri Broulikc2be93b2017-10-03 14:20:00 +02008 import shlex
Alena Kiseleva52dae772019-02-06 16:58:36 +03009 import json
Jiri Broulikc2be93b2017-10-03 14:20:00 +020010 import os
11 import re
12
Jiri Broulik72e8a632017-11-22 17:52:53 +010013 if os.path.exists('/etc/ceph'):
14 grain = {}
15 grain["ceph"] = {}
Jiri Broulik42552052018-02-15 15:23:29 +010016 conf_dir = '/etc/ceph/'
17 for filename in os.listdir(conf_dir):
18 if filename.endswith(".conf"):
19 cluster_name = re.search('(.+?).conf', filename).group(1)
20 break
21 conf_file = conf_dir + cluster_name + '.conf'
Jiri Broulikc2be93b2017-10-03 14:20:00 +020022
Michel Nederlof1baf1de2018-06-12 09:40:00 +020023 # get the fsid from config file, for salt-formulas to filter on in case of multiple ceph clusters
24 with open(conf_file, 'r') as conf_fh:
25 for line in conf_fh.read().splitlines():
26 if 'fsid' in line:
27 attr = shlex.split(line)
28 grain['ceph']['fsid'] = attr[2]
29
Jiri Broulik72e8a632017-11-22 17:52:53 +010030 # osd
31 if os.path.exists('/var/lib/ceph/osd'):
Alena Kiseleva52dae772019-02-06 16:58:36 +030032 cmd = "ceph-volume lvm list --format json"
Jiri Broulik72e8a632017-11-22 17:52:53 +010033 osd_output = check_output(cmd, shell=True)
Alena Kiseleva52dae772019-02-06 16:58:36 +030034 osd_output = json.loads(osd_output)
35 dev_id = ''
36 devices = {}
Jiri Broulik72e8a632017-11-22 17:52:53 +010037 if osd_output:
Alena Kiseleva52dae772019-02-06 16:58:36 +030038 for osd, params in osd_output.iteritems():
39 dev_id = osd
40 devices[dev_id] = {}
41 devices[dev_id]['dev'] = params[0]['devices'][0]
42 devices[dev_id]['path'] = params[0]['path']
43 devices[dev_id]['uuid'] = params[0]['tags']['ceph.osd_fsid']
44
45 cmd = "ceph osd tree --format json"
46 osd_tree_output = check_output(cmd, shell=True)
47 osd_tree_output = json.loads(osd_tree_output)
48 for osd in osd_tree_output['nodes']:
49 if 'type_id' in osd.keys():
50 if str(osd['type_id']) == '0':
51 for dev_id in devices.keys():
52 if str(osd['id']) == str(dev_id):
53 devices[dev_id]['weight'] = osd['crush_weight']
54 devices[dev_id]['class'] = osd['device_class']
55 grain["ceph"]["ceph_volume"] = devices
56 else:
57 cmd = "ceph-disk list --format json"
58 osd_output = check_output(cmd, shell=True).decode("utf-8")
59 osd_output = json.loads(osd_output)
60 dev_id = ''
Jiri Broulik72e8a632017-11-22 17:52:53 +010061 devices = {}
Alena Kiseleva52dae772019-02-06 16:58:36 +030062 if osd_output:
63 for line in osd_output:
64 if "is_partition" not in line.keys():
65 dev = line["path"]
66 parts = line["partitions"]
67 for p in parts:
68 if "mount" in p.keys() and "ceph" in p["mount"]:
69 dev_id = p["whoami"]
70 devices[dev_id] = {}
71 devices[dev_id]['dev'] = dev
72 if len(p["dmcrypt"]) > 0:
73 devices[dev_id]['dmcrypt'] = 'true'
74
75 cmd = "ceph osd tree --format json"
76 osd_tree_output = check_output(cmd, shell=True).decode("utf-8")
77 osd_tree_output = json.loads(osd_tree_output)
78 for osd in osd_tree_output['nodes']:
79 if 'type_id' in osd.keys():
80 if str(osd['type_id']) == '0':
81 for dev_id in devices.keys():
82 if str(osd['id']) == str(dev_id):
83 devices[dev_id]['weight'] = osd['crush_weight']
84 devices[dev_id]['class'] = osd['device_class']
85 grain["ceph"]["ceph_disk"] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020086
Jiri Broulik72e8a632017-11-22 17:52:53 +010087 # keyrings
88 directory = '/etc/ceph/'
89 keyrings = {}
90 if os.path.isdir(directory):
91 for filename in os.listdir(directory):
Jiri Broulik42552052018-02-15 15:23:29 +010092 if filename.endswith(".keyring") and re.search(".client.", filename):
Jiri Broulik72e8a632017-11-22 17:52:53 +010093 keyring_output = open(os.path.join(directory, filename), "r")
Jiri Broulik42552052018-02-15 15:23:29 +010094 keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
Jiri Broulik72e8a632017-11-22 17:52:53 +010095 if keyring_output:
96 keyrings[keyring_name] = {}
97 for line in keyring_output:
98 attr = shlex.split(line)
99 if attr:
100 if attr[0] == 'key':
101 keyrings[keyring_name]['key'] = attr[2]
102 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
103 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
104 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
105 keyrings[keyring_name]['caps'] = {}
106 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
107 if keyrings:
108 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +0200109
Jiri Broulik72e8a632017-11-22 17:52:53 +0100110 # mon keyring
111 hostname = check_output("hostname", shell=True).rstrip()
Jiri Broulik42552052018-02-15 15:23:29 +0100112 filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
Jiri Broulik72e8a632017-11-22 17:52:53 +0100113 if os.path.isfile(filepath):
114 mon_key_output = open(filepath, "r")
115 if mon_key_output:
116 keyrings['mon'] = {}
117 for line in mon_key_output:
118 attr = shlex.split(line)
119 if attr:
120 if attr[0] == 'key':
121 keyrings['mon']['key'] = attr[2]
122 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
123 keyrings['mon']['caps'][attr[1]] = attr[3]
124 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
125 keyrings['mon']['caps'] = {}
126 keyrings['mon']['caps'][attr[1]] = attr[3]
127 grain["ceph"]["ceph_keyring"] = keyrings
128
129 return grain
130 else:
131 return None