blob: f080377be437b62c530f647e8cd52dcd09599d62 [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
Tomek Jaroszykc48b81a2022-03-09 12:32:50 +010029 # crush id
30 cmd = "ceph osd crush tree | grep `hostname -s` | awk '{print $1}'"
31 crush_id = check_output(cmd, shell=True).strip()
32 if crush_id:
33 grain['ceph']['crush_id'] = crush_id
34
Jiri Broulik72e8a632017-11-22 17:52:53 +010035 # osd
36 if os.path.exists('/var/lib/ceph/osd'):
Alena Kiseleva52dae772019-02-06 16:58:36 +030037 devices = {}
Tomek Jaroszyk93272502020-03-29 23:39:05 +020038 for osd in os.listdir('/var/lib/ceph/osd'):
Alena Kiseleva52dae772019-02-06 16:58:36 +030039
Tomek Jaroszyk93272502020-03-29 23:39:05 +020040 try:
41 with open('/var/lib/ceph/osd/%s/whoami' % osd) as file:
42 dev_id = file.readline().strip()
Tomek Jaroszyk5e99c0f2020-03-31 17:30:11 +020043 with open('/var/lib/ceph/osd/%s/fsid' % osd) as file:
44 fsid = file.readline().strip()
Tomek Jaroszyk93272502020-03-29 23:39:05 +020045 path = os.readlink('/var/lib/ceph/osd/%s/block' % osd)
46 except IOError:
47 continue
Alena Kiseleva52dae772019-02-06 16:58:36 +030048
Tomek Jaroszyk93272502020-03-29 23:39:05 +020049 cmd = "lsblk -no pkname %s" % path
50 dev = check_output(cmd, shell=True).strip()
51 if dev:
52 dev = '/dev/%s' % dev
53 else:
54 cmd = "pvs | grep %s | awk '{print $1}'" % path.split('/')[-2]
55 dev = check_output(cmd, shell=True).strip()
56 devices[dev_id] = {}
Tomek Jaroszyk5e99c0f2020-03-31 17:30:11 +020057 devices[dev_id]['fsid'] = fsid
Tomek Jaroszyk93272502020-03-29 23:39:05 +020058 devices[dev_id]['path'] = path
59 devices[dev_id]['dev'] = dev
60
61 cmd = "ceph osd tree --format json"
62 osd_tree_output = check_output(cmd, shell=True).decode("utf-8")
63 osd_tree_output = json.loads(osd_tree_output)
64 for osd in osd_tree_output['nodes']:
65 dev_id = str(osd['id'])
66 if dev_id >= 0 and devices.has_key(dev_id):
67 devices[dev_id]['weight'] = osd['crush_weight']
68 devices[dev_id]['class'] = osd['device_class']
69
70 if devices:
71 grain['ceph']['ceph_disk'] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020072
Jiri Broulik72e8a632017-11-22 17:52:53 +010073 # keyrings
74 directory = '/etc/ceph/'
75 keyrings = {}
76 if os.path.isdir(directory):
77 for filename in os.listdir(directory):
Jiri Broulik42552052018-02-15 15:23:29 +010078 if filename.endswith(".keyring") and re.search(".client.", filename):
Jiri Broulik72e8a632017-11-22 17:52:53 +010079 keyring_output = open(os.path.join(directory, filename), "r")
Jiri Broulik42552052018-02-15 15:23:29 +010080 keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
Jiri Broulik72e8a632017-11-22 17:52:53 +010081 if keyring_output:
82 keyrings[keyring_name] = {}
83 for line in keyring_output:
84 attr = shlex.split(line)
85 if attr:
86 if attr[0] == 'key':
87 keyrings[keyring_name]['key'] = attr[2]
88 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
89 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
90 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
91 keyrings[keyring_name]['caps'] = {}
92 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
93 if keyrings:
94 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +020095
Jiri Broulik72e8a632017-11-22 17:52:53 +010096 # mon keyring
97 hostname = check_output("hostname", shell=True).rstrip()
Jiri Broulik42552052018-02-15 15:23:29 +010098 filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
Jiri Broulik72e8a632017-11-22 17:52:53 +010099 if os.path.isfile(filepath):
100 mon_key_output = open(filepath, "r")
101 if mon_key_output:
102 keyrings['mon'] = {}
103 for line in mon_key_output:
104 attr = shlex.split(line)
105 if attr:
106 if attr[0] == 'key':
107 keyrings['mon']['key'] = attr[2]
108 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
109 keyrings['mon']['caps'][attr[1]] = attr[3]
110 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
111 keyrings['mon']['caps'] = {}
112 keyrings['mon']['caps'][attr[1]] = attr[3]
113 grain["ceph"]["ceph_keyring"] = keyrings
114
115 return grain
116 else:
117 return None
Tomek Jaroszyk93272502020-03-29 23:39:05 +0200118