前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx结构原理全解析(17)

Nginx结构原理全解析(17)

原创
作者头像
陈不成i
修改2021-05-11 14:37:18
2160
修改2021-05-11 14:37:18
举报
文章被收录于专栏:ops技术分享ops技术分享

二.脚本

log_analysis.sh

  1. #!/bin/bash
  2. ###############################################
  3. # Desc :nginx日志分析脚本 #
  4. # Author : Bertram #
  5. # Date : 2019-12-21 #
  6. # Copyright : Personal belongs #
  7. ###############################################
  8. public(){
  9. echo ""
  10. read -p "请输入要分析的访问日志: " log_file
  11. echo ""
  12. if [ ! -f $log_file ];then
  13. echo "未找到: ${log_file}"
  14. exit 1
  15. fi
  16. if [ ! -s $log_file ];then
  17. echo "${log_file}是空文件"
  18. exit 1
  19. fi
  20. #输出日志访问量排名前top_num条数据,可自定义
  21. top_num=5
  22. input_file=`echo $log_file | awk -F '/' '{print $(NF)}'`
  23. analyze_dir=/home/Bertram/`date +%F`
  24. top_ip_file=$analyze_dir/ngx_log_top_ip_${input_file}.txt
  25. top_src_url_file=$analyze_dir/ngx_log_top_src_url_${input_file}.txt
  26. top_dest_url_file=$analyze_dir/ngx_log_top_dest_url_${input_file}.txt
  27. top_code_file=$analyze_dir/ngx_log_top_code_${input_file}.txt
  28. top_terminal_file=$analyze_dir/ngx_log_top_terminal_${input_file}.txt
  29. mkdir -p $analyze_dir
  30. start_time=`head -1 $log_file | awk '{print $4}'|cut -d "[" -f2`
  31. end_time=`tail -1 $log_file | awk '{print $4}'|cut -d "[" -f2`
  32. total_nums=`wc -l $log_file | awk '{print $1}'`
  33. size=`du -sh $log_file | awk '{print $1}'`
  34. #获取起始与截止时间
  35. echo "访问起始时间: $start_time ; 截止时间: $end_time"
  36. #获取总行数与大小
  37. echo "共访问 $total_nums 次 ; 日志大小: $size"
  38. //以下需根据自己的nginx日志格式来进行截取:
  39. #获取最活跃IP
  40. cat $log_file | awk '{print $1}' | sort | uniq -c | sort -rn | head -${top_num} > $top_ip_file
  41. #获取访问来源最多的url
  42. cat $log_file | awk '{print $11}' | sort | uniq -c | sort -rn | head -${top_num} > $top_src_url_file
  43. #获取请求最多的url
  44. cat $log_file | awk '{print $7}' | sort | uniq -c | sort -rn | head -${top_num} > $top_dest_url_file
  45. #获取返回最多的状态码
  46. cat $log_file | awk '{print $9}'| sort | uniq -c | sort -rn | head -${top_num} > $top_code_file
  47. #获取返回最多的终端类型
  48. cat $log_file | awk '{print $13}'| sort | uniq -c | sort -rn | head -${top_num} > $top_terminal_file
  49. }
  50. simple(){
  51. echo "+-+-+-+-+-+- 下面是分析内容 +-+-+-+-+-+-"
  52. #获取最活跃IP
  53. printf "最活跃的前${top_num}个访问IP: \n"
  54. cat $top_ip_file
  55. echo ""
  56. #获取访问来源最多的url
  57. printf "访问来源最多的前${top_num}个url: \n"
  58. cat $top_src_url_file
  59. echo ""
  60. #获取请求最多的url
  61. printf "请求最多的前${top_num}个url: \n"
  62. cat $top_dest_url_file
  63. echo ""
  64. #获取返回最多的状态码
  65. printf "返回最多的前${top_num}个状态码: \n"
  66. cat $top_code_file
  67. echo ""
  68. printf ""
  69. #获取返回最多的终端号
  70. printf "返回最多的前${top_num}个终端号: \n"
  71. cat $top_terminal_file
  72. echo ""
  73. printf ""
  74. printf "返回最多的前${top_num}个IP所属城市(查询时间有点慢,耐心等待!): \n"
  75. echo ''
  76. printf "%-15s %-15s %-30s\n" "访问次数" " IP地址" " 归属地"
  77. echo '-----------------------------------------------'
  78. a=0
  79. cat $analyze_dir/ngx_log_top_ip_${input_file}.txt | while read line
  80. do
  81. ip=$(echo $line | cut -d ' ' -f2)
  82. count=$(echo $line | cut -d ' ' -f1)
  83. printf "%-10s %-15s %-30s\n" $count $ip $(curl -s "http://freeapi.ipip.net/$(echo $line | cut -d ' ' -f2)" | awk -F '\"' {'print $2"--"$4"--"$6'})
  84. echo '-----------------------------------------------'
  85. let a=a+1
  86. done
  87. echo ""
  88. printf ""
  89. }
  90. case $1 in
  91. help)
  92. echo ""
  93. echo -e $"Usage: $0 enter a log file \n"
  94. ;;
  95. *)
  96. public
  97. simple
  98. ;;
  99. esac
  100. exit 0

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 二.脚本
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档