summaryrefslogtreecommitdiff
path: root/ceilometer/image/glance.py
blob: 0d2d619b94b088a335bfb5805bff142770f3a19f (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
#
# Copyright 2012 New Dream Network, LLC (DreamHost)
#
# Author: Julien Danjou <julien@danjou.info>
#
# 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 images
"""

from __future__ import absolute_import
import itertools

import glanceclient
from oslo.config import cfg
from oslo.utils import timeutils

from ceilometer.central import plugin
from ceilometer import sample


OPTS = [
    cfg.IntOpt('glance_page_size',
               default=0,
               help="Number of items to request in "
                    "each paginated Glance API request "
                    "(parameter used by glancecelient). "
                    "If this is less than or equal to 0, "
                    "page size is not specified "
                    "(default value in glanceclient is used)."),
]

cfg.CONF.register_opts(OPTS)


class _Base(plugin.CentralPollster):

    @staticmethod
    def get_glance_client(ksclient):
        endpoint = ksclient.service_catalog.url_for(
            service_type='image',
            endpoint_type=cfg.CONF.service_credentials.os_endpoint_type)

        # hard-code v1 glance API version selection while v2 API matures
        service_credentials = cfg.CONF.service_credentials
        return glanceclient.Client('1', endpoint,
                                   token=ksclient.auth_token,
                                   cacert=service_credentials.os_cacert,
                                   insecure=service_credentials.insecure)

    def _get_images(self, ksclient):
        client = self.get_glance_client(ksclient)
        page_size = cfg.CONF.glance_page_size
        kwargs = {}
        if page_size > 0:
            kwargs['page_size'] = page_size
        rawImageList = list(itertools.chain(
            client.images.list(filters={"is_public": True}, **kwargs),
            # TODO(eglynn): extend glance API with all_tenants logic to
            #              avoid second call to retrieve private images
            client.images.list(filters={"is_public": False}, **kwargs)))

        # When retrieving images from glance, glance will check
        # whether the user is of 'admin_role' which is
        # configured in glance-api.conf. If the user is of
        # admin_role, and is querying public images(which means
        # that the 'is_public' param is set to be True),
        # glance will ignore 'is_public' parameter and returns
        # all the public images together with private images.
        # As a result, if the user/tenant has an admin role
        # for ceilometer to collect image list,
        # the _Base.iter_images method will return an image list
        # which contains duplicate images. Add the following
        # code to avoid recording down duplicate image events.
        imageIdSet = set(image.id for image in rawImageList)

        for image in rawImageList:
            if image.id in imageIdSet:
                imageIdSet -= set([image.id])
                yield image

    def _iter_images(self, ksclient, cache):
        """Iterate over all images."""
        if 'images' not in cache:
            cache['images'] = list(self._get_images(ksclient))
        return iter(cache['images'])

    @staticmethod
    def extract_image_metadata(image):
        return dict((k, getattr(image, k))
                    for k in
                    [
                        "status",
                        "is_public",
                        "name",
                        "deleted",
                        "container_format",
                        "created_at",
                        "disk_format",
                        "updated_at",
                        "properties",
                        "min_disk",
                        "protected",
                        "checksum",
                        "deleted_at",
                        "min_ram",
                        "size", ])


class ImagePollster(_Base):
    @plugin.check_keystone
    def get_samples(self, manager, cache, resources=None):
        for image in self._iter_images(manager.keystone, cache):
            yield sample.Sample(
                name='image',
                type=sample.TYPE_GAUGE,
                unit='image',
                volume=1,
                user_id=None,
                project_id=image.owner,
                resource_id=image.id,
                timestamp=timeutils.isotime(),
                resource_metadata=self.extract_image_metadata(image),
            )


class ImageSizePollster(_Base):
    @plugin.check_keystone
    def get_samples(self, manager, cache, resources=None):
        for image in self._iter_images(manager.keystone, cache):
            yield sample.Sample(
                name='image.size',
                type=sample.TYPE_GAUGE,
                unit='B',
                volume=image.size,
                user_id=None,
                project_id=image.owner,
                resource_id=image.id,
                timestamp=timeutils.isotime(),
                resource_metadata=self.extract_image_metadata(image),
            )