summaryrefslogtreecommitdiff
path: root/bin/ceilometer-send-counter
blob: 1d46932fd96800b084b38f531fc0e5c93240cc0d (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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright © 2012-2013 Julien Danjou
#
# 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.

"""Command line tool for creating counter for Ceilometer.
"""

import logging
import sys

from oslo.config import cfg
from stevedore import dispatch

from ceilometer.openstack.common import gettextutils
gettextutils.install('ceilometer')

from ceilometer import counter
from ceilometer import pipeline
from ceilometer import publisher
from ceilometer import service
from ceilometer import transformer
from ceilometer.openstack.common import timeutils
from ceilometer.openstack.common import context


cfg.CONF.register_cli_opts([
    cfg.StrOpt('counter-name',
               short='n',
               help='counter name',
               required=True),
    cfg.StrOpt('counter-type',
               short='y',
               help='counter type (gauge, delta, cumulative)',
               default='gauge',
               required=True),
    cfg.StrOpt('counter-unit',
               short='U',
               help='counter unit',
               default=None),
    cfg.IntOpt('counter-volume',
               short='l',
               help='counter volume value',
               default=1),
    cfg.StrOpt('counter-resource',
               short='r',
               help='counter resource id',
               required=True),
    cfg.StrOpt('counter-user',
               short='u',
               help='counter user id'),
    cfg.StrOpt('counter-project',
               short='p',
               help='counter project id'),
    cfg.StrOpt('counter-timestamp',
               short='i',
               help='counter timestamp',
               default=timeutils.utcnow().isoformat()),
    cfg.StrOpt('counter-metadata',
               short='m',
               help='counter metadata'),
])

service.prepare_service(sys.argv)

# Set up logging to use the console
console = logging.StreamHandler(sys.stderr)
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s')
console.setFormatter(formatter)
root_logger = logging.getLogger('')
root_logger.addHandler(console)
root_logger.setLevel(logging.DEBUG)

pipeline_manager = pipeline.setup_pipeline(
    transformer.TransformerExtensionManager(
        'ceilometer.transformer',
    ),
    publisher.PublisherExtensionManager(
        'ceilometer.publisher',
    ),
)

with pipeline_manager.publisher(context.get_admin_context(),
                                cfg.CONF.counter_source) as p:
    p([counter.Counter(
        name=cfg.CONF.counter_name,
        type=cfg.CONF.counter_type,
        unit=cfg.CONF.counter_unit,
        volume=cfg.CONF.counter_volume,
        user_id=cfg.CONF.counter_user,
        project_id=cfg.CONF.counter_project,
        resource_id=cfg.CONF.counter_resource,
        timestamp=cfg.CONF.counter_timestamp,
        resource_metadata=cfg.CONF.counter_metadata and eval(
            cfg.CONF.counter_metadata))])