blob: 1b4dfb249ec8bd15c57658b8692e8672291c8a10 [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()
28 crush_class = osd[1]
29 crush_weight = osd[2]
30 devices[device[0]]['class'] = crush_class
31 devices[device[0]]['weight'] = crush_weight
32 grain["ceph"]["ceph_disk"] = devices
33
34 # keyrings
35 directory = '/etc/ceph/'
36 keyrings = {}
37 if os.path.isdir(directory):
38 for filename in os.listdir(directory):
39 if filename.endswith(".keyring") and filename.startswith("ceph.client"):
40 keyring_output = open(os.path.join(directory, filename), "r")
41 keyring_name = re.search('ceph.client.(.+?).keyring', filename).group(1)
42 if keyring_output:
43 keyrings[keyring_name] = {}
44 for line in keyring_output:
45 attr = shlex.split(line)
46 if attr:
47 if attr[0] == 'key':
48 keyrings[keyring_name]['key'] = attr[2]
49 if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]:
50 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
51 elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]:
52 keyrings[keyring_name]['caps'] = {}
53 keyrings[keyring_name]['caps'][attr[1]] = attr[3]
54 if keyrings:
55 grain["ceph"]["ceph_keyring"] = keyrings
56
57 # mon keyring
58 hostname = check_output("hostname", shell=True).rstrip()
59 filepath = "/var/lib/ceph/mon/ceph-{0}/keyring".format(hostname)
60 if os.path.isfile(filepath):
61 mon_key_output = open(filepath, "r")
62 if mon_key_output:
63 keyrings['mon'] = {}
64 for line in mon_key_output:
65 attr = shlex.split(line)
66 if attr:
67 if attr[0] == 'key':
68 keyrings['mon']['key'] = attr[2]
69 if attr[0] == 'caps' and 'caps' in keyrings['mon']:
70 keyrings['mon']['caps'][attr[1]] = attr[3]
71 elif attr[0] == 'caps' and 'caps' not in keyrings['mon']:
72 keyrings['mon']['caps'] = {}
73 keyrings['mon']['caps'][attr[1]] = attr[3]
74 grain["ceph"]["ceph_keyring"] = keyrings
75
76 return grain