1 # -*- coding: utf-8 -*-
4 # Copyright 2016-2019 VMware Inc.
5 # This file is part of ETSI OSM
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: osslegalrouting@vmware.com
29 from osm_mon
.collector
.vnf_metric
import VnfMetric
30 from osm_mon
.collector
.vnf_collectors
.vrops
.metrics
import METRIC_MAPPINGS
33 log
= logging
.getLogger(__name__
)
36 # If the unit from vROPS does not align with the expected value. multiply by the specified amount to ensure
37 # the correct unit is returned.
38 METRIC_MULTIPLIERS
= {
39 "disk_read_bytes": 1024,
40 "disk_write_bytes": 1024,
41 "packets_received": 1024,
49 vrops_site
='https://vrops',
52 self
.vrops_site
= vrops_site
53 self
.vrops_user
= vrops_user
54 self
.vrops_password
= vrops_password
56 def get_vm_resource_list_from_vrops(self
):
57 """ Find all known resource IDs in vROPs
59 api_url
= '/suite-api/api/resources?resourceKind=VirtualMachine'
60 headers
= {'Accept': 'application/json'}
63 resp
= requests
.get(self
.vrops_site
+ api_url
,
64 auth
=(self
.vrops_user
, self
.vrops_password
),
65 verify
=False, headers
=headers
)
67 if resp
.status_code
!= 200:
68 log
.error("Failed to get resource list from vROPS: {} {}".format(resp
.status_code
,
73 resp_data
= json
.loads(resp
.content
.decode('utf-8'))
74 if resp_data
.get('resourceList') is not None:
75 resource_list
= resp_data
.get('resourceList')
77 except Exception as exp
:
78 log
.error("get_vm_resource_id: Error in parsing {}\n{}".format(exp
, traceback
.format_exc()))
88 # Collect the names of all the metrics we need to query
89 for metric_entry
in monitoring_params
:
90 metric_name
= metric_entry
['nfvi-metric']
91 if metric_name
not in METRIC_MAPPINGS
:
92 log
.debug("Metric {} not supported, ignoring".format(metric_name
))
94 monitoring_keys
[metric_name
] = METRIC_MAPPINGS
[metric_name
]
97 # Make a query for only the stats we have been asked for
99 for stat
in monitoring_keys
.values():
100 stats_key
+= "&statKey={}".format(stat
)
102 # And only ask for the resource ids that we are interested in
104 sanitized_vdu_mappings
= copy
.deepcopy(vdu_mappings
)
105 for key
in vdu_mappings
.keys():
106 vdu
= vdu_mappings
[key
]
107 if 'vrops_id' not in vdu
:
108 log
.info("Could not find vROPS id for vdu {}".format(vdu
))
109 del sanitized_vdu_mappings
[key
]
111 resource_ids
+= "&resourceId={}".format(vdu
['vrops_id'])
112 vdu_mappings
= sanitized_vdu_mappings
116 # Now we can make a single call to vROPS to collect all relevant metrics for resources we need to monitor
117 api_url
= "/suite-api/api/resources/stats?IntervalType=MINUTES&IntervalCount=1"\
118 "&rollUpType=MAX¤tOnly=true{}{}".format(stats_key
, resource_ids
)
119 headers
= {'Accept': 'application/json'}
121 resp
= requests
.get(self
.vrops_site
+ api_url
,
122 auth
=(self
.vrops_user
, self
.vrops_password
), verify
=False, headers
=headers
)
124 if resp
.status_code
!= 200:
125 log
.error("Failed to get Metrics data from vROPS for {} {}".format(resp
.status_code
,
129 m_data
= json
.loads(resp
.content
.decode('utf-8'))
130 if 'values' not in m_data
:
133 statistics
= m_data
['values']
134 for vdu_stat
in statistics
:
135 vrops_id
= vdu_stat
['resourceId']
137 for vdu
in vdu_mappings
.values():
138 if vdu
['vrops_id'] == vrops_id
:
139 vdu_name
= vdu
['name']
142 for item
in vdu_stat
['stat-list']['stat']:
143 reported_metric
= item
['statKey']['key']
144 if reported_metric
not in METRIC_MAPPINGS
.values():
147 # Convert the vROPS metric name back to OSM key
148 metric_name
= list(METRIC_MAPPINGS
.keys())[list(METRIC_MAPPINGS
.values()).
149 index(reported_metric
)]
150 if metric_name
in monitoring_keys
.keys():
151 metric_value
= item
['data'][-1]
152 if metric_name
in METRIC_MULTIPLIERS
:
153 metric_value
*= METRIC_MULTIPLIERS
[metric_name
]
154 metric
= VnfMetric(vnfr
['nsr-id-ref'],
155 vnfr
['member-vnf-index-ref'],
160 metrics
.append(metric
)
162 except Exception as exp
:
163 log
.error("Exception while parsing metrics data from vROPS {}\n{}".format(exp
, traceback
.format_exc()))