Skip to content

Instantly share code, notes, and snippets.

@elgholm
Last active October 5, 2015 11:37
Show Gist options
  • Save elgholm/96291123da2325e430ba to your computer and use it in GitHub Desktop.
Save elgholm/96291123da2325e430ba to your computer and use it in GitHub Desktop.
Simple script for calculating bandwidth-requirements from a common Apache logfile
#!/bin/bash
# Simple script for calculating bandwidth-requirements from a common Apache logfile
# Written 2015-10-05 by Charlie Elgholm
# Usage: ./calc_bandwidth.sh logfile
cat $1 | awk '
function left(s, s2){ t=index(s, s2); if(t>0) return substr(s, 1, t-1); else return s; }
function right(s, s2){ t=index(s, s2); if(t>0) return substr(s, t+length(s2)); else return ""; }
function toDate(s)
{
s2=left(s, ":");
s2=right(s2, "[");
return right(right(s2, "/"), "/") "-" months[left(right(s2, "/"), "/")] "-" left(s2, "/")
}
BEGIN {
totalRequests=0;
totalBandwidth=0;
m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|")
for(o=1;o<=m;o++){
months[d[o]]=sprintf("%02d",o)
}
}
{
if(NF==9){
tDate=toDate($4);
if(length(tDate)==10){
bw=strtonum($7);
if(bw>=0){
if(totalRequests==0){
print "Start date: " $4
}
totalRequests++;
totalBandwidth+=bw;
bandwidth[tDate]+=bw;
requests[tDate]++;
}
}
}else{
errors++;
}
if(NR % 100000 == 0){
print NR " lines processed (" tDate ", " errors " errors)"
}
}
END{
print "End date: " $4
n=asorti(bandwidth, sorted)
for (i=1; i<=n; i++){
printf "%s: %d MB in %d requests\n", sorted[i], bandwidth[sorted[i]]/1024/1024, requests[sorted[i]]
}
printf "Total bandwidth: %d MB in %d requests\n", totalBandwidth/1024/1024, totalRequests
printf "Errors: %d\n", errors
}
' FPAT='([^ ]+)|(\\[[^\\]]+\\])|("[^"]+")'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment