
Change-Id: Idcd46ce2d50c8d3d135e52153f9da4250c5b5ae7 fix ci errors Change-Id: I2c75fdb1698a6e87f04951dcbe7b0dc5e2bd95e5 update unit test to create test directory Change-Id: Ie650de50421be2a5ffc12114a72184b2d88c3972 fix use user home if cannot write to /var/lib/ceilometer Change-Id: I3083c8004a325506e00fe6d57c3be47f589126f2 revise path handling for mock Change-Id: I9f072383c954b962ce6ca9daec879832f37672be
175 lines
6.9 KiB
Python
175 lines
6.9 KiB
Python
# Copyright 2015, 2020 IBM Corp.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
|
|
import six
|
|
import time
|
|
|
|
from ceilometer.compute.virt import inspector as virt_inspector
|
|
from ceilometer.i18n import _
|
|
from ceilometer_zvm.compute.virt.zvm import exception
|
|
from ceilometer_zvm.compute.virt.zvm import utils as zvmutils
|
|
from oslo_config import cfg
|
|
from oslo_utils import units
|
|
from oslo_log import log
|
|
|
|
LOG = log.getLogger(__name__)
|
|
|
|
zvm_opts = [
|
|
cfg.URIOpt('zvm_cloud_connector_url',
|
|
help="""
|
|
URL to be used to communicate with z/VM Cloud Connector.
|
|
Example: https://10.10.10.1:8080.
|
|
"""),
|
|
cfg.StrOpt('zvm_cloud_connector_token_file',
|
|
default=None,
|
|
help="""
|
|
Token file that contains the admin token to be used when sending
|
|
request to z/VM Cloud Connector.
|
|
"""),
|
|
cfg.StrOpt('zvm_cloud_connector_ca_file',
|
|
default=None,
|
|
help="""
|
|
CA certificate file to be used to verify z/VM Cloud Connector
|
|
server certificate.
|
|
|
|
A string, it must be a path to a CA bundle to use.
|
|
"""),
|
|
]
|
|
|
|
|
|
class ZVMInspector(virt_inspector.Inspector):
|
|
|
|
def __init__(self, conf):
|
|
super(ZVMInspector, self).__init__(conf)
|
|
self.cache = {}
|
|
self.conf.register_opts(zvm_opts)
|
|
self._reqh = zvmutils.zVMConnectorRequestHandler(
|
|
self.conf)
|
|
|
|
def inspect_vnics(self, instance, duration):
|
|
nics_data = self._inspect_inst_data(instance, 'vnics')
|
|
# Construct the final result
|
|
for nic in nics_data:
|
|
yield virt_inspector.InterfaceStats(
|
|
name=nic['nic_vdev'],
|
|
mac=None,
|
|
fref=None,
|
|
parameters=None,
|
|
rx_bytes=nic['nic_rx'],
|
|
rx_packets=nic['nic_fr_rx'],
|
|
rx_bytes_delta=nic['nic_rx_delta'],
|
|
rx_errors=nic['nic_fr_rx_err'],
|
|
rx_drop=None,
|
|
tx_bytes=nic['nic_tx'],
|
|
tx_packets=nic['nic_fr_tx'],
|
|
tx_bytes_delta=nic['nic_tx_delta'],
|
|
tx_errors=nic['nic_fr_tx_err'],
|
|
tx_drop=None
|
|
)
|
|
|
|
def inspect_instance(self, instance, duration):
|
|
inst_stats = self._inspect_inst_data(instance, 'stats')
|
|
LOG.debug("instance stats:%s", str(inst_stats))
|
|
cpu_number = inst_stats['guest_cpus']
|
|
used_cpu_time = (inst_stats['used_cpu_time_us'] * units.k)
|
|
cpu_util = self._calc_cpu_util(instance, inst_stats)
|
|
used_mem_mb = inst_stats['used_mem_kb'] / units.Ki
|
|
# Construct the final result
|
|
return virt_inspector.InstanceStats(cpu_number=cpu_number,
|
|
cpu_time=used_cpu_time,
|
|
memory_usage=used_mem_mb,
|
|
cpu_util=cpu_util
|
|
)
|
|
|
|
def _save_inst_data(self, instance, inst_stats):
|
|
inst_name = zvmutils.get_inst_name(instance).upper()
|
|
data = inst_stats
|
|
data['time_stamp'] = time.time()
|
|
self.cache[inst_name] = data
|
|
|
|
def _load_inst_data(self, instance):
|
|
inst_name = zvmutils.get_inst_name(instance).upper()
|
|
try:
|
|
return self.cache[inst_name]
|
|
except KeyError:
|
|
LOG.info('no cache for instance %s', inst_name)
|
|
return None
|
|
|
|
def _calc_cpu_util(self, instance, inst_stats):
|
|
previous_stats = self._load_inst_data(instance)
|
|
if previous_stats:
|
|
current_time = time.time()
|
|
previous_time = previous_stats['time_stamp']
|
|
cpu_util = (inst_stats['used_cpu_time_us']/inst_stats['guest_cpus'] - \
|
|
previous_stats['used_cpu_time_us']/previous_stats['guest_cpus']) / \
|
|
((current_time-previous_time)*units.k)
|
|
self._save_inst_data(instance, inst_stats)
|
|
return cpu_util
|
|
else:
|
|
cpu_util = inst_stats['used_cpu_time_us']/ \
|
|
inst_stats['elapsed_cpu_time_us']
|
|
self._save_inst_data(instance, inst_stats)
|
|
return cpu_util
|
|
|
|
def _inspect_inst_data(self, instance, inspect_type):
|
|
inspect_data = {}
|
|
# Make sure the inst name in upper case
|
|
inst_name = zvmutils.get_inst_name(instance).upper()
|
|
msg_shutdown = _("Can not get vm info in shutdown state "
|
|
"for %s") % inst_name
|
|
msg_notexist = _("Can not get vm info for %s, vm not exist"
|
|
) % inst_name
|
|
msg_nodata = _("Failed to get vm info for %s") % inst_name
|
|
# zvm inspector can not get instance info in shutdown stat
|
|
if zvmutils.get_inst_power_state(instance) == 0x04:
|
|
raise virt_inspector.InstanceShutOffException(msg_shutdown)
|
|
try:
|
|
if inspect_type == 'stats':
|
|
inspect_data = self._reqh.call('guest_inspect_stats',
|
|
inst_name)
|
|
elif inspect_type == 'vnics':
|
|
inspect_data = self._reqh.call('guest_inspect_vnics',
|
|
inst_name)
|
|
except Exception as err:
|
|
msg_nodata += _(". Error: %s") % six.text_type(err)
|
|
raise virt_inspector.NoDataException(msg_nodata)
|
|
|
|
# Check the inst data is in the returned result
|
|
index_key = inst_name
|
|
if index_key not in inspect_data:
|
|
# Check the reason: shutdown or not exist or other error
|
|
power_stat = ''
|
|
try:
|
|
power_stat = self._reqh.call('guest_get_power_state',
|
|
inst_name)
|
|
except exception.ZVMConnectorRequestFailed as err:
|
|
if err.results['overallRC'] == 404:
|
|
# instance not exists
|
|
raise virt_inspector.InstanceNotFoundException(msg_notexist
|
|
)
|
|
else:
|
|
msg_nodata += _(". Error: %s") % six.text_type(err)
|
|
raise virt_inspector.NoDataException(msg_nodata)
|
|
except Exception as err:
|
|
msg_nodata += _(". Error: %s") % six.text_type(err)
|
|
raise virt_inspector.NoDataException(msg_nodata)
|
|
|
|
if power_stat == 'off':
|
|
raise virt_inspector.InstanceShutOffException(msg_shutdown)
|
|
else:
|
|
raise virt_inspector.NoDataException(msg_nodata)
|
|
else:
|
|
return inspect_data[index_key]
|