#!/usr/bin/python2.2 import ensimapplpath import appldb import vh3.db.bandwidth as bdb import operator import logging import traceback import sys import os import time def get_version(): f = open('/usr/lib/opcenter/VERSION', 'r') v = f.read() maj, min, patch = v.strip().split('.') return int(maj), int(min), patch version = get_version() info = {} info['domain'] = "'" + sys.argv[1] + "'" info['startdate'] = time.time() sql = "" if version[1] > 1: sql = bdb.current_svc_totals_sql % info else: sql = bdb.new_svc_totals_sql % info db = appldb.opendb() c = db.cursor() c.execute(sql) rows = c.fetchall() c.close() total_bw = rows[0][0] total_gb = total_bw / 1024.00 / 1024 / 1024 total_bw *= 1.000001 total_svcs = reduce(operator.add, map(lambda x: x[3] + x[4], rows), 0) total_in = reduce(operator.add, map(lambda x: x[3], rows), 0) total_out = reduce(operator.add, map(lambda x: x[4], rows), 0) print """ ********************************************************************* * BANDWIDTH REPORT * ********************************************************************* ======================================================== = Site name: %-20s = = Period starting on: %-20s = = Allowed GB bandwidth/month: %-6.3f = = GB bandwidth used this month: %-6.3f = = %% bandwidth used this month: %-6.3f = ======================================================== """ % (info['domain'].replace("'", ''), rows[0][5], total_gb*1024/1000, total_svcs/(1024*1024*1000), (total_svcs/total_bw*100)) print """\ ============================================================ = Bandwidth usage by service, in MB = ============================================================ Service RCVD SENT PCT ============================================================""" for svc in rows: name = svc[1] inbw = svc[3] outbw = svc[4] total = (inbw + outbw) * 1.00000001 inb = inbw/(1024*1024.00) outb = outbw/(1024*1024.00) if inbw > 0 or outbw > 0: print " %-22s %8.3f %8.3f %%%7.3f " % ( name, inb, outb, (total/total_svcs)*100 ) print " ============================================================" print " Subtotals %8.3f %8.3f %% 100.00 " % ( (total_in/(1024*1024.00)), (total_out/(1024*1024.00))) print " ============================================================" print " Total MB used for services: %.3f" % ( (total_in/(1024*1024.00)) + (total_out/(1024*1024.00))) print """ ********************************************************************* * END OF REPORT * ********************************************************************* """