Adds collection of vim status metric
[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 keystoneauth1 import session
30 from keystoneauth1.identity import v3
31
32 from osm_mon.collector.metric import Metric
33 from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector
34 from osm_mon.collector.vnf_metric import VnfMetric
35 from osm_mon.core.auth import AuthManager
36 from osm_mon.core.common_db import CommonDbClient
37 from osm_mon.core.settings import Config
38
39 log = logging.getLogger(__name__)
40
41 METRIC_MAPPINGS = {
42 "average_memory_utilization": "memory.usage",
43 "disk_read_ops": "disk.read.requests",
44 "disk_write_ops": "disk.write.requests",
45 "disk_read_bytes": "disk.read.bytes",
46 "disk_write_bytes": "disk.write.bytes",
47 "packets_dropped": "interface.if_dropped",
48 "packets_received": "interface.if_packets",
49 "packets_sent": "interface.if_packets",
50 "cpu_utilization": "cpu_util",
51 }
52
53
54 class OpenstackCollector(BaseVimCollector):
55 def __init__(self, vim_account_id: str):
56 super().__init__(vim_account_id)
57 self.common_db = CommonDbClient()
58 self.auth_manager = AuthManager()
59 self.granularity = self._get_granularity(vim_account_id)
60 self.gnocchi_client = self._build_gnocchi_client(vim_account_id)
61
62 def _get_resource_uuid(self, nsr_id, vnf_member_index, vdur_name) -> str:
63 vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name)
64 return vdur['vim-id']
65
66 def _build_gnocchi_client(self, vim_account_id: str) -> gnocchi_client.Client:
67 creds = self.auth_manager.get_credentials(vim_account_id)
68 verify_ssl = self.auth_manager.is_verify_ssl(vim_account_id)
69 auth = v3.Password(auth_url=creds.url,
70 username=creds.user,
71 password=creds.password,
72 project_name=creds.tenant_name,
73 project_domain_id='default',
74 user_domain_id='default')
75 sess = session.Session(auth=auth, verify=verify_ssl)
76 return gnocchi_client.Client(session=sess)
77
78 def _get_granularity(self, vim_account_id: str):
79 creds = self.auth_manager.get_credentials(vim_account_id)
80 vim_config = json.loads(creds.config)
81 if 'granularity' in vim_config:
82 return int(vim_config['granularity'])
83 else:
84 cfg = Config.instance()
85 return cfg.OS_DEFAULT_GRANULARITY
86
87 def collect(self, vnfr: dict) -> List[Metric]:
88 nsr_id = vnfr['nsr-id-ref']
89 vnf_member_index = vnfr['member-vnf-index-ref']
90 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
91 metrics = []
92 for vdur in vnfr['vdur']:
93 # This avoids errors when vdur records have not been completely filled
94 if 'name' not in vdur:
95 continue
96 vdu = next(
97 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
98 )
99 if 'monitoring-param' in vdu:
100 for param in vdu['monitoring-param']:
101 metric_name = param['nfvi-metric']
102 gnocchi_metric_name = METRIC_MAPPINGS[metric_name]
103 delta = 10 * self.granularity
104 start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
105 resource_id = self._get_resource_uuid(nsr_id, vnf_member_index, vdur['name'])
106 try:
107 measures = self.gnocchi_client.metric.get_measures(gnocchi_metric_name,
108 start=start_date,
109 resource_id=resource_id,
110 granularity=self.granularity)
111 if len(measures):
112 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, measures[-1][2])
113 metrics.append(metric)
114 except gnocchiclient.exceptions.NotFound as e:
115 log.debug("No metric found: %s", e)
116 pass
117 return metrics