def setUp(self): super(TestListEvents, self).setUp() self.counter1 = counter.Counter('source1', 'instance', 'cumulative', 1, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), duration=0, resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }) msg = meter.meter_message_from_counter(self.counter1) self.conn.record_metering_data(msg) self.counter2 = counter.Counter('source2', 'instance', 'cumulative', 1, 'user-id2', 'project2', 'resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), duration=0, resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) msg2 = meter.meter_message_from_counter(self.counter2) self.conn.record_metering_data(msg2)
def setUp(self): super(TestListEvents, self).setUp() self.counter1 = counter.Counter( 'source1', 'instance', 'cumulative', 1, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = meter.meter_message_from_counter(self.counter1) self.conn.record_metering_data(msg) self.counter2 = counter.Counter( 'source2', 'instance', 'cumulative', 1, 'user-id2', 'project2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(self.counter2) self.conn.record_metering_data(msg2)
def message_from_data(self, raw_data): msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': {'data': meter.meter_message_from_counter(raw_data), }, } return 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) name_map = {'name': 'counter_name', 'type': 'counter_type', 'volume': 'counter_volume', 'duration': 'counter_duration', } 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 test_with_source(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-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) data = self.get('/sources/test_list_resources/resources') ids = [r['resource_id'] for r in data['resources']] self.assertEquals(['resource-id'], ids)
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('/resources') self.assertEquals(2, len(data['resources']))
def test_with_source(self): counter1 = counter.Counter( 'test_list_resources', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = meter.meter_message_from_counter(counter1) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'not-test', 'instance', 'cumulative', 1, 'user-id2', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(counter2) self.conn.record_metering_data(msg2) data = self.get('/sources/test_list_resources/resources') ids = [r['resource_id'] for r in data['resources']] self.assertEquals(['resource-id'], ids)
def test_with_source(self): counter1 = counter.Counter( 'test_list_users', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = meter.meter_message_from_counter(counter1) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'not-test', 'instance', 'cumulative', 1, 'user-id2', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(counter2) self.conn.record_metering_data(msg2) data = self.get('/sources/test_list_users/users') self.assertEquals(['user-id'], data['users'])
def test_instances(self): counter1 = counter.Counter( 'test', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = meter.meter_message_from_counter(counter1) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'test', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = meter.meter_message_from_counter(counter2) self.conn.record_metering_data(msg2) data = self.get('/resources') self.assertEquals(2, len(data['resources']))
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 _make_events(self, *timestamps): for t in timestamps: c = counter.Counter('test', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=t, duration=0, resource_metadata={ 'display_name': 'test-server', }) msg = meter.meter_message_from_counter(c) self.conn.record_metering_data(msg)
def publish_counter(context, counter): """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 """ msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': { 'data': meter.meter_message_from_counter(counter), }, } LOG.debug('PUBLISH: %s', str(msg)) rpc.cast(context, cfg.CONF.metering_topic, msg) rpc.cast(context, cfg.CONF.metering_topic + '.' + counter.name, msg)
def _make_events(self, *timestamps): for t in timestamps: c = counter.Counter( 'test', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=t, duration=0, resource_metadata={'display_name': 'test-server', } ) msg = meter.meter_message_from_counter(c) self.conn.record_metering_data(msg)
def publish_counter(context, counter): """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 """ msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': {'data': meter.meter_message_from_counter(counter), }, } LOG.debug('PUBLISH: %s', str(msg)) rpc.cast(context, cfg.CONF.metering_topic, msg) rpc.cast(context, cfg.CONF.metering_topic + '.' + counter.name, msg)
def _publish_counter(self, counter): """Create a metering message for the counter and publish it.""" msg = meter.meter_message_from_counter(counter) LOG.info('PUBLISH: %s', str(msg)) # FIXME(dhellmann): Need to publish the message on the # metering queue. msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': {'data': msg, }, } ctxt = context.get_admin_context() nova_rpc.cast(ctxt, cfg.CONF.metering_topic, msg) nova_rpc.cast(ctxt, cfg.CONF.metering_topic + '.' + counter.name, msg)
def _make_events(self, *timestamps): for t in timestamps: c = counter.Counter( name='instance', type=counter.TYPE_CUMULATIVE, volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=t, resource_metadata={'display_name': 'test-server', } ) msg = meter.meter_message_from_counter(counter=c, secret='not-so-secret', source='test', ) self.conn.record_metering_data(msg)
def periodic_tasks(self, context, raise_on_error=False): """Tasks to be run at a periodic interval.""" for name, pollster in self.pollsters: try: LOG.info('polling %s', name) for c in pollster.get_counters(self, context): LOG.info('COUNTER: %s', c) msg = { 'method': 'record_metering_data', 'version': '1.0', 'args': {'data': meter.meter_message_from_counter(c), }, } rpc.cast(context, cfg.CONF.metering_topic, msg) rpc.cast(context, cfg.CONF.metering_topic + '.' + c.name, msg) except Exception as err: LOG.warning('Continuing after error from %s: %s', name, err) LOG.exception(err)
def setUp(self): super(MaxResourceTest, self).setUp() self.counters = [] for i in range(3): c = counter.Counter( 'volume.size', 'gauge', 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, cfg.CONF.metering_secret, 'source1', ) self.conn.record_metering_data(msg)
def test_meter_message_from_counter_signed(): msg = meter.meter_message_from_counter(TEST_COUNTER) assert "message_signature" in msg
def setUp(self): super(SQLAlchemyEngineTestBase, self).setUp() self.conf = cfg.CONF self.conf.database_connection = 'sqlite://' # Use a real MySQL server if we can connect, but fall back # to a Sqlite in-memory connection if we cannot. if CEILOMETER_TEST_LIVE: # should pull from conf file but for now manually specified # just make sure ceilometer_test db exists in mysql self.conf.database_connection = MYSQL_URL self.conn = Connection(self.conf) self.session = self.conn.session migration.db_sync() self.counter = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 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.counter2 = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 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.counter3 = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 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) for i in range(2, 4): c = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 1, 'user-id-%s' % i, 'project-id-%s' % i, '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)
def setUp(self): super(MongoDBEngineTestBase, self).setUp() self.conn.conn.drop_database('testdb') self.db = self.conn.conn['testdb'] self.conn.db = self.db self.counter = counter.Counter('test-1', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), duration=0, resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }) self.msg = meter.meter_message_from_counter(self.counter) self.conn.record_metering_data(self.msg) self.counter2 = counter.Counter('test-2', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), duration=0, resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) self.msg2 = meter.meter_message_from_counter(self.counter2) self.conn.record_metering_data(self.msg2) self.counter3 = counter.Counter('test-3', 'instance', 'cumulative', 1, 'user-id-alternate', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), duration=0, resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter3', }) self.msg3 = meter.meter_message_from_counter(self.counter3) self.conn.record_metering_data(self.msg3) for i in range(2, 4): c = counter.Counter('test', 'instance', 'cumulative', 1, 'user-id-%s' % i, 'project-id-%s' % i, 'resource-id-%s' % i, timestamp=datetime.datetime( 2012, 7, 2, 10, 40 + i), duration=0, resource_metadata={ 'display_name': 'test-server', 'tag': 'counter-%s' % i, }) msg = meter.meter_message_from_counter(c) self.conn.record_metering_data(msg)
def test_meter_message_from_counter_signed(): msg = meter.meter_message_from_counter(TEST_COUNTER) assert 'message_signature' in msg
def main(): cfg.CONF([]) 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=('absolute', 'cumulative'), default='absolute', help='counter type', ) 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(source='artificial', name=args.counter, type=args.type, volume=args.volume, user_id=args.user, project_id=args.project, resource_id=args.resource, timestamp=timestamp, duration=0, resource_metadata={}, ) data = meter.meter_message_from_counter(c) conn.record_metering_data(data) n += 1 timestamp = timestamp + increment print 'Added %d new events' % n return 0
def setUp(self): super(MongoDBEngineTestBase, self).setUp() self.conn.conn.drop_database('testdb') self.db = self.conn.conn['testdb'] self.conn.db = self.db self.counter = counter.Counter( 'test-1', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) self.msg = meter.meter_message_from_counter(self.counter) self.conn.record_metering_data(self.msg) self.counter2 = counter.Counter( 'test-2', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) self.msg2 = meter.meter_message_from_counter(self.counter2) self.conn.record_metering_data(self.msg2) self.counter3 = counter.Counter( 'test-3', 'instance', 'cumulative', 1, 'user-id-alternate', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter3', } ) self.msg3 = meter.meter_message_from_counter(self.counter3) self.conn.record_metering_data(self.msg3) for i in range(2, 4): c = counter.Counter( 'test', 'instance', 'cumulative', 1, 'user-id-%s' % i, 'project-id-%s' % i, 'resource-id-%s' % i, timestamp=datetime.datetime(2012, 7, 2, 10, 40 + i), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'counter-%s' % i, } ) msg = meter.meter_message_from_counter(c) self.conn.record_metering_data(msg)
def test_meter_message_from_counter_event_type(): msg = meter.meter_message_from_counter(TEST_COUNTER) assert msg['event_type'] == 'metering.' + TEST_COUNTER.type
def setUp(self): super(MongoDBEngineTestBase, self).setUp() conf = mox.Mox().CreateMockAnything() conf.database_connection = 'mongodb://localhost/%s' % self.DBNAME self.conn = TestConnection(conf) self.conn.drop_database(self.DBNAME) self.db = self.conn.conn[self.DBNAME] self.counter = counter.Counter( 'instance', counter.TYPE_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', } ) self.msg = meter.meter_message_from_counter(self.counter, 'not-so-secret', 'test-1') self.conn.record_metering_data(self.msg) self.counter2 = counter.Counter( 'instance', counter.TYPE_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', } ) self.msg2 = meter.meter_message_from_counter(self.counter2, 'not-so-secret', 'test-2') self.conn.record_metering_data(self.msg2) self.counter3 = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 1, 'user-id-alternate', 'project-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, 'not-so-secret', 'test-3') self.conn.record_metering_data(self.msg3) for i in range(2, 4): c = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 1, 'user-id-%s' % i, 'project-id-%s' % i, '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, 'not-so-secret', 'test') 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 setUp(self): super(MongoDBEngineTestBase, self).setUp() self.conf = mox.Mox().CreateMockAnything() self.conf.metering_storage_engine = 'mongodb' self.conf.mongodb_host = 'localhost' self.conf.mongodb_port = 27017 self.conf.mongodb_dbname = 'testdb' self.conn = Connection(self.conf) self.conn.conn.drop_database(self.conf.mongodb_dbname) self.db = self.conn.conn[self.conf.mongodb_dbname] self.conn.db = self.db self.counter = counter.Counter( 'test', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) self.msg = meter.meter_message_from_counter(self.counter) self.conn.record_metering_data(self.msg) self.counter2 = counter.Counter( 'test', 'instance', 'cumulative', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) self.msg2 = meter.meter_message_from_counter(self.counter2) self.conn.record_metering_data(self.msg2) for i in range(2, 4): c = counter.Counter( 'test', 'instance', 'cumulative', 1, 'user-id-%s' % i, 'project-id-%s' % i, 'resource-id-%s' % i, timestamp=datetime.datetime(2012, 7, 2, 10, 40 + i), duration=0, resource_metadata={'display_name': 'test-server', 'tag': 'counter-%s' % i, } ) msg = meter.meter_message_from_counter(c) self.conn.record_metering_data(msg)
def main(): cfg.CONF([]) 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=('absolute', 'cumulative'), default='absolute', help='counter type', ) 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( source='artificial', name=args.counter, type=args.type, volume=args.volume, user_id=args.user, project_id=args.project, resource_id=args.resource, timestamp=timestamp, duration=0, resource_metadata={}, ) data = meter.meter_message_from_counter(c) conn.record_metering_data(data) n += 1 timestamp = timestamp + increment print 'Added %d new events' % n return 0
def setUp(self): super(SQLAlchemyEngineTestBase, self).setUp() self.counter = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 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.counter2 = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 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.counter3 = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 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) for i in range(2, 4): c = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, 1, 'user-id-%s' % i, 'project-id-%s' % i, '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)