Adds partial support for interface metrics in OpenStack plugin
[osm/MON.git] / osm_mon / collector / vnf_collectors / openstack.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 datetime
23 import json
24 import logging
25 from typing import List
26
27 import gnocchiclient.exceptions
28 from gnocchiclient.v1 import client as gnocchi_client
29 from ceilometerclient.v2 import client as ceilometer_client
30 from keystoneauth1 import session
31 from keystoneauth1.exceptions import EndpointNotFound
32 from keystoneauth1.identity import v3
33
34 from osm_mon.collector.metric import Metric
35 from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector
36 from osm_mon.collector.vnf_metric import VnfMetric
37 from osm_mon.core.auth import AuthManager
38 from osm_mon.core.common_db import CommonDbClient
39 from osm_mon.core.config import Config
40
41 log = logging.getLogger(__name__)
42
43 METRIC_MAPPINGS = {
44 "average_memory_utilization": "memory.usage",
45 "disk_read_ops": "disk.read.requests.rate",
46 "disk_write_ops": "disk.write.requests.rate",
47 "disk_read_bytes": "disk.read.bytes.rate",
48 "disk_write_bytes": "disk.write.bytes.rate",
49 "packets_in_dropped": "network.outgoing.packets.drop",
50 "packets_out_dropped": "network.incoming.packets.drop",
51 "packets_received": "network.incoming.packets.rate",
52 "packets_sent": "network.outgoing.packets.rate",
53 "cpu_utilization": "cpu_util",
54 }
55
56 INTERFACE_METRICS = ['packets_in_dropped', 'packets_out_dropped', 'packets_received', 'packets_sent']
57
58
59 class OpenstackCollector(BaseVimCollector):
60 def __init__(self, config: Config, vim_account_id: str):
61 super().__init__(config, vim_account_id)
62 self.conf = config
63 self.common_db = CommonDbClient(config)
64 self.auth_manager = AuthManager(config)
65 self.granularity = self._get_granularity(vim_account_id)
66 self.backend = self._get_backend(vim_account_id)
67 self.client = self._build_client(vim_account_id)
68
69 def _get_resource_uuid(self, nsr_id, vnf_member_index, vdur_name) -> str:
70 vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name)
71 return vdur['vim-id']
72
73 def _build_gnocchi_client(self, vim_account_id: str) -> gnocchi_client.Client:
74 creds = self.auth_manager.get_credentials(vim_account_id)
75 verify_ssl = self.auth_manager.is_verify_ssl(vim_account_id)
76 auth = v3.Password(auth_url=creds.url,
77 username=creds.user,
78 password=creds.password,
79 project_name=creds.tenant_name,
80 project_domain_id='default',
81 user_domain_id='default')
82 sess = session.Session(auth=auth, verify=verify_ssl)
83 return gnocchi_client.Client(session=sess)
84
85 def _build_ceilometer_client(self, vim_account_id: str) -> ceilometer_client.Client:
86 creds = self.auth_manager.get_credentials(vim_account_id)
87 verify_ssl = self.auth_manager.is_verify_ssl(vim_account_id)
88 auth = v3.Password(auth_url=creds.url,
89 username=creds.user,
90 password=creds.password,
91 project_name=creds.tenant_name,
92 project_domain_id='default',
93 user_domain_id='default')
94 sess = session.Session(auth=auth, verify=verify_ssl)
95 return ceilometer_client.Client(session=sess)
96
97 def _get_granularity(self, vim_account_id: str):
98 creds = self.auth_manager.get_credentials(vim_account_id)
99 vim_config = json.loads(creds.config)
100 if 'granularity' in vim_config:
101 return int(vim_config['granularity'])
102 else:
103 return int(self.conf.get('openstack', 'default_granularity'))
104
105 def collect(self, vnfr: dict) -> List[Metric]:
106 nsr_id = vnfr['nsr-id-ref']
107 vnf_member_index = vnfr['member-vnf-index-ref']
108 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
109 metrics = []
110 for vdur in vnfr['vdur']:
111 # This avoids errors when vdur records have not been completely filled
112 if 'name' not in vdur:
113 continue
114 vdu = next(
115 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
116 )
117 if 'monitoring-param' in vdu:
118 for param in vdu['monitoring-param']:
119 metric_name = param['nfvi-metric']
120 openstack_metric_name = METRIC_MAPPINGS[metric_name]
121 try:
122 resource_id = self._get_resource_uuid(nsr_id, vnf_member_index, vdur['name'])
123 except ValueError:
124 log.warning(
125 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
126 "Was it recently deleted?".format(
127 vdur['name'], vnf_member_index, nsr_id))
128 continue
129 if self.backend == 'ceilometer':
130 measures = self.client.samples.list(meter_name=openstack_metric_name, limit=1, q=[
131 {'field': 'resource_id', 'op': 'eq', 'value': resource_id}])
132 if len(measures):
133 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name,
134 measures[0].counter_volume)
135 metrics.append(metric)
136 elif self.backend == 'gnocchi':
137 delta = 10 * self.granularity
138 start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
139 if metric_name in INTERFACE_METRICS:
140 total_measure = 0.0
141 interfaces = self.client.resource.search(resource_type='instance_network_interface',
142 query={'=': {'instance_id': resource_id}})
143 for interface in interfaces:
144 try:
145 measures = self.client.metric.get_measures(openstack_metric_name,
146 start=start_date,
147 resource_id=interface['id'],
148 granularity=self.granularity)
149 if len(measures):
150 total_measure += measures[-1][2]
151
152 except gnocchiclient.exceptions.NotFound as e:
153 log.debug("No metric %s found for interface %s: %s", openstack_metric_name,
154 interface['id'], e)
155 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name,
156 total_measure)
157 metrics.append(metric)
158 else:
159 try:
160 measures = self.client.metric.get_measures(openstack_metric_name,
161 start=start_date,
162 resource_id=resource_id,
163 granularity=self.granularity)
164 if len(measures):
165 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name,
166 measures[-1][2])
167 metrics.append(metric)
168 except gnocchiclient.exceptions.NotFound as e:
169 log.debug("No metric %s found for instance %s: %s", openstack_metric_name, resource_id,
170 e)
171
172 else:
173 raise Exception('Unknown metric backend: %s', self.backend)
174 return metrics
175
176 def _build_client(self, vim_account_id):
177 if self.backend == 'ceilometer':
178 return self._build_ceilometer_client(vim_account_id)
179 elif self.backend == 'gnocchi':
180 return self._build_gnocchi_client(vim_account_id)
181 else:
182 raise Exception('Unknown metric backend: %s', self.backend)
183
184 def _get_backend(self, vim_account_id):
185 try:
186 gnocchi = self._build_gnocchi_client(vim_account_id)
187 gnocchi.resource.list(limit=1)
188 return 'gnocchi'
189 except EndpointNotFound:
190 try:
191 ceilometer = self._build_ceilometer_client(vim_account_id)
192 ceilometer.resources.list(limit=1)
193 return 'ceilometer'
194 except Exception:
195 log.exception('Error trying to determine metric backend')
196 raise Exception('Could not determine metric backend')