def process_notification(self, message): LOG.info('network notification %r', message) message['payload'] = message['payload'][self.resource_name] metadata = self.notification_to_metadata(message) counter_name = getattr(self, 'counter_name', self.resource_name) unit_value = getattr(self, 'unit', self.resource_name) yield counter.Counter( name=counter_name, type=counter.TYPE_GAUGE, unit=unit_value, volume=1, user_id=message['_context_user_id'], project_id=message['payload']['tenant_id'], resource_id=message['payload']['id'], timestamp=message['timestamp'], resource_metadata=metadata, ) event_type_split = message['event_type'].split('.') if len(event_type_split) > 2: yield counter.Counter( name=counter_name + "." + event_type_split[1], type=counter.TYPE_DELTA, unit=unit_value, volume=1, user_id=message['_context_user_id'], project_id=message['payload']['tenant_id'], resource_id=message['payload']['id'], timestamp=message['timestamp'], resource_metadata=metadata, )
def get_counters(self, manager): for tenant, account in self.iter_accounts(manager.keystone): yield counter.Counter( name='storage.objects', type=counter.TYPE_GAUGE, volume=int(account['x-account-object-count']), unit='object', user_id=None, project_id=tenant, resource_id=tenant, timestamp=timeutils.isotime(), resource_metadata=None, ) yield counter.Counter( name='storage.objects.size', type=counter.TYPE_GAUGE, volume=int(account['x-account-bytes-used']), unit='B', user_id=None, project_id=tenant, resource_id=tenant, timestamp=timeutils.isotime(), resource_metadata=None, ) yield counter.Counter( name='storage.objects.containers', type=counter.TYPE_GAUGE, volume=int(account['x-account-container-count']), unit='container', user_id=None, project_id=tenant, resource_id=tenant, timestamp=timeutils.isotime(), resource_metadata=None, )
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(TestAPIACL, self).setUp() self.environ = {'fake.cache': FakeMemcache()} for cnt in [ counter.Counter( 'meter.test', 'cumulative', '', 1, 'user-good', 'project-good', 'resource-good', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}), counter.Counter( 'meter.mine', 'gauge', '', 1, 'user-fred', 'project-good', 'resource-56', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter4'})]: msg = rpc.meter_message_from_counter( cnt, cfg.CONF.publisher_rpc.metering_secret, 'test_source') self.conn.record_metering_data(msg)
def publish_counter(self, env, bytes_received, bytes_sent): req = REQUEST.Request(env) version, account, container, obj = split_path(req.path, 1, 4, True) now = timeutils.utcnow().isoformat() resource_metadata = { "path": req.path, "version": version, "container": container, "object": obj, } for header in self.metadata_headers: if header.upper() in req.headers: resource_metadata['http_header_%s' % header] = req.headers.get( header.upper()) with pipeline.PublishContext( context.get_admin_context(), cfg.CONF.counter_source, self.pipeline_manager.pipelines, ) as publisher: if bytes_received: publisher([counter.Counter( name='storage.objects.incoming.bytes', type=counter.TYPE_DELTA, unit='B', volume=bytes_received, user_id=env.get('HTTP_X_USER_ID'), project_id=env.get('HTTP_X_TENANT_ID'), resource_id=account.partition('AUTH_')[2], timestamp=now, resource_metadata=resource_metadata)]) if bytes_sent: publisher([counter.Counter( name='storage.objects.outgoing.bytes', type=counter.TYPE_DELTA, unit='B', volume=bytes_sent, user_id=env.get('HTTP_X_USER_ID'), project_id=env.get('HTTP_X_TENANT_ID'), resource_id=account.partition('AUTH_')[2], timestamp=now, resource_metadata=resource_metadata)]) # publish the event for each request # request method will be recorded in the metadata resource_metadata['method'] = req.method.lower() publisher([counter.Counter( name='storage.api.request', type=counter.TYPE_DELTA, unit='request', volume=1, user_id=env.get('HTTP_X_USER_ID'), project_id=env.get('HTTP_X_TENANT_ID'), resource_id=account.partition('AUTH_')[2], timestamp=now, resource_metadata=resource_metadata)])
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.publisher_meter.metering_secret, 'test_list_resources') 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.publisher_meter.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.publisher_meter.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.publisher_meter.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.publisher_meter.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.publisher_meter.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 get_counters(self, manager, context): try: ips = manager.db.floating_ip_get_all(context) except exception.FloatingIpNotFoundForHost: pass else: for ip in ips: self.LOG.info("FLOATING IP USAGE: %s" % ip.address) yield counter.Counter( source='?', name='floating_ip', type='cumulative', volume=1, user_id=None, project_id=ip.project_id, resource_id=ip.id, timestamp=None, # FIXME(dhellmann): This needs to be now() duration=None, resource_metadata={ 'address': ip.address, 'fixed_ip_id': ip.fixed_ip_id, 'host': ip.host, 'pool': ip.pool, 'auto_assigned': ip.auto_assigned })
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_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.publisher_meter.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'), ])
class TestPublish(base.TestCase): test_data = counter.Counter( source='test', name='test', type='cumulative', volume=1, user_id='test', project_id='test', resource_id='test_run_tasks', timestamp=datetime.datetime.utcnow().isoformat(), duration=0, resource_metadata={'name': 'TestPublish', }, ) def faux_notify(self, context, topic, msg): self.notifications.append((topic, msg)) def setUp(self): super(TestPublish, self).setUp() self.notifications = [] self.stubs.Set(rpc, 'cast', self.faux_notify) publish.publish_counter(None, self.test_data) def test_notify(self): assert len(self.notifications) == 2 def test_notify_topics(self): topics = [n[0] for n in self.notifications] assert topics == ['metering', 'metering.test']
def test_with_user_wrong_tenant(self): counter1 = 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.counter1', }) msg2 = meter.meter_message_from_counter( counter1, cfg.CONF.publisher_meter.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources', headers={ "X-Roles": "Member", "X-Tenant-Id": "project-wrong" }) ids = set(r['resource_id'] for r in data) self.assertEquals(set(), ids)
def test_resource_meter_links(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.publisher_meter.metering_secret, 'test_list_resources', ) self.conn.record_metering_data(msg) data = self.get_json('/resources') links = data[0]['links'] self.assertEqual(len(links), 2) self.assertEqual(links[0]['rel'], 'self') self.assertTrue((self.PATH_PREFIX + '/resources/resource-id') in links[0]['href']) self.assertEqual(links[1]['rel'], 'instance') self.assertTrue( (self.PATH_PREFIX + '/meters/instance?' 'q.field=resource_id&q.value=resource-id') in links[1]['href'])
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.publisher_meter.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.publisher_meter.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_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.publisher_meter.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.publisher_meter.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 = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.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 = rpc.meter_message_from_counter( counter2, cfg.CONF.publisher_rpc.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2)
def make_counter_from_instance(instance, name, type, unit, volume): return counter.Counter( name=name, type=type, unit=unit, volume=volume, user_id=instance.user_id, project_id=instance.tenant_id, resource_id=instance.id, timestamp=timeutils.isotime(), resource_metadata=compute_instance.get_metadata_from_object(instance), )
def process_notification(self, message): return [ counter.Counter(name='memory', type=counter.TYPE_GAUGE, unit='B', volume=message['payload']['memory_mb'], user_id=message['payload']['user_id'], project_id=message['payload']['tenant_id'], resource_id=message['payload']['instance_id'], timestamp=message['timestamp'], resource_metadata=self.notification_to_metadata( message)), ]
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 make_counter_from_instance(instance, name, type, volume): return counter.Counter( source='?', name=name, type=type, volume=volume, user_id=instance.user_id, project_id=instance.project_id, resource_id=instance.uuid, timestamp=timeutils.isotime(), duration=None, resource_metadata=compute_instance.get_metadata_from_dbobject( instance), )
def make_vnic_counter(instance, name, type, volume, vnic_data): resource_metadata = copy.copy(vnic_data) resource_metadata['instance_id'] = instance.id return counter.Counter(source='?', name=name, type=type, volume=volume, user_id=instance.user_id, project_id=instance.project_id, resource_id=vnic_data['fref'], timestamp=timeutils.isotime(), duration=None, resource_metadata=resource_metadata)
def setUp(self): super(TestUDPCollectorService, self).setUp() self.srv = service.UDPCollectorService() self.counter = dict(counter.Counter( name='foobar', type='bad', unit='F', volume=1, user_id='jd', project_id='ceilometer', resource_id='cat', timestamp='NOW!', resource_metadata={}, )._asdict())
def process_notification(self, message): return [ counter.Counter( name=sample['name'], type=sample['type'], unit=sample['unit'], volume=sample['volume'], user_id=message['payload']['user_id'], project_id=message['payload']['tenant_id'], resource_id=message['payload']['instance_id'], timestamp=message['timestamp'], resource_metadata=self.notification_to_metadata(message), ) for sample in message['payload'].get('samples', []) ]
def get_counters(self, manager): """Returns all counters.""" for probe in self.iter_probes(manager.keystone): yield counter.Counter(name='energy', type=counter.TYPE_CUMULATIVE, unit='kWh', volume=probe['kwh'], user_id=None, project_id=None, resource_id=probe['id'], timestamp=datetime.datetime.fromtimestamp( probe['timestamp']).isoformat(), resource_metadata={}) yield counter.Counter(name='power', type=counter.TYPE_GAUGE, unit='W', volume=probe['w'], user_id=None, project_id=None, resource_id=probe['id'], timestamp=datetime.datetime.fromtimestamp( probe['timestamp']).isoformat(), resource_metadata={})
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 process_notification(self, message): return [ counter.Counter( source='?', name='volume_size', type='absolute', volume=message['payload']['size'], user_id=message['payload']['user_id'], project_id=message['payload']['tenant_id'], resource_id=message['payload']['volume_id'], timestamp=message['timestamp'], duration=None, resource_metadata=self.notification_to_metadata(message), ), ]
def get_counters(self, manager): for image in self.iter_images(manager.keystone): yield counter.Counter( name='image', type=counter.TYPE_GAUGE, unit='image', volume=1, user_id=None, project_id=image.owner, resource_id=image.id, timestamp=timeutils.isotime(), resource_metadata=self.extract_image_metadata(image), ) yield counter.Counter( name='image.size', type=counter.TYPE_GAUGE, unit='B', volume=image.size, user_id=None, project_id=image.owner, resource_id=image.id, timestamp=timeutils.isotime(), resource_metadata=self.extract_image_metadata(image), )
def process_notification(self, message): metadata = self.notification_to_metadata(message) return [ counter.Counter( name=message['event_type'], type=counter.TYPE_DELTA, unit='event', volume=1, resource_id=message['payload']['id'], user_id=None, project_id=message['payload']['owner'], timestamp=message['timestamp'], resource_metadata=metadata, ), ]