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