Fix 2111: Added config parameter to disable vms status check
[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 keystoneclient.v3 import client as keystone_client
26 from novaclient import client as nova_client
27
28 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector
29 from osm_mon.collector.metric import Metric
30 from osm_mon.collector.utils.openstack import OpenstackUtils
31 from osm_mon.core.common_db import CommonDbClient
32 from osm_mon.core.config import Config
33
34 log = logging.getLogger(__name__)
35
36
37 class BaseOpenStackInfraCollector(BaseVimInfraCollector):
38 def __init__(self, config: Config, vim_account_id: str):
39 super().__init__(config, vim_account_id)
40 self.conf = config
41 self.common_db = CommonDbClient(config)
42 self.vim_account = self.common_db.get_vim_account(vim_account_id)
43 self.keystone = self._build_keystone_client(self.vim_account)
44 self.nova = self._build_nova_client(self.vim_account)
45
46 def collect(self) -> List[Metric]:
47 metrics = []
48 vim_status = self.is_vim_ok()
49 if self.vim_account["_admin"]["projects_read"]:
50 vim_project_id = self.vim_account["_admin"]["projects_read"][0]
51 else:
52 vim_project_id = ""
53 vim_tags = {
54 "vim_account_id": self.vim_account["_id"],
55 "project_id": vim_project_id,
56 }
57 vim_status_metric = Metric(vim_tags, "vim_status", vim_status)
58 metrics.append(vim_status_metric)
59 vnfrs = self.common_db.get_vnfrs(vim_account_id=self.vim_account["_id"])
60 if self.conf.get("collector", "vm_infra_metrics"):
61 vm_infra_metrics_enabled = str(self.conf.get("collector", "vm_infra_metrics")).lower() in ("yes", "true", "1")
62 else:
63 vm_infra_metrics_enabled = True
64 if vm_infra_metrics_enabled:
65 for vnfr in vnfrs:
66 nsr_id = vnfr["nsr-id-ref"]
67 ns_name = self.common_db.get_nsr(nsr_id)["name"]
68 vnf_member_index = vnfr["member-vnf-index-ref"]
69 if vnfr["_admin"]["projects_read"]:
70 vnfr_project_id = vnfr["_admin"]["projects_read"][0]
71 else:
72 vnfr_project_id = ""
73 for vdur in vnfr["vdur"]:
74 if "vim-id" not in vdur:
75 log.debug("Field vim-id is not present in vdur")
76 continue
77 resource_uuid = vdur["vim-id"]
78 tags = {
79 "vim_account_id": self.vim_account["_id"],
80 "resource_uuid": resource_uuid,
81 "nsr_id": nsr_id,
82 "ns_name": ns_name,
83 "vnf_member_index": vnf_member_index,
84 "vdur_name": vdur.get("name", ""),
85 "project_id": vnfr_project_id,
86 }
87 try:
88 vm = self.nova.servers.get(resource_uuid)
89 vm_status = 1 if vm.status == "ACTIVE" else 0
90 vm_status_metric = Metric(tags, "vm_status", vm_status)
91 except Exception as e:
92 log.warning("VM status is not OK: %s" % e)
93 vm_status_metric = Metric(tags, "vm_status", 0)
94 metrics.append(vm_status_metric)
95
96 return metrics
97
98 def is_vim_ok(self) -> bool:
99 try:
100 self.nova.servers.list()
101 return True
102 except Exception as e:
103 log.warning("VIM status is not OK: %s" % e)
104 return False
105
106 def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
107 sess = OpenstackUtils.get_session(vim_account)
108 return keystone_client.Client(session=sess, timeout=10)
109
110 def _build_nova_client(self, vim_account: dict) -> nova_client.Client:
111 sess = OpenstackUtils.get_session(vim_account)
112 return nova_client.Client("2", session=sess, timeout=10)