Fix 2188: 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 from cinderclient import client as cinder_client
28 from neutronclient.neutron import client as neutron_client
29
30 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector
31 from osm_mon.collector.metric import Metric
32 from osm_mon.collector.utils.openstack import OpenstackUtils
33 from osm_mon.core.common_db import CommonDbClient
34 from osm_mon.core.config import Config
35
36 log = logging.getLogger(__name__)
37
38
39 class BaseOpenStackInfraCollector(BaseVimInfraCollector):
40 def __init__(self, config: Config, vim_account_id: str):
41 super().__init__(config, vim_account_id)
42 self.conf = config
43 self.common_db = CommonDbClient(config)
44 self.vim_account = self.common_db.get_vim_account(vim_account_id)
45 # self.keystone = self._build_keystone_client(self.vim_account)
46 self.vim_session = None
47 self.nova = self._build_nova_client(self.vim_account)
48 self.cinder = self._build_cinder_client(self.vim_account)
49 self.neutron, self.tenant_id = self._build_neutron_client(self.vim_account)
50
51 def collect(self) -> List[Metric]:
52 metrics = []
53 vim_status = self.is_vim_ok()
54 if vim_status:
55 # Updating the resources in mongoDB
56 self.update_resources()
57 if self.vim_account["_admin"]["projects_read"]:
58 vim_project_id = self.vim_account["_admin"]["projects_read"][0]
59 else:
60 vim_project_id = ""
61 vim_tags = {
62 "vim_account_id": self.vim_account["_id"],
63 "project_id": vim_project_id,
64 }
65 vim_status_metric = Metric(vim_tags, "vim_status", vim_status)
66 metrics.append(vim_status_metric)
67 vnfrs = self.common_db.get_vnfrs(vim_account_id=self.vim_account["_id"])
68 if self.conf.get("collector", "vm_infra_metrics"):
69 vm_infra_metrics_enabled = str(self.conf.get("collector", "vm_infra_metrics")).lower() in ("yes", "true", "1")
70 else:
71 vm_infra_metrics_enabled = True
72 if vm_infra_metrics_enabled:
73 for vnfr in vnfrs:
74 nsr_id = vnfr["nsr-id-ref"]
75 ns_name = self.common_db.get_nsr(nsr_id)["name"]
76 vnf_member_index = vnfr["member-vnf-index-ref"]
77 if vnfr["_admin"]["projects_read"]:
78 vnfr_project_id = vnfr["_admin"]["projects_read"][0]
79 else:
80 vnfr_project_id = ""
81 for vdur in vnfr["vdur"]:
82 if "vim-id" not in vdur:
83 log.debug("Field vim-id is not present in vdur")
84 continue
85 resource_uuid = vdur["vim-id"]
86 tags = {
87 "vim_account_id": self.vim_account["_id"],
88 "resource_uuid": resource_uuid,
89 "ns_id": nsr_id,
90 "ns_name": ns_name,
91 "vnf_member_index": vnf_member_index,
92 "vdu_name": vdur.get("name", ""),
93 "project_id": vnfr_project_id,
94 }
95 try:
96 vm = self.nova.servers.get(resource_uuid)
97 vm_status = (0 if (vm.status == 'ERROR') else 1)
98 vm_status_metric = Metric(tags, "vm_status", vm_status)
99 except Exception as e:
100 log.warning("VM status is not OK: %s" % e)
101 vm_status_metric = Metric(tags, "vm_status", 0)
102 metrics.append(vm_status_metric)
103
104 return metrics
105
106 def is_vim_ok(self) -> bool:
107 try:
108 self.nova.servers.list()
109 return True
110 except Exception as e:
111 log.warning("VIM status is not OK: %s" % e)
112 return False
113
114 def update_resources(self):
115 if "resources" in self.vim_account:
116 vimacc_resources = self.vim_account["resources"]
117 # Compute resources
118 try:
119 com_lim = self.nova.limits.get()._info['absolute']
120 if ("compute" in vimacc_resources) \
121 and ((vimacc_resources["compute"]["ram"]["total"] != com_lim['maxTotalRAMSize'])
122 or (vimacc_resources["compute"]["vcpus"]["total"] != com_lim['maxTotalCores'])
123 or (vimacc_resources["compute"]["ram"]["used"] != com_lim['totalRAMUsed'])
124 or (vimacc_resources["compute"]["vcpus"]["used"] != com_lim['totalCoresUsed'])
125 or (vimacc_resources["compute"]["instances"]["total"] != com_lim['maxTotalInstances'])
126 or (vimacc_resources["compute"]["instances"]["used"] != com_lim['totalInstancesUsed'])):
127 update_dict = {"resources.compute": {"ram": {"total": com_lim['maxTotalRAMSize'],
128 "used": com_lim['totalRAMUsed']},
129 "vcpus": {"total": com_lim['maxTotalCores'],
130 "used": com_lim['totalCoresUsed']},
131 "instances": {"total": com_lim['maxTotalInstances'],
132 "used": com_lim['totalInstancesUsed']}}}
133 suc_value = self.common_db.set_vim_account(str(self.vim_account['_id']), update_dict)
134 log.info("Compute resources update in mongoDB = %s" % suc_value)
135 except Exception as e:
136 log.warning("Error in updating compute resources: %s" % e)
137
138 # Volume resources
139 try:
140 vol_lim = self.cinder.limits.get()._info['absolute']
141 if ("storage" in vimacc_resources) and\
142 ((vimacc_resources["storage"]["volumes"]["total"] != vol_lim['maxTotalVolumes'])
143 or (vimacc_resources["storage"]["snapshots"]["total"] != vol_lim['maxTotalSnapshots'])
144 or (vimacc_resources["storage"]["volumes"]["used"] != vol_lim['totalVolumesUsed'])
145 or (vimacc_resources["storage"]["snapshots"]["used"] != vol_lim['totalSnapshotsUsed'])
146 or (vimacc_resources["storage"]["storage"]["total"] != vol_lim['maxTotalVolumeGigabytes'])
147 or (vimacc_resources["storage"]["storage"]["used"] != vol_lim['totalGigabytesUsed'])):
148 update_dict = {"resources.storage": {"volumes": {"total": vol_lim['maxTotalVolumes'],
149 "used": vol_lim['totalVolumesUsed']},
150 "snapshots": {"total": vol_lim['maxTotalSnapshots'],
151 "used": vol_lim['totalSnapshotsUsed']},
152 "storage": {"total": vol_lim['maxTotalVolumeGigabytes'],
153 "used": vol_lim['totalGigabytesUsed']}}}
154 suc_value = self.common_db.set_vim_account(str(self.vim_account['_id']), update_dict)
155 log.info("Volume resources update in mongoDB = %s" % suc_value)
156 except Exception as e:
157 log.warning("Error in updating volume resources: %s" % e)
158
159 # Network resources
160 try:
161 net_lim = self.neutron.show_quota_details(self.tenant_id)["quota"]
162 if ("network" in vimacc_resources) and\
163 ((vimacc_resources["network"]["networks"]["total"] != net_lim["network"]["limit"])
164 or (vimacc_resources["network"]["networks"]["used"] != net_lim['network']['used'])
165 or (vimacc_resources["network"]["subnets"]["total"] != net_lim['subnet']['limit'])
166 or (vimacc_resources["network"]["subnets"]["used"] != net_lim['subnet']['used'])
167 or (vimacc_resources["network"]["floating_ips"]["total"] != net_lim['floatingip']['limit'])
168 or (vimacc_resources["network"]["floating_ips"]["used"] != net_lim['floatingip']['used'])):
169 update_dict = {"resources.network": {"networks": {"total": net_lim['network']['limit'],
170 "used": net_lim['network']['used']},
171 "subnets": {"total": net_lim['subnet']['limit'],
172 "used": net_lim['subnet']['used']},
173 "floating_ips": {"total": net_lim['floatingip']['limit'],
174 "used": net_lim['floatingip']['used']}}}
175 suc_value = self.common_db.set_vim_account(str(self.vim_account['_id']), update_dict)
176 log.info("Network resources update in mongoDB = %s" % suc_value)
177 except Exception as e:
178 log.warning("Error in updating network resources: %s" % e)
179
180 def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
181 sess = OpenstackUtils.get_session(vim_account)
182 return keystone_client.Client(session=sess, timeout=10)
183
184 def _build_nova_client(self, vim_account: dict) -> nova_client.Client:
185 sess = OpenstackUtils.get_session(vim_account)
186 self.vim_session = sess
187 return nova_client.Client("2", session=sess, timeout=10)
188
189 def _build_cinder_client(self, vim_account: dict) -> cinder_client.Client:
190 # sess = OpenstackUtils.get_session(vim_account)
191 return cinder_client.Client("3", session=self.vim_session, timeout=10)
192
193 def _build_neutron_client(self, vim_account: dict) -> tuple:
194 # sess = OpenstackUtils.get_session(vim_account)
195 tenant_id = self.vim_session.get_project_id()
196 return neutron_client.Client("2", session=self.vim_session, timeout=10), tenant_id