Rebase hardware pollsters to use new inspector interface

Change all the hardware pollsters to use new inspector interface
inspect_generic(), and remove the old interfaces.

Partial implements: blueprint snmp-improvement.
Partial-Bug: 1286132

Change-Id: Icc566ec8c4dbe1c0883c7e0d653480c5659f30b9
This commit is contained in:
Lianhao Lu 2014-07-24 15:55:11 +08:00
parent 9af6af5e52
commit e9e6c97f68
12 changed files with 173 additions and 602 deletions

View File

@ -18,96 +18,12 @@
"""Inspector abstraction for read-only access to hardware components"""
import abc
import collections
import six
# Named tuple representing CPU statistics.
#
# cpu1MinLoad: 1 minute load
# cpu5MinLoad: 5 minute load
# cpu15MinLoad: 15 minute load
#
CPUStats = collections.namedtuple(
'CPUStats',
['cpu_1_min', 'cpu_5_min', 'cpu_15_min'])
# Named tuple representing RAM statistics.
#
# total: Total Memory (bytes)
# used: Used Memory (bytes)
#
MemoryStats = collections.namedtuple('MemoryStats', ['total', 'used'])
# Named tuple representing disks.
#
# device: the device name for the disk
# path: the path from the disk
#
Disk = collections.namedtuple('Disk', ['device', 'path'])
# Named tuple representing disk statistics.
#
# size: storage size (bytes)
# used: storage used (bytes)
#
DiskStats = collections.namedtuple('DiskStats', ['size', 'used'])
# Named tuple representing an interface.
#
# name: the name of the interface
# mac: the MAC of the interface
# ip: the IP of the interface
# speed: the speed of the interface (bytes/s)
#
Interface = collections.namedtuple('Interface', ['name', 'mac', 'ip', 'speed'])
# Named tuple representing network interface statistics.
#
# rx_bytes: total number of octets received (bytes)
# tx_bytes: total number of octets transmitted (bytes)
# error: number of outbound packets not transmitted because of errors
#
InterfaceStats = collections.namedtuple('InterfaceStats',
['rx_bytes', 'tx_bytes', 'error'])
@six.add_metaclass(abc.ABCMeta)
class Inspector(object):
@abc.abstractmethod
def inspect_cpu(self, host):
"""Inspect the CPU statistics for a host.
:param host: the target host
:return: iterator of CPUStats
"""
@abc.abstractmethod
def inspect_disk(self, host):
"""Inspect the disk statistics for a host.
:param : the target host
:return: iterator of tuple (Disk, DiskStats)
"""
@abc.abstractmethod
def inspect_memory(self, host):
"""Inspect the ram statistics for a host.
:param : the target host
:return: iterator of MemoryStats
"""
@abc.abstractmethod
def inspect_network(self, host):
"""Inspect the network interfaces for a host.
:param : the target host
:return: iterator of tuple (Interface, InterfaceStats)
"""
@abc.abstractmethod
def inspect_generic(self, host, identifier, cache):
"""A generic inspect function.

View File

@ -256,7 +256,6 @@ class SNMPInspector(base.Inspector):
else:
for name, val in data:
oid_cache[name.prettyPrint()] = val
cache[self._CACHE_KEY_OID] = oid_cache
@staticmethod
def find_matching_oids(oid_cache, oid, match_type, find_one=True):
@ -351,7 +350,6 @@ class SNMPInspector(base.Inspector):
PREFIX):
# populate the oid into cache
self._query_oids(host, [self._interface_ip_oid], cache, True)
oid_cache = cache[self._CACHE_KEY_OID]
ip_addr = ''
for k, v in oid_cache.iteritems():
if k.startswith(self._interface_ip_oid) and v == int(suffix[1:]):
@ -359,26 +357,6 @@ class SNMPInspector(base.Inspector):
metadata.update(ip=ip_addr)
return value
def _get_or_walk_oid(self, oid, host, get=True):
if get:
func = self._cmdGen.getCmd
ret_func = lambda x: x[0][1]
else:
func = self._cmdGen.nextCmd
ret_func = lambda x: x
ret = func(self._get_auth_strategy(host),
cmdgen.UdpTransportTarget((host.hostname,
host.port or self._port)),
oid)
(error, data) = parse_snmp_return(ret)
if error:
raise SNMPException("An error occurred, oid %(oid)s, "
"host %(host)s, %(err)s" %
dict(oid=oid, host=host.hostname, err=data))
else:
return ret_func(data)
def _get_auth_strategy(self, host):
if host.password:
auth_strategy = cmdgen.UsmUserData(host.username,
@ -387,96 +365,3 @@ class SNMPInspector(base.Inspector):
auth_strategy = cmdgen.CommunityData(host.username or 'public')
return auth_strategy
def _get_value_from_oid(self, oid, host):
return self._get_or_walk_oid(oid, host, True)
def _walk_oid(self, oid, host):
return self._get_or_walk_oid(oid, host, False)
def inspect_cpu(self, host):
# get 1 minute load
cpu_1_min_load = (
str(self._get_value_from_oid(self._cpu_1_min_load_oid, host)))
# get 5 minute load
cpu_5_min_load = (
str(self._get_value_from_oid(self._cpu_5_min_load_oid, host)))
# get 15 minute load
cpu_15_min_load = (
str(self._get_value_from_oid(self._cpu_15_min_load_oid, host)))
yield base.CPUStats(cpu_1_min=float(cpu_1_min_load),
cpu_5_min=float(cpu_5_min_load),
cpu_15_min=float(cpu_15_min_load))
def inspect_memory(self, host):
# get total memory
total = self._get_value_from_oid(self._memory_total_oid, host)
# get used memory
used = self._get_value_from_oid(self._memory_used_oid, host)
yield base.MemoryStats(total=int(total), used=int(used))
def inspect_disk(self, host):
disks = self._walk_oid(self._disk_index_oid, host)
for disk in disks:
for object_name, value in disk:
path_oid = "%s.%s" % (self._disk_path_oid, str(value))
path = self._get_value_from_oid(path_oid, host)
device_oid = "%s.%s" % (self._disk_device_oid, str(value))
device = self._get_value_from_oid(device_oid, host)
size_oid = "%s.%s" % (self._disk_size_oid, str(value))
size = self._get_value_from_oid(size_oid, host)
used_oid = "%s.%s" % (self._disk_used_oid, str(value))
used = self._get_value_from_oid(used_oid, host)
disk = base.Disk(device=str(device),
path=str(path))
stats = base.DiskStats(size=int(size),
used=int(used))
yield (disk, stats)
def inspect_network(self, host):
net_interfaces = self._walk_oid(self._interface_index_oid, host)
for interface in net_interfaces:
for object_name, value in interface:
ip = self._get_ip_for_interface(host, value)
name_oid = "%s.%s" % (self._interface_name_oid,
str(value))
name = self._get_value_from_oid(name_oid, host)
mac_oid = "%s.%s" % (self._interface_mac_oid,
str(value))
mac = self._get_value_from_oid(mac_oid, host)
speed_oid = "%s.%s" % (self._interface_speed_oid,
str(value))
# bits/s to byte/s
speed = self._get_value_from_oid(speed_oid, host) / 8
rx_oid = "%s.%s" % (self._interface_received_oid,
str(value))
rx_bytes = self._get_value_from_oid(rx_oid, host)
tx_oid = "%s.%s" % (self._interface_transmitted_oid,
str(value))
tx_bytes = self._get_value_from_oid(tx_oid, host)
error_oid = "%s.%s" % (self._interface_error_oid,
str(value))
error = self._get_value_from_oid(error_oid, host)
adapted_mac = mac.prettyPrint().replace('0x', '')
interface = base.Interface(name=str(name),
mac=adapted_mac,
ip=str(ip),
speed=int(speed))
stats = base.InterfaceStats(rx_bytes=int(rx_bytes),
tx_bytes=int(tx_bytes),
error=int(error))
yield (interface, stats)
def _get_ip_for_interface(self, host, interface_id):
ip_addresses = self._walk_oid(self._interface_ip_oid, host)
for ip in ip_addresses:
for name, value in ip:
if value == interface_id:
return str(name).replace(self._interface_ip_oid + ".", "")

View File

@ -37,7 +37,7 @@ class HardwarePollster(plugin.CentralPollster):
"""Base class for plugins that support the polling API."""
CACHE_KEY = None
INSPECT_METHOD = None
IDENTIFIER = None
def __init__(self):
super(HardwarePollster, self).__init__()
@ -55,22 +55,24 @@ class HardwarePollster(plugin.CentralPollster):
sample_iters = []
for res in resources:
parsed_url = netutils.urlsplit(res)
inspector = self._get_inspector(parsed_url)
func = getattr(inspector, self.INSPECT_METHOD)
ins = self._get_inspector(parsed_url)
try:
# Call hardware inspector to poll for the data
i_cache = h_cache.setdefault(res, {})
if res not in i_cache:
i_cache[res] = list(func(parsed_url))
if self.IDENTIFIER not in i_cache:
i_cache[self.IDENTIFIER] = list(ins.inspect_generic(
parsed_url,
self.IDENTIFIER,
i_cache))
# Generate samples
if i_cache[res]:
sample_iters.append(self.generate_samples(parsed_url,
i_cache[res]))
if i_cache[self.IDENTIFIER]:
sample_iters.append(self.generate_samples(
parsed_url,
i_cache[self.IDENTIFIER]))
except Exception as err:
LOG.exception(_('inspector call %(func)r failed for '
LOG.exception(_('inspector call failed for %(ident)s '
'host %(host)s: %(err)s'),
dict(func=func,
dict(ident=self.IDENTIFIER,
host=parsed_url.hostname,
err=err))
return itertools.chain(*sample_iters)
@ -89,9 +91,8 @@ class HardwarePollster(plugin.CentralPollster):
"""Return one Sample.
:param host_url: host url of the endpoint
:param c_data: data returned by the corresponding inspector, of
one of the types defined in the file
ceilometer.hardware.inspector.base.CPUStats
:param c_data: data returned by the inspector.inspect_generic,
tuple of (value, metadata, extra)
"""
def _get_inspector(self, parsed_url):

View File

@ -26,40 +26,25 @@ from ceilometer import sample
class _Base(plugin.HardwarePollster):
CACHE_KEY = 'cpu'
INSPECT_METHOD = 'inspect_cpu'
def generate_one_sample(self, host, c_data):
value, metadata, extra = c_data
return util.make_sample_from_host(host,
name=self.IDENTIFIER,
sample_type=sample.TYPE_GAUGE,
unit='process',
volume=value,
res_metadata=metadata,
extra=extra)
class CPULoad1MinPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
return util.make_sample_from_host(host,
name='cpu.load.1min',
type=sample.TYPE_GAUGE,
unit='process',
volume=c_data.cpu_1_min,
)
IDENTIFIER = 'cpu.load.1min'
class CPULoad5MinPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
return util.make_sample_from_host(host,
name='cpu.load.5min',
type=sample.TYPE_GAUGE,
unit='process',
volume=c_data.cpu_5_min,
)
IDENTIFIER = 'cpu.load.5min'
class CPULoad15MinPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
return util.make_sample_from_host(host,
name='cpu.load.15min',
type=sample.TYPE_GAUGE,
unit='process',
volume=c_data.cpu_15_min,
)
IDENTIFIER = 'cpu.load.15min'

View File

@ -26,32 +26,25 @@ from ceilometer import sample
class _Base(plugin.HardwarePollster):
CACHE_KEY = 'disk'
INSPECT_METHOD = 'inspect_disk'
def generate_one_sample(self, host, c_data):
value, metadata, extra = c_data
res_id = host.hostname
if metadata.get('device'):
res_id = res_id + ".%s" % metadata.get('device')
return util.make_sample_from_host(host,
name=self.IDENTIFIER,
sample_type=sample.TYPE_GAUGE,
unit='B',
volume=value,
res_metadata=metadata,
extra=extra,
resource_id=res_id)
class DiskTotalPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
(disk, info) = c_data
return util.make_sample_from_host(host,
name='disk.size.total',
type=sample.TYPE_GAUGE,
unit='B',
volume=info.size,
res_metadata=disk,
)
IDENTIFIER = 'disk.size.total'
class DiskUsedPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
(disk, info) = c_data
return util.make_sample_from_host(host,
name='disk.size.used',
type=sample.TYPE_GAUGE,
unit='B',
volume=info.used,
res_metadata=disk,
)
IDENTIFIER = 'disk.size.used'

View File

@ -26,28 +26,21 @@ from ceilometer import sample
class _Base(plugin.HardwarePollster):
CACHE_KEY = 'memory'
INSPECT_METHOD = 'inspect_memory'
def generate_one_sample(self, host, c_data):
value, metadata, extra = c_data
return util.make_sample_from_host(host,
name=self.IDENTIFIER,
sample_type=sample.TYPE_GAUGE,
unit='B',
volume=value,
res_metadata=metadata,
extra=extra)
class MemoryTotalPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
return util.make_sample_from_host(host,
name='memory.total',
type=sample.TYPE_GAUGE,
unit='B',
volume=c_data.total,
)
IDENTIFIER = 'memory.total'
class MemoryUsedPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
return util.make_sample_from_host(host,
name='memory.used',
type=sample.TYPE_GAUGE,
unit='B',
volume=c_data.used,
)
IDENTIFIER = 'memory.used'

View File

@ -26,46 +26,32 @@ from ceilometer import sample
class _Base(plugin.HardwarePollster):
CACHE_KEY = 'nic'
INSPECT_METHOD = 'inspect_network'
def generate_one_sample(self, host, c_data):
value, metadata, extra = c_data
res_id = host.hostname
if metadata.get('name'):
res_id = res_id + ".%s" % metadata.get('name')
return util.make_sample_from_host(host,
name=self.IDENTIFIER,
sample_type=sample.TYPE_CUMULATIVE,
unit=self.unit,
volume=value,
res_metadata=metadata,
extra=extra,
resource_id=res_id)
class IncomingBytesPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
(nic, info) = c_data
return util.make_sample_from_host(host,
name='network.incoming.bytes',
type=sample.TYPE_CUMULATIVE,
unit='B',
volume=info.rx_bytes,
res_metadata=nic,
)
IDENTIFIER = 'network.incoming.bytes'
unit = 'B'
class OutgoingBytesPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
(nic, info) = c_data
return util.make_sample_from_host(host,
name='network.outgoing.bytes',
type=sample.TYPE_CUMULATIVE,
unit='B',
volume=info.tx_bytes,
res_metadata=nic,
)
IDENTIFIER = 'network.outgoing.bytes'
unit = 'B'
class OutgoingErrorsPollster(_Base):
@staticmethod
def generate_one_sample(host, c_data):
(nic, info) = c_data
return util.make_sample_from_host(host,
name='network.outgoing.errors',
type=sample.TYPE_CUMULATIVE,
unit='packet',
volume=info.error,
res_metadata=nic,
)
IDENTIFIER = 'network.outgoing.errors'
unit = 'packet'

View File

@ -34,24 +34,26 @@ def make_resource_metadata(res_metadata=None, host_url=None):
resource_metadata = dict()
if res_metadata is not None:
metadata = copy.copy(res_metadata)
resource_metadata = dict(zip(metadata._fields, metadata))
resource_metadata.update(metadata)
resource_metadata.update(get_metadata_from_host(host_url))
return resource_metadata
def make_sample_from_host(host_url, name, type, unit, volume,
project_id=None, user_id=None, res_metadata=None):
def make_sample_from_host(host_url, name, sample_type, unit, volume,
project_id=None, user_id=None, resource_id=None,
res_metadata=None, extra={}):
resource_metadata = make_resource_metadata(res_metadata, host_url)
res_id = extra.get('resource_id') or resource_id or host_url.hostname
return sample.Sample(
name='hardware.' + name,
type=type,
type=sample_type,
unit=unit,
volume=volume,
user_id=project_id,
project_id=user_id,
resource_id=host_url.hostname,
user_id=extra.get('user_id') or user_id,
project_id=extra.get('project_id') or project_id,
resource_id=res_id,
timestamp=timeutils.isotime(),
resource_metadata=resource_metadata,
source='hardware',

View File

@ -1,60 +0,0 @@
#
# Copyright 2014 Intel Corp
#
# Authors: Lianhao Lu <lianhao.lu@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.
from ceilometer.hardware.inspector import base
class InspectorBaseTest(object):
"""Subclass must set self.inspector and self.host in self.setUp()."""
cpu = [base.CPUStats(cpu_1_min=0.1,
cpu_5_min=0.2,
cpu_15_min=0.3),
]
network = [(base.Interface(name='eth0',
mac='112233445566',
ip='10.0.0.1',
speed=1250000 / 8),
base.InterfaceStats(rx_bytes=1000,
tx_bytes=2000,
error=1)),
]
diskspace = [(base.Disk(device='/dev/sda1', path='/'),
base.DiskStats(size=1000, used=500),
),
(base.Disk(device='/dev/sda2', path='/home'),
base.DiskStats(size=2000, used=1000),
),
]
memory = [base.MemoryStats(total=1000, used=500)]
def test_inspect_cpu(self):
self.assertEqual(list(self.inspector.inspect_cpu(self.host)),
self.cpu)
def test_inspect_network(self):
self.assertEqual(list(self.inspector.inspect_network(self.host)),
self.network)
def test_inspect_disk(self):
self.assertEqual(list(self.inspector.inspect_disk(self.host)),
self.diskspace)
def test_inspect_memory(self):
self.assertEqual(list(self.inspector.inspect_memory(self.host)),
self.memory)

View File

@ -21,173 +21,8 @@ from oslotest import mockpatch
from ceilometer.hardware.inspector import snmp
from ceilometer.tests import base as test_base
from ceilometer.tests.hardware.inspector import base
Base = base.InspectorBaseTest
class FakeMac(object):
def __init__(self):
self.val = "0x%s" % Base.network[0][0].mac
def prettyPrint(self):
return str(self.val)
ins = snmp.SNMPInspector
GETCMD_MAP = {
ins._cpu_1_min_load_oid: (None,
None,
0,
[('',
Base.cpu[0].cpu_1_min,
)],
),
ins._cpu_5_min_load_oid: (None,
None,
0,
[('',
Base.cpu[0].cpu_5_min,
)],
),
ins._cpu_15_min_load_oid: (None,
None,
0,
[('',
Base.cpu[0].cpu_15_min,
)],
),
ins._memory_total_oid: (None,
None,
0,
[('',
Base.memory[0].total,
)],
),
ins._memory_used_oid: (None,
None,
0,
[('',
Base.memory[0].used,
)],
),
ins._disk_path_oid + '.1': (None,
None,
0,
[('',
Base.diskspace[0][0].path,
)],
),
ins._disk_device_oid + '.1': (None,
None,
0,
[('',
Base.diskspace[0][0].device,
)],
),
ins._disk_size_oid + '.1': (None,
None,
0,
[('',
Base.diskspace[0][1].size,
)],
),
ins._disk_used_oid + '.1': (None,
None,
0,
[('',
Base.diskspace[0][1].used,
)],
),
ins._disk_path_oid + '.2': (None,
None,
0,
[('',
Base.diskspace[1][0].path,
)],
),
ins._disk_device_oid + '.2': (None,
None,
0,
[('',
Base.diskspace[1][0].device,
)],
),
ins._disk_size_oid + '.2': (None,
None,
0,
[('',
Base.diskspace[1][1].size,
)],
),
ins._disk_used_oid + '.2': (None,
None,
0,
[('',
Base.diskspace[1][1].used,
)],
),
ins._interface_name_oid + '.1': (None,
None,
0,
[('',
Base.network[0][0].name,
)],
),
ins._interface_mac_oid + '.1': (None,
None,
0,
[('',
FakeMac(),
)],
),
ins._interface_speed_oid + '.1': (None,
None,
0,
[('',
Base.network[0][0].speed * 8,
)],
),
ins._interface_received_oid + '.1': (None,
None,
0,
[('',
Base.network[0][1].rx_bytes,
)],
),
ins._interface_transmitted_oid + '.1': (None,
None,
0,
[('',
Base.network[0][1].tx_bytes,
)],
),
ins._interface_error_oid + '.1': (None,
None,
0,
[('',
Base.network[0][1].error,
)],
),
}
NEXTCMD_MAP = {
ins._disk_index_oid: (None,
None,
0,
[[('1.3.6.1.4.1.2021.9.1.1.1', 1)],
[('1.3.6.1.4.1.2021.9.1.1.2', 2)]]),
ins._interface_index_oid: (None,
None,
0,
[[('1.3.6.1.2.1.2.2.1.1.1', 1)],
]),
ins._interface_ip_oid: (None,
None,
0,
[[('1.3.6.1.2.1.4.20.1.2.10.0.0.1',
1)],
]),
}
class FakeObjectName(object):
@ -198,20 +33,6 @@ class FakeObjectName(object):
return str(self.name)
def faux_getCmd(authData, transportTarget, oid):
try:
return GETCMD_MAP[oid]
except KeyError:
return "faux_getCmd Error", None, 0, []
def faux_nextCmd(authData, transportTarget, oid):
try:
return NEXTCMD_MAP[oid]
except KeyError:
return "faux_nextCmd Error", None, 0, []
def faux_getCmd_new(authData, transportTarget, *oids, **kwargs):
varBinds = [(FakeObjectName(oid),
int(oid.split('.')[-1])) for oid in oids]
@ -227,7 +48,7 @@ def faux_bulkCmd_new(authData, transportTarget, nonRepeaters, maxRepetitions,
return (None, None, 0, varBindTable)
class TestSNMPInspector(Base, test_base.BaseTestCase):
class TestSNMPInspector(test_base.BaseTestCase):
mapping = {
'test_exact': {
'matching_type': snmp.EXACT,
@ -253,21 +74,26 @@ class TestSNMPInspector(Base, test_base.BaseTestCase):
self.host = netutils.urlsplit("snmp://localhost")
self.inspector.MAPPING = self.mapping
self.useFixture(mockpatch.PatchObject(
self.inspector._cmdGen, 'getCmd', new=faux_getCmd))
self.inspector._cmdGen, 'getCmd', new=faux_getCmd_new))
self.useFixture(mockpatch.PatchObject(
self.inspector._cmdGen, 'nextCmd', new=faux_nextCmd))
def test_get_cmd_error(self):
self.useFixture(mockpatch.PatchObject(
self.inspector, '_memory_total_oid', new='failure'))
self.inspector._cmdGen, 'bulkCmd', new=faux_bulkCmd_new))
def test_snmp_error(self):
def get_list(func, *args, **kwargs):
return list(func(*args, **kwargs))
def faux_parse(ret, is_bulk):
return (True, 'forced error')
self.useFixture(mockpatch.PatchObject(
snmp, 'parse_snmp_return', new=faux_parse))
self.assertRaises(snmp.SNMPException,
get_list,
self.inspector.inspect_memory,
self.host)
self.inspector.inspect_generic,
self.host,
'test_exact',
{})
def _fake_post_op(self, host, cache, meter_def,
value, metadata, extra, suffix):
@ -276,8 +102,6 @@ class TestSNMPInspector(Base, test_base.BaseTestCase):
return value
def test_inspect_generic_exact(self):
self.useFixture(mockpatch.PatchObject(
self.inspector._cmdGen, 'getCmd', new=faux_getCmd_new))
self.inspector._fake_post_op = self._fake_post_op
cache = {}
ret = list(self.inspector.inspect_generic(self.host,
@ -293,8 +117,6 @@ class TestSNMPInspector(Base, test_base.BaseTestCase):
self.assertEqual(2, ret[0][2]['project_id'])
def test_inspect_generic_prefix(self):
self.useFixture(mockpatch.PatchObject(
self.inspector._cmdGen, 'bulkCmd', new=faux_bulkCmd_new))
cache = {}
ret = list(self.inspector.inspect_generic(self.host,
'test_prefix',

View File

@ -24,34 +24,26 @@ from ceilometer.tests import base as test_base
class FakeInspector(inspector_base.Inspector):
CPU = inspector_base.CPUStats(cpu_1_min=0.99,
cpu_5_min=0.77,
cpu_15_min=0.55)
DISK = (inspector_base.Disk(device='/dev/sda1', path='/'),
inspector_base.DiskStats(size=1000, used=90))
MEMORY = inspector_base.MemoryStats(total=1000, used=90)
NET = (inspector_base.Interface(name='test.teest',
mac='001122334455',
ip='10.0.0.2',
speed=1000),
inspector_base.InterfaceStats(rx_bytes=90,
tx_bytes=80,
error=1))
def inspect_cpu(self, host):
yield self.CPU
def inspect_disk(self, host):
yield self.DISK
def inspect_memory(self, host):
yield self.MEMORY
def inspect_network(self, host):
yield self.NET
net_metadata = dict(name='test.teest',
mac='001122334455',
ip='10.0.0.2',
speed=1000)
disk_metadata = dict(device='/dev/sda1', path='/')
DATA = {
'cpu.load.1min': (0.99, {}, {}),
'cpu.load.5min': (0.77, {}, {}),
'cpu.load.15min': (0.55, {}, {}),
'memory.total': (1000, {}, {}),
'memory.used': (90, {}, {}),
'network.incoming.bytes': (90, net_metadata, {}),
'network.outgoing.bytes': (80, net_metadata, {}),
'network.outgoing.errors': (1, net_metadata, {}),
'disk.size.total': (1000, disk_metadata, {}),
'disk.size.used': (90, disk_metadata, {}),
}
def inspect_generic(self, host, identifier, cache):
yield (None, {}, {})
yield self.DATA[identifier]
class TestPollsterBase(test_base.BaseTestCase):

View File

@ -0,0 +1,56 @@
#
# Copyright 2013 Intel Corp
#
# Authors: Lianhao Lu <lianhao.lu@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.
from oslo.utils import netutils
from ceilometer.hardware.pollsters import util
from ceilometer import sample
from ceilometer.tests import base as test_base
class TestPollsterUtils(test_base.BaseTestCase):
def setUp(self):
super(TestPollsterUtils, self).setUp()
self.host_url = netutils.urlsplit("snmp://127.0.0.1:161")
def test_make_sample(self):
s = util.make_sample_from_host(self.host_url,
name='test',
sample_type=sample.TYPE_GAUGE,
unit='B',
volume=1,
res_metadata={
'metakey': 'metaval',
})
self.assertEqual('127.0.0.1', s.resource_id)
self.assertIn('snmp://127.0.0.1:161', s.resource_metadata.values())
self.assertIn('metakey', s.resource_metadata.keys())
def test_make_sample_extra(self):
extra = {
'project_id': 'project',
'resource_id': 'resource'
}
s = util.make_sample_from_host(self.host_url,
name='test',
sample_type=sample.TYPE_GAUGE,
unit='B',
volume=1,
extra=extra)
self.assertEqual(None, s.user_id)
self.assertEqual('project', s.project_id)
self.assertEqual('resource', s.resource_id)