1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 from typing
import List
25 from keystoneclient
.v3
import client
as keystone_client
26 from novaclient
import client
as nova_client
27 from novaclient
import v2
as nova_client_v2
29 from osm_mon
.collector
.infra_collectors
.base_vim
import BaseVimInfraCollector
30 from osm_mon
.collector
.metric
import Metric
31 from osm_mon
.collector
.utils
.openstack
import OpenstackUtils
32 from osm_mon
.core
.common_db
import CommonDbClient
33 from osm_mon
.core
.config
import Config
35 log
= logging
.getLogger(__name__
)
38 class BaseOpenStackInfraCollector(BaseVimInfraCollector
):
39 def __init__(self
, config
: Config
, vim_account_id
: str):
40 super().__init
__(config
, vim_account_id
)
41 self
.keystone
= self
._build
_keystone
_client
(vim_account_id
)
42 self
.nova
= self
._build
_nova
_client
(vim_account_id
)
43 self
.vim_account_id
= vim_account_id
44 self
.common_db
= CommonDbClient(config
)
46 def collect(self
) -> List
[Metric
]:
48 vim_status
= self
.is_vim_ok()
49 vim_status_metric
= Metric({'vim_account_id': self
.vim_account_id
}, 'vim_status', vim_status
)
50 metrics
.append(vim_status_metric
)
51 vnfrs
= self
.common_db
.get_vnfrs(vim_account_id
=self
.vim_account_id
)
53 nsr_id
= vnfr
['nsr-id-ref']
54 vnf_member_index
= vnfr
['member-vnf-index-ref']
55 for vdur
in vnfr
['vdur']:
56 if 'vim-id' not in vdur
:
57 log
.debug("Field vim-id is not present in vdur")
59 resource_uuid
= vdur
['vim-id']
61 'vim_account_id': self
.vim_account_id
,
62 'resource_uuid': resource_uuid
,
64 'vnf_member_index': vnf_member_index
,
65 'vdur_name': vdur
['name']
68 vm
= self
.nova
.servers
.get(resource_uuid
)
69 vm_status
= (1 if vm
.status
== 'ACTIVE' else 0)
70 vm_status_metric
= Metric(tags
, 'vm_status', vm_status
)
71 except Exception as e
:
72 log
.warning("VM status is not OK: %s" % e
)
73 vm_status_metric
= Metric(tags
, 'vm_status', 0)
74 metrics
.append(vm_status_metric
)
78 def is_vim_ok(self
) -> bool:
80 self
.nova
.servers
.list()
82 except Exception as e
:
83 log
.warning("VIM status is not OK: %s" % e
)
86 def _build_keystone_client(self
, vim_account_id
) -> keystone_client
.Client
:
87 sess
= OpenstackUtils
.get_session(vim_account_id
)
88 return keystone_client
.Client(session
=sess
)
90 def _build_nova_client(self
, vim_account_id
) -> nova_client_v2
.Client
:
91 sess
= OpenstackUtils
.get_session(vim_account_id
)
92 return nova_client
.Client("2", session
=sess
)