blob: ec568c965387aea4d3c50a3bb091f64e0ebfd670 [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
9 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
Jiri Broulik72e8a632017-11-22 17:52:53 +010022 # osd
23 if os.path.exists('/var/lib/ceph/osd'):
Jiri Broulik42552052018-02-15 15:23:29 +010024 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()
25 sed = 'sed \'s#{0}-##g\''.format(mount_path)
Jiri Broulik72e8a632017-11-22 17:52:53 +010026 cmd = "lsblk -rp | awk '{print $1,$6,$7}' | grep -v lockbox | grep ceph | " + sed
27 osd_output = check_output(cmd, shell=True)
28 if osd_output:
29 devices = {}
30 for line in osd_output.splitlines():
31 device = line.split()
32 encrypted = False
33 if "crypt" in device[1]:
34 output = check_output("lsblk -rp | grep -B1 " + device[0], shell=True)
35 for l in output.splitlines():
36 d = l.split()
Jiri Broulik42552052018-02-15 15:23:29 +010037 dev = re.sub("\d+", "", device[0])
Jiri Broulik72e8a632017-11-22 17:52:53 +010038 encrypted = True
39 break
40 else:
41 dev = device[0].replace('1','')
Jiri Broulik42552052018-02-15 15:23:29 +010042 dev = re.sub("\d+", "", device[0])
Jiri Broulik72e8a632017-11-22 17:52:53 +010043 device[0] = device[2]
Machi Hoshinobf4a6382018-04-05 10:04:55 +090044 try:
45 devices[device[0]] = {}
46 devices[device[0]]['dev'] = dev
47 if encrypted:
48 devices[device[0]]['dmcrypt'] = 'true'
49 tline = check_output("ceph -c " + conf_file + " osd tree | awk '{print $1,$2,$3,$4}' | grep -w 'osd." + device[0] + "'", shell=True)
50 osd = tline.split()
51 if "osd" not in osd[2]:
52 crush_class = osd[1]
53 crush_weight = osd[2]
54 devices[device[0]]['class'] = crush_class
55 devices[device[0]]['weight'] = crush_weight
56 else:
57 crush_weight = osd[1]
58 devices[device[0]]['weight'] = crush_weight
59 except CalledProcessError:
60 continue
Jiri Broulik72e8a632017-11-22 17:52:53 +010061 grain["ceph"]["ceph_disk"] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020062
Jiri Broulik72e8a632017-11-22 17:52:53 +010063 # keyrings
64 directory = '/etc/ceph/'
65 keyrings = {}
66 if os.path.isdir(directory):
67 for filename in os.listdir(directory):
Jiri Broulik42552052018-02-15 15:23:29 +010068 if filename.endswith(".keyring") and re.search(".client.", filename):
Jiri Broulik72e8a632017-11-22 17:52:53 +010069 keyring_output = open(os.path.join(directory, filename), "r")
Jiri Broulik42552052018-02-15 15:23:29 +010070 keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
Jiri Broulik72e8a632017-11-22 17:52:53 +010071 if keyring_output:
72 keyrings[keyring_name] = {}
73 for line in keyring_output:
74 attr = shlex.split(line)
75 if attr:
76 if attr[0] == 'key':
77 keyrings[keyring_name]['key'] = attr[2]
78 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
79 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
80 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
81 keyrings[keyring_name]['caps'] = {}
82 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
83 if keyrings:
84 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +020085
Jiri Broulik72e8a632017-11-22 17:52:53 +010086 # mon keyring
87 hostname = check_output("hostname", shell=True).rstrip()
Jiri Broulik42552052018-02-15 15:23:29 +010088 filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
Jiri Broulik72e8a632017-11-22 17:52:53 +010089 if os.path.isfile(filepath):
90 mon_key_output = open(filepath, "r")
91 if mon_key_output:
92 keyrings['mon'] = {}
93 for line in mon_key_output:
94 attr = shlex.split(line)
95 if attr:
96 if attr[0] == 'key':
97 keyrings['mon']['key'] = attr[2]
98 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
99 keyrings['mon']['caps'][attr[1]] = attr[3]
100 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
101 keyrings['mon']['caps'] = {}
102 keyrings['mon']['caps'][attr[1]] = attr[3]
103 grain["ceph"]["ceph_keyring"] = keyrings
104
105 return grain
106 else:
107 return None