1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 Cloudbase Solutions Srl
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
Management class for host operations.
21
import multiprocessing
25
from nova.openstack.common import cfg
26
from nova.openstack.common import log as logging
27
from nova.virt.hyperv import baseops
30
LOG = logging.getLogger(__name__)
33
class HostOps(baseops.BaseOps):
35
super(HostOps, self).__init__()
38
def _get_vcpu_total(self):
39
"""Get vcpu number of physical computer.
40
:returns: the number of cpu core.
42
# On certain platforms, this will raise a NotImplementedError.
44
return multiprocessing.cpu_count()
45
except NotImplementedError:
46
LOG.warn(_("Cannot get the number of cpu, because this "
47
"function is not implemented for this platform. "
48
"This error can be safely ignored for now."))
51
def _get_memory_mb_total(self):
52
"""Get the total memory size(MB) of physical computer.
53
:returns: the total amount of memory(MB).
55
total_kb = self._conn_cimv2.query(
56
"SELECT TotalVisibleMemorySize FROM win32_operatingsystem")[0]\
57
.TotalVisibleMemorySize
58
total_mb = long(total_kb) / 1024
61
def _get_local_hdd_info_gb(self):
62
"""Get the total and used size of the volume containing
63
CONF.instances_path expressed in GB.
65
A tuple with the total and used space in GB.
67
normalized_path = os.path.normpath(CONF.instances_path)
68
drive, path = os.path.splitdrive(normalized_path)
69
hdd_info = self._conn_cimv2.query(
70
("SELECT FreeSpace,Size FROM win32_logicaldisk WHERE DeviceID='%s'"
72
total_gb = long(hdd_info.Size) / (1024 ** 3)
73
free_gb = long(hdd_info.FreeSpace) / (1024 ** 3)
74
used_gb = total_gb - free_gb
75
return total_gb, used_gb
77
def _get_vcpu_used(self):
78
""" Get vcpu usage number of physical computer.
79
:returns: The total number of vcpu that currently used.
81
#TODO(jordanrinke) figure out a way to count assigned VCPUs
85
def _get_memory_mb_used(self):
86
"""Get the free memory size(MB) of physical computer.
87
:returns: the total usage of memory(MB).
89
total_kb = self._conn_cimv2.query(
90
"SELECT FreePhysicalMemory FROM win32_operatingsystem")[0]\
92
total_mb = long(total_kb) / 1024
96
def _get_hypervisor_version(self):
97
"""Get hypervisor version.
98
:returns: hypervisor version (ex. 12003)
100
version = self._conn_cimv2.Win32_OperatingSystem()[0]\
101
.Version.replace('.', '')
102
LOG.info(_('Windows version: %s ') % version)
105
def get_available_resource(self):
106
"""Retrieve resource info.
108
This method is called when nova-compute launches, and
109
as part of a periodic task.
111
:returns: dictionary describing resources
114
LOG.info(_('get_available_resource called'))
116
local_gb, used_gb = self._get_local_hdd_info_gb()
117
# TODO(alexpilotti) implemented cpu_info
118
dic = {'vcpus': self._get_vcpu_total(),
119
'memory_mb': self._get_memory_mb_total(),
120
'local_gb': local_gb,
121
'vcpus_used': self._get_vcpu_used(),
122
'memory_mb_used': self._get_memory_mb_used(),
123
'local_gb_used': used_gb,
124
'hypervisor_type': "hyperv",
125
'hypervisor_version': self._get_hypervisor_version(),
126
'hypervisor_hostname': platform.node(),
127
'cpu_info': 'unknown'}
131
def _update_stats(self):
132
LOG.debug(_("Updating host stats"))
135
data["disk_total"], data["disk_used"] = self._get_local_hdd_info_gb()
136
data["disk_available"] = data["disk_total"] - data["disk_used"]
137
data["host_memory_total"] = self._get_memory_mb_total()
138
data["host_memory_overhead"] = self._get_memory_mb_used()
139
data["host_memory_free"] = \
140
data["host_memory_total"] - data["host_memory_overhead"]
141
data["host_memory_free_computed"] = data["host_memory_free"]
142
data["supported_instances"] = \
143
[('i686', 'hyperv', 'hvm'),
144
('x86_64', 'hyperv', 'hvm')]
145
data["hypervisor_hostname"] = platform.node()
149
def get_host_stats(self, refresh=False):
150
"""Return the current state of the host. If 'refresh' is
151
True, run the update first."""
152
LOG.info(_("get_host_stats called"))
154
if refresh or not self._stats:
158
def host_power_action(self, host, action):
159
"""Reboots, shuts down or powers up the host."""