blob: 45652e831052ff0e97f8f92727da03449f2032ce [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
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'):
Jiri Broulik42552052018-02-15 15:23:29 +010031 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()
32 sed = 'sed \'s#{0}-##g\''.format(mount_path)
Jiri Broulik72e8a632017-11-22 17:52:53 +010033 cmd = "lsblk -rp | awk '{print $1,$6,$7}' | grep -v lockbox | grep ceph | " + sed
34 osd_output = check_output(cmd, shell=True)
35 if osd_output:
36 devices = {}
37 for line in osd_output.splitlines():
38 device = line.split()
39 encrypted = False
40 if "crypt" in device[1]:
41 output = check_output("lsblk -rp | grep -B1 " + device[0], shell=True)
42 for l in output.splitlines():
43 d = l.split()
Jiri Broulik42552052018-02-15 15:23:29 +010044 dev = re.sub("\d+", "", device[0])
Jiri Broulik72e8a632017-11-22 17:52:53 +010045 encrypted = True
46 break
47 else:
48 dev = device[0].replace('1','')
Jiri Broulik42552052018-02-15 15:23:29 +010049 dev = re.sub("\d+", "", device[0])
Jiri Broulik72e8a632017-11-22 17:52:53 +010050 device[0] = device[2]
Machi Hoshinobf4a6382018-04-05 10:04:55 +090051 try:
52 devices[device[0]] = {}
53 devices[device[0]]['dev'] = dev
54 if encrypted:
55 devices[device[0]]['dmcrypt'] = 'true'
56 tline = check_output("ceph -c " + conf_file + " osd tree | awk '{print $1,$2,$3,$4}' | grep -w 'osd." + device[0] + "'", shell=True)
57 osd = tline.split()
58 if "osd" not in osd[2]:
59 crush_class = osd[1]
60 crush_weight = osd[2]
61 devices[device[0]]['class'] = crush_class
62 devices[device[0]]['weight'] = crush_weight
63 else:
64 crush_weight = osd[1]
65 devices[device[0]]['weight'] = crush_weight
66 except CalledProcessError:
67 continue
Jiri Broulik72e8a632017-11-22 17:52:53 +010068 grain["ceph"]["ceph_disk"] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020069
Jiri Broulik72e8a632017-11-22 17:52:53 +010070 # keyrings
71 directory = '/etc/ceph/'
72 keyrings = {}
73 if os.path.isdir(directory):
74 for filename in os.listdir(directory):
Jiri Broulik42552052018-02-15 15:23:29 +010075 if filename.endswith(".keyring") and re.search(".client.", filename):
Jiri Broulik72e8a632017-11-22 17:52:53 +010076 keyring_output = open(os.path.join(directory, filename), "r")
Jiri Broulik42552052018-02-15 15:23:29 +010077 keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2)
Jiri Broulik72e8a632017-11-22 17:52:53 +010078 if keyring_output:
79 keyrings[keyring_name] = {}
80 for line in keyring_output:
81 attr = shlex.split(line)
82 if attr:
83 if attr[0] == 'key':
84 keyrings[keyring_name]['key'] = attr[2]
85 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
86 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
87 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
88 keyrings[keyring_name]['caps'] = {}
89 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
90 if keyrings:
91 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +020092
Jiri Broulik72e8a632017-11-22 17:52:53 +010093 # mon keyring
94 hostname = check_output("hostname", shell=True).rstrip()
Jiri Broulik42552052018-02-15 15:23:29 +010095 filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname)
Jiri Broulik72e8a632017-11-22 17:52:53 +010096 if os.path.isfile(filepath):
97 mon_key_output = open(filepath, "r")
98 if mon_key_output:
99 keyrings['mon'] = {}
100 for line in mon_key_output:
101 attr = shlex.split(line)
102 if attr:
103 if attr[0] == 'key':
104 keyrings['mon']['key'] = attr[2]
105 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
106 keyrings['mon']['caps'][attr[1]] = attr[3]
107 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
108 keyrings['mon']['caps'] = {}
109 keyrings['mon']['caps'][attr[1]] = attr[3]
110 grain["ceph"]["ceph_keyring"] = keyrings
111
112 return grain
113 else:
114 return None