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
24 from typing
import List
26 from n2vc
.vnf
import N2VC
28 from osm_mon
.collector
.metric
import Metric
29 from osm_mon
.collector
.vnf_collectors
.base
import BaseCollector
30 from osm_mon
.collector
.vnf_metric
import VnfMetric
31 from osm_mon
.core
.common_db
import CommonDbClient
32 from osm_mon
.core
.config
import Config
33 from osm_mon
.core
.exceptions
import VcaDeploymentInfoNotFound
35 log
= logging
.getLogger(__name__
)
38 class VCACollector(BaseCollector
):
39 def __init__(self
, config
: Config
):
40 super().__init
__(config
)
41 self
.common_db
= CommonDbClient(config
)
42 self
.loop
= asyncio
.get_event_loop()
43 self
.n2vc
= N2VC(server
=config
.get('vca', 'host'),
44 user
=config
.get('vca', 'user'),
45 secret
=config
.get('vca', 'secret'),
46 ca_cert
=config
.get('vca', 'cacert'))
48 def collect(self
, vnfr
: dict) -> List
[Metric
]:
49 nsr_id
= vnfr
['nsr-id-ref']
50 vnf_member_index
= vnfr
['member-vnf-index-ref']
51 vnfd
= self
.common_db
.get_vnfd(vnfr
['vnfd-id'])
53 # Populate extra tags for metrics
55 tags
['ns_name'] = self
.common_db
.get_nsr(nsr_id
)['name']
56 if vnfr
['_admin']['projects_read']:
57 tags
['project_id'] = vnfr
['_admin']['projects_read'][0]
59 tags
['project_id'] = ''
62 for vdur
in vnfr
['vdur']:
63 # This avoids errors when vdur records have not been completely filled
64 if 'name' not in vdur
:
67 filter(lambda vdu
: vdu
['id'] == vdur
['vdu-id-ref'], vnfd
['vdu'])
69 if 'vdu-configuration' in vdu
and 'metrics' in vdu
['vdu-configuration']:
71 vca_deployment_info
= self
.get_vca_deployment_info(nsr_id
, vnf_member_index
, vdur
['vdu-id-ref'],
73 except VcaDeploymentInfoNotFound
as e
:
76 measures
= self
.loop
.run_until_complete(self
.n2vc
.GetMetrics(vca_deployment_info
['model'],
77 vca_deployment_info
['application']))
78 log
.debug('Measures: %s', measures
)
79 for measure_list
in measures
.values():
80 for measure
in measure_list
:
81 log
.debug("Measure: %s", measure
)
82 metric
= VnfMetric(nsr_id
, vnf_member_index
, vdur
['name'], measure
['key'],
83 float(measure
['value']), tags
)
84 metrics
.append(metric
)
85 if 'vnf-configuration' in vnfd
and 'metrics' in vnfd
['vnf-configuration']:
87 vca_deployment_info
= self
.get_vca_deployment_info(nsr_id
, vnf_member_index
)
88 except VcaDeploymentInfoNotFound
as e
:
91 measures
= self
.loop
.run_until_complete(self
.n2vc
.GetMetrics(vca_deployment_info
['model'],
92 vca_deployment_info
['application']))
93 # Search for Mgmt VDU name, needed to query Prometheus based on alarm tags
94 # TODO: check a better way to look for Mgmt VDU
95 for vdur
in vnfr
['vdur']:
96 for interface
in vdur
['interfaces']:
97 if 'mgmt-vnf' in interface
:
98 vdu_name
= vdur
['name']
100 log
.debug('Measures: %s', measures
)
101 for measure_list
in measures
.values():
102 for measure
in measure_list
:
103 log
.debug("Measure: %s", measure
)
104 metric
= VnfMetric(nsr_id
, vnf_member_index
, vdu_name
,
105 measure
['key'], float(measure
['value']), tags
)
106 metrics
.append(metric
)
109 def get_vca_deployment_info(self
, nsr_id
, vnf_member_index
, vdu_id
=None, vdu_count
=0):
110 nsr
= self
.common_db
.get_nsr(nsr_id
)
111 for vca_deployment
in nsr
["_admin"]["deployed"]["VCA"]:
114 if vca_deployment
['member-vnf-index'] == vnf_member_index
and vca_deployment
['vdu_id'] is None:
115 return vca_deployment
117 if vca_deployment
['member-vnf-index'] == vnf_member_index
and \
118 vca_deployment
['vdu_id'] == vdu_id
and vca_deployment
['vdu_count_index'] == vdu_count
:
119 return vca_deployment
120 raise VcaDeploymentInfoNotFound(
121 "VCA deployment info for nsr_id {}, index {}, vdu_id {} and vdu_count_index {} not found.".format(