zoukankan      html  css  js  c++  java
  • nginx日志统计分析-shell

    nginx日志分析常用命令,包括IP相关统计、页面访问统计、性能分析、蜘蛛抓取统计、TCP连接统计等相关命令的总结

    1. IP相关统计

    1.1 ip访问量统计

    awk '{print $1}' access.log | sort -n | uniq | wc -l

    1.2 查看某一时间段的IP访问量(5-6点)

    grep "07/Jan/2019:0[5-6]" access.log | awk '{print $1}' | sort | uniq -c| sort -nr | wc -l

    1.3 查看访问最频繁的10个ip

    awk '{print $1}' access.log | sort -n |uniq -c | sort -rn | head -n 10
    

    1.4 查询某个IP的详细访问情况,按访问频率排序

    grep '172.168.1.10' access.log |awk '{print $7}'|sort |uniq -c |sort -rn |head -n 10

    2.页面统计访问

    2.1 查看访问最频的页面(TOP10)

    awk '{print $7}' access.log | sort |uniq -c | sort -rn | head -n 10

    2.2 查看访问最频的页面([排除php页面】(TOP10)

    grep -v ".php" access.log | awk '{print $7}' | sort |uniq -c | sort -rn | head -n 10

    2.3 查看页面访问次数超过100次的页面

    cat access.log | cut -d ' ' -f 7 | sort |uniq -c | awk '{if ($1 > 100) print $0}' | less

    2.4 查看最近1000条记录,访问量最高的页面

    tail -1000 access.log |awk '{print $7}'|sort|uniq -c|sort -nr|less

    3. 请求量统计

    3.1统计每秒的请求数,top10的时间点(精确到秒)

    awk '{print $4}' access.log |cut -c 14-21|sort|uniq -c|sort -nr|head -n 100
    

    每分钟请求量统计

    3.2 统计每分钟的请求数,top100的时间点(精确到分钟)

    awk '{print $4}' access.log |cut -c 14-18|sort|uniq -c|sort -nr|head -n 100

    每小时请求量统计

    3.3 统计每小时的请求数,top100的时间点(精确到小时)

    awk '{print $4}' access.log |cut -c 14-15|sort|uniq -c|sort -nr|head -n 100
    

    4.性能分析

    在nginx log中最后一个字段加入$request_time

    列出传输时间超过3秒的页面,显示前20条

    cat access.log|awk '($NF > 3){print $7}'|sort -n|uniq -c|sort -nr|head -20

    列出php页面请求时间超过3秒的页面,并统计其出现的次数,显示前100条

    cat access.log|awk '($NF > 1 && $7~/.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

    5.蜘蛛抓取统计

    5.1统计蜘蛛抓取次数

    grep 'Baiduspider' access.log |wc -l
    

    5.2 统计蜘蛛抓取404的次数

    grep 'Baiduspider' access.log |grep '404' | wc -l
    

    6. TCP链接统计

    6.1 查看当前TCP连接数

    netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l
    

    6.2 用tcpdump嗅探80端口的访问看看谁最高

    tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr

    实例脚本

    获取前一分钟nginx访问日志条数

    #!/bin/bash
    
    export LANG=C
    export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    TIME=$(date -d "1 minute ago" +"%d/%h/%Y:%H:%M")
    
    grep "$TIME" /var/log/nginx/access.log | wc -l

    获取前一分钟nginx错误日志条数

    #!/bin/bash
    
    export LANG=C
    export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    TIME=$(date -d "1 minute ago" +"%Y-%m-%d %H:%M")
    
    grep "$TIME" /var/log/nginx/error.log | wc -l

    原文链接:https://blog.csdn.net/wanchaopeng/article/details/86235893

  • 相关阅读:
    <转>反调试技巧总结原理和实现
    反调试功能<IsDebuggerPresent>
    通过取得MAC地址判断是否在VM中
    任何值得拥有的东西
    我的程序里
    吸引力法则之谜——十一条被遗忘的定律
    要成功,请忘掉自尊
    我是个只顾着想,却不去做的人
    现在有12个金币,其中一个有质量问题(或重或轻),还有一个无砝码的天平,让你称三次怎么样找到那个有质量问题的金币?
    惆怅
  • 原文地址:https://www.cnblogs.com/xzlive/p/15212555.html
Copyright © 2011-2022 走看看