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