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