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