Skip to content

Instantly share code, notes, and snippets.

@craigpatten
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save craigpatten/5d20c007903f13e9f557 to your computer and use it in GitHub Desktop.

Select an option

Save craigpatten/5d20c007903f13e9f557 to your computer and use it in GitHub Desktop.
swift-report.py
#!/usr/bin/env python
import json
import logging
import os
import random
from multiprocessing.pool import ThreadPool
import requests
from keystoneclient.v2_0 import client
from swift.common.ring import Ring
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
keystone_config = {
"username" : os.getenv("OS_USERNAME"),
"password" : os.getenv("OS_PASSWORD"),
"tenant_name" : os.getenv("OS_TENANT_NAME"),
"auth_url" : os.getenv("OS_AUTH_URL")
}
keystone = client.Client(**keystone_config)
ring = Ring("/etc/swift/account.ring.gz")
def fetch(tenant):
account = "AUTH_%s" % tenant
partition = ring.get_part(account, None, None)
nodes = ring.get_part_nodes(partition)
random.shuffle(nodes)
for node in nodes:
url = "http://%s:%s/%s/%s/%s" % (node["ip"], node["port"], node["device"], partition, account)
try:
response = requests.head(url, timeout=5)
if response.status_code == 204:
return {
"containers" : int(response.headers["x-account-container-count"]),
"objects" : int(response.headers["x-account-object-count"]),
"bytes" : int(response.headers["x-account-bytes-used"]),
"quota" : int(response.headers["x-account-meta-quota-bytes"]) if "x-account-meta-quota-bytes" in response.headers else None
}
elif response.status_code == 404:
return None
else:
logging.warning("error fetching %s [HTTP %s]", url, response.status_code)
except:
logging.warning("error fetching %s", url, exc_info=True)
logging.error("failed to fetch info for tenant %s", tenant)
return None
tenants = [tenant.id for tenant in keystone.tenants.list()]
random.shuffle(tenants)
report = {}
for tenant, stats in zip(tenants, ThreadPool().map(fetch, tenants)):
report[tenant] = stats
print json.dumps(report)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment