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