summaryrefslogtreecommitdiff
path: root/ceilometer/publisher/zaqar.py
blob: 1f00e9ff09b94ebdf04aebf509c311bf65bc7ed9 (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
#
# 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.

from urllib import parse as urlparse

from ceilometer import keystone_client
from ceilometer import publisher

from zaqarclient.queues.v2 import client as zaqarclient

DEFAULT_TTL = 3600


class ZaqarPublisher(publisher.ConfigPublisherBase):
    """Publish metering data to a Zaqar queue.

    The target queue name must be configured in the ceilometer pipeline
    configuration file. The TTL can also optionally be specified as a query
    argument::

        meter:
            - name: meter_zaqar
            meters:
                - "*"
            sinks:
                - zaqar_sink
        sinks:
            - name: zaqar_sink
            publishers:
                - zaqar://?queue=meter_queue&ttl=1200

    The credentials to access Zaqar must be set in the [zaqar] section in the
    configuration.
    """
    def __init__(self, conf, parsed_url):
        super(ZaqarPublisher, self).__init__(conf, parsed_url)
        options = urlparse.parse_qs(parsed_url.query)
        self.queue_name = options.get('queue', [None])[0]
        if not self.queue_name:
            raise ValueError('Must specify a queue in the zaqar publisher')
        self.ttl = int(options.pop('ttl', [DEFAULT_TTL])[0])
        self._client = None

    @property
    def client(self):
        if self._client is None:
            session = keystone_client.get_session(
                self.conf, group=self.conf.zaqar.auth_section)
            self._client = zaqarclient.Client(session=session)
        return self._client

    def publish_samples(self, samples):
        """Send a metering message for publishing

        :param samples: Samples from pipeline.
        """
        queue = self.client.queue(self.queue_name)
        messages = [{'body': sample.as_dict(), 'ttl': self.ttl}
                    for sample in samples]
        queue.post(messages)

    def publish_events(self, events):
        """Send an event message for publishing

        :param events: events from pipeline.
        """
        queue = self.client.queue(self.queue_name)
        messages = [{'body': event.serialize(), 'ttl': self.ttl}
                    for event in events]
        queue.post(messages)