Skip to content

Instantly share code, notes, and snippets.

@willianantunes
Created March 3, 2017 13:11
Show Gist options
  • Save willianantunes/54da15a0c779eeb5b862b789cf1f0565 to your computer and use it in GitHub Desktop.
Save willianantunes/54da15a0c779eeb5b862b789cf1f0565 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Checks the requests status available in an access.log file and outputs the first 20 lowest responses.
# The following pattern is expected: '%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" %Dms'
# Author: Thiago Ferreira (https://github.com/tfc1304)
# Example of command to use it: ./check-access-log.sh '/my-path/services/rest/users' '/tmp/access_log.2017-03-03'
RED='\033[0;31m'
BLUE='\033[1;34m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NO_COLOR='\033[0m'
SCREEN_REFRESH_TIME=30
log="/var/log/tomcat/logs/access_log.log"
logLines=10000
if [ -z "$1" ]
then
echo -e "\n Please inform the PATH which will be used by the query!\n"
exit 3
fi
if [ ! -z "$2" ]
then
log=$2
fi
if [ -z "$3" ]
then
logLines=`wc -l ${log} | awk '{ print $NR }'`
fi
echo $logLines
fragmentPath=$1
function checkstatus() {
CMD_STATUS_200="tail -n $logLines $log | grep '$fragmentPath' | grep ' 200 ' | wc -l"
CMD_STATUS_400="tail -n $logLines $log | grep '$fragmentPath' | grep ' 400 ' | wc -l"
CMD_STATUS_404="tail -n $logLines $log | grep '$fragmentPath' | grep ' 404 ' | wc -l"
CMD_STATUS_500="tail -n $logLines $log | grep '$fragmentPath' | grep ' 500 ' | wc -l"
CMD_SUM="tail -n $logLines $log | grep '$fragmentPath' | awk '{ print \$NF }' |sed 's/ms//g' | awk '{sum+=\$1} END { print sum }'"
CMD_TOP_20="tail -n $logLines $log | grep '$fragmentPath' | awk '{ print \$NF \" \" \$7 }' | sed 's/ms//g' | sort -nr | head -n 20"
`eval $CMD_TEST`
top_20=`eval $CMD_TOP_20`
status_200=`eval $CMD_STATUS_200`
status_400=`eval $CMD_STATUS_400`
status_404=`eval $CMD_STATUS_404`
status_500=`eval $CMD_STATUS_500`
sum=`eval $CMD_SUM`
average=`expr $sum / $logLines`
clear
echo ""
echo ""
echo -e "################################################################"
echo -e "# CHECK #"
echo -e "################################################################"
echo -e "# ${GREEN} HTTP 200: $status_200 ${NO_COLOR}"
echo -e "# ${YELLOW} HTTP 400: $status_400 ${NO_COLOR}"
echo -e "# ${YELLOW} HTTP 404: $status_404 ${NO_COLOR}"
echo -e "# ${RED} HTTP 500: $status_500 ${NO_COLOR}"
echo -e "################################################################"
echo -e "# ${BLUE} Response average time: ${NO_COLOR} ${YELLOW}${average} ms${NO_COLOR}"
echo -e "################################################################"
echo -e "$top_20"
echo ""
echo ""
}
checkstatus
while sleep $SCREEN_REFRESH_TIME; do clear; checkstatus; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment