Shell script parsing Apache log status code

Source: Internet
Author: User
Tags apache log

First, the Apache logs are cut by day

Vi/etc/httpd/conf/httpd.conf
Errorlog "|rotatelogs/var/log/httpd/%y%m%derror_log 86400 480"
Customlog "|rotatelogs/var/log/httpd/%y%m%daccess_log 86400 480" combined

Second, restart Apache service

Service httpd Restart

################# #apahce日志服务器按天切割完毕 ######################

Iii. analysis of the generated Apache log status code (waiting for a day's log)

Log file is 20111228access_log

       VI http.sh 
       #!/ Bin/bash
       total= ' cat 20111228access_log|wc-l '
      array= (' Cat 20111228access_log |awk ' {print $9} ' |sort|uniq-c|sort-n ')
      len=${#array [@]}
    & nbsp echo "################### #http状态码统计 ###########################"
      echo Total number of connections: $total

For ((i=0;i< $len; i=i+2))
{
echo "########################################################"
echo ${array[$i]}:${array[$i +1]}
echo "########################################################"
echo ${array[$i +1]} Percentage of connections: ' awk ' begin{printf '%.6f%\n ', (' ${array[$i]} '/' ${total} ' *100)} '

}

Execute script result (in percent sign):

SH http.sh
################### #http状态码统计 ###########################
Total number of connections: 688965
########################################################
1:303
########################################################
303 Percentage of connections: 0.000145%
########################################################
1:416
########################################################
416 Percentage of connections: 0.000145%
########################################################
2:299
########################################################
299 Percentage of connections: 0.00029%
########################################################
8:501
########################################################
501 Percentage of connections: 0.001161%
########################################################
145:403
########################################################
403 Percentage of connections: 0.021046%
########################################################
197:400
########################################################
400 percentage of connections: 0.028594%
########################################################
738:206
########################################################
206 Percentage of connections: 0.107117%
########################################################
7,307:404
########################################################
404 Percentage of connections: 1.060576%
########################################################
9,213:302
########################################################
302 Percentage of connections: 1.337223%
########################################################
30,574:301
########################################################
301 Percentage of connections: 4.437671%
########################################################
201,197:304
########################################################
304 Percentage of connections: 29.20279%
########################################################
439,582:200
########################################################
200 Percentage of connections: 63.803241%

####################### #简单讲解 #########################

Array= (' Cat 20111228access_log |awk ' {print $9} ' |sort|uniq-c|sort-n ')

# # #这个拿出来是一个数组了

len=${#array [@]}
# # #这个是数组的长度

echo ${array[$i]}:${array[$i +1]}
# # #这个是以for循环输入状态码和状态码的连接数

awk ' begin{printf '%.6f%\n, (' ${array[$i]} '/' ${total} ' *100)}

# # #以百分比计算各个状态码大约占的连接数

Shell script parsing Apache log status code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.