#!/usr/bin/env python
import sys
import urllib2
import json


def main(command, args):
    if len(args) != 1:
        print u"Usage: {0} <url>".format(command)
        return 1

    url = args[0]

    try:
        result = json.loads(urllib2.urlopen(url).read())
    except urllib2.URLError, e:
        print u"Status request failed for {0}: {1}".format(url, e)
        return 1

    printSection("Consumers", result["consumers"])
    printSection("Backends", result["backends"])
    printSection("Metadata", result["metadataBackends"])
    printSection("Cluster", result["cluster"])

    if result["ok"]:
        return 0

    return 1


def printSection(name, data):
    print u"# {0} ({1})".format(name, "OK" if data["ok"] else "NOT OK")

    for key, value in data.items():
        print u"  {0}: {1}".format(key, value)


if __name__ == "__main__":
    sys.exit(main(sys.argv[0], sys.argv[1:]))
