blob: 981ec18bf1653c7142ceab8493001843f5e5afba [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()
37 path = os.readlink('/var/lib/ceph/osd/%s/block' % osd)
38 except IOError:
39 continue
Alena Kiseleva52dae772019-02-06 16:58:36 +030040
Tomek Jaroszyk93272502020-03-29 23:39:05 +020041 cmd = "lsblk -no pkname %s" % path
42 dev = check_output(cmd, shell=True).strip()
43 if dev:
44 dev = '/dev/%s' % dev
45 else:
46 cmd = "pvs | grep %s | awk '{print $1}'" % path.split('/')[-2]
47 dev = check_output(cmd, shell=True).strip()
48 devices[dev_id] = {}
49 devices[dev_id]['path'] = path
50 devices[dev_id]['dev'] = dev
51
52 cmd = "ceph osd tree --format json"
53 osd_tree_output = check_output(cmd, shell=True).decode("utf-8")
54 osd_tree_output = json.loads(osd_tree_output)
55 for osd in osd_tree_output['nodes']:
56 dev_id = str(osd['id'])
57 if dev_id >= 0 and devices.has_key(dev_id):
58 devices[dev_id]['weight'] = osd['crush_weight']
59 devices[dev_id]['class'] = osd['device_class']
60
61 if devices:
62 grain['ceph']['ceph_disk'] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020063
Jiri Broulik72e8a632017-11-22 17:52:53 +010064 # keyrings
65 directory = '/etc/ceph/'
66 keyrings = {}
67 if os.path.isdir(directory):
68 for filename in os.listdir(directory):
Jiri Broulik42552052018-02-15 15:23:29 +010069 if filename.endswith(".keyring") and re.search(".client.", filename):
Jiri Broulik72e8a632017-11-22 17:52:53 +010070 keyring_output = open(os.path.join(directory, filename), "r")
Jiri Broulik42552052018-02-15 15:23:29 +010071 keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
Jiri Broulik72e8a632017-11-22 17:52:53 +010072 if keyring_output:
73 keyrings[keyring_name] = {}
74 for line in keyring_output:
75 attr = shlex.split(line)
76 if attr:
77 if attr[0] == 'key':
78 keyrings[keyring_name]['key'] = attr[2]
79 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
80 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
81 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
82 keyrings[keyring_name]['caps'] = {}
83 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
84 if keyrings:
85 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +020086
Jiri Broulik72e8a632017-11-22 17:52:53 +010087 # mon keyring
88 hostname = check_output("hostname", shell=True).rstrip()
Jiri Broulik42552052018-02-15 15:23:29 +010089 filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
Jiri Broulik72e8a632017-11-22 17:52:53 +010090 if os.path.isfile(filepath):
91 mon_key_output = open(filepath, "r")
92 if mon_key_output:
93 keyrings['mon'] = {}
94 for line in mon_key_output:
95 attr = shlex.split(line)
96 if attr:
97 if attr[0] == 'key':
98 keyrings['mon']['key'] = attr[2]
99 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
100 keyrings['mon']['caps'][attr[1]] = attr[3]
101 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
102 keyrings['mon']['caps'] = {}
103 keyrings['mon']['caps'][attr[1]] = attr[3]
104 grain["ceph"]["ceph_keyring"] = keyrings
105
106 return grain
107 else:
108 return None
Tomek Jaroszyk93272502020-03-29 23:39:05 +0200109