Merge "Instance Cache in Node Discovery Pollster"

This commit is contained in:
Jenkins 2015-07-29 17:44:57 +00:00 committed by Gerrit Code Review
commit 29d3cd405b
2 changed files with 26 additions and 5 deletions

View File

@ -14,6 +14,7 @@
from oslo_config import cfg
from oslo_log import log
from oslo_utils import timeutils
from ceilometer.agent import plugin_base
from ceilometer.i18n import _
@ -41,17 +42,31 @@ class NodesDiscoveryTripleO(plugin_base.DiscoveryBase):
def __init__(self):
super(NodesDiscoveryTripleO, self).__init__()
self.nova_cli = nova_client.Client()
self.last_run = None
self.instances = {}
@staticmethod
def _address(instance, field):
return instance.addresses['ctlplane'][0].get(field)
def discover(self, manager, param=None):
"""Discover resources to monitor."""
"""Discover resources to monitor.
instances = self.nova_cli.instance_get_all()
resources = []
instance_get_all will return all instances if last_run is None,
and will return only the instances changed since the last_run time.
"""
instances = self.nova_cli.instance_get_all(self.last_run)
for instance in instances:
if getattr(instance, 'OS-EXT-STS:vm_state', None) in ['deleted',
'error']:
self.instances.pop(instance.id, None)
else:
self.instances[instance.id] = instance
self.last_run = timeutils.utcnow(True).isoformat()
resources = []
for instance in self.instances.values():
try:
ip_address = self._address(instance, 'addr')
final_address = (

View File

@ -150,9 +150,15 @@ class Client(object):
search_opts=search_opts))
@logged
def instance_get_all(self):
"""Returns list of all instances."""
def instance_get_all(self, since=None):
"""Returns list of all instances.
If since is supplied, it will return the instances changes since that
datetime. since should be in ISO Format '%Y-%m-%dT%H:%M:%SZ'
"""
search_opts = {'all_tenants': True}
if since:
search_opts['changes-since'] = since
return self.nova_client.servers.list(
detailed=True,
search_opts=search_opts)