Created
March 1, 2011 00:02
-
-
Save JshWright/848320 to your computer and use it in GitHub Desktop.
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/env python | |
from collections import namedtuple | |
log_file = '/var/log/apt-cacher-ng/apt-cacher.log' | |
def humanize_bytes(bytes, precision=1): | |
''' | |
Borrowed from: | |
http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/ | |
''' | |
abbrevs = ( | |
(1<<50L, 'PB'), | |
(1<<40L, 'TB'), | |
(1<<30L, 'GB'), | |
(1<<20L, 'MB'), | |
(1<<10L, 'kB'), | |
(1, 'bytes') | |
) | |
if bytes == 1: | |
return '1 byte' | |
for factor, suffix in abbrevs: | |
if bytes >= factor: | |
break | |
return '%.*f %s' % (precision, bytes / factor, suffix) | |
def parse_log(): | |
clients = [] | |
transfer_in = [] | |
transfer_out = [] | |
LogLine = namedtuple('LogLine', 'timestamp direction bytes client file') | |
log_lines = (LogLine(*line.split('|')) for line in open(log_file)) | |
for line in log_lines: | |
if line.client not in clients: | |
clients.append(line.client) | |
if line.direction == 'I': | |
transfer_in.append(int(line.bytes)) | |
else: | |
transfer_out.append(int(line.bytes)) | |
total_in = sum(transfer_in) | |
total_out = sum(transfer_out) | |
total_saved = total_out - total_in | |
report_template = """ | |
Total Clients: {client_count} | |
Total Transfer In: {transfer_in} | |
Total Transfer Out: {transfer_out} | |
Total Transfer Saved: {transfer_saved} | |
""" | |
print(report_template.format(client_count=len(clients), | |
transfer_in=humanize_bytes(total_in), | |
transfer_out=humanize_bytes(total_out), | |
transfer_saved=humanize_bytes(total_saved) | |
)) | |
if __name__ == '__main__': | |
parse_log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment