Apache Bandwidth from access.log
Wednesday, March 3rd, 2010Quick and dirty Apache Bandwidth graphs with GnuPlot
#!/bin/bash
PATH=/opt/local/bin:$PATH
TMP=/tmp/`basename $0`.$$
trap "/bin/rm ${TMP} >/dev/null 2>&1" 0 1 15
gzcat *access.log.*.gz |\
sed -e 's/\[//g' -e 's/\]//g' |\
awk '
{
day=substr($4,0,2)
month=substr($4,4,3)
year=substr($4,8,4)
hhmm=substr($4,13,5)
if ( month =="Jan" ) month="01"
if ( month =="Feb" ) month="02"
if ( month =="Mar" ) month="03"
if ( month =="Apr" ) month="04"
if ( month =="May" ) month="05"
if ( month =="Jun" ) month="06"
if ( month =="Jul" ) month="07"
if ( month =="Aug" ) month="08"
if ( month =="Sep" ) month="09"
if ( month =="Oct" ) month="10"
if ( month =="Nov" ) month="11"
if ( month =="Dec" ) month="12"
timestamp=year"-"month"-"day"-"hhmm
hits[timestamp]++
bytes[timestamp] += int($(10))
}
END { for (timestamp in hits)
printf("%s %10d %20d\n",
timestamp,
hits[timestamp],
bytes[timestamp]*8/60/1024)
}
' | sort > ${TMP}
# Now we have YYYY-MM-DD-HH:MM hitspermin and Kbps
cat < < EOF_GNUPLOT_SCRIPT | gnuplot
set terminal png enhanced size 1024,768
set xdata time
set timefmt "%Y-%m-%d-%H:%M"
set format x "%d-%H:00"
set xlabel "time"
set grid
set style data points
set xrange [ "2010-02-24-00:00" : "2010-03-02-23:59" ]
set output "hits.png"
set ylabel "Hits per minute"
set title "Apache Hits"
plot "$TMP" using 1:2 title ""
EOF_GNUPLOT_SCRIPT
cat << EOF_GNUPLOT_SCRIPT | gnuplot
set terminal png enhanced size 1024,768
set xdata time
set timefmt "%Y-%m-%d-%H:%M"
set format x "%d-%H:00"
set xlabel "Time"
set grid
set style data points
set xrange [ "2010-02-24-00:00" : "2010-03-02-23:59" ]
set yrange [ 0 : 6000 ]
set output "bandwidth.png"
set ylabel "Kbps"
set title "Apache Bandwidth Past Week"
plot "$TMP" using 1:3 title ""
EOF_GNUPLOT_SCRIPT

