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
25 from typing
import List
27 import gnocchiclient
.exceptions
28 from ceilometerclient
.v2
import client
as ceilometer_client
29 from gnocchiclient
.v1
import client
as gnocchi_client
30 from keystoneauth1
import session
31 from keystoneauth1
.exceptions
import EndpointNotFound
32 from keystoneauth1
.identity
import v3
34 from osm_mon
.collector
.metric
import Metric
35 from osm_mon
.collector
.utils
import CollectorUtils
36 from osm_mon
.collector
.vnf_collectors
.base_vim
import BaseVimCollector
37 from osm_mon
.collector
.vnf_metric
import VnfMetric
38 from osm_mon
.core
.common_db
import CommonDbClient
39 from osm_mon
.core
.config
import Config
41 log
= logging
.getLogger(__name__
)
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",
56 INTERFACE_METRICS
= ['packets_in_dropped', 'packets_out_dropped', 'packets_received', 'packets_sent']
59 class OpenstackCollector(BaseVimCollector
):
60 def __init__(self
, config
: Config
, vim_account_id
: str):
61 super().__init
__(config
, vim_account_id
)
63 self
.common_db
= CommonDbClient(config
)
64 self
.backend
= self
._get
_backend
(vim_account_id
)
65 self
.client
= self
._build
_client
(vim_account_id
)
66 self
.granularity
= self
._get
_granularity
(vim_account_id
)
68 def _get_resource_uuid(self
, nsr_id
, vnf_member_index
, vdur_name
) -> str:
69 vdur
= self
.common_db
.get_vdur(nsr_id
, vnf_member_index
, vdur_name
)
72 def _build_gnocchi_client(self
, vim_account_id
: str) -> gnocchi_client
.Client
:
73 creds
= CollectorUtils
.get_credentials(vim_account_id
)
74 verify_ssl
= CollectorUtils
.is_verify_ssl(creds
)
75 auth
= v3
.Password(auth_url
=creds
.url
,
77 password
=creds
.password
,
78 project_name
=creds
.tenant_name
,
79 project_domain_id
='default',
80 user_domain_id
='default')
81 sess
= session
.Session(auth
=auth
, verify
=verify_ssl
)
82 return gnocchi_client
.Client(session
=sess
)
84 def _build_ceilometer_client(self
, vim_account_id
: str) -> ceilometer_client
.Client
:
85 creds
= CollectorUtils
.get_credentials(vim_account_id
)
86 verify_ssl
= CollectorUtils
.is_verify_ssl(creds
)
87 auth
= v3
.Password(auth_url
=creds
.url
,
89 password
=creds
.password
,
90 project_name
=creds
.tenant_name
,
91 project_domain_id
='default',
92 user_domain_id
='default')
93 sess
= session
.Session(auth
=auth
, verify
=verify_ssl
)
94 return ceilometer_client
.Client(session
=sess
)
96 def _get_granularity(self
, vim_account_id
):
97 creds
= CollectorUtils
.get_credentials(vim_account_id
)
98 vim_config
= json
.loads(creds
.config
)
99 if 'granularity' in vim_config
:
100 return int(vim_config
['granularity'])
102 return int(self
.conf
.get('openstack', 'default_granularity'))
104 def collect(self
, vnfr
: dict) -> List
[Metric
]:
105 nsr_id
= vnfr
['nsr-id-ref']
106 vnf_member_index
= vnfr
['member-vnf-index-ref']
107 vnfd
= self
.common_db
.get_vnfd(vnfr
['vnfd-id'])
109 for vdur
in vnfr
['vdur']:
110 # This avoids errors when vdur records have not been completely filled
111 if 'name' not in vdur
:
114 filter(lambda vdu
: vdu
['id'] == vdur
['vdu-id-ref'], vnfd
['vdu'])
116 if 'monitoring-param' in vdu
:
117 for param
in vdu
['monitoring-param']:
118 metric_name
= param
['nfvi-metric']
119 openstack_metric_name
= METRIC_MAPPINGS
[metric_name
]
121 resource_id
= self
._get
_resource
_uuid
(nsr_id
, vnf_member_index
, vdur
['name'])
124 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
125 "Was it recently deleted?",
126 vdur
['name'], vnf_member_index
, nsr_id
)
128 if self
.backend
== 'ceilometer':
129 measures
= self
.client
.samples
.list(meter_name
=openstack_metric_name
, limit
=1, q
=[
130 {'field': 'resource_id', 'op': 'eq', 'value': resource_id
}])
132 metric
= VnfMetric(nsr_id
, vnf_member_index
, vdur
['name'], metric_name
,
133 measures
[0].counter_volume
)
134 metrics
.append(metric
)
135 if self
.backend
== 'gnocchi':
136 delta
= 10 * self
.granularity
137 start_date
= datetime
.datetime
.now() - datetime
.timedelta(seconds
=delta
)
138 if metric_name
in INTERFACE_METRICS
:
140 interfaces
= self
.client
.resource
.search(resource_type
='instance_network_interface',
141 query
={'=': {'instance_id': resource_id
}})
142 for interface
in interfaces
:
144 measures
= self
.client
.metric
.get_measures(openstack_metric_name
,
146 resource_id
=interface
['id'],
147 granularity
=self
.granularity
)
149 if not total_measure
:
151 total_measure
+= measures
[-1][2]
153 except gnocchiclient
.exceptions
.NotFound
as e
:
154 log
.debug("No metric %s found for interface %s: %s", openstack_metric_name
,
157 metric
= VnfMetric(nsr_id
, vnf_member_index
, vdur
['name'], metric_name
,
159 metrics
.append(metric
)
162 measures
= self
.client
.metric
.get_measures(openstack_metric_name
,
164 resource_id
=resource_id
,
165 granularity
=self
.granularity
)
167 metric
= VnfMetric(nsr_id
, vnf_member_index
, vdur
['name'], metric_name
,
169 metrics
.append(metric
)
170 except gnocchiclient
.exceptions
.NotFound
as e
:
171 log
.debug("No metric %s found for instance %s: %s", openstack_metric_name
, resource_id
,
175 raise Exception('Unknown client class: %s', self
.client
)
178 def _build_client(self
, vim_account_id
):
179 if self
.backend
== 'ceilometer':
180 return self
._build
_ceilometer
_client
(vim_account_id
)
181 elif self
.backend
== 'gnocchi':
182 return self
._build
_gnocchi
_client
(vim_account_id
)
184 raise Exception('Unknown metric backend: %s', self
.backend
)
186 def _get_backend(self
, vim_account_id
):
188 gnocchi
= self
._build
_gnocchi
_client
(vim_account_id
)
189 gnocchi
.resource
.list(limit
=1)
191 except EndpointNotFound
:
193 ceilometer
= self
._build
_ceilometer
_client
(vim_account_id
)
194 ceilometer
.resources
.list(limit
=1)
197 log
.exception('Error trying to determine metric backend')
198 raise Exception('Could not determine metric backend')