Created
November 29, 2018 09:22
-
-
Save jeffrey4l/99a30098044fa63620ae37e50dc3b8b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import shade | |
import collections | |
import prettytable | |
cloud = shade.openstack_cloud() | |
projects = cloud.list_projects() | |
project_mapping = {} | |
for project in projects: | |
project_mapping[project.id] = project.name | |
users = cloud.list_users() | |
user_mapping = {} | |
for user in users: | |
user_mapping[user.id] = user.name | |
flavors = cloud.list_flavors() | |
flavor_mapping = {} | |
for flavor in flavors: | |
flavor_mapping[flavor.id] = flavor | |
def bytes_2_human_readable(number_of_bytes): | |
if number_of_bytes < 0: | |
raise ValueError("!!! number_of_bytes can't be smaller than 0 !!!") | |
step_to_greater_unit = 1024. | |
number_of_bytes = float(number_of_bytes) | |
unit = 'bytes' | |
if (number_of_bytes / step_to_greater_unit) >= 1: | |
number_of_bytes /= step_to_greater_unit | |
unit = 'KB' | |
if (number_of_bytes / step_to_greater_unit) >= 1: | |
number_of_bytes /= step_to_greater_unit | |
unit = 'MB' | |
if (number_of_bytes / step_to_greater_unit) >= 1: | |
number_of_bytes /= step_to_greater_unit | |
unit = 'GB' | |
if (number_of_bytes / step_to_greater_unit) >= 1: | |
number_of_bytes /= step_to_greater_unit | |
unit = 'TB' | |
precision = 1 | |
number_of_bytes = round(number_of_bytes, precision) | |
return str(number_of_bytes) + ' ' + unit | |
def get_flavor(id, prop): | |
flavor = flavor_mapping.get(id, {}) | |
if not flavor: | |
return 0 | |
return getattr(flavor, prop) | |
def nova_static(): | |
servers = cloud.list_servers(all_projects=True) | |
#servers = cloud.list_servers() | |
statics = collections.defaultdict(list) | |
for server in servers: | |
project_name = project_mapping[server.project_id] | |
statics[project_name].append(server) | |
table = prettytable.PrettyTable(['Project', 'Active VM', 'None-Active VM', 'Total', 'total cpu', 'total size', 'total ram']) | |
for project_name, servers in statics.items(): | |
row = [project_name, | |
len([server for server in servers if server.status == 'ACTIVE']), | |
len([server for server in servers if server.status != 'ACTIVE']), | |
len(servers), | |
sum([get_flavor(server.flavor.id, 'vcpus') for server in servers]), | |
sum([get_flavor(server.flavor.id, 'disk') for server in servers]), | |
bytes_2_human_readable(sum([get_flavor(server.flavor.id, 'ram') for server in servers]) * 1024 * 1024), | |
] | |
table.add_row(row) | |
print(table.get_string(sortby='Active VM', | |
reversesort=True)) | |
def volume_static(): | |
search_opts = { 'all_tenants': True } | |
cinderclient = cloud.cinder_client | |
volumes = cinderclient.volumes.list(search_opts=search_opts) | |
statics = collections.defaultdict(list) | |
for volume in volumes: | |
user_name = user_mapping[volume.user_id] | |
statics[user_name].append(volume) | |
table = prettytable.PrettyTable(['User', | |
'Total Vol', | |
'Attached Vol', | |
'Avail Vol', | |
'Totol Size', | |
'Total Used Size', | |
'Total Free Size' | |
]) | |
for user_name, volumes in statics.items(): | |
row = [user_name, | |
len(volumes), | |
len([volume for volume in volumes if volume.status == 'in-use']), | |
len([volume for volume in volumes if volume.status != 'in-use']), | |
sum([volume.size for volume in volumes]), | |
sum([volume.size for volume in volumes if volume.status == 'in-use']), | |
sum([volume.size for volume in volumes if volume.status != 'in-use']), | |
] | |
table.add_row(row) | |
print(table.get_string(sortby='Total Free Size', reversesort=True)) | |
def image_static(): | |
images = cloud.list_images() | |
statics = collections.defaultdict(list) | |
for image in images: | |
project_name = project_mapping.get(image.location.project.id, 'None') | |
statics[project_name].append(image) | |
table = prettytable.PrettyTable(['Project', | |
'total image', | |
'total size', | |
'total size in byte', | |
'names' | |
]) | |
for project_name, images in statics.items(): | |
image_names = ','.join([image.name for image in images]) | |
row = [project_name, | |
len(images), | |
bytes_2_human_readable(sum([image.size for image in images])), | |
sum([image.size for image in images]), | |
image_names[:80] | |
] | |
table.add_row(row) | |
print(table.get_string(sortby='total size in byte', | |
fields=['Project', 'total image', 'total size', 'names'], | |
reversesort=True)) | |
if __name__ == "__main__": | |
nova_static() | |
volume_static() | |
image_static() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment