设为首页 收藏本站
查看: 1114|回复: 0

[经验分享] 用shell脚本分析Nginx日志

[复制链接]

尚未签到

发表于 2016-12-26 11:32:45 | 显示全部楼层 |阅读模式
本文中的shell脚本又分为两种情况,第一种情况是Nginx作为最前端的负载均衡器,其集群架构为Nginx+Keepalived时,脚本内容如下所示:
vim log-nginx.sh  
#!/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
#!/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的文件。[iyunv@localhost 03]# sh counter_nginx.sh www_tomcat_20110331.log大家应该跟我一样比较关注脚本运行后的第一项和第二项结果,即访问我们网站最多的IP和哪个时间段IP访问比较多,如下所示:

  


    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服务器和邮件服务器。另外,如果对日志有特殊需求的话,还可以架设专用的日志服务器来收集Linux服务器日志。总之一句话:一切看需求而定。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-319685-1-1.html 上篇帖子: Nginx和LVS集群负载均衡的比较 下篇帖子: nginx+php-fpm性能参数优化原则
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表