Created
August 8, 2013 22:37
-
-
Save wimleers/6189478 to your computer and use it in GitHub Desktop.
Retrieves the quota details of the given SSH hots and outputs JSON. Tested with http://rsync.net.
This file contains hidden or 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 | |
# Retrieves the quota details of the given SSH hots and outputs JSON. | |
import os | |
import optparse | |
import subprocess | |
def calculate(hosts): | |
records = {} | |
for host in hosts: | |
quota = get_quota(host) | |
record = {} | |
record["host"] = host | |
if quota is None: | |
record["online"] = False | |
elif quota is True: | |
# No quota, i.e. unlimited storage. | |
record["online"] = True | |
record["fs"] = "" | |
record["usage"] = 0 | |
record["quota"] = 0 | |
record["limit"] = 0 | |
record["files"] = 0 | |
record["relusage"] = 0 | |
record["avgfilesize"] = 0 | |
record["free"] = 2**10000 | |
record["maxfree"] = 2**10000 | |
else: | |
(fs, usage, quota, limit, files) = quota | |
record["online"] = True | |
record["fs"] = fs | |
record["usage"] = usage * 1000 | |
record["quota"] = quota * 1000 | |
record["limit"] = limit * 1000 | |
record["files"] = files | |
record["relusage"] = 1.0*usage/quota | |
record["avgfilesize"] = 1.0*usage*1000/files | |
record["free"] = quota*1000 - usage*1000 | |
record["maxfree"] = limit*1000 - usage*1000 | |
records[host] = record | |
return records | |
def get_quota(host): | |
"""get quota of a host""" | |
(stdout, stderr) = cmd('ssh -t %s "quota"' % (host)) | |
if len(stdout) == 0: | |
return None | |
elif len(stdout.split("\n")) < 3: | |
return True | |
else: | |
# Parse | |
line = stdout.split("\n")[2] | |
parts = line.split() | |
(fs, usage, quota, limit, files) = (parts[0], int(parts[1]), int(parts[2]), int(parts[3]), int(parts[4])) | |
return (fs, usage, quota, limit, files) | |
def cmd(command): | |
"""run a command and get (stdout, stderr) back""" | |
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
(stdout, stderr) = p.communicate() | |
(stdout, stderr) = (stdout.rstrip(), stderr.rstrip()) | |
return (stdout, stderr) | |
def main(): | |
p = optparse.OptionParser(description="Checks the quota of given SSH hosts. Key authentication must be set up for them.") | |
options, arguments = p.parse_args() | |
if len(arguments) > 0: | |
results = calculate(arguments) | |
# TODO: use the json module when I get Python 2.6 on my server. | |
print "{" | |
for i in xrange(len(results.keys())): | |
# Key: host | |
host = results.keys()[i] | |
if i > 0: | |
print ', ' | |
print '"%s":' % (host) | |
# Value: details for the host | |
r = results[host] | |
parts = [] | |
for key in r.keys(): | |
if type(r[key]) == type("s"): | |
parts.append('"%s": "%s"' % (key, r[key])) | |
elif type(r[key]) == type(1.0): | |
parts.append('"%s": %f' % (key, r[key])) | |
else: | |
parts.append('"%s": %d' % (key, r[key])) | |
print "{" + ", ".join(parts) + "}" | |
print "}" | |
else: | |
p.print_help() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment