Address performance issues with listorphans.py script

listorphans.py lists orphaned Neutron objects.  'Orphans' in this
context are objects which OpenStack knows about and manages but which do
not have a valid project (tenant) ID.

The previous version was very inefficient in that for every object being
checked, it would do a discrete Keystone API call to see if the
associated tenant ID was valid or not.  For an installation of any
reasonable size, i.e one with 100s of Neutron routers, this method was
particularly slow.

The script has been updated to first build a list of all tenant IDs, and
then for every Neutron object check project ownership validity against
this list instead.

Output has also changed slightly to print out a list of discovered
orphans, simplifying workflow e.g when piping to another command which
cleans up these objects.

Closes-Bug: #1515300

Change-Id: I72ca84fe48beb623d43ee446a32ea1bb30730bcc
This commit is contained in:
Nick Jones 2015-11-11 15:21:12 +00:00
parent a93d2ddbad
commit f243b8c739
1 changed files with 8 additions and 10 deletions

View File

@ -21,18 +21,19 @@ credentials = get_credentials()
neutron = nclient.Client(**credentials)
keystone = ksclient.Client(**credentials)
def get_tenantids():
return [tenant.id for tenant in keystone.tenants.list()]
def get_orphaned_neutron_objects(object):
objects = getattr(neutron, 'list_' + object)()
tenantids = get_tenantids()
orphans = []
for object in objects.get(object):
try:
keystone.tenants.get(object['tenant_id'])
# If the tenant ID doesn't exist, then this object is orphaned
except ksclient.exceptions.NotFound:
if object['tenant_id'] not in tenantids:
orphans.append(object['id'])
return orphans
def main():
if __name__ == '__main__':
if len(sys.argv) > 1:
if sys.argv[1] == 'all':
objects = [ 'networks', 'routers', 'subnets', 'floatingips' ]
@ -40,11 +41,8 @@ def main():
objects = sys.argv[1:]
for object in objects:
orphans = get_orphaned_neutron_objects(object)
print len(orphans), 'orphan(s) found of type', object, '[%s]' % ', '.join(map(str, orphans))
print len(orphans), 'orphan(s) found of type', object
print '\n'.join(map(str, orphans))
else:
usage()
sys.exit(1)
if __name__ == '__main__':
main()