Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 3 | def main(): |
| 4 | |
| 5 | from subprocess import check_output |
Machi Hoshino | bf4a638 | 2018-04-05 10:04:55 +0900 | [diff] [blame] | 6 | from subprocess import CalledProcessError |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 7 | import shlex |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 8 | import json |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 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 | |
Michel Nederlof | 1baf1de | 2018-06-12 09:40:00 +0200 | [diff] [blame] | 22 | # get the fsid from config file, for salt-formulas to filter on in case of multiple ceph clusters |
| 23 | with open(conf_file, 'r') as conf_fh: |
| 24 | for line in conf_fh.read().splitlines(): |
| 25 | if 'fsid' in line: |
| 26 | attr = shlex.split(line) |
| 27 | grain['ceph']['fsid'] = attr[2] |
| 28 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 29 | # osd |
| 30 | if os.path.exists('/var/lib/ceph/osd'): |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 31 | devices = {} |
Tomek Jaroszyk | 9327250 | 2020-03-29 23:39:05 +0200 | [diff] [blame] | 32 | for osd in os.listdir('/var/lib/ceph/osd'): |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 33 | |
Tomek Jaroszyk | 9327250 | 2020-03-29 23:39:05 +0200 | [diff] [blame] | 34 | try: |
| 35 | with open('/var/lib/ceph/osd/%s/whoami' % osd) as file: |
| 36 | dev_id = file.readline().strip() |
Tomek Jaroszyk | 5e99c0f | 2020-03-31 17:30:11 +0200 | [diff] [blame] | 37 | with open('/var/lib/ceph/osd/%s/fsid' % osd) as file: |
| 38 | fsid = file.readline().strip() |
Tomek Jaroszyk | 9327250 | 2020-03-29 23:39:05 +0200 | [diff] [blame] | 39 | path = os.readlink('/var/lib/ceph/osd/%s/block' % osd) |
| 40 | except IOError: |
| 41 | continue |
Alena Kiseleva | 52dae77 | 2019-02-06 16:58:36 +0300 | [diff] [blame] | 42 | |
Tomek Jaroszyk | 9327250 | 2020-03-29 23:39:05 +0200 | [diff] [blame] | 43 | cmd = "lsblk -no pkname %s" % path |
| 44 | dev = check_output(cmd, shell=True).strip() |
| 45 | if dev: |
| 46 | dev = '/dev/%s' % dev |
| 47 | else: |
| 48 | cmd = "pvs | grep %s | awk '{print $1}'" % path.split('/')[-2] |
| 49 | dev = check_output(cmd, shell=True).strip() |
| 50 | devices[dev_id] = {} |
Tomek Jaroszyk | 5e99c0f | 2020-03-31 17:30:11 +0200 | [diff] [blame] | 51 | devices[dev_id]['fsid'] = fsid |
Tomek Jaroszyk | 9327250 | 2020-03-29 23:39:05 +0200 | [diff] [blame] | 52 | devices[dev_id]['path'] = path |
| 53 | devices[dev_id]['dev'] = dev |
| 54 | |
| 55 | cmd = "ceph osd tree --format json" |
| 56 | osd_tree_output = check_output(cmd, shell=True).decode("utf-8") |
| 57 | osd_tree_output = json.loads(osd_tree_output) |
| 58 | for osd in osd_tree_output['nodes']: |
| 59 | dev_id = str(osd['id']) |
| 60 | if dev_id >= 0 and devices.has_key(dev_id): |
| 61 | devices[dev_id]['weight'] = osd['crush_weight'] |
| 62 | devices[dev_id]['class'] = osd['device_class'] |
| 63 | |
| 64 | if devices: |
| 65 | grain['ceph']['ceph_disk'] = devices |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 66 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 67 | # keyrings |
| 68 | directory = '/etc/ceph/' |
| 69 | keyrings = {} |
| 70 | if os.path.isdir(directory): |
| 71 | for filename in os.listdir(directory): |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame] | 72 | if filename.endswith(".keyring") and re.search(".client.", filename): |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 73 | keyring_output = open(os.path.join(directory, filename), "r") |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame] | 74 | keyring_name = re.search('(.+?).client.(.+?).keyring', filename).group(2) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 75 | if keyring_output: |
| 76 | keyrings[keyring_name] = {} |
| 77 | for line in keyring_output: |
| 78 | attr = shlex.split(line) |
| 79 | if attr: |
| 80 | if attr[0] == 'key': |
| 81 | keyrings[keyring_name]['key'] = attr[2] |
| 82 | if attr[0] == 'caps' and 'caps' in keyrings[keyring_name]: |
| 83 | keyrings[keyring_name]['caps'][attr[1]] = attr[3] |
| 84 | elif attr[0] == 'caps' and 'caps' not in keyrings[keyring_name]: |
| 85 | keyrings[keyring_name]['caps'] = {} |
| 86 | keyrings[keyring_name]['caps'][attr[1]] = attr[3] |
| 87 | if keyrings: |
| 88 | grain["ceph"]["ceph_keyring"] = keyrings |
Jiri Broulik | c2be93b | 2017-10-03 14:20:00 +0200 | [diff] [blame] | 89 | |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 90 | # mon keyring |
| 91 | hostname = check_output("hostname", shell=True).rstrip() |
Jiri Broulik | 4255205 | 2018-02-15 15:23:29 +0100 | [diff] [blame] | 92 | filepath = "/var/lib/ceph/mon/{0}-{1}/keyring".format(cluster_name, hostname) |
Jiri Broulik | 72e8a63 | 2017-11-22 17:52:53 +0100 | [diff] [blame] | 93 | if os.path.isfile(filepath): |
| 94 | mon_key_output = open(filepath, "r") |
| 95 | if mon_key_output: |
| 96 | keyrings['mon'] = {} |
| 97 | for line in mon_key_output: |
| 98 | attr = shlex.split(line) |
| 99 | if attr: |
| 100 | if attr[0] == 'key': |
| 101 | keyrings['mon']['key'] = attr[2] |
| 102 | if attr[0] == 'caps' and 'caps' in keyrings['mon']: |
| 103 | keyrings['mon']['caps'][attr[1]] = attr[3] |
| 104 | elif attr[0] == 'caps' and 'caps' not in keyrings['mon']: |
| 105 | keyrings['mon']['caps'] = {} |
| 106 | keyrings['mon']['caps'][attr[1]] = attr[3] |
| 107 | grain["ceph"]["ceph_keyring"] = keyrings |
| 108 | |
| 109 | return grain |
| 110 | else: |
| 111 | return None |
Tomek Jaroszyk | 9327250 | 2020-03-29 23:39:05 +0200 | [diff] [blame] | 112 | |