blob: e9f38a6243c89a3d88a8c66694cc80d43c3fc025 [file] [log] [blame]
Jiri Broulikc2be93b2017-10-03 14:20:00 +02001#!/usr/bin/env python
2
Jiri Broulikc2be93b2017-10-03 14:20:00 +02003def main():
4
5 from subprocess import check_output
Machi Hoshinobf4a6382018-04-05 10:04:55 +09006 from subprocess import CalledProcessError
Jiri Broulikc2be93b2017-10-03 14:20:00 +02007 import shlex
Alena Kiseleva52dae772019-02-06 16:58:36 +03008 import json
Jiri Broulikc2be93b2017-10-03 14:20:00 +02009 import os
10 import re
11
Jiri Broulik72e8a632017-11-22 17:52:53 +010012 if os.path.exists('/etc/ceph'):
13 grain = {}
14 grain["ceph"] = {}
Jiri Broulik42552052018-02-15 15:23:29 +010015 conf_dir = '/etc/ceph/'
16 for filename in os.listdir(conf_dir):
17 if filename.endswith(".conf"):
18 cluster_name = re.search('(.+?).conf', filename).group(1)
19 break
20 conf_file = conf_dir + cluster_name + '.conf'
Jiri Broulikc2be93b2017-10-03 14:20:00 +020021
Michel Nederlof1baf1de2018-06-12 09:40:00 +020022 # get the fsid from config file, for salt-formulas to filter on in case of multiple ceph clusters
23 with open(conf_file, 'r') as conf_fh:
24 for line in conf_fh.read().splitlines():
25 if 'fsid' in line:
26 attr = shlex.split(line)
27 grain['ceph']['fsid'] = attr[2]
28
Jiri Broulik72e8a632017-11-22 17:52:53 +010029 # osd
30 if os.path.exists('/var/lib/ceph/osd'):
Alena Kiseleva52dae772019-02-06 16:58:36 +030031 devices = {}
Tomek Jaroszyk93272502020-03-29 23:39:05 +020032 for osd in os.listdir('/var/lib/ceph/osd'):
Alena Kiseleva52dae772019-02-06 16:58:36 +030033
Tomek Jaroszyk93272502020-03-29 23:39:05 +020034 try:
35 with open('/var/lib/ceph/osd/%s/whoami' % osd) as file:
36 dev_id = file.readline().strip()
Tomek Jaroszyk5e99c0f2020-03-31 17:30:11 +020037 with open('/var/lib/ceph/osd/%s/fsid' % osd) as file:
38 fsid = file.readline().strip()
Tomek Jaroszyk93272502020-03-29 23:39:05 +020039 path = os.readlink('/var/lib/ceph/osd/%s/block' % osd)
40 except IOError:
41 continue
Alena Kiseleva52dae772019-02-06 16:58:36 +030042
Tomek Jaroszyk93272502020-03-29 23:39:05 +020043 cmd = "lsblk -no pkname %s" % path
44 dev = check_output(cmd, shell=True).strip()
45 if dev:
46 dev = '/dev/%s' % dev
47 else:
48 cmd = "pvs | grep %s | awk '{print $1}'" % path.split('/')[-2]
49 dev = check_output(cmd, shell=True).strip()
50 devices[dev_id] = {}
Tomek Jaroszyk5e99c0f2020-03-31 17:30:11 +020051 devices[dev_id]['fsid'] = fsid
Tomek Jaroszyk93272502020-03-29 23:39:05 +020052 devices[dev_id]['path'] = path
53 devices[dev_id]['dev'] = dev
54
55 cmd = "ceph osd tree --format json"
56 osd_tree_output = check_output(cmd, shell=True).decode("utf-8")
57 osd_tree_output = json.loads(osd_tree_output)
58 for osd in osd_tree_output['nodes']:
59 dev_id = str(osd['id'])
60 if dev_id >= 0 and devices.has_key(dev_id):
61 devices[dev_id]['weight'] = osd['crush_weight']
62 devices[dev_id]['class'] = osd['device_class']
63
64 if devices:
65 grain['ceph']['ceph_disk'] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020066
Jiri Broulik72e8a632017-11-22 17:52:53 +010067 # keyrings
68 directory = '/etc/ceph/'
69 keyrings = {}
70 if os.path.isdir(directory):
71 for filename in os.listdir(directory):
Jiri Broulik42552052018-02-15 15:23:29 +010072 if filename.endswith(".keyring") and re.search(".client.", filename):
Jiri Broulik72e8a632017-11-22 17:52:53 +010073 keyring_output = open(os.path.join(directory, filename), "r")
Jiri Broulik42552052018-02-15 15:23:29 +010074 keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
Jiri Broulik72e8a632017-11-22 17:52:53 +010075 if keyring_output:
76 keyrings[keyring_name] = {}
77 for line in keyring_output:
78 attr = shlex.split(line)
79 if attr:
80 if attr[0] == 'key':
81 keyrings[keyring_name]['key'] = attr[2]
82 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
83 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
84 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
85 keyrings[keyring_name]['caps'] = {}
86 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
87 if keyrings:
88 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +020089
Jiri Broulik72e8a632017-11-22 17:52:53 +010090 # mon keyring
91 hostname = check_output("hostname", shell=True).rstrip()
Jiri Broulik42552052018-02-15 15:23:29 +010092 filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
Jiri Broulik72e8a632017-11-22 17:52:53 +010093 if os.path.isfile(filepath):
94 mon_key_output = open(filepath, "r")
95 if mon_key_output:
96 keyrings['mon'] = {}
97 for line in mon_key_output:
98 attr = shlex.split(line)
99 if attr:
100 if attr[0] == 'key':
101 keyrings['mon']['key'] = attr[2]
102 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
103 keyrings['mon']['caps'][attr[1]] = attr[3]
104 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
105 keyrings['mon']['caps'] = {}
106 keyrings['mon']['caps'][attr[1]] = attr[3]
107 grain["ceph"]["ceph_keyring"] = keyrings
108
109 return grain
110 else:
111 return None
Tomek Jaroszyk93272502020-03-29 23:39:05 +0200112