#!/usr/bin/python3 import sys, urllib2, re, time hostname = sys.argv[1] URL = '''http://%s/Squid/cgi-bin/cachemgr.cgi?host=localhost&port=3128&user_name=&operation=active_requests&auth=''' % hostname while True: u = urllib2.urlopen(URL) records = [] peer = None for line in u.readlines(): if line.startswith("<"): continue elif line.startswith("Generated"): break elif line.startswith("uri "): url = line.strip().split(" ", 1)[1] elif line.startswith("\tpeer: "): peer = line.strip().split(" ", 1)[1] elif line.startswith("username "): try: username = line.strip().split(" ", 1)[1] except IndexError: username = "" elif line.startswith("out.offset "): out = line.strip().split(" ") out_offset = int(out[1].strip(" ,")) out_size = int(out[3]) elif line.startswith("start "): start = float(line.split(" ", 2)[1]) elif line.strip()=="" and peer: if url.startswith("cache_object://"): continue records.append(dict( peer = peer, url = url, username = username, out_offset = float(out_offset)/1024.0/1024.0, out_size = float(out_size)/1024.0/1024.0, start = start, speed = float(out_offset)/(time.time()-start)*8/1024.0/1024.0 )) print '-'*78 for row in sorted(records, key=lambda x: x['out_offset'], reverse=True): print row['url'], row['peer'], row['out_offset'], row['speed'] time.sleep(1)