Ceilometer 增加新的指标(gnocchi)

我们以负载均衡器的连接数指标为例进行说明。

增加指标的采集

  1. 修改 ceilometer/network/services/discovery.py 文件,增加Discovery实现。
class LBListenersDiscovery(_BaseServicesDiscovery):
    def discover(self, manager, param=None):
        """Discover load balancer listener resources to monitor."""

        listeners = self.neutron_cli.list_listener()
        return [i for i in listeners
                if i.get('operating_status', None) != 'error']
  1. 修改 ceilometer/network/services/lbaas.py 文件,增加Pollster实现。

class LBConnectionsLimitPollster(base.BaseServicesPollster):

    FIELDS = ['protocol_port',
              'name',
              'protocol',
              'admin_state_up',
              'connection_limit',
              ]

    @property
    def default_discovery(self):
        # 需要和setup.cfg配置文件中ceilometer.discover.central配置中的discovery名称对应一致
        return 'lb_listeners'

    def get_samples(self, manager, cache, resources):
        resources = resources or []

        for listener in resources:
            LOG.debug("Load Balancer listener : %s" % listener)

            if not listener.get('admin_state_up', True):
                continue
            provisioning_status = listener.get('provisioning_status', 'active')
            if provisioning_status.lower() in ['error']:
                continue
            connection_limit = listener.get('connection_limit', -1)
            loadbalancers = listener.get("loadbalancers", [])
            loadbalancer_ids = list()
            for loadbalancer in loadbalancers:
                if 'id' in loadbalancer:
                    loadbalancer_ids.append(loadbalancer['id'])

            resource_metadata = self.extract_metadata(listener)
            resource_metadata["loadbalancers"] = loadbalancer_ids
            yield sample.Sample(
                name='network.services.lb.limit.connections',
                type=sample.TYPE_CUMULATIVE,
                unit='connection',
                volume=connection_limit,
                user_id=None,
                project_id=listener['tenant_id'],
                resource_id=listener['id'],
                resource_metadata=resource_metadata,
            )
  1. 修改setup.cfg文件,配置采集组件信息。

ceilometer.discover.central =
    ...
    lb_listeners = ceilometer.network.services.discovery:LBListenersDiscovery
    ...
    
ceilometer.poll.central =
    ...
    network.services.lb.listener.limit.connections = ceilometer.network.services.lbaas:LBConnectionsLimitPollster
    ...

增加指标信息定义及初始化

  1. 修改ceilometer/dispatcher/data/gnocchi_resources.yaml配置文件。
  - resource_type: network_lb_listener
    metrics:
      - 'network.services.lb.limit.connections'
    attributes:
      display_name: resource_metadata.name      
  1. 修改ceilometer/gnocchi_client.py增加相关初始化信息。
# NOTE(sileht): This is the initial resource types created in Gnocchi
# This list must never change to keep in sync with what Gnocchi early
# database contents was containing
resources_initial = {
    ...    
    # 新增信息,如果需要其他附加属性,可参照上面字典来定义
    "network_lb_listener": {
        "display_name": {"type": "string", "min_length": 0, "max_length": 255, "required": False},},
    ...
}