Created
December 3, 2013 15:30
-
-
Save boopathi/7771179 to your computer and use it in GitHub Desktop.
Show bandwidth usage for cPanel users;
uses WHM JSON-API
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/python | |
import socket, sys | |
import urllib2, os | |
from operator import itemgetter | |
import subprocess, datetime | |
from time import strftime | |
debug = True | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
# Settings | |
hostname = socket.gethostname() | |
accesshash = "/root/.accesshash" | |
restapi = "https://%s:2087/json-api/showbw" % hostname | |
lowerbound = 1024 * 1024 * 1024 | |
currentmonth = int(strftime("%m")) | |
currentyear = int(strftime("%Y")) | |
whmhash = open(accesshash).read().replace("\n","").replace("\r","") | |
def sizeof_fmt(num): | |
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: | |
if num < 1024.0: | |
return "%3.1f %s" % (num,x) | |
num /= 1024.0 | |
def previous_month(dt): | |
previous = dt.date().replace(day=1) - datetime.timedelta(days=1) | |
return datetime.datetime.combine(previous.replace(day=1), datetime.time.min) | |
class BW: | |
def __init__(self): | |
self.bw = {} | |
self.updaten(6) | |
self.procs() | |
def update(self, month=currentmonth, year=currentyear): | |
req = urllib2.Request(restapi + "?month=%d&year=%d" % (month,year)) | |
req.add_header('Authorization', "WHM root:%s" % whmhash) | |
_t_resp1 = urllib2.urlopen(req) | |
_t_resp2 = _t_resp1.read() | |
resp = json.loads(_t_resp2) | |
userinfo = resp["bandwidth"][0]["acct"] | |
for acct in userinfo: | |
bytes = int(acct["totalbytes"]) | |
if not self.bw.has_key(acct["user"]): | |
self.bw[acct["user"]] = ([],[]) | |
self.bw[acct["user"]][0].append(bytes) | |
def procs(self): | |
command = "ps -U root -u root -N ux".split() | |
p = subprocess.Popen(command, stdout=subprocess.PIPE) | |
out = p.communicate()[0].split('\n') | |
for l in out: | |
if not l: | |
continue | |
line = l.split() | |
user = line[0] | |
process = line[10] | |
if self.bw.has_key(user): | |
self.bw[user][1].append(process) | |
# update for n months | |
def updaten(self, n): | |
dt = datetime.datetime(currentyear, currentmonth, 1, 0, 0) | |
for i in range(n): | |
self.update(dt.month, dt.year) | |
dt = previous_month(dt) | |
def get(self): | |
return serialize() | |
def serialize(self): | |
BW = [] | |
for user in self.bw: | |
flag = False | |
for b in self.bw[user][0]: | |
if b > lowerbound: | |
flag = True | |
if flag: | |
BW.append((user, self.bw[user][0], self.bw[user][1])) | |
return BW | |
# pretty print | |
def pp(self): | |
for ud in self.serialize(): | |
print ud[0], | |
if len(ud[0]) < 7: | |
print "\t", | |
print "\t", | |
for bw in ud[1]: | |
to = sizeof_fmt(bw) | |
print to, | |
if len(to) < 7: | |
print "\t", | |
print "\t", | |
print ud[2] | |
if __name__ == "__main__": | |
b = BW() | |
b.pp() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment