blob: 61254bff6d118b986ce2c1a55b1c882111c4acee [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
7 import shlex
8 import os
9 import re
10
Jiri Broulik72e8a632017-11-22 17:52:53 +010011 if os.path.exists('/etc/ceph'):
12 grain = {}
13 grain["ceph"] = {}
Jiri Broulik42552052018-02-15 15:23:29 +010014 conf_dir = '/etc/ceph/'
15 for filename in os.listdir(conf_dir):
16 if filename.endswith(".conf"):
17 cluster_name = re.search('(.+?).conf', filename).group(1)
18 break
19 conf_file = conf_dir + cluster_name + '.conf'
Jiri Broulikc2be93b2017-10-03 14:20:00 +020020
Jiri Broulik72e8a632017-11-22 17:52:53 +010021 # osd
22 if os.path.exists('/var/lib/ceph/osd'):
Jiri Broulik42552052018-02-15 15:23:29 +010023 mount_path = check_output("df -h | awk '{print $6}' | grep ceph | grep -v lockbox | sed 's/-[0-9]*//g' | awk 'NR==1{print $1}'", shell=True).rstrip()
24 sed = 'sed \'s#{0}-##g\''.format(mount_path)
Jiri Broulik72e8a632017-11-22 17:52:53 +010025 cmd = "lsblk -rp | awk '{print $1,$6,$7}' | grep -v lockbox | grep ceph | " + sed
26 osd_output = check_output(cmd, shell=True)
27 if osd_output:
28 devices = {}
29 for line in osd_output.splitlines():
30 device = line.split()
31 encrypted = False
32 if "crypt" in device[1]:
33 output = check_output("lsblk -rp | grep -B1 " + device[0], shell=True)
34 for l in output.splitlines():
35 d = l.split()
Jiri Broulik42552052018-02-15 15:23:29 +010036 dev = re.sub("\d+", "", device[0])
Jiri Broulik72e8a632017-11-22 17:52:53 +010037 encrypted = True
38 break
39 else:
40 dev = device[0].replace('1','')
Jiri Broulik42552052018-02-15 15:23:29 +010041 dev = re.sub("\d+", "", device[0])
Jiri Broulik72e8a632017-11-22 17:52:53 +010042 device[0] = device[2]
43 devices[device[0]] = {}
44 devices[device[0]]['dev'] = dev
45 if encrypted:
46 devices[device[0]]['dmcrypt'] = 'true'
Jiri Broulik42552052018-02-15 15:23:29 +010047 tline = check_output("ceph -c " + conf_file + " osd tree | awk '{print $1,$2,$3,$4}' | grep -w 'osd." + device[0] + "'", shell=True)
Jiri Broulik72e8a632017-11-22 17:52:53 +010048 osd = tline.split()
49 if "osd" not in osd[2]:
50 crush_class = osd[1]
51 crush_weight = osd[2]
52 devices[device[0]]['class'] = crush_class
53 devices[device[0]]['weight'] = crush_weight
54 else:
55 crush_weight = osd[1]
56 devices[device[0]]['weight'] = crush_weight
57 grain["ceph"]["ceph_disk"] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020058
Jiri Broulik72e8a632017-11-22 17:52:53 +010059 # keyrings
60 directory = '/etc/ceph/'
61 keyrings = {}
62 if os.path.isdir(directory):
63 for filename in os.listdir(directory):
Jiri Broulik42552052018-02-15 15:23:29 +010064 if filename.endswith(".keyring") and re.search(".client.", filename):
Jiri Broulik72e8a632017-11-22 17:52:53 +010065 keyring_output = open(os.path.join(directory, filename), "r")
Jiri Broulik42552052018-02-15 15:23:29 +010066 keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
Jiri Broulik72e8a632017-11-22 17:52:53 +010067 if keyring_output:
68 keyrings[keyring_name] = {}
69 for line in keyring_output:
70 attr = shlex.split(line)
71 if attr:
72 if attr[0] == 'key':
73 keyrings[keyring_name]['key'] = attr[2]
74 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
75 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
76 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
77 keyrings[keyring_name]['caps'] = {}
78 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
79 if keyrings:
80 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +020081
Jiri Broulik72e8a632017-11-22 17:52:53 +010082 # mon keyring
83 hostname = check_output("hostname", shell=True).rstrip()
Jiri Broulik42552052018-02-15 15:23:29 +010084 filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
Jiri Broulik72e8a632017-11-22 17:52:53 +010085 if os.path.isfile(filepath):
86 mon_key_output = open(filepath, "r")
87 if mon_key_output:
88 keyrings['mon'] = {}
89 for line in mon_key_output:
90 attr = shlex.split(line)
91 if attr:
92 if attr[0] == 'key':
93 keyrings['mon']['key'] = attr[2]
94 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
95 keyrings['mon']['caps'][attr[1]] = attr[3]
96 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
97 keyrings['mon']['caps'] = {}
98 keyrings['mon']['caps'][attr[1]] = attr[3]
99 grain["ceph"]["ceph_keyring"] = keyrings
100
101 return grain
102 else:
103 return None