blob: 5df302c40441b01b6e2aa40c6bb156658004cfc3 [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 Broulikc2be93b2017-10-03 14:20:00 +020014
Jiri Broulik72e8a632017-11-22 17:52:53 +010015 # osd
16 if os.path.exists('/var/lib/ceph/osd'):
17 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()
18 sed = 'sed \'s#{0}##g\''.format(mount_path)
19 cmd = "lsblk -rp | awk '{print $1,$6,$7}' | grep -v lockbox | grep ceph | " + sed
20 osd_output = check_output(cmd, shell=True)
21 if osd_output:
22 devices = {}
23 for line in osd_output.splitlines():
24 device = line.split()
25 encrypted = False
26 if "crypt" in device[1]:
27 output = check_output("lsblk -rp | grep -B1 " + device[0], shell=True)
28 for l in output.splitlines():
29 d = l.split()
30 dev = d[0].replace('1','')
31 encrypted = True
32 break
33 else:
34 dev = device[0].replace('1','')
35 device[0] = device[2]
36 devices[device[0]] = {}
37 devices[device[0]]['dev'] = dev
38 if encrypted:
39 devices[device[0]]['dmcrypt'] = 'true'
40 tline = check_output("ceph osd tree | awk '{print $1,$2,$3,$4}' | grep -w 'osd." + device[0] + "'", shell=True)
41 osd = tline.split()
42 if "osd" not in osd[2]:
43 crush_class = osd[1]
44 crush_weight = osd[2]
45 devices[device[0]]['class'] = crush_class
46 devices[device[0]]['weight'] = crush_weight
47 else:
48 crush_weight = osd[1]
49 devices[device[0]]['weight'] = crush_weight
50 grain["ceph"]["ceph_disk"] = devices
Jiri Broulikc2be93b2017-10-03 14:20:00 +020051
Jiri Broulik72e8a632017-11-22 17:52:53 +010052 # keyrings
53 directory = '/etc/ceph/'
54 keyrings = {}
55 if os.path.isdir(directory):
56 for filename in os.listdir(directory):
57 if filename.endswith(".keyring") and filename.startswith("ceph.client"):
58 keyring_output = open(os.path.join(directory, filename), "r")
59 keyring_name = re.search('ceph.client.(.+?).keyring', filename).group(1)
60 if keyring_output:
61 keyrings[keyring_name] = {}
62 for line in keyring_output:
63 attr = shlex.split(line)
64 if attr:
65 if attr[0] == 'key':
66 keyrings[keyring_name]['key'] = attr[2]
67 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
68 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
69 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
70 keyrings[keyring_name]['caps'] = {}
71 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
72 if keyrings:
73 grain["ceph"]["ceph_keyring"] = keyrings
Jiri Broulikc2be93b2017-10-03 14:20:00 +020074
Jiri Broulik72e8a632017-11-22 17:52:53 +010075 # mon keyring
76 hostname = check_output("hostname", shell=True).rstrip()
77 filepath = "/var/lib/ceph/mon/ceph-{0}/keyring".format(hostname)
78 if os.path.isfile(filepath):
79 mon_key_output = open(filepath, "r")
80 if mon_key_output:
81 keyrings['mon'] = {}
82 for line in mon_key_output:
83 attr = shlex.split(line)
84 if attr:
85 if attr[0] == 'key':
86 keyrings['mon']['key'] = attr[2]
87 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
88 keyrings['mon']['caps'][attr[1]] = attr[3]
89 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
90 keyrings['mon']['caps'] = {}
91 keyrings['mon']['caps'][attr[1]] = attr[3]
92 grain["ceph"]["ceph_keyring"] = keyrings
93
94 return grain
95 else:
96 return None