summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/services/cluster.py
blob: 9caaf1963366b978b5ed7e3236500c8363e559d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
from enum import Enum
from typing import NamedTuple

from .. import mgr


class ClusterCapacity(NamedTuple):
    total_avail_bytes: int
    total_bytes: int
    total_used_raw_bytes: int
    total_objects: int
    total_pool_bytes_used: int
    average_object_size: int


class ClusterModel:

    class Status(Enum):
        INSTALLED = 0
        POST_INSTALLED = 1

    status: Status

    def __init__(self, status=Status.POST_INSTALLED.name):
        """
        :param status: The status of the cluster. Assume that the cluster
            is already functional by default.
        :type status: str
        """
        self.status = self.Status[status]

    def dict(self):
        return {'status': self.status.name}

    def to_db(self):
        mgr.set_store('cluster/status', self.status.name)

    @classmethod
    def from_db(cls):
        """
        Get the stored cluster status from the configuration key/value store.
        If the status is not set, assume it is already fully functional.
        """
        return cls(status=mgr.get_store('cluster/status', cls.Status.POST_INSTALLED.name))

    @classmethod
    def get_capacity(cls) -> ClusterCapacity:
        df = mgr.get('df')
        total_pool_bytes_used = 0
        average_object_size = 0
        total_data_pool_objects = 0
        total_data_pool_bytes_used = 0
        rgw_pools_data = cls.get_rgw_pools()

        for pool in df['pools']:
            pool_name = str(pool['name'])
            if pool_name in rgw_pools_data:
                if pool_name.endswith('.data'):
                    objects = pool['stats']['objects']
                    pool_bytes_used = pool['stats']['bytes_used']
                    total_pool_bytes_used += pool_bytes_used
                    total_data_pool_objects += objects
                    replica = rgw_pools_data[pool_name]
                    total_data_pool_bytes_used += pool_bytes_used / replica

        average_object_size = total_data_pool_bytes_used / total_data_pool_objects if total_data_pool_objects != 0 else 0  # noqa E501  #pylint: disable=line-too-long

        return ClusterCapacity(
            total_avail_bytes=df['stats']['total_avail_bytes'],
            total_bytes=df['stats']['total_bytes'],
            total_used_raw_bytes=df['stats']['total_used_raw_bytes'],
            total_objects=total_data_pool_objects,
            total_pool_bytes_used=total_pool_bytes_used,
            average_object_size=average_object_size
        )._asdict()

    @classmethod
    def get_rgw_pools(cls):
        rgw_pool_size = {}

        osd_map = mgr.get('osd_map')
        for pool in osd_map['pools']:
            if 'rgw' in pool.get('application_metadata', {}):
                name = pool['pool_name']
                rgw_pool_size[name] = pool['size']
        return rgw_pool_size