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