def setUp(self): super(TestListProjects, self).setUp() counter1 = counter.Counter( "instance", "cumulative", "instance", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.counter"}, ) msg = meter.meter_message_from_counter(counter1, cfg.CONF.metering_secret, "test_list_projects") self.conn.record_metering_data(msg) counter2 = counter.Counter( "instance", "cumulative", "instance", 1, "user-id2", "project-id2", "resource-id-alternate", timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={"display_name": "test-server", "tag": "self.counter2"}, ) msg2 = meter.meter_message_from_counter(counter2, cfg.CONF.metering_secret, "test_list_users") self.conn.record_metering_data(msg2)
def prepare_data(self): c = counter.Counter( 'dummyBigCounter', counter.TYPE_CUMULATIVE, unit='', volume=3372036854775807, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={} ) msg = meter.meter_message_from_counter( c, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(msg) c = counter.Counter( 'dummySmallCounter', counter.TYPE_CUMULATIVE, unit='', volume=-3372036854775807, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={} ) msg = meter.meter_message_from_counter( c, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(msg) c = counter.Counter( 'floatCounter', counter.TYPE_CUMULATIVE, unit='', volume=1938495037.53697, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={} ) msg = meter.meter_message_from_counter( c, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(msg)
def prepare_data(self): c = counter.Counter('dummyBigCounter', counter.TYPE_CUMULATIVE, unit='', volume=3372036854775807, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={}) msg = meter.meter_message_from_counter( c, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(msg) c = counter.Counter('dummySmallCounter', counter.TYPE_CUMULATIVE, unit='', volume=-3372036854775807, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={}) msg = meter.meter_message_from_counter( c, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(msg) c = counter.Counter('floatCounter', counter.TYPE_CUMULATIVE, unit='', volume=1938495037.53697, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={}) msg = meter.meter_message_from_counter( c, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(msg)
def test_with_project(self): counter1 = counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }) msg = meter.meter_message_from_counter( counter1, cfg.CONF.metering_secret, 'test_list_resources', ) self.conn.record_metering_data(msg) counter2 = counter.Counter('instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) msg2 = meter.meter_message_from_counter( counter2, cfg.CONF.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources', q=[{ 'field': 'project_id', 'value': 'project-id', }]) ids = [r['resource_id'] for r in data] self.assertEquals(['resource-id'], ids)
def test_metadata(self): counter1 = counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', 'ignored_dict': { 'key': 'value' }, 'ignored_list': ['not-returned'], }) msg = meter.meter_message_from_counter( counter1, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg) data = self.get_json('/resources') metadata = data[0]['metadata'] self.assertEqual(list(sorted(metadata.iteritems())), [ ('display_name', 'test-server'), ('tag', 'self.counter'), ])
def test_metadata(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', 'ignored_dict': {'key': 'value'}, 'ignored_list': ['not-returned'], } ) msg = meter.meter_message_from_counter(counter1, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg) data = self.get_json('/resources') metadata = data[0]['metadata'] self.assertEqual( list(sorted(metadata.iteritems())), [('display_name', 'test-server'), ('tag', 'self.counter'), ])
def publish_counters(self, context, counters, source): """Send a metering message for publishing :param context: Execution context from the service or RPC call :param counter: Counter from pipeline after transformation :param source: counter source """ meters = [ meter_api.meter_message_from_counter(counter, cfg.CONF.metering_secret, source) for counter in counters ] topic = cfg.CONF.metering_topic msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': {'data': meters}, } LOG.debug('PUBLISH: %s', str(msg)) rpc.cast(context, topic, msg) for meter_name, meter_list in itertools.groupby( sorted(meters, key=lambda m: m['counter_name']), lambda m: m['counter_name']): msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': {'data': list(meter_list)}, } rpc.cast(context, topic + '.' + meter_name, msg)
def setUp(self): super(TestMaxProjectVolume, self).setUp() require_map_reduce(self.conn) self.counters = [] for i in range(3): c = counter.Counter( 'volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id-%s' % i, timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = meter.meter_message_from_counter(c, cfg.CONF.metering_secret, 'source1', ) self.conn.record_metering_data(msg)
def setUp(self): super(TestMaxProjectVolume, self).setUp() require_map_reduce(self.conn) self.counters = [] for i in range(3): c = counter.Counter('volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id-%s' % i, timestamp=datetime.datetime( 2012, 9, 25, 10 + i, 30 + i), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.counter', }) self.counters.append(c) msg = meter.meter_message_from_counter( c, cfg.CONF.metering_secret, 'source1', ) self.conn.record_metering_data(msg)
def setUp(self): super(TestListEvents, self).setUp() self.counter1 = counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', 'ignored_dict': { 'key': 'value' }, 'ignored_list': ['not-returned'], }) msg = meter.meter_message_from_counter( self.counter1, cfg.CONF.metering_secret, 'test_source', ) self.conn.record_metering_data(msg) self.counter2 = counter.Counter('instance', 'cumulative', '', 1, 'user-id2', 'project2', 'resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) msg2 = meter.meter_message_from_counter( self.counter2, cfg.CONF.metering_secret, 'source2', ) self.conn.record_metering_data(msg2)
def prepare_data(self): self.counters = [] for i in range(3): c = counter.Counter('volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime( 2012, 9, 25, 10 + i, 30 + i), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.counter', }) self.counters.append(c) msg = meter.meter_message_from_counter( c, secret='not-so-secret', source='test', ) self.conn.record_metering_data(msg) for i in range(3): c = counter.Counter('volume.size', 'gauge', 'GiB', 8 + i, 'user-5', 'project2', 'resource-6', timestamp=datetime.datetime( 2012, 9, 25, 10 + i, 30 + i), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.counter', }) self.counters.append(c) msg = meter.meter_message_from_counter( c, secret='not-so-secret', source='test', ) self.conn.record_metering_data(msg)
def test_with_project(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = meter.meter_message_from_counter(counter1, cfg.CONF.metering_secret, 'test_list_resources', ) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(counter2, cfg.CONF.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources', q=[{'field': 'project_id', 'value': 'project-id', }]) ids = [r['resource_id'] for r in data] self.assertEquals(['resource-id'], ids)
def setUp(self): super(TestListResources, self).setUp() for cnt in [ counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}, ), counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2'}, ), counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter3'}, ), counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter4'} )]: msg = meter.meter_message_from_counter(cnt, cfg.CONF.metering_secret, 'test_list_resources') self.conn.record_metering_data(msg)
def test_instances(self): counter1 = counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }) msg = meter.meter_message_from_counter( counter1, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg) counter2 = counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) msg2 = meter.meter_message_from_counter( counter2, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources') self.assertEquals(2, len(data))
def setUp(self): super(TestListUsers, self).setUp() counter1 = counter.Counter('instance', 'cumulative', 'instance', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }) msg = meter.meter_message_from_counter( counter1, cfg.CONF.metering_secret, 'test_list_users', ) self.conn.record_metering_data(msg) counter2 = counter.Counter('instance', 'cumulative', '', 1, 'user-id2', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) msg2 = meter.meter_message_from_counter( counter2, cfg.CONF.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2)
def prepare_data(self): self.counters = [] for i in range(3): c = counter.Counter( 'volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = meter.meter_message_from_counter(c, secret='not-so-secret', source='test', ) self.conn.record_metering_data(msg) for i in range(3): c = counter.Counter( 'volume.size', 'gauge', 'GiB', 8 + i, 'user-5', 'project2', 'resource-6', timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = meter.meter_message_from_counter(c, secret='not-so-secret', source='test', ) self.conn.record_metering_data(msg)
def test_instances(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = meter.meter_message_from_counter(counter1, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(counter2, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources') self.assertEquals(2, len(data))
def setUp(self): super(TestListEvents, self).setUp() self.counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', 'ignored_dict': {'key': 'value'}, 'ignored_list': ['not-returned'], } ) msg = meter.meter_message_from_counter(self.counter1, cfg.CONF.metering_secret, 'test_source', ) self.conn.record_metering_data(msg) self.counter2 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(self.counter2, cfg.CONF.metering_secret, 'source2', ) self.conn.record_metering_data(msg2)
def setUp(self): super(TestListUsers, self).setUp() counter1 = counter.Counter( 'instance', 'cumulative', 'instance', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = meter.meter_message_from_counter(counter1, cfg.CONF.metering_secret, 'test_list_users', ) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(counter2, cfg.CONF.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2)
def test_meter_message_from_counter_field(): def compare(f, c, msg_f, msg): assert msg == c msg = meter.meter_message_from_counter(TEST_COUNTER, 'not-so-secret', 'src') name_map = {'name': 'counter_name', 'type': 'counter_type', 'unit': 'counter_unit', 'volume': 'counter_volume', } for f in TEST_COUNTER._fields: msg_f = name_map.get(f, f) yield compare, f, getattr(TEST_COUNTER, f), msg_f, msg[msg_f]
def setUp(self): super(TestListResources, self).setUp() for cnt in [ counter.Counter( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.counter"}, ), counter.Counter( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id-alternate", timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={"display_name": "test-server", "tag": "self.counter2"}, ), counter.Counter( "instance", "cumulative", "", 1, "user-id2", "project-id2", "resource-id2", timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={"display_name": "test-server", "tag": "self.counter3"}, ), counter.Counter( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={"display_name": "test-server", "tag": "self.counter4"}, ), ]: msg = meter.meter_message_from_counter(cnt, cfg.CONF.metering_secret, "test_list_resources") self.conn.record_metering_data(msg)
def test_meter_message_from_counter_field(): def compare(f, c, msg_f, msg): assert msg == c msg = meter.meter_message_from_counter(TEST_COUNTER, 'not-so-secret', 'src') name_map = { 'name': 'counter_name', 'type': 'counter_type', 'unit': 'counter_unit', 'volume': 'counter_volume', } for f in TEST_COUNTER._fields: msg_f = name_map.get(f, f) yield compare, f, getattr(TEST_COUNTER, f), msg_f, msg[msg_f]
def _make_events(self, *timestamps): for t in timestamps: c = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, '', 1, '11', '1', '111', timestamp=t, resource_metadata={'display_name': 'test-server', } ) msg = meter.meter_message_from_counter(c, cfg.CONF.metering_secret, 'test') self.conn.record_metering_data(msg)
def publish_counter(context, counter, topic, secret, source): """Send a metering message for the data represented by the counter. :param context: Execution context from the service or RPC call :param counter: ceilometer.counter.Counter instance :param source: counter source """ msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': {'data': meter.meter_message_from_counter(counter, secret, source), }, } LOG.debug('PUBLISH: %s', str(msg)) rpc.cast(context, topic, msg) rpc.cast(context, topic + '.' + counter.name, msg)
def setUp(self): super(TestMaxProjectVolume, self).setUp() require_map_reduce(self.conn) self.counters = [] for i in range(3): c = counter.Counter( "volume.size", "gauge", "GiB", 5 + i, "user-id", "project1", "resource-id-%s" % i, timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={"display_name": "test-volume", "tag": "self.counter"}, ) self.counters.append(c) msg = meter.meter_message_from_counter(c, cfg.CONF.metering_secret, "source1") self.conn.record_metering_data(msg)
def test_instance_no_metadata(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata=None ) msg = meter.meter_message_from_counter(counter1, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg) data = self.get_json('/resources') self.assertEquals(1, len(data))
def test_instance_no_metadata(self): counter1 = counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata=None) msg = meter.meter_message_from_counter( counter1, cfg.CONF.metering_secret, 'test', ) self.conn.record_metering_data(msg) data = self.get_json('/resources') self.assertEquals(1, len(data))
def publish_counters(self, context, counters, source): """Send a metering message for publishing :param context: Execution context from the service or RPC call :param counter: Counter from pipeline after transformation :param source: counter source """ meters = [ meter_api.meter_message_from_counter(counter, cfg.CONF.metering_secret, source) for counter in counters ] topic = cfg.CONF.metering_topic msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': { 'data': meters }, } LOG.debug('PUBLISH: %s', str(msg)) rpc.cast(context, topic, msg) for meter_name, meter_list in itertools.groupby( sorted(meters, key=lambda m: m['counter_name']), lambda m: m['counter_name']): msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': { 'data': list(meter_list) }, } rpc.cast(context, topic + '.' + meter_name, msg)
def setUp(self): super(TestListResourcesBase, self).setUp() for cnt in [ counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter' }, ), counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2' }, ), counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter3' }, ), counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 43), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter4' }) ]: msg = meter.meter_message_from_counter(cnt, cfg.CONF.metering_secret, 'test_list_resources') self.conn.record_metering_data(msg)
def main(): cfg.CONF([], project='ceilometer') parser = argparse.ArgumentParser( description='generate metering data', ) parser.add_argument( '--interval', default=10, type=int, help='the period between events, in minutes', ) parser.add_argument( '--start', default=31, help='the number of days in the past to start timestamps', ) parser.add_argument( '--end', default=2, help='the number of days into the future to continue timestamps', ) parser.add_argument( '--type', choices=('gauge', 'cumulative'), default='gauge', help='counter type', ) parser.add_argument( '--unit', default=None, help='counter unit', ) parser.add_argument( '--project', help='project id of owner', ) parser.add_argument( '--user', help='user id of owner', ) parser.add_argument( 'resource', help='the resource id for the meter data', ) parser.add_argument( 'counter', help='the counter name for the meter data', ) parser.add_argument( 'volume', help='the amount to attach to the meter', type=int, default=1, ) args = parser.parse_args() # 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) # Connect to the metering database conn = storage.get_connection(cfg.CONF) # Find the user and/or project for a real resource if not (args.user or args.project): for r in conn.get_resources(): if r['resource_id'] == args.resource: args.user = r['user_id'] args.project = r['project_id'] break # Compute start and end timestamps for the # new data. timestamp = timeutils.parse_isotime(args.start) end = timeutils.parse_isotime(args.end) increment = datetime.timedelta(minutes=args.interval) # Generate events n = 0 while timestamp <= end: c = counter.Counter(name=args.counter, type=args.type, unit=args.unit, volume=args.volume, user_id=args.user, project_id=args.project, resource_id=args.resource, timestamp=timestamp, resource_metadata={}, ) data = meter.meter_message_from_counter(c, cfg.CONF.metering_secret, 'artificial') conn.record_metering_data(data) n += 1 timestamp = timestamp + increment print 'Added %d new events' % n return 0
def prepare_data(self): self.msgs = [] self.counter = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) self.msg1 = meter.meter_message_from_counter(self.counter, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(self.msg1) self.msgs.append(self.msg1) self.counter2 = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) self.msg2 = meter.meter_message_from_counter(self.counter2, cfg.CONF.metering_secret, 'test-2', ) self.conn.record_metering_data(self.msg2) self.msgs.append(self.msg2) self.counter3 = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-alternate', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter3', } ) self.msg3 = meter.meter_message_from_counter(self.counter3, cfg.CONF.metering_secret, 'test-3', ) self.conn.record_metering_data(self.msg3) self.msgs.append(self.msg3) for i in range(2, 4): c = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-%s' % i, project_id='project-id-%s' % i, resource_id='resource-id-%s' % i, timestamp=datetime.datetime(2012, 7, 2, 10, 40 + i), resource_metadata={'display_name': 'test-server', 'tag': 'counter-%s' % i}, ) msg = meter.meter_message_from_counter(c, cfg.CONF.metering_secret, 'test') self.conn.record_metering_data(msg) self.msgs.append(msg)
def prepare_data(self): self.msgs = [] self.counter = counter.Counter('instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }) self.msg1 = meter.meter_message_from_counter( self.counter, cfg.CONF.metering_secret, 'test-1', ) self.conn.record_metering_data(self.msg1) self.msgs.append(self.msg1) self.counter2 = counter.Counter('instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) self.msg2 = meter.meter_message_from_counter( self.counter2, cfg.CONF.metering_secret, 'test-2', ) self.conn.record_metering_data(self.msg2) self.msgs.append(self.msg2) self.counter3 = counter.Counter('instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-alternate', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter3', }) self.msg3 = meter.meter_message_from_counter( self.counter3, cfg.CONF.metering_secret, 'test-3', ) self.conn.record_metering_data(self.msg3) self.msgs.append(self.msg3) for i in range(2, 4): c = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-%s' % i, project_id='project-id-%s' % i, resource_id='resource-id-%s' % i, timestamp=datetime.datetime(2012, 7, 2, 10, 40 + i), resource_metadata={ 'display_name': 'test-server', 'tag': 'counter-%s' % i }, ) msg = meter.meter_message_from_counter(c, cfg.CONF.metering_secret, 'test') self.conn.record_metering_data(msg) self.msgs.append(msg)
def prepare_data(self): def old_record_metering_data(self, data): self.db.user.update( {'_id': data['user_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) self.db.project.update( {'_id': data['project_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) received_timestamp = datetime.datetime.utcnow() self.db.resource.update( {'_id': data['resource_id']}, { '$set': { 'project_id': data['project_id'], 'user_id': data['user_id'], # Current metadata being used and when it was # last updated. 'timestamp': data['timestamp'], 'received_timestamp': received_timestamp, 'metadata': data['resource_metadata'], 'source': data['source'], }, '$addToSet': { 'meter': { 'counter_name': data['counter_name'], 'counter_type': data['counter_type'], }, }, }, upsert=True, ) record = copy.copy(data) self.db.meter.insert(record) return # Stubout with the old version DB schema, the one w/o 'counter_unit' self.stubs.Set(self.conn, 'record_metering_data', old_record_metering_data) self.counters = [] c = counter.Counter('volume.size', 'gauge', 'GiB', 5, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10, 30), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.counter', }) self.counters.append(c) msg = meter.meter_message_from_counter(c, secret='not-so-secret', source='test') self.conn.record_metering_data(self.conn, msg)
def setUp(self): super(TestListMeters, self).setUp() for cnt in [ counter.Counter( 'meter.test', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}), counter.Counter( 'meter.test', 'cumulative', '', 3, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 11, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}), counter.Counter( 'meter.mine', 'gauge', '', 1, 'user-id', 'project-id', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'two.counter'}), counter.Counter( 'meter.test', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id3', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={'display_name': 'test-server', 'tag': 'three.counter'}), counter.Counter( 'meter.mine', 'gauge', '', 1, 'user-id4', 'project-id2', 'resource-id4', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={'display_name': 'test-server', 'tag': 'four.counter'})]: msg = meter.meter_message_from_counter(cnt, cfg.CONF.metering_secret, 'test_list_resources') self.conn.record_metering_data(msg)
def setUp(self): super(TestListMeters, self).setUp() for cnt in [ counter.Counter( 'meter.test', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}), counter.Counter( 'meter.test', 'cumulative', '', 3, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 11, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}), counter.Counter( 'meter.mine', 'gauge', '', 1, 'user-id', 'project-id', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2'}), counter.Counter( 'meter.test', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id3', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter3'}), counter.Counter( 'meter.mine', 'gauge', '', 1, 'user-id4', 'project-id2', 'resource-id4', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter4'})]: msg = meter.meter_message_from_counter(cnt, cfg.CONF.metering_secret, 'test_source') self.conn.record_metering_data(msg)
def test_meter_message_from_counter_signed(): msg = meter.meter_message_from_counter(TEST_COUNTER, 'not-so-secret', 'src') assert 'message_signature' in msg
def prepare_data(self): def old_record_metering_data(self, data): self.db.user.update( {'_id': data['user_id']}, {'$addToSet': {'source': data['source'], }, }, upsert=True, ) self.db.project.update( {'_id': data['project_id']}, {'$addToSet': {'source': data['source'], }, }, upsert=True, ) received_timestamp = datetime.datetime.utcnow() self.db.resource.update( {'_id': data['resource_id']}, {'$set': {'project_id': data['project_id'], 'user_id': data['user_id'], # Current metadata being used and when it was # last updated. 'timestamp': data['timestamp'], 'received_timestamp': received_timestamp, 'metadata': data['resource_metadata'], 'source': data['source'], }, '$addToSet': {'meter': {'counter_name': data['counter_name'], 'counter_type': data['counter_type'], }, }, }, upsert=True, ) record = copy.copy(data) self.db.meter.insert(record) return # Stubout with the old version DB schema, the one w/o 'counter_unit' self.stubs.Set(self.conn, 'record_metering_data', old_record_metering_data) self.counters = [] c = counter.Counter( 'volume.size', 'gauge', 'GiB', 5, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10, 30), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = meter.meter_message_from_counter(c, secret='not-so-secret', source='test') self.conn.record_metering_data(self.conn, msg)
def main(): cfg.CONF([], project='ceilometer') parser = argparse.ArgumentParser(description='generate metering data', ) parser.add_argument( '--interval', default=10, type=int, help='the period between events, in minutes', ) parser.add_argument( '--start', default=31, help='the number of days in the past to start timestamps', ) parser.add_argument( '--end', default=2, help='the number of days into the future to continue timestamps', ) parser.add_argument( '--type', choices=('gauge', 'cumulative'), default='gauge', help='counter type', ) parser.add_argument( '--unit', default=None, help='counter unit', ) parser.add_argument( '--project', help='project id of owner', ) parser.add_argument( '--user', help='user id of owner', ) parser.add_argument( 'resource', help='the resource id for the meter data', ) parser.add_argument( 'counter', help='the counter name for the meter data', ) parser.add_argument( 'volume', help='the amount to attach to the meter', type=int, default=1, ) args = parser.parse_args() # 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) # Connect to the metering database conn = storage.get_connection(cfg.CONF) # Find the user and/or project for a real resource if not (args.user or args.project): for r in conn.get_resources(): if r['resource_id'] == args.resource: args.user = r['user_id'] args.project = r['project_id'] break # Compute start and end timestamps for the # new data. timestamp = timeutils.parse_isotime(args.start) end = timeutils.parse_isotime(args.end) increment = datetime.timedelta(minutes=args.interval) # Generate events n = 0 while timestamp <= end: c = counter.Counter( name=args.counter, type=args.type, unit=args.unit, volume=args.volume, user_id=args.user, project_id=args.project, resource_id=args.resource, timestamp=timestamp, resource_metadata={}, ) data = meter.meter_message_from_counter(c, cfg.CONF.metering_secret, 'artificial') conn.record_metering_data(data) n += 1 timestamp = timestamp + increment print 'Added %d new events' % n return 0