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 gnocchiclient
.v1
import client
as gnocchi_client
29 from keystoneauth1
import session
30 from keystoneauth1
.identity
import v3
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
38 log
= logging
.getLogger(__name__
)
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",
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
)
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
)
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
,
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
)
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'])
83 cfg
= Config
.instance()
84 return cfg
.OS_DEFAULT_GRANULARITY
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'])
91 for vdur
in vnfr
['vdur']:
92 # This avoids errors when vdur records have not been completely filled
93 if 'name' not in vdur
:
96 filter(lambda vdu
: vdu
['id'] == vdur
['vdu-id-ref'], vnfd
['vdu'])
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 start_date
= datetime
.datetime
.now() - datetime
.timedelta(seconds
=self
.granularity
)
103 resource_id
= self
._get
_resource
_uuid
(nsr_id
, vnf_member_index
, vdur
['name'])
105 measures
= self
.gnocchi_client
.metric
.get_measures(gnocchi_metric_name
,
107 resource_id
=resource_id
,
108 granularity
=self
.granularity
)
110 metric
= Metric(nsr_id
, vnf_member_index
, vdur
['name'], metric_name
, measures
[-1][2])
111 metrics
.append(metric
)
112 except gnocchiclient
.exceptions
.NotFound
as e
:
113 log
.debug("No metric found: %s", e
)