19a21f057acb0e87e0a365dff899628daa16c0bb
[osm/MON.git] / osm_mon / collector / infra_collectors / base_osinfra.py
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: bdiaz@whitestack.com or glavado@whitestack.com
21 ##
22 import logging
23 from typing import List
24
25 from keystoneauth1 import session
26 from keystoneauth1.identity import v3
27 from keystoneclient.v3 import client as keystone_client
28 from novaclient import client as nova_client
29 from novaclient import v2 as nova_client_v2
30
31 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector
32 from osm_mon.collector.metric import Metric
33 from osm_mon.collector.utils import CollectorUtils
34 from osm_mon.core.common_db import CommonDbClient
35 from osm_mon.core.config import Config
36
37 log = logging.getLogger(__name__)
38
39
40 class BaseOpenStackInfraCollector(BaseVimInfraCollector):
41 def __init__(self, config: Config, vim_account_id: str):
42 super().__init__(config, vim_account_id)
43 self.keystone = self._build_keystone_client(vim_account_id)
44 self.nova = self._build_nova_client(vim_account_id)
45 self.vim_account_id = vim_account_id
46 self.common_db = CommonDbClient(config)
47
48 def collect(self) -> List[Metric]:
49 metrics = []
50 vim_status = self.is_vim_ok()
51 vim_status_metric = Metric({'vim_account_id': self.vim_account_id}, 'vim_status', vim_status)
52 metrics.append(vim_status_metric)
53 vnfrs = self.common_db.get_vnfrs(vim_account_id=self.vim_account_id)
54 for vnfr in vnfrs:
55 nsr_id = vnfr['nsr-id-ref']
56 vnf_member_index = vnfr['member-vnf-index-ref']
57 for vdur in vnfr['vdur']:
58 if 'vim-id' not in vdur:
59 log.debug("Field vim-id is not present in vdur")
60 continue
61 resource_uuid = vdur['vim-id']
62 tags = {
63 'vim_account_id': self.vim_account_id,
64 'resource_uuid': resource_uuid,
65 'nsr_id': nsr_id,
66 'vnf_member_index': vnf_member_index,
67 'vdur_name': vdur['name']
68 }
69 try:
70 vm = self.nova.servers.get(resource_uuid)
71 vm_status = (1 if vm.status == 'ACTIVE' else 0)
72 vm_status_metric = Metric(tags, 'vm_status', vm_status)
73 except Exception as e:
74 log.warning("VM status is not OK: %s" % e)
75 vm_status_metric = Metric(tags, 'vm_status', 0)
76 metrics.append(vm_status_metric)
77
78 return metrics
79
80 def is_vim_ok(self) -> bool:
81 try:
82 self.nova.servers.list()
83 return True
84 except Exception as e:
85 log.warning("VIM status is not OK: %s" % e)
86 return False
87
88 def _build_keystone_client(self, vim_account_id) -> keystone_client.Client:
89 sess = self._get_session(vim_account_id)
90 return keystone_client.Client(session=sess)
91
92 def _build_nova_client(self, vim_account_id) -> nova_client_v2.Client:
93 sess = self._get_session(vim_account_id)
94 return nova_client.Client("2", session=sess)
95
96 def _get_session(self, vim_account_id: str):
97 creds = CollectorUtils.get_credentials(vim_account_id)
98 verify_ssl = CollectorUtils.is_verify_ssl(creds)
99 auth = v3.Password(auth_url=creds.url,
100 username=creds.user,
101 password=creds.password,
102 project_name=creds.tenant_name,
103 project_domain_id='default',
104 user_domain_id='default')
105 return session.Session(auth=auth, verify=verify_ssl)