Skip to content

Instantly share code, notes, and snippets.

@mamchenkov
Created August 5, 2013 08:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamchenkov/6154250 to your computer and use it in GitHub Desktop.
Save mamchenkov/6154250 to your computer and use it in GitHub Desktop.
Chart total number of web requests per hour using Google Charts
#!/bin/bash
# Chart total number of web requests per hour using Google Charts
# Usage: chart_web_request.sh [DATE] [PATH]
#
# If DATE is not given, yesterday is used. Date should be in the format
# that your web server is using in access logs
#
# If PATH is not given /var/log/httpd/*-access_log is used.
#
DATE=$1
PATH=$2
# Use yesterday, if no date is given
if [ -z "$DATE" ]
then
DATE=$(/bin/date +'%d/%b/%Y' --date=yesterday)
fi
# Use default path, if no path is given
if [ -z "$PATH" ]
then
PATH="/var/log/httpd/*-access_log"
fi
DATA=
LABEL=
MAX=0
for HOUR in $(/usr/bin/seq 0 23)
do
# Handle 0 prefix for early hours
if [ "$HOUR" -lt "10" ]
then
HOUR="0$HOUR"
fi
LABEL="$LABEL|$HOUR"
# Sum total number of calls from all files
CALLS=$(/bin/grep -c "$DATE:$HOUR:" $PATH | /bin/grep -v ":0" | /bin/cut -d ":" -f 2 | /usr/bin/paste -sd+ | /usr/bin/bc)
# Use 0 for calls, when no calls found for the hour
if [ -z "$CALLS" ]
then
CALLS=0
fi
# Remember the max value of calls (for charting later)
if [ "$CALLS" -gt "$MAX" ]
then
MAX=$CALLS
fi
DATA="$DATA,$CALLS"
done
# Chart the chart
echo "Paste the following URL into your browser"
echo "https://chart.googleapis.com/chart?cht=bvs&chs=900x250&chco=0000ff&chtt=Web+Requests+per+hour+for+$DATE&chxt=y&chxr=0,0,$MAX&chds=0,$MAX&chd=t:$DATA&chl=$LABEL" | /bin/sed -e 's/t:,/t:/' -e 's/chl=|/chl=/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment