分享下shell脚本分析nginx日志的方法,负责ginx前端日志分析的shell脚本,以及以Nginx作为Web端,置于LVS后面时的日志分析脚本
nginx日志分析shell脚本
复制代码代码示例:
#!/bin/bash
if [$# -eq 0 ]; then echo "Error: please specify logfile." exit 0 else LOG=$1 fi if [ ! -f $1 ]; then echo "Sorry, sir, I can""t find this apache log file, pls try again!" exit 0 fi ################################ echo "Most of the ip:" echo "-------------------------------------------" awk ""{ print $1 }""$LOG| sort| uniq -c| sort -nr| head -10 echo echo ################### echo "Most of the time:" echo "--------------------------------------------" awk ""{ print $4 }""$LOG| cut -c 14-18| sort| uniq -c| sort -nr| head -10 echo echo ####################### echo "Most of the page:" echo "--------------------------------------------" awk ""{print $11}""$LOG| sed ""s/^.*\\(.cn*\\)\"/\\1/g""| sort| uniq -c| sort -rn| head -10 echo echo #####################3 echo "Most of the time / Most of the ip:" echo "--------------------------------------------" awk ""{ print $4 }""$LOG| cut -c 14-18| sort -n| uniq -c| sort -nr| head -10 > timelog for i in ""awk ""{ print $2 }"" timelog"" do num=""grep $i timelog| awk ""{ print $1 }"""" echo "$i $num" ip=""grep $i $LOG| awk ""{ print $1}""| sort -n| uniq -c| sort -nr| head -10"" echo "$ip" echo done rm -f timelog
情况二,以nginx作为web端,置于lvs后面,这时要剔除掉lvs的ip地址,比如lvs服务器的公网ip地址(像203.93.236.141、203.93.236.145等)。
复制代码代码示例:
#!/bin/bash
if [$# -eq 0 ]; then echo "Error: please specify logfile." exit 0 else cat$1| egrep -v '203.93.236.141|145' > LOG fi if [ ! -f$1 ]; then echo "Sorry, sir, I can't find this apache log file, pls try again!" exit 0 fi ################################################### echo "Most of the ip:" echo "-------------------------------------------" awk '{ print$1 }' LOG| sort| uniq -c| sort -nr| head -10 echo echo #################################################### echo "Most of the time:" echo "--------------------------------------------" awk '{ print$4 }' LOG| cut -c 14-18| sort| uniq -c| sort -nr| head -10 echo echo #################################################### echo "Most of the page:" echo "--------------------------------------------" awk '{print$11}' LOG| sed 's/^.*\\(.cn*\\)\"/\\1/g'| sort| uniq -c| sort -rn| head -10 echo echo #################################################### echo "Most of the time / Most of the ip:" echo "--------------------------------------------" awk '{ print$4 }' LOG| cut -c 14-18| sort -n| uniq -c| sort -nr| head -10 > timelog for i in 'awk '{ print$2 }' timelog' do num='grep$i timelog| awk '{ print$1 }'' echo "$i$num" ip='grep$i LOG| awk '{ print$1}'| sort -n| uniq -c| sort -nr| head -10' echo "$ip" echo done rm -f timelog
可以用此脚本分析文件名为www_tomcat_20110331.log的文件。
Most of the ip:
------------------------------------------- 5440 117.34.91.54 9 119.97.226.226 4 210.164.156.66 4 173.19.0.240 4 109.230.251.35 2 96.247.52.15 2 85.91.140.124 2 74.168.71.253 2 71.98.41.114 2 70.61.253.194 Most of the time: -------------------------------------------- 12 15:31 11 09:45 10 23:55 10 21:45 10 21:37 10 20:29 10 19:54 10 19:44 10 19:32 10 19:13
如果对日志的要求不高,可以直接通过awk和sed来分析linux日志(如果对perl熟练也可以用它来操作),还可以通过awstats来进行详细分析,后者尤其适合web服务器和邮件服务器。 |