Skip to content

Instantly share code, notes, and snippets.

@cschwede
Last active December 17, 2015 05:39
Show Gist options
  • Save cschwede/5559268 to your computer and use it in GitHub Desktop.
Save cschwede/5559268 to your computer and use it in GitHub Desktop.
Get usage of Openstack Swift cluster using Swauth for authentication.
import logging
import json
import threading
import Queue
import argparse
import sys
from os import environ
from swiftclient import client
logging.basicConfig(level=logging.INFO, format='%(message)s')
GLOBAL_SUM = 0
def sizeof_fmt(num):
""" Human-friendly byte format. Found on StackOverflow """
for suffix in ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB']:
if num < 1024.0:
return "%3.1f\t%5s" % (num, suffix)
num /= 1024.0
class Worker(threading.Thread):
""" Worker which executes get on account """
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
""" Get bytes from accounts """
global GLOBAL_SUM
while not self.queue.empty():
(name, storage_url, auth_token) = self.queue.get()
account = client.get_object(storage_url, auth_token,
name, '.services')
accounts = json.loads(account[1])
this_storage_url = accounts['storage']['cluster_name']
containers = client.get_account(this_storage_url, auth_token)
used_bytes = containers[0]['x-account-bytes-used']
GLOBAL_SUM += int(used_bytes)
logging.info("%30s %s", name, sizeof_fmt(int(used_bytes)))
self.queue.task_done()
def get_usage(auth_url, user, key):
""" Gets token, creates account list, starts worker threads """
(storage_url, auth_token) = client.get_auth(auth_url, user, key)
(_head, containers) = client.get_account(storage_url, auth_token)
queue = Queue.Queue()
for container in containers:
container_name = container['name']
if not container_name.startswith('.'):
queue.put((container_name, storage_url, auth_token))
workers = [Worker(queue) for _i in range(10)]
for worker in workers:
worker.start()
for worker in workers:
worker.join()
def main(argv):
""" Get command line args """
parser = argparse.ArgumentParser(
description='Prints used bytes of all accounts',
usage='%(prog)s https://127.0.0.1/auth/v1.0 account:user secret')
parser.add_argument('auth', metavar='AUTH', nargs='+',
help='URL for obtaining an auth token',
default=environ.get('ST_AUTH'))
parser.add_argument('user', metavar='USER', nargs='+',
help='User name for obtaining an auth token.',
default=environ.get('ST_USER'))
parser.add_argument('key', metavar='KEY', nargs='+',
help='Key for obtaining an auth token.',
default=environ.get('ST_KEY'))
args = vars(parser.parse_args())
get_usage(args['auth'][0], args['user'][0], args['key'][0])
logging.info("\n%30s %s", 'TOTAL', sizeof_fmt(int(GLOBAL_SUM)))
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment