首页
CloudKitty新增service计费(gnocchi)

这里,我们以loadbanlancer为例进行说明。

增加collector

  1. 增加默认配置
# 修改cloudkitty/collector/__init__.py
# 找到默认配置collect_opts,修改services的default值

collect_opts = [
    ...               
    cfg.ListOpt('services',
                default=['compute',
                         'image',
                         'volume',
                         'router',
                         'volume.snapshot',
                         'network.bw.in',
                         'network.bw.out',
                         'network.floating',
                         #增加监听器的配置
                         'network.lb.listener'],
                help='Services to monitor.'), ]
  1. 增加gnocchi采集
# 修改cloudkitty/collector/gnocchi.py

class GnocchiCollector(collector.BaseCollector):

    ...
	# 修改相关属性定义
    retrieve_mappings = {
        'compute': 'instance',
        ...
        # 新增service查询定义(要和gnocchi中的resource对应)
        'network.lb.listener': 'network_lb_listener',
        
    }
    metrics_mappings = {
        'compute': [
            ('vcpus', 'max'),
            ('memory', 'max'),
            ('cpu', 'max'),
            ('disk.root.size', 'max'),
            ('disk.ephemeral.size', 'max')],
        ...
        # 新增指标定义(要和gnocchi中的指标对应)
        'network.lb.listener': [
            ('network.services.lb.limit.connections', 'max')],
        
    }
    units_mappings = {
        'compute': (1, 'instance'),
        'image': ('image.size', 'MB'),
        'volume': ('volume.size', 'GB'),
        ...
        # 增加指标单位定义
        'network.lb.listener': ('network.services.lb.limit.connections', 'connection'),
        
    }
    
    # 修改资源处理函数,根据需要进行处理,一般不需要修改(本例子就没有修改此函数)
    def resource_info(self, resource_name, start, end, project_id,
                      q_filter=None):
        qty, unit = self.units_mappings.get(resource_name, self.default_unit)
        resources = self.get_resources(resource_name, start, end,
                                       project_id=project_id,
                                       q_filter=q_filter)
        formated_resources = list()
        for resource in resources:
            resource_data = self.t_gnocchi.strip_resource_data(
                resource_name, resource)
            mappings = self.metrics_mappings[resource_name]
            self._expand_metrics([resource_data], mappings, start, end)
            resource_data = self.t_gnocchi.strip_metrics_data(
                resource_name, resource_data)
            resource_data.pop('metrics', None)
            # Convert network.bw.in, network.bw.out and image unit to MB
            if resource.get('type') == 'instance_network_interface':
                resource_data[qty] = (
                    decimal.Decimal(resource_data[qty]) / units.M)
            elif resource.get('type') == 'image':
                resource_data[qty] = (
                    decimal.Decimal(resource_data[qty]) / units.Mi)
            elif resource.get('type') == 'network':
                resource_data[qty] = (
                    decimal.Decimal(resource_data[qty]) / units.k)
            data = self.t_cloudkitty.format_item(
                resource_data, unit,
                decimal.Decimal(
                    qty if isinstance(qty, int) else resource_data[qty]))
            # NOTE(sheeprine): Reference to gnocchi resource used by storage
            data['resource_id'] = data['desc']['resource_id']
            formated_resources.append(data)
        return formated_resources

增加transformer

# 编辑cloudkitty/transformer/gnocchi.py

class GnocchiTransformer(transformer.BaseTransformer):
    ...

    # 增加转换配置,主要目的是将在存储中的属性或指标名称转换成我们需要的
    # 属性的转换配置
    network_lb_listener_map = {
        'name': ['display_name', 'name'],
    }
    # 指标的转换配置
    network_lb_listener_metrics_map = {
        'connection_limit': ['network.services.lb.limit.connections']
    }