Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 1 | """ Collect data about ceph nodes""" |
| 2 | import json |
| 3 | |
| 4 | import sh |
Ved-vampir | 2993171 | 2015-03-18 16:31:02 +0300 | [diff] [blame] | 5 | from node import Node |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 6 | |
| 7 | |
| 8 | def discover_ceph_node(): |
| 9 | """ Return list of ceph's nodes ips """ |
| 10 | |
| 11 | ips = {} |
| 12 | osd_list = get_osds_list() |
| 13 | osd_ips = get_osds_ips(osd_list) |
| 14 | mon_ips = get_mons_or_mds_ips("mon") |
| 15 | mds_ips = get_mons_or_mds_ips("mds") |
| 16 | for ip in osd_ips: |
| 17 | url = "ssh://%s" % ip |
| 18 | if url in ips: |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 19 | ips[url].append("ceph-osd") |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 20 | else: |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 21 | ips[url] = ["ceph-osd"] |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 22 | for ip in mon_ips: |
| 23 | url = "ssh://%s" % ip |
| 24 | if url in ips: |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 25 | ips[url].append("ceph-mon") |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 26 | else: |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 27 | ips[url] = ["ceph-mon"] |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 28 | for ip in mds_ips: |
| 29 | url = "ssh://%s" % ip |
| 30 | if url in ips: |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 31 | ips[url].append("ceph-mds") |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 32 | else: |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 33 | ips[url] = ["ceph-mds"] |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 34 | |
| 35 | res = [] |
| 36 | for url, roles in ips: |
Ved-vampir | 2993171 | 2015-03-18 16:31:02 +0300 | [diff] [blame] | 37 | res.append(Node(ip=url, roles=list(roles))) |
Ved-vampir | a915a01 | 2015-03-18 14:38:52 +0300 | [diff] [blame] | 38 | |
| 39 | return res |
| 40 | |
| 41 | |
| 42 | # internal services |
| 43 | |
| 44 | |
| 45 | class CephException(Exception): |
| 46 | """ Exceptions from ceph call""" |
| 47 | pass |
| 48 | |
| 49 | class ParameterException(Exception): |
| 50 | """ Bad parameter in function""" |
| 51 | pass |
| 52 | |
| 53 | |
| 54 | def get_osds_list(): |
| 55 | """ Get list of osds id""" |
| 56 | try: |
| 57 | res = sh.ceph.osd.ls() |
| 58 | osd_list = [osd_id |
| 59 | for osd_id in res.split("\n") if osd_id != ''] |
| 60 | return osd_list |
| 61 | except sh.CommandNotFound: |
| 62 | raise CephException("Ceph command not found") |
| 63 | except: |
| 64 | raise CephException("Ceph command 'osd ls' execution error") |
| 65 | |
| 66 | |
| 67 | def get_mons_or_mds_ips(who): |
| 68 | """ Return mon ip list |
| 69 | :param who - "mon" or "mds" """ |
| 70 | try: |
| 71 | ips = set() |
| 72 | if who == "mon": |
| 73 | res = sh.ceph.mon.dump() |
| 74 | elif who == "mds": |
| 75 | res = sh.ceph.mds.dump() |
| 76 | else: |
| 77 | raise ParameterException("'%s' in get_mons_or_mds_ips instead of mon/mds" % who) |
| 78 | |
| 79 | line_res = res.split("\n") |
| 80 | for line in line_res: |
| 81 | fields = line.split() |
| 82 | if len(fields) > 2 and who in fields[2]: |
| 83 | ips.add(fields[1].split(":")[0]) |
| 84 | |
| 85 | return ips |
| 86 | |
| 87 | except sh.CommandNotFound: |
| 88 | raise CephException("Ceph command not found") |
| 89 | except ParameterException as e: |
| 90 | raise e |
| 91 | except: |
| 92 | mes = "Ceph command '%s dump' execution error" % who |
| 93 | raise CephException(mes) |
| 94 | |
| 95 | |
| 96 | def get_osds_ips(osd_list): |
| 97 | """ Get osd's ips |
| 98 | :param osd_list - list of osd names from osd ls command""" |
| 99 | try: |
| 100 | ips = set() |
| 101 | for osd_id in osd_list: |
| 102 | res = sh.ceph.osd.find(osd_id) |
| 103 | ip = json.loads(str(res))["ip"] |
| 104 | ips.add(ip.split(":")[0]) |
| 105 | return ips |
| 106 | |
| 107 | except sh.CommandNotFound: |
| 108 | raise CephException("Ceph command not found") |
| 109 | except: |
| 110 | raise CephException("Ceph command 'osd find' execution error") |
| 111 | |