blob: a678404e61701e7846b1be4484e6e7afbb7f717e [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
12 mount_path = check_output("df -h | awk '{print $6}' | grep ceph | sed 's/[0-9]*//g' | awk 'NR==1{print $1}'", shell=True).rstrip()
13 sed = 'sed \'s#{0}##g\''.format(mount_path)
14 cmd = "df -h | awk '{print $1,$6}' | grep ceph | " + sed
15 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()
22 dev = device[0].replace('1','')
23 device[0] = device[1]
24 devices[device[0]] = {}
25 devices[device[0]]['dev'] = dev
26 tline = check_output("ceph osd tree | awk '{print $1,$2,$3,$4}' | grep -w 'osd." + device[0] + "'", shell=True)
27 osd = tline.split()
Jiri Broulikeaf41472017-10-18 09:56:33 +020028 if "osd" not in osd[2]:
29 crush_class = osd[1]
30 crush_weight = osd[2]
31 devices[device[0]]['class'] = crush_class
32 devices[device[0]]['weight'] = crush_weight
33 else:
34 crush_weight = osd[1]
35 devices[device[0]]['weight'] = crush_weight
Jiri Broulikc2be93b2017-10-03 14:20:00 +020036 grain["ceph"]["ceph_disk"] = devices
37
38 # keyrings
39 directory = '/etc/ceph/'
40 keyrings = {}
41 if os.path.isdir(directory):
42 for filename in os.listdir(directory):
43 if filename.endswith(".keyring") and filename.startswith("ceph.client"):
44 keyring_output = open(os.path.join(directory, filename), "r")
45 keyring_name = re.search('ceph.client.(.+?).keyring', filename).group(1)
46 if keyring_output:
47 keyrings[keyring_name] = {}
48 for line in keyring_output:
49 attr = shlex.split(line)
50 if attr:
51 if attr[0] == 'key':
52 keyrings[keyring_name]['key'] = attr[2]
53 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
54 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
55 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
56 keyrings[keyring_name]['caps'] = {}
57 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
58 if keyrings:
59 grain["ceph"]["ceph_keyring"] = keyrings
60
61 # mon keyring
62 hostname = check_output("hostname", shell=True).rstrip()
63 filepath = "/var/lib/ceph/mon/ceph-{0}/keyring".format(hostname)
64 if os.path.isfile(filepath):
65 mon_key_output = open(filepath, "r")
66 if mon_key_output:
67 keyrings['mon'] = {}
68 for line in mon_key_output:
69 attr = shlex.split(line)
70 if attr:
71 if attr[0] == 'key':
72 keyrings['mon']['key'] = attr[2]
73 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
74 keyrings['mon']['caps'][attr[1]] = attr[3]
75 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
76 keyrings['mon']['caps'] = {}
77 keyrings['mon']['caps'][attr[1]] = attr[3]
78 grain["ceph"]["ceph_keyring"] = keyrings
79
80 return grain