summaryrefslogtreecommitdiff
path: root/ceilometer/objectstore/rgw.py
blob: 2c5cdcb51cc44676a3838d9b7f158fedab3fea28 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# Copyright 2015 Reliance Jio Infocomm Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Common code for working with ceph object stores
"""

from keystoneauth1 import exceptions
from oslo_config import cfg
from oslo_log import log
from urllib import parse as urlparse

from ceilometer import keystone_client
from ceilometer.polling import plugin_base
from ceilometer import sample

LOG = log.getLogger(__name__)

SERVICE_OPTS = [
    cfg.StrOpt('radosgw',
               help='Radosgw service type.'),
]

CREDENTIAL_OPTS = [
    cfg.StrOpt('access_key',
               secret=True,
               help='Access key for Radosgw Admin.'),
    cfg.StrOpt('secret_key',
               secret=True,
               help='Secret key for Radosgw Admin.')
]

CLIENT_OPTS = [
    cfg.BoolOpt('implicit_tenants',
                default=False,
                help='Whether RGW uses implicit tenants or not.'),
]


class _Base(plugin_base.PollsterBase):
    METHOD = 'bucket'
    _ENDPOINT = None

    def __init__(self, conf):
        super(_Base, self).__init__(conf)
        self.access_key = self.conf.rgw_admin_credentials.access_key
        self.secret = self.conf.rgw_admin_credentials.secret_key
        self.implicit_tenants = self.conf.rgw_client.implicit_tenants

    @property
    def default_discovery(self):
        return 'tenant'

    @property
    def CACHE_KEY_METHOD(self):
        return 'rgw.get_%s' % self.METHOD

    @staticmethod
    def _get_endpoint(conf, ksclient):
        # we store the endpoint as a base class attribute, so keystone is
        # only ever called once, also we assume that in a single deployment
        # we may be only deploying `radosgw` or `swift` as the object-store
        if _Base._ENDPOINT is None and conf.service_types.radosgw:
            try:
                creds = conf.service_credentials
                rgw_url = keystone_client.get_service_catalog(
                    ksclient).url_for(
                        service_type=conf.service_types.radosgw,
                        interface=creds.interface,
                        region_name=creds.region_name)
                _Base._ENDPOINT = urlparse.urljoin(rgw_url, '/admin')
            except exceptions.EndpointNotFound:
                LOG.debug("Radosgw endpoint not found")
        return _Base._ENDPOINT

    def _iter_accounts(self, ksclient, cache, tenants):
        if self.CACHE_KEY_METHOD not in cache:
            cache[self.CACHE_KEY_METHOD] = list(self._get_account_info(
                ksclient, tenants))
        return iter(cache[self.CACHE_KEY_METHOD])

    def _get_account_info(self, ksclient, tenants):
        endpoint = self._get_endpoint(self.conf, ksclient)
        if not endpoint:
            return

        try:
            from ceilometer.objectstore import rgw_client as c_rgw_client
            rgw_client = c_rgw_client.RGWAdminClient(endpoint,
                                                     self.access_key,
                                                     self.secret,
                                                     self.implicit_tenants)
        except ImportError:
            raise plugin_base.PollsterPermanentError(tenants)

        for t in tenants:
            api_method = 'get_%s' % self.METHOD
            yield t.id, getattr(rgw_client, api_method)(t.id)


class ContainersObjectsPollster(_Base):
    """Get info about object counts in a container using RGW Admin APIs."""

    def get_samples(self, manager, cache, resources):
        for tenant, bucket_info in self._iter_accounts(manager.keystone,
                                                       cache, resources):
            for it in bucket_info['buckets']:
                yield sample.Sample(
                    name='radosgw.containers.objects',
                    type=sample.TYPE_GAUGE,
                    volume=int(it.num_objects),
                    unit='object',
                    user_id=None,
                    project_id=tenant,
                    resource_id=tenant + '/' + it.name,
                    resource_metadata=None,
                )


class ContainersSizePollster(_Base):
    """Get info about object sizes in a container using RGW Admin APIs."""

    def get_samples(self, manager, cache, resources):
        for tenant, bucket_info in self._iter_accounts(manager.keystone,
                                                       cache, resources):
            for it in bucket_info['buckets']:
                yield sample.Sample(
                    name='radosgw.containers.objects.size',
                    type=sample.TYPE_GAUGE,
                    volume=int(it.size * 1024),
                    unit='B',
                    user_id=None,
                    project_id=tenant,
                    resource_id=tenant + '/' + it.name,
                    resource_metadata=None,
                )


class ObjectsSizePollster(_Base):
    """Iterate over all accounts, using keystone."""

    def get_samples(self, manager, cache, resources):
        for tenant, bucket_info in self._iter_accounts(manager.keystone,
                                                       cache, resources):
            yield sample.Sample(
                name='radosgw.objects.size',
                type=sample.TYPE_GAUGE,
                volume=int(bucket_info['size'] * 1024),
                unit='B',
                user_id=None,
                project_id=tenant,
                resource_id=tenant,
                resource_metadata=None,
                )


class ObjectsPollster(_Base):
    """Iterate over all accounts, using keystone."""

    def get_samples(self, manager, cache, resources):
        for tenant, bucket_info in self._iter_accounts(manager.keystone,
                                                       cache, resources):
            yield sample.Sample(
                name='radosgw.objects',
                type=sample.TYPE_GAUGE,
                volume=int(bucket_info['num_objects']),
                unit='object',
                user_id=None,
                project_id=tenant,
                resource_id=tenant,
                resource_metadata=None,
                )


class ObjectsContainersPollster(_Base):
    def get_samples(self, manager, cache, resources):
        for tenant, bucket_info in self._iter_accounts(manager.keystone,
                                                       cache, resources):
            yield sample.Sample(
                name='radosgw.objects.containers',
                type=sample.TYPE_GAUGE,
                volume=int(bucket_info['num_buckets']),
                unit='object',
                user_id=None,
                project_id=tenant,
                resource_id=tenant,
                resource_metadata=None,
                )


class UsagePollster(_Base):

    METHOD = 'usage'

    def get_samples(self, manager, cache, resources):
        for tenant, usage in self._iter_accounts(manager.keystone,
                                                 cache, resources):
            yield sample.Sample(
                name='radosgw.api.request',
                type=sample.TYPE_GAUGE,
                volume=int(usage),
                unit='request',
                user_id=None,
                project_id=tenant,
                resource_id=tenant,
                resource_metadata=None,
                )