X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FMON.git;a=blobdiff_plain;f=osm_mon%2Fcollector%2Finfra_collectors%2Fopenstack.py;h=5c34e9c240be4e65db7209406e890ff9da5dc251;hp=5f62edf340f33a7df8d46be8535470da20d826b1;hb=616fde7e4671be3da07ea59765c311c26bfe2e16;hpb=b2b43da9ec8230664b19479c457dd54711b8986a 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)