summaryrefslogtreecommitdiff
path: root/ceilometer/volume/cinder.py
blob: 06d5fca052aeab8202ce335e5f0b07012c090dc0 (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
#
# 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 volumes
"""

from ceilometer.polling import plugin_base
from ceilometer import sample


class _Base(plugin_base.PollsterBase):
    def extract_metadata(self, obj):
        metadata = dict((k, getattr(obj, k)) for k in self.FIELDS)
        if getattr(obj, "volume_image_metadata", None):
            metadata["image_id"] = obj.volume_image_metadata.get("image_id")
        else:
            metadata["image_id"] = None
        if getattr(obj, "attachments", None):
            metadata["instance_id"] = obj.attachments[0]["server_id"]
        else:
            metadata["instance_id"] = None
        return metadata


class VolumeSizePollster(_Base):
    @property
    def default_discovery(self):
        return 'volumes'

    FIELDS = ['name',
              'status',
              'volume_type',
              'availability_zone',
              'os-vol-host-attr:host',
              'migration_status',
              'attachments',
              'snapshot_id',
              'source_volid']

    def get_samples(self, manager, cache, resources):
        for volume in resources:
            yield sample.Sample(
                name='volume.size',
                type=sample.TYPE_GAUGE,
                unit='GB',
                volume=volume.size,
                user_id=volume.user_id,
                project_id=getattr(volume,
                                   'os-vol-tenant-attr:tenant_id'),
                resource_id=volume.id,
                resource_metadata=self.extract_metadata(volume),
            )


class VolumeSnapshotSize(_Base):
    @property
    def default_discovery(self):
        return 'volume_snapshots'

    FIELDS = ['name',
              'volume_id',
              'status',
              'description',
              'metadata',
              'os-extended-snapshot-attributes:progress',
              ]

    def get_samples(self, manager, cache, resources):
        for snapshot in resources:
            yield sample.Sample(
                name='volume.snapshot.size',
                type=sample.TYPE_GAUGE,
                unit='GB',
                volume=snapshot.size,
                user_id=snapshot.user_id,
                project_id=getattr(
                    snapshot,
                    'os-extended-snapshot-attributes:project_id'),
                resource_id=snapshot.id,
                resource_metadata=self.extract_metadata(snapshot),
            )


class VolumeBackupSize(_Base):
    @property
    def default_discovery(self):
        return 'volume_backups'

    FIELDS = ['name',
              'object_count',
              'container',
              'volume_id',
              'status',
              'description']

    def get_samples(self, manager, cache, resources):
        for backup in resources:
            yield sample.Sample(
                name='volume.backup.size',
                type=sample.TYPE_GAUGE,
                unit='GB',
                volume=backup.size,
                user_id=None,
                project_id=getattr(
                    backup, 'os-backup-project-attr:project_id', None),
                resource_id=backup.id,
                resource_metadata=self.extract_metadata(backup),
            )