summaryrefslogtreecommitdiff
path: root/ceilometer/ipmi/notifications/ironic.py
blob: f33571469ea04c3d5c12cb0d6b085d73906367cb (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
#
# Copyright 2014 Red Hat
#
# Author: Chris Dent <chdent@redhat.com>
#
# 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.
"""Converters for producing hardware sensor data sample messages from
notification events.
"""

from oslo.config import cfg
from oslo import messaging

from ceilometer.openstack.common import log
from ceilometer import plugin
from ceilometer import sample

LOG = log.getLogger(__name__)

OPTS = [
    cfg.StrOpt('ironic_exchange',
               default='ironic',
               help='Exchange name for Ironic notifications.'),
]


cfg.CONF.register_opts(OPTS)


# Map unit name to SI
UNIT_MAP = {
    'Watts': 'W',
    'Volts': 'V',
}


def validate_reading(data):
    """Some sensors read "Disabled"."""
    return data != 'Disabled'


def transform_id(data):
    return data.lower().replace(' ', '_')


def parse_reading(data):
    try:
        volume, unit = data.split(' ', 1)
        unit = unit.rsplit(' ', 1)[-1]
        return float(volume), UNIT_MAP.get(unit, unit)
    except ValueError:
        raise InvalidSensorData('unable to parse sensor reading: %s' %
                                data)


class InvalidSensorData(ValueError):
    pass


class SensorNotification(plugin.NotificationBase):
    """A generic class for extracting samples from sensor data notifications.

    A notification message can contain multiple samples from multiple
    sensors, all with the same basic structure: the volume for the sample
    is found as part of the value of a 'Sensor Reading' key. The unit
    is in the same value.

    Subclasses exist solely to allow flexibility with stevedore configuration.
    """

    event_types = ['hardware.ipmi.*']
    metric = None

    @staticmethod
    def get_targets(conf):
        """oslo.messaging.TargetS for this plugin."""
        return [messaging.Target(topic=topic,
                                 exchange=conf.ironic_exchange)
                for topic in conf.notification_topics]

    def _get_sample(self, message):
        try:
            return (payload for _, payload
                    in message['payload'][self.metric].items())
        except KeyError:
            return []

    def _package_payload(self, message, payload):
        # NOTE(chdent): How much of the payload should we keep?
        payload['node'] = message['payload']['node_uuid']
        info = {'publisher_id': message['publisher_id'],
                'timestamp': message['payload']['timestamp'],
                'event_type': message['payload']['event_type'],
                'user_id': message['payload'].get('user_id'),
                'project_id': message['payload'].get('project_id'),
                'payload': payload}
        return info

    def process_notification(self, message):
        """Read and process a notification.

        The guts of a message are in dict value of a 'payload' key
        which then itself has a payload key containing a dict of
        multiple sensor readings.

        If expected keys in the payload are missing or values
        are not in the expected form for transformations,
        KeyError and ValueError are caught and the current
        sensor payload is skipped.
        """
        payloads = self._get_sample(message['payload'])
        for payload in payloads:
            try:
                # Provide a fallback resource_id in case parts are missing.
                resource_id = 'missing id'
                try:
                    resource_id = '%(nodeid)s-%(sensorid)s' % {
                        'nodeid': message['payload']['node_uuid'],
                        'sensorid': transform_id(payload['Sensor ID'])
                    }
                except KeyError as exc:
                    raise InvalidSensorData('missing key in payload: %s' % exc)

                info = self._package_payload(message, payload)

                try:
                    sensor_reading = info['payload']['Sensor Reading']
                except KeyError as exc:
                    raise InvalidSensorData(
                        "missing 'Sensor Reading' in payload"
                    )

                if validate_reading(sensor_reading):
                    volume, unit = parse_reading(sensor_reading)
                    yield sample.Sample.from_notification(
                        name='hardware.ipmi.%s' % self.metric.lower(),
                        type=sample.TYPE_GAUGE,
                        unit=unit,
                        volume=volume,
                        resource_id=resource_id,
                        message=info,
                        user_id=info['user_id'],
                        project_id=info['project_id'])

            except InvalidSensorData as exc:
                LOG.warn(
                    'invalid sensor data for %(resource)s: %(error)s' %
                    dict(resource=resource_id, error=exc)
                )
                continue


class TemperatureSensorNotification(SensorNotification):
    metric = 'Temperature'


class CurrentSensorNotification(SensorNotification):
    metric = 'Current'


class FanSensorNotification(SensorNotification):
    metric = 'Fan'


class VoltageSensorNotification(SensorNotification):
    metric = 'Voltage'