Qiaowei Ren cca1987ab2 XenAPI support: Memory Usage
Implemented inspect_memory_usage() to inspect the memory usage statistics
for an instance.

Change-Id: I0a8549e9e80ffeab18acc3b236c8fdc63d970783
Implements: blueprint xenapi-support
2014-08-27 17:14:47 +08:00

148 lines
5.5 KiB
Python

# Copyright 2014 Intel
#
# Author: Ren Qiaowei <qiaowei.ren@intel.com>
#
# 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.
"""Implementation of Inspector abstraction for XenAPI."""
from eventlet import timeout
from oslo.config import cfg
from oslo.utils import units
try:
import XenAPI as api
except ImportError:
api = None
from ceilometer.compute.pollsters import util
from ceilometer.compute.virt import inspector as virt_inspector
from ceilometer.openstack.common.gettextutils import _
opt_group = cfg.OptGroup(name='xenapi',
title='Options for XenAPI')
xenapi_opts = [
cfg.StrOpt('connection_url',
help='URL for connection to XenServer/Xen Cloud Platform'),
cfg.StrOpt('connection_username',
default='root',
help='Username for connection to XenServer/Xen Cloud Platform'),
cfg.StrOpt('connection_password',
help='Password for connection to XenServer/Xen Cloud Platform',
secret=True),
cfg.IntOpt('login_timeout',
default=10,
help='Timeout in seconds for XenAPI login.'),
]
CONF = cfg.CONF
CONF.register_group(opt_group)
CONF.register_opts(xenapi_opts, group=opt_group)
class XenapiException(virt_inspector.InspectorException):
pass
def get_api_session():
if not api:
raise ImportError(_('XenAPI not installed'))
url = CONF.xenapi.connection_url
username = CONF.xenapi.connection_username
password = CONF.xenapi.connection_password
if not url or password is None:
raise XenapiException(_('Must specify connection_url, and '
'connection_password to use'))
exception = api.Failure(_("Unable to log in to XenAPI "
"(is the Dom0 disk full?)"))
try:
session = api.Session(url)
with timeout.Timeout(CONF.xenapi.login_timeout, exception):
session.login_with_password(username, password)
except api.Failure as e:
msg = _("Could not connect to XenAPI: %s") % e.details[0]
raise XenapiException(msg)
return session
class XenapiInspector(virt_inspector.Inspector):
def __init__(self):
super(XenapiInspector, self).__init__()
self.session = get_api_session()
def _get_host_ref(self):
"""Return the xenapi host on which nova-compute runs on."""
return self.session.xenapi.session.get_this_host(self.session.handle)
def _call_xenapi(self, method, *args):
return self.session.xenapi_request(method, args)
def _list_vms(self):
host_ref = self._get_host_ref()
vms = self._call_xenapi("VM.get_all_records_where",
'field "is_control_domain"="false" and '
'field "is_a_template"="false" and '
'field "resident_on"="%s"' % host_ref)
for vm_ref in vms.keys():
yield vm_ref, vms[vm_ref]
def _lookup_by_name(self, instance_name):
vm_refs = self._call_xenapi("VM.get_by_name_label", instance_name)
n = len(vm_refs)
if n == 0:
raise virt_inspector.InstanceNotFoundException(
_('VM %s not found in XenServer') % instance_name)
elif n > 1:
raise XenapiException(
_('Multiple VM %s found in XenServer') % instance_name)
else:
return vm_refs[0]
def inspect_instances(self):
for vm_ref, vm_rec in self._list_vms():
name = vm_rec['name_label']
other_config = vm_rec['other_config']
uuid = other_config.get('nova_uuid')
if uuid:
yield virt_inspector.Instance(name, uuid)
def inspect_cpu_util(self, instance, duration=None):
instance_name = util.instance_name(instance)
vm_ref = self._lookup_by_name(instance_name)
metrics_ref = self._call_xenapi("VM.get_metrics", vm_ref)
metrics_rec = self._call_xenapi("VM_metrics.get_record",
metrics_ref)
vcpus_number = metrics_rec['VCPUs_number']
vcpus_utils = metrics_rec['VCPUs_utilisation']
if len(vcpus_utils) == 0:
msg = _("Could not get VM %s CPU Utilization") % instance_name
raise XenapiException(msg)
utils = 0.0
for num in range(int(vcpus_number)):
utils += vcpus_utils.get(str(num))
utils = utils / int(vcpus_number) * 100
return virt_inspector.CPUUtilStats(util=utils)
def inspect_memory_usage(self, instance, duration=None):
instance_name = util.instance_name(instance)
vm_ref = self._lookup_by_name(instance_name)
metrics_ref = self._call_xenapi("VM.get_metrics", vm_ref)
metrics_rec = self._call_xenapi("VM_metrics.get_record",
metrics_ref)
# Stat provided from XenServer is in B, converting it to MB.
memory = long(metrics_rec['memory_actual']) / units.Mi
return virt_inspector.MemoryUsageStats(usage=memory)