Move ceilometer-send-counter to a console script

Change-Id: I504f999a04da0e58da5e60201325e062e1c7d9c5
Blueprint: remove-counter
This commit is contained in:
Julien Danjou 2014-01-30 11:20:57 +01:00
parent 693204a37e
commit e5dc59609d
4 changed files with 109 additions and 107 deletions

View File

@ -1,101 +0,0 @@
#!/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 ceilometer.openstack.common import context
from ceilometer.openstack.common import timeutils
from ceilometer import pipeline
from ceilometer import sample
from ceilometer import service
from ceilometer import transformer
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()
# 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',
),
)
with pipeline_manager.publisher(context.get_admin_context()) as p:
p([sample.Sample(
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))])

102
ceilometer/cli.py Normal file
View File

@ -0,0 +1,102 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright © 2012-2014 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 meter for Ceilometer.
"""
import logging
import sys
from oslo.config import cfg
from ceilometer.openstack.common import context
from ceilometer.openstack.common import timeutils
from ceilometer import pipeline
from ceilometer import sample
from ceilometer import service
from ceilometer import transformer
def send_sample():
cfg.CONF.register_cli_opts([
cfg.StrOpt('sample-name',
short='n',
help='meter name',
required=True),
cfg.StrOpt('sample-type',
short='y',
help='meter type (gauge, delta, cumulative)',
default='gauge',
required=True),
cfg.StrOpt('sample-unit',
short='U',
help='meter unit',
default=None),
cfg.IntOpt('sample-volume',
short='l',
help='meter volume value',
default=1),
cfg.StrOpt('sample-resource',
short='r',
help='meter resource id',
required=True),
cfg.StrOpt('sample-user',
short='u',
help='meter user id'),
cfg.StrOpt('sample-project',
short='p',
help='meter project id'),
cfg.StrOpt('sample-timestamp',
short='i',
help='meter timestamp',
default=timeutils.utcnow().isoformat()),
cfg.StrOpt('sample-metadata',
short='m',
help='meter metadata'),
])
service.prepare_service()
# 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',
),
)
with pipeline_manager.publisher(context.get_admin_context()) as p:
p([sample.Sample(
name=cfg.CONF.sample_name,
type=cfg.CONF.sample_type,
unit=cfg.CONF.sample_unit,
volume=cfg.CONF.sample_volume,
user_id=cfg.CONF.sample_user,
project_id=cfg.CONF.sample_project,
resource_id=cfg.CONF.sample_resource,
timestamp=cfg.CONF.sample_timestamp,
resource_metadata=cfg.CONF.sample_metadata and eval(
cfg.CONF.sample_metadata))])

View File

@ -73,9 +73,9 @@ class BinTestCase(base.BaseTestCase):
self.assertIn("Dropping data with TTL 1", err)
class BinSendCounterTestCase(base.BaseTestCase):
class BinSendSampleTestCase(base.BaseTestCase):
def setUp(self):
super(BinSendCounterTestCase, self).setUp()
super(BinSendSampleTestCase, self).setUp()
pipeline_cfg_file = self.path_get('etc/ceilometer/pipeline.yaml')
content = "[DEFAULT]\n"\
"rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n"\
@ -86,14 +86,14 @@ class BinSendCounterTestCase(base.BaseTestCase):
suffix='.conf')
def tearDown(self):
super(BinSendCounterTestCase, self).tearDown()
super(BinSendSampleTestCase, self).tearDown()
os.remove(self.tempfile)
def test_send_counter_run(self):
subp = subprocess.Popen([self.path_get('bin/ceilometer-send-counter'),
subp = subprocess.Popen(['ceilometer-send-sample',
"--config-file=%s" % self.tempfile,
"--counter-resource=someuuid",
"--counter-name=mycounter"])
"--sample-resource=someuuid",
"--sample-name=mycounter"])
self.assertEqual(subp.wait(), 0)

View File

@ -136,6 +136,7 @@ console_scripts =
ceilometer-agent-central = ceilometer.central.manager:agent_central
ceilometer-agent-compute = ceilometer.compute.manager:agent_compute
ceilometer-agent-notification = ceilometer.notification:agent
ceilometer-send-sample = ceilometer.cli:send_sample
ceilometer-dbsync = ceilometer.storage:dbsync
ceilometer-expirer = ceilometer.storage:expirer
ceilometer-collector = ceilometer.collector:collector