Adds collection of vim status metric
[osm/MON.git] / osm_mon / collector / infra_collectors / openstack.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
24 from keystoneauth1 import session
25 from keystoneauth1.identity import v3
26 from keystoneclient.v3 import client
27
28 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector
29 from osm_mon.core.auth import AuthManager
30
31 log = logging.getLogger(__name__)
32
33
34 class OpenstackInfraCollector(BaseVimInfraCollector):
35 def __init__(self, vim_account_id: str):
36 super().__init__(vim_account_id)
37 self.auth_manager = AuthManager()
38 self.keystone_client = self._build_keystone_client(vim_account_id)
39
40 def is_vim_ok(self) -> bool:
41 try:
42 self.keystone_client.projects.list()
43 return True
44 except Exception:
45 log.exception("VIM status is not OK!")
46 return False
47
48 def _build_keystone_client(self, vim_account_id):
49 creds = self.auth_manager.get_credentials(vim_account_id)
50 verify_ssl = self.auth_manager.is_verify_ssl(vim_account_id)
51 auth = v3.Password(auth_url=creds.url,
52 username=creds.user,
53 password=creds.password,
54 project_name=creds.tenant_name,
55 project_domain_id='default',
56 user_domain_id='default')
57 sess = session.Session(auth=auth, verify=verify_ssl)
58 return client.Client(session=sess)