Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | |
| 4 | def main(): |
| 5 | |
| 6 | from subprocess import check_output |
| 7 | import shlex |
| 8 | import os |
| 9 | import re |
| 10 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 11 | if os.path.exists('/etc/ceph'): |
| 12 | grain = {} |
| 13 | grain["ceph"] = {} |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 14 | conf_dir = '/etc/ceph/' |
| 15 | for filename in os.listdir(conf_dir): |
| 16 | if filename.endswith(".conf"): |
| 17 | cluster_name = re.search('(.+?).conf', filename).group(1) |
| 18 | break |
| 19 | conf_file = conf_dir + cluster_name + '.conf' |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 20 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 21 | # osd |
| 22 | if os.path.exists('/var/lib/ceph/osd'): |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 23 | 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() |
| 24 | sed = 'sed \'s#{0}-##g\''.format(mount_path) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 25 | cmd = "lsblk -rp | awk '{print $1,$6,$7}' | grep -v lockbox | grep ceph | " + sed |
| 26 | osd_output = check_output(cmd, shell=True) |
| 27 | if osd_output: |
| 28 | devices = {} |
| 29 | for line in osd_output.splitlines(): |
| 30 | device = line.split() |
| 31 | encrypted = False |
| 32 | if "crypt" in device[1]: |
| 33 | output = check_output("lsblk -rp | grep -B1 " + device[0], shell=True) |
| 34 | for l in output.splitlines(): |
| 35 | d = l.split() |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 36 | dev = re.sub("\d+", "", device[0]) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 37 | encrypted = True |
| 38 | break |
| 39 | else: |
| 40 | dev = device[0].replace('1','') |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 41 | dev = re.sub("\d+", "", device[0]) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 42 | device[0] = device[2] |
| 43 | devices[device[0]] = {} |
| 44 | devices[device[0]]['dev'] = dev |
| 45 | if encrypted: |
| 46 | devices[device[0]]['dmcrypt'] = 'true' |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 47 | tline = check_output("ceph -c " + conf_file + " osd tree | awk '{print $1,$2,$3,$4}' | grep -w 'osd." + device[0] + "'", shell=True) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 48 | osd = tline.split() |
| 49 | if "osd" not in osd[2]: |
| 50 | crush_class = osd[1] |
| 51 | crush_weight = osd[2] |
| 52 | devices[device[0]]['class'] = crush_class |
| 53 | devices[device[0]]['weight'] = crush_weight |
| 54 | else: |
| 55 | crush_weight = osd[1] |
| 56 | devices[device[0]]['weight'] = crush_weight |
| 57 | grain["ceph"]["ceph_disk"] = devices |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 58 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 59 | # keyrings |
| 60 | directory = '/etc/ceph/' |
| 61 | keyrings = {} |
| 62 | if os.path.isdir(directory): |
| 63 | for filename in os.listdir(directory): |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 64 | if filename.endswith(".keyring") and re.search(".client.", filename): |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 65 | keyring_output = open(os.path.join(directory, filename), "r") |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 66 | keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 67 | if keyring_output: |
| 68 | keyrings[keyring_name] = {} |
| 69 | for line in keyring_output: |
| 70 | attr = shlex.split(line) |
| 71 | if attr: |
| 72 | if attr[0] == 'key': |
| 73 | keyrings[keyring_name]['key'] = attr[2] |
| 74 | if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]: |
| 75 | keyrings[keyring_name]['caps'][attr[1]] = attr[3] |
| 76 | elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]: |
| 77 | keyrings[keyring_name]['caps'] = {} |
| 78 | keyrings[keyring_name]['caps'][attr[1]] = attr[3] |
| 79 | if keyrings: |
| 80 | grain["ceph"]["ceph_keyring"] = keyrings |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 81 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 82 | # mon keyring |
| 83 | hostname = check_output("hostname", shell=True).rstrip() |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame^] | 84 | filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 85 | if os.path.isfile(filepath): |
| 86 | mon_key_output = open(filepath, "r") |
| 87 | if mon_key_output: |
| 88 | keyrings['mon'] = {} |
| 89 | for line in mon_key_output: |
| 90 | attr = shlex.split(line) |
| 91 | if attr: |
| 92 | if attr[0] == 'key': |
| 93 | keyrings['mon']['key'] = attr[2] |
| 94 | if attr[0] == 'caps' and 'caps' in keyrings['mon']: |
| 95 | keyrings['mon']['caps'][attr[1]] = attr[3] |
| 96 | elif attr[0] == 'caps' and 'caps' not in keyrings['mon']: |
| 97 | keyrings['mon']['caps'] = {} |
| 98 | keyrings['mon']['caps'][attr[1]] = attr[3] |
| 99 | grain["ceph"]["ceph_keyring"] = keyrings |
| 100 | |
| 101 | return grain |
| 102 | else: |
| 103 | return None |