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 |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 9 | import json |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 10 | import os |
| 11 | import re |
| 12 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 13 | if os.path.exists('/etc/ceph'): |
| 14 | grain = {} |
| 15 | grain["ceph"] = {} |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame] | 16 | conf_dir = '/etc/ceph/' |
| 17 | for filename in os.listdir(conf_dir): |
| 18 | if filename.endswith(".conf"): |
| 19 | cluster_name = re.search('(.+?).conf', filename).group(1) |
| 20 | break |
| 21 | conf_file = conf_dir + cluster_name + '.conf' |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 22 | |
Michel Nederlof | 1baf1de | 2018-06-12 09:40:00 +0200 | [diff] [blame] | 23 | # get the fsid from config file, for salt-formulas to filter on in case of multiple ceph clusters |
| 24 | with open(conf_file, 'r') as conf_fh: |
| 25 | for line in conf_fh.read().splitlines(): |
| 26 | if 'fsid' in line: |
| 27 | attr = shlex.split(line) |
| 28 | grain['ceph']['fsid'] = attr[2] |
| 29 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 30 | # osd |
| 31 | if os.path.exists('/var/lib/ceph/osd'): |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 32 | cmd = "ceph-volume lvm list --format json" |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 33 | osd_output = check_output(cmd, shell=True) |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 34 | osd_output = json.loads(osd_output) |
| 35 | dev_id = '' |
| 36 | devices = {} |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 37 | if osd_output: |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 38 | for osd, params in osd_output.iteritems(): |
| 39 | dev_id = osd |
| 40 | devices[dev_id] = {} |
| 41 | devices[dev_id]['dev'] = params[0]['devices'][0] |
| 42 | devices[dev_id]['path'] = params[0]['path'] |
| 43 | devices[dev_id]['uuid'] = params[0]['tags']['ceph.osd_fsid'] |
| 44 | |
| 45 | cmd = "ceph osd tree --format json" |
| 46 | osd_tree_output = check_output(cmd, shell=True) |
| 47 | osd_tree_output = json.loads(osd_tree_output) |
| 48 | for osd in osd_tree_output['nodes']: |
| 49 | if 'type_id' in osd.keys(): |
| 50 | if str(osd['type_id']) == '0': |
| 51 | for dev_id in devices.keys(): |
| 52 | if str(osd['id']) == str(dev_id): |
| 53 | devices[dev_id]['weight'] = osd['crush_weight'] |
| 54 | devices[dev_id]['class'] = osd['device_class'] |
| 55 | grain["ceph"]["ceph_volume"] = devices |
| 56 | else: |
| 57 | cmd = "ceph-disk list --format json" |
| 58 | osd_output = check_output(cmd, shell=True).decode("utf-8") |
| 59 | osd_output = json.loads(osd_output) |
| 60 | dev_id = '' |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 61 | devices = {} |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 62 | if osd_output: |
| 63 | for line in osd_output: |
| 64 | if "is_partition" not in line.keys(): |
| 65 | dev = line["path"] |
| 66 | parts = line["partitions"] |
| 67 | for p in parts: |
| 68 | if "mount" in p.keys() and "ceph" in p["mount"]: |
| 69 | dev_id = p["whoami"] |
| 70 | devices[dev_id] = {} |
| 71 | devices[dev_id]['dev'] = dev |
| 72 | if len(p["dmcrypt"]) > 0: |
| 73 | devices[dev_id]['dmcrypt'] = 'true' |
| 74 | |
| 75 | cmd = "ceph osd tree --format json" |
| 76 | osd_tree_output = check_output(cmd, shell=True).decode("utf-8") |
| 77 | osd_tree_output = json.loads(osd_tree_output) |
| 78 | for osd in osd_tree_output['nodes']: |
| 79 | if 'type_id' in osd.keys(): |
| 80 | if str(osd['type_id']) == '0': |
| 81 | for dev_id in devices.keys(): |
| 82 | if str(osd['id']) == str(dev_id): |
| 83 | devices[dev_id]['weight'] = osd['crush_weight'] |
| 84 | devices[dev_id]['class'] = osd['device_class'] |
| 85 | grain["ceph"]["ceph_disk"] = devices |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 86 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 87 | # keyrings |
| 88 | directory = '/etc/ceph/' |
| 89 | keyrings = {} |
| 90 | if os.path.isdir(directory): |
| 91 | for filename in os.listdir(directory): |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame] | 92 | if filename.endswith(".keyring") and re.search(".client.", filename): |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 93 | keyring_output = open(os.path.join(directory, filename), "r") |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame] | 94 | keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 95 | if keyring_output: |
| 96 | keyrings[keyring_name] = {} |
| 97 | for line in keyring_output: |
| 98 | attr = shlex.split(line) |
| 99 | if attr: |
| 100 | if attr[0] == 'key': |
| 101 | keyrings[keyring_name]['key'] = attr[2] |
| 102 | if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]: |
| 103 | keyrings[keyring_name]['caps'][attr[1]] = attr[3] |
| 104 | elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]: |
| 105 | keyrings[keyring_name]['caps'] = {} |
| 106 | keyrings[keyring_name]['caps'][attr[1]] = attr[3] |
| 107 | if keyrings: |
| 108 | grain["ceph"]["ceph_keyring"] = keyrings |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 109 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 110 | # mon keyring |
| 111 | hostname = check_output("hostname", shell=True).rstrip() |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame] | 112 | filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 113 | if os.path.isfile(filepath): |
| 114 | mon_key_output = open(filepath, "r") |
| 115 | if mon_key_output: |
| 116 | keyrings['mon'] = {} |
| 117 | for line in mon_key_output: |
| 118 | attr = shlex.split(line) |
| 119 | if attr: |
| 120 | if attr[0] == 'key': |
| 121 | keyrings['mon']['key'] = attr[2] |
| 122 | if attr[0] == 'caps' and 'caps' in keyrings['mon']: |
| 123 | keyrings['mon']['caps'][attr[1]] = attr[3] |
| 124 | elif attr[0] == 'caps' and 'caps' not in keyrings['mon']: |
| 125 | keyrings['mon']['caps'] = {} |
| 126 | keyrings['mon']['caps'][attr[1]] = attr[3] |
| 127 | grain["ceph"]["ceph_keyring"] = keyrings |
| 128 | |
| 129 | return grain |
| 130 | else: |
| 131 | return None |