From: Benjamin Diaz Date: Fri, 22 Feb 2019 19:36:00 +0000 (-0300) Subject: Adds collection of VM status metric in OpenStack infra plugin X-Git-Tag: v6.0.0~23 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=616fde7e4671be3da07ea59765c311c26bfe2e16;p=osm%2FMON.git Adds collection of VM status metric in OpenStack infra plugin OpenStack infra collector plugin now collects metrics regarding VM status. It iterates over vdus and using nova client gets info regarding status. Active status translates to 1, anything else to 0. Change-Id: Ia6e36a79a1d20cea07d5c7b3aa2af85b5c0e18d0 Signed-off-by: Benjamin Diaz --- diff --git a/debian/python3-osm-mon.postinst b/debian/python3-osm-mon.postinst index f83eef1..26ac8d7 100644 --- a/debian/python3-osm-mon.postinst +++ b/debian/python3-osm-mon.postinst @@ -36,4 +36,5 @@ pip3 install gnocchiclient==7.0.* pip3 install pyvcloud==19.1.1 pip3 install python-ceilometerclient==2.9.* pip3 install peewee-migrate==1.1.* +pip3 install python-novaclient==12.0.* echo "Installation of python dependencies finished" diff --git a/osm_mon/collector/collector.py b/osm_mon/collector/collector.py index aad395a..8b0d425 100644 --- a/osm_mon/collector/collector.py +++ b/osm_mon/collector/collector.py @@ -28,7 +28,6 @@ import peewee from osm_mon.collector.backends.prometheus import PrometheusBackend from osm_mon.collector.infra_collectors.openstack import OpenstackInfraCollector -from osm_mon.collector.metric import Metric from osm_mon.collector.vnf_collectors.juju import VCACollector from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector from osm_mon.collector.vnf_collectors.vmware import VMwareCollector @@ -89,9 +88,9 @@ class Collector: vim_type = database_manager.get_vim_type(vim_account_id) if vim_type in VIM_INFRA_COLLECTORS: collector = VIM_INFRA_COLLECTORS[vim_type](self.conf, vim_account_id) - status = collector.is_vim_ok() - status_metric = Metric({'vim_id': vim_account_id}, 'vim_status', status) - self.queue.put(status_metric) + metrics = collector.collect() + for metric in metrics: + self.queue.put(metric) else: log.debug("vimtype %s is not supported.", vim_type) diff --git a/osm_mon/collector/infra_collectors/base.py b/osm_mon/collector/infra_collectors/base.py index 6f137d0..586169a 100644 --- a/osm_mon/collector/infra_collectors/base.py +++ b/osm_mon/collector/infra_collectors/base.py @@ -19,7 +19,12 @@ # For those usages not covered by the Apache License, Version 2.0 please # contact: bdiaz@whitestack.com or glavado@whitestack.com ## +from typing import List + +from osm_mon.collector.metric import Metric class BaseInfraCollector: - pass + + def collect(self) -> List[Metric]: + pass diff --git a/osm_mon/collector/infra_collectors/base_vim.py b/osm_mon/collector/infra_collectors/base_vim.py index 2f5954a..698dde4 100644 --- a/osm_mon/collector/infra_collectors/base_vim.py +++ b/osm_mon/collector/infra_collectors/base_vim.py @@ -19,14 +19,11 @@ # For those usages not covered by the Apache License, Version 2.0 please # contact: bdiaz@whitestack.com or glavado@whitestack.com ## -from osm_mon.core.config import Config - from osm_mon.collector.infra_collectors.base import BaseInfraCollector +from osm_mon.core.config import Config + class BaseVimInfraCollector(BaseInfraCollector): def __init__(self, config: Config, vim_account_id: str): pass - - def is_vim_ok(self) -> bool: - pass diff --git a/osm_mon/collector/infra_collectors/openstack.py b/osm_mon/collector/infra_collectors/openstack.py index 5f62edf..5c34e9c 100644 --- a/osm_mon/collector/infra_collectors/openstack.py +++ b/osm_mon/collector/infra_collectors/openstack.py @@ -20,13 +20,18 @@ # contact: bdiaz@whitestack.com or glavado@whitestack.com ## import logging +from typing import List from keystoneauth1 import session from keystoneauth1.identity import v3 -from keystoneclient.v3 import client +from keystoneclient.v3 import client as keystone_client +from novaclient import client as nova_client +from novaclient import v2 as nova_client_v2 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector +from osm_mon.collector.metric import Metric from osm_mon.core.auth import AuthManager +from osm_mon.core.common_db import CommonDbClient from osm_mon.core.config import Config log = logging.getLogger(__name__) @@ -36,17 +41,53 @@ class OpenstackInfraCollector(BaseVimInfraCollector): def __init__(self, config: Config, vim_account_id: str): super().__init__(config, vim_account_id) self.auth_manager = AuthManager(config) - self.keystone_client = self._build_keystone_client(vim_account_id) + self.keystone = self._build_keystone_client(vim_account_id) + self.nova = self._build_nova_client(vim_account_id) + self.vim_account_id = vim_account_id + self.common_db = CommonDbClient(config) + + def collect(self) -> List[Metric]: + metrics = [] + vim_status = self.is_vim_ok() + vim_status_metric = Metric({'vim_account_id': self.vim_account_id}, 'vim_status', vim_status) + metrics.append(vim_status_metric) + vnfrs = self.common_db.get_vnfrs(vim_account_id=self.vim_account_id) + for vnfr in vnfrs: + nsr_id = vnfr['nsr-id-ref'] + vnf_member_index = vnfr['member-vnf-index-ref'] + for vdur in vnfr['vdur']: + resource_uuid = vdur['vim-id'] + vm = self.nova.servers.get(resource_uuid) + vm_status = (1 if vm.status == 'ACTIVE' else 0) + tags = { + 'vim_account_id': self.vim_account_id, + 'resource_uuid': resource_uuid, + 'nsr_id': nsr_id, + 'vnf_member_index': vnf_member_index, + 'vdur_name': vdur['name'] + } + vm_status_metric = Metric(tags, 'vm_status', vm_status) + metrics.append(vm_status_metric) + + return metrics def is_vim_ok(self) -> bool: try: - self.keystone_client.projects.list() + self.keystone.projects.list() return True except Exception: log.exception("VIM status is not OK!") return False - def _build_keystone_client(self, vim_account_id): + def _build_keystone_client(self, vim_account_id) -> keystone_client.Client: + sess = self._get_session(vim_account_id) + return keystone_client.Client(session=sess) + + def _build_nova_client(self, vim_account_id) -> nova_client_v2.Client: + sess = self._get_session(vim_account_id) + return nova_client.Client("2", session=sess) + + def _get_session(self, vim_account_id): creds = self.auth_manager.get_credentials(vim_account_id) verify_ssl = self.auth_manager.is_verify_ssl(vim_account_id) auth = v3.Password(auth_url=creds.url, @@ -55,5 +96,4 @@ class OpenstackInfraCollector(BaseVimInfraCollector): project_name=creds.tenant_name, project_domain_id='default', user_domain_id='default') - sess = session.Session(auth=auth, verify=verify_ssl) - return client.Client(session=sess) + return session.Session(auth=auth, verify=verify_ssl) diff --git a/osm_mon/core/common_db.py b/osm_mon/core/common_db.py index 5922290..33eff02 100644 --- a/osm_mon/core/common_db.py +++ b/osm_mon/core/common_db.py @@ -41,12 +41,18 @@ class CommonDbClient: {"nsr-id-ref": nsr_id, "member-vnf-index-ref": str(member_index)}) return vnfr - def get_vnfrs(self, nsr_id: str = None): + def get_vnfrs(self, nsr_id: str = None, vim_account_id: str = None): + if nsr_id and vim_account_id: + raise NotImplementedError("Only one filter is currently supported") if nsr_id: - return [self.get_vnfr(nsr_id, member['member-vnf-index']) for member in - self.get_nsr(nsr_id)['nsd']['constituent-vnfd']] + vnfrs = [self.get_vnfr(nsr_id, member['member-vnf-index']) for member in + self.get_nsr(nsr_id)['nsd']['constituent-vnfd']] + elif vim_account_id: + vnfrs = self.common_db.get_list("vnfrs", + {"vim-account-id": vim_account_id}) else: - return self.common_db.get_list('vnfrs') + vnfrs = self.common_db.get_list('vnfrs') + return vnfrs def get_vnfd(self, vnfd_id: str): vnfr = self.common_db.get_one("vnfds", diff --git a/requirements.txt b/requirements.txt index f44e08a..9da8612 100644 --- a/requirements.txt +++ b/requirements.txt @@ -32,5 +32,6 @@ pymysql==0.9.* pyvcloud==19.1.* python-ceilometerclient==2.9.* peewee-migrate==1.1.* +python-novaclient==12.0.* git+https://osm.etsi.org/gerrit/osm/common.git@v5.0#egg=osm-common git+https://osm.etsi.org/gerrit/osm/N2VC.git@v5.0#egg=n2vc diff --git a/setup.py b/setup.py index ed2bf7d..2126358 100644 --- a/setup.py +++ b/setup.py @@ -64,6 +64,7 @@ setup( "pyvcloud==19.1.1", "python-ceilometerclient==2.9.*", "peewee-migrate==1.1.*", + "python-novaclient==12.0.*", "osm-common", "n2vc" ], diff --git a/stdeb.cfg b/stdeb.cfg index bc035de..1490634 100644 --- a/stdeb.cfg +++ b/stdeb.cfg @@ -1,3 +1,3 @@ [DEFAULT] X-Python3-Version : >= 3.4 -Depends3 : libmysqlclient-dev, libssl-dev, libffi-dev, libxml2-dev, libxslt-dev, python3-pip, python3-osm-common \ No newline at end of file +Depends3 : libmysqlclient-dev, libssl-dev, libffi-dev, libxml2-dev, libxslt-dev, python3-pip, python3-osm-common, python3-n2vc \ No newline at end of file