用shell脚本分析Nginx日志

发表于:2011-12-02 14:45

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:余洪春    来源:51Testing软件测试网采编

  本文将介绍用shell脚本来分析Nginx负载均衡器的日志,这样可以快速得出排名靠前的网站和IP等,推荐大家使用线上环境下的shell脚本。本文中的shell脚本又分为两种情况,第一种情况是Nginx作为最前端的负载均衡器,其集群架构为Nginx+Keepalived时,脚本内容如下所示:

  1. vim log-nginx.sh  
  2. #!/bin/bash  
  3. if [$# -eq 0 ]; then  
  4.   echo "Error: please specify logfile."  
  5.   exit 0  
  6. else  
  7.   LOG=$1  
  8. fi  
  9. if [ ! -f$1 ]; then  
  10.   echo "Sorry, sir, I can't find this apache log file, pls try again!"  
  11. exit 0  
  12. fi  
  13. ####################################################  
  14. echo "Most of the ip:"  
  15. echo "-------------------------------------------"  
  16. awk '{ print$1 }'$LOG| sort| uniq -c| sort -nr| head -10  
  17. echo  
  18. echo  
  19. ####################################################  
  20. echo "Most of the time:"  
  21. echo "--------------------------------------------"  
  22. awk '{ print$4 }'$LOG| cut -c 14-18| sort| uniq -c| sort -nr| head -10  
  23. echo  
  24. echo  
  25. ####################################################  
  26. echo "Most of the page:"  
  27. echo "--------------------------------------------"  
  28. awk '{print$11}'$LOG| sed 's/^.*\\(.cn*\\)\"/\\1/g'| sort| uniq -c| sort -rn| head -10  
  29. echo  
  30. echo  
  31. ####################################################  
  32. echo "Most of the time / Most of the ip:"  
  33. echo "--------------------------------------------"  
  34. awk '{ print$4 }'$LOG| cut -c 14-18| sort -n| uniq -c| sort -nr| head -10 > timelog  
  35. for i in 'awk '{ print$2 }' timelog'  
  36. do  
  37.   num='grep$i timelog| awk '{ print$1 }''  
  38.   echo "$i$num"  
  39.   ip='grep$i$LOG| awk '{ print$1}'| sort -n| uniq -c| sort -nr| head -10'  
  40.   echo "$ip"  
  41.   echo  
  42. done  
  43. rm -f timelog

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号