]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/services/cluster.py
9caaf1963366b978b5ed7e3236500c8363e559d9
[ceph.git] / ceph / src / pybind / mgr / dashboard / services / cluster.py
1 # -*- coding: utf-8 -*-
2 from enum import Enum
3 from typing import NamedTuple
4
5 from .. import mgr
6
7
8 class ClusterCapacity(NamedTuple):
9 total_avail_bytes: int
10 total_bytes: int
11 total_used_raw_bytes: int
12 total_objects: int
13 total_pool_bytes_used: int
14 average_object_size: int
15
16
17 class ClusterModel:
18
19 class Status(Enum):
20 INSTALLED = 0
21 POST_INSTALLED = 1
22
23 status: Status
24
25 def __init__(self, status=Status.POST_INSTALLED.name):
26 """
27 :param status: The status of the cluster. Assume that the cluster
28 is already functional by default.
29 :type status: str
30 """
31 self.status = self.Status[status]
32
33 def dict(self):
34 return {'status': self.status.name}
35
36 def to_db(self):
37 mgr.set_store('cluster/status', self.status.name)
38
39 @classmethod
40 def from_db(cls):
41 """
42 Get the stored cluster status from the configuration key/value store.
43 If the status is not set, assume it is already fully functional.
44 """
45 return cls(status=mgr.get_store('cluster/status', cls.Status.POST_INSTALLED.name))
46
47 @classmethod
48 def get_capacity(cls) -> ClusterCapacity:
49 df = mgr.get('df')
50 total_pool_bytes_used = 0
51 average_object_size = 0
52 total_data_pool_objects = 0
53 total_data_pool_bytes_used = 0
54 rgw_pools_data = cls.get_rgw_pools()
55
56 for pool in df['pools']:
57 pool_name = str(pool['name'])
58 if pool_name in rgw_pools_data:
59 if pool_name.endswith('.data'):
60 objects = pool['stats']['objects']
61 pool_bytes_used = pool['stats']['bytes_used']
62 total_pool_bytes_used += pool_bytes_used
63 total_data_pool_objects += objects
64 replica = rgw_pools_data[pool_name]
65 total_data_pool_bytes_used += pool_bytes_used / replica
66
67 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
68
69 return ClusterCapacity(
70 total_avail_bytes=df['stats']['total_avail_bytes'],
71 total_bytes=df['stats']['total_bytes'],
72 total_used_raw_bytes=df['stats']['total_used_raw_bytes'],
73 total_objects=total_data_pool_objects,
74 total_pool_bytes_used=total_pool_bytes_used,
75 average_object_size=average_object_size
76 )._asdict()
77
78 @classmethod
79 def get_rgw_pools(cls):
80 rgw_pool_size = {}
81
82 osd_map = mgr.get('osd_map')
83 for pool in osd_map['pools']:
84 if 'rgw' in pool.get('application_metadata', {}):
85 name = pool['pool_name']
86 rgw_pool_size[name] = pool['size']
87 return rgw_pool_size