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 cinderclient
import client
as cinder_client
28 from neutronclient
.neutron
import client
as neutron_client
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
36 log
= logging
.getLogger(__name__
)
39 class BaseOpenStackInfraCollector(BaseVimInfraCollector
):
40 def __init__(self
, config
: Config
, vim_account_id
: str):
41 super().__init
__(config
, vim_account_id
)
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
)
51 def collect(self
) -> List
[Metric
]:
53 vim_status
= self
.is_vim_ok()
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]
62 "vim_account_id": self
.vim_account
["_id"],
63 "project_id": vim_project_id
,
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"])
69 nsr_id
= vnfr
["nsr-id-ref"]
70 ns_name
= self
.common_db
.get_nsr(nsr_id
)["name"]
71 vnf_member_index
= vnfr
["member-vnf-index-ref"]
72 if vnfr
["_admin"]["projects_read"]:
73 vnfr_project_id
= vnfr
["_admin"]["projects_read"][0]
76 for vdur
in vnfr
["vdur"]:
77 if "vim-id" not in vdur
:
78 log
.debug("Field vim-id is not present in vdur")
80 resource_uuid
= vdur
["vim-id"]
82 "vim_account_id": self
.vim_account
["_id"],
83 "resource_uuid": resource_uuid
,
86 "vnf_member_index": vnf_member_index
,
87 "vdur_name": vdur
.get("name", ""),
88 "project_id": vnfr_project_id
,
91 vm
= self
.nova
.servers
.get(resource_uuid
)
92 vm_status
= 1 if vm
.status
== "ACTIVE" else 0
93 vm_status_metric
= Metric(tags
, "vm_status", vm_status
)
94 except Exception as e
:
95 log
.warning("VM status is not OK: %s" % e
)
96 vm_status_metric
= Metric(tags
, "vm_status", 0)
97 metrics
.append(vm_status_metric
)
101 def is_vim_ok(self
) -> bool:
103 self
.nova
.servers
.list()
105 except Exception as e
:
106 log
.warning("VIM status is not OK: %s" % e
)
109 def update_resources(self
):
110 if "resources" in self
.vim_account
:
111 vimacc_resources
= self
.vim_account
["resources"]
114 com_lim
= self
.nova
.limits
.get()._info
['absolute']
115 if ("compute" in vimacc_resources
) \
116 and ((vimacc_resources
["compute"]["ram"]["total"] != com_lim
['maxTotalRAMSize'])
117 or (vimacc_resources
["compute"]["vcpus"]["total"] != com_lim
['maxTotalCores'])
118 or (vimacc_resources
["compute"]["ram"]["used"] != com_lim
['totalRAMUsed'])
119 or (vimacc_resources
["compute"]["vcpus"]["used"] != com_lim
['totalCoresUsed'])
120 or (vimacc_resources
["compute"]["instances"]["total"] != com_lim
['maxTotalInstances'])
121 or (vimacc_resources
["compute"]["instances"]["used"] != com_lim
['totalInstancesUsed'])):
122 update_dict
= {"resources.compute": {"ram": {"total": com_lim
['maxTotalRAMSize'],
123 "used": com_lim
['totalRAMUsed']},
124 "vcpus": {"total": com_lim
['maxTotalCores'],
125 "used": com_lim
['totalCoresUsed']},
126 "instances": {"total": com_lim
['maxTotalInstances'],
127 "used": com_lim
['totalInstancesUsed']}}}
128 suc_value
= self
.common_db
.set_vim_account(str(self
.vim_account
['_id']), update_dict
)
129 log
.info("Compute resources update in mongoDB = %s" % suc_value
)
130 except Exception as e
:
131 log
.warning("Error in updating compute resources: %s" % e
)
135 vol_lim
= self
.cinder
.limits
.get()._info
['absolute']
136 if ("storage" in vimacc_resources
) and\
137 ((vimacc_resources
["storage"]["volumes"]["total"] != vol_lim
['maxTotalVolumes'])
138 or (vimacc_resources
["storage"]["snapshots"]["total"] != vol_lim
['maxTotalSnapshots'])
139 or (vimacc_resources
["storage"]["volumes"]["used"] != vol_lim
['totalVolumesUsed'])
140 or (vimacc_resources
["storage"]["snapshots"]["used"] != vol_lim
['totalSnapshotsUsed'])
141 or (vimacc_resources
["storage"]["storage"]["total"] != vol_lim
['maxTotalVolumeGigabytes'])
142 or (vimacc_resources
["storage"]["storage"]["used"] != vol_lim
['totalGigabytesUsed'])):
143 update_dict
= {"resources.storage": {"volumes": {"total": vol_lim
['maxTotalVolumes'],
144 "used": vol_lim
['totalVolumesUsed']},
145 "snapshots": {"total": vol_lim
['maxTotalSnapshots'],
146 "used": vol_lim
['totalSnapshotsUsed']},
147 "storage": {"total": vol_lim
['maxTotalVolumeGigabytes'],
148 "used": vol_lim
['totalGigabytesUsed']}}}
149 suc_value
= self
.common_db
.set_vim_account(str(self
.vim_account
['_id']), update_dict
)
150 log
.info("Volume resources update in mongoDB = %s" % suc_value
)
151 except Exception as e
:
152 log
.warning("Error in updating volume resources: %s" % e
)
156 net_lim
= self
.neutron
.show_quota_details(self
.tenant_id
)["quota"]
157 if ("network" in vimacc_resources
) and\
158 ((vimacc_resources
["network"]["networks"]["total"] != net_lim
["network"]["limit"])
159 or (vimacc_resources
["network"]["networks"]["used"] != net_lim
['network']['used'])
160 or (vimacc_resources
["network"]["subnets"]["total"] != net_lim
['subnet']['limit'])
161 or (vimacc_resources
["network"]["subnets"]["used"] != net_lim
['subnet']['used'])
162 or (vimacc_resources
["network"]["floating_ips"]["total"] != net_lim
['floatingip']['limit'])
163 or (vimacc_resources
["network"]["floating_ips"]["used"] != net_lim
['floatingip']['used'])):
164 update_dict
= {"resources.network": {"networks": {"total": net_lim
['network']['limit'],
165 "used": net_lim
['network']['used']},
166 "subnets": {"total": net_lim
['subnet']['limit'],
167 "used": net_lim
['subnet']['used']},
168 "floating_ips": {"total": net_lim
['floatingip']['limit'],
169 "used": net_lim
['floatingip']['used']}}}
170 suc_value
= self
.common_db
.set_vim_account(str(self
.vim_account
['_id']), update_dict
)
171 log
.info("Network resources update in mongoDB = %s" % suc_value
)
172 except Exception as e
:
173 log
.warning("Error in updating network resources: %s" % e
)
175 def _build_keystone_client(self
, vim_account
: dict) -> keystone_client
.Client
:
176 sess
= OpenstackUtils
.get_session(vim_account
)
177 return keystone_client
.Client(session
=sess
, timeout
=10)
179 def _build_nova_client(self
, vim_account
: dict) -> nova_client
.Client
:
180 sess
= OpenstackUtils
.get_session(vim_account
)
181 self
.vim_session
= sess
182 return nova_client
.Client("2", session
=sess
, timeout
=10)
184 def _build_cinder_client(self
, vim_account
: dict) -> cinder_client
.Client
:
185 # sess = OpenstackUtils.get_session(vim_account)
186 return cinder_client
.Client("3", session
=self
.vim_session
, timeout
=10)
188 def _build_neutron_client(self
, vim_account
: dict) -> tuple:
189 # sess = OpenstackUtils.get_session(vim_account)
190 tenant_id
= self
.vim_session
.get_project_id()
191 return neutron_client
.Client("2", session
=self
.vim_session
, timeout
=10), tenant_id