• GoAccess安装


    编译安装

    yum install geoip-devel openssl-devel libmaxminddb-devel ncurses-devel bzip2-devel tokyocabinet-devel 
    
    cd /usr/local/src
    wget https://tar.goaccess.io/goaccess-1.3.tar.gz
    tar -xzvf goaccess-1.3.tar.gz
    cd goaccess-1.2/
    ./configure --enable-utf8 --enable-geoip=legacy --with-openssl --enable-tcb=memhash --with-getline
    make
    make install
    

    注意: 仅仅需要依赖 ncurses 模块。

    提示:如果需要使用实时 HTML 报告, 请确保开放 7890 端口。

    验证安装是否成功:goaccess --v

    # goaccess --v
    GoAccess - 1.3.
    For more details visit: http://goaccess.io
    Copyright (C) 2009-2016 by Gerardo Orellana
    
    Build configure arguments:
      --enable-utf8
      --enable-geoip=mmdb
      --enable-tcb=memhash
      --with-getline
      --with-openssl
    

    或者最简单的使用yum方式安装:yum install goaccess,不过不是最新版的

    编译选项

    GoAccess 拥有多个配置选项。获取完整的最新配置选项列表,请运行:./configure --help

    • --enable-debug

      使用调试标志编译且关闭编译器优化。

    • --enable-utf8

      宽字符支持。依赖 Ncursesw 模块。

    • --enable-geoip=<legacy|mmdb>

      地理位置支持。依赖 MaxMind GeoIP 模块。legacy 将使用原始 GeoIP 数据库。mmdb 将使用增强版 GeoIP2 数据库。

    • --enable-tcb=<memhash|btree>

      Tokyo Cabinet 存储支持。 memhash 将使用 Tokyo Cabinet 的内存哈希数据库。 btree 将使用 Tokyo Cabinet 的磁盘 B+Tree 数据库。

    • --disable-zlib

      禁止在 B+Tree 数据库上使用 zlib 压缩。

    • --disable-bzip

      禁止在 B+Tree 数据库上使用 bzip2 压缩。

    • --with-getline

      使用动态扩展行缓冲区用来解析完整的行请求,否则将使用固定大小(4096)的缓冲区。

    • --with-openssl

      使 GoAccess 与其 WebSocket 服务器之间的通信能够支持 OpenSSL。

    安装好后配置文件路径:/usr/local/etc/goaccess.conf

    相关发行版安装包

    GoAccess 使用纯C编写,仅依赖 ncurses 模块。 但是如果通过源码来构建 GoAccess,不同发行版还是有一些区别,下表列出了一些发行版的可选依赖模块。

    Distro NCurses GeoIP (optional) Tokyo Cabinet (optional) OpenSSL (optional)
    Ubuntu/Debian libncursesw5-dev libgeoip-dev libtokyocabinet-dev libssl-dev
    Fedora/RHEL/CentOS ncurses-devel geoip-devel tokyocabinet-devel openssl-devel
    Arch Linux ncurses geoip compile from source openssl
    Gentoo sys-libs/ncurses dev-libs/geoip dev-db/tokyocabinet dev-libs/openssl
    Slackware ncurses GeoIP tokyocabinet openssl

    注意: 通过源码构建 GoAccess 您可能需要一些编译/构建工具比如 gcc, make, base-devel, build-essential, "Development Tools" 等等。

    GoAccess配置

    在配置文件最后一行加入下面代码,配置文件路径:/usr/local/etc/goaccess.conf

    log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"  
    date-format %d/%b/%Y  
    time-format %H:%M:%S 
    

    1、中文展示

    如果你的系统默认设置时英文显示,想要以中文显示日志报表,就需要进行字符设置

    临时设置为中文的命令:
    # LANG="zh_CN.UTF-8"
    # goaccess -p /usr/local/etc/goaccess/goaccess.conf -f /var/log/nginx/access.log  -o /usr/share/nginx/html/report.html
    

    2、配置nginx中日志log_format中的日志格式

    该功能是识别nginx日志中关于时间的格式,生成goaccess可以识别的时间格式,然后把这个时间格式配置到Goaccess配置文件中。若是nginx的access日志是格式使用默认的设置,则这一步不用配置

    改完记得reload配置

    2.1 获取nginx.conf中log_format配置

    2.2 获取nginx2goaccess脚本,可以将日志格式转换为goaccess能识别的日志格式,脚本地址:https://github.com/stockrt/nginx2goaccess/blob/master/nginx2goaccess.sh

    #!/bin/bash
    #
    # Convert from this:
    #   http://nginx.org/en/docs/http/ngx_http_log_module.html
    # To this:
    #   https://goaccess.io/man
    #
    # Conversion table:
    #   $time_local         %d:%t %^
    #   $host               %v
    #   $http_host          %v
    #   $remote_addr        %h
    #   $request_time       %T
    #   $request_method     %m
    #   $request_uri        %U
    #   $server_protocol    %H
    #   $request            %r
    #   $status             %s
    #   $body_bytes_sent    %b
    #   $bytes_sent         %b
    #   $http_referer       %R
    #   $http_user_agent    %u
    #
    # Samples:
    #
    # log_format combined '$remote_addr - $remote_user [$time_local] '
    # '"$request" $status $body_bytes_sent '
    # '"$http_referer" "$http_user_agent"';
    #   ./nginx2goaccess.sh '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'
    #
    # log_format compression '$remote_addr - $remote_user [$time_local] '
    # '"$request" $status $bytes_sent '
    # '"$http_referer" "$http_user_agent" "$gzip_ratio"';
    #   ./nginx2goaccess.sh '$remote_addr - $remote_user [$time_local] "$request" $status $bytes_sent "$http_referer" "$http_user_agent" "$gzip_ratio"'
    #
    # log_format main
    # '$remote_addr	$time_local	$host	$request	$http_referer	$http_x_mobile_group	'
    # 'Local:	$status	$body_bytes_sent	$request_time	'
    # 'Proxy:	$upstream_cache_status	$upstream_status	$upstream_response_length	$upstream_response_time	'
    # 'Agent:	$http_user_agent	'
    # 'Fwd:	$http_x_forwarded_for';
    #   ./nginx2goaccess.sh '$remote_addr	$time_local	$host	$request	$http_referer	$http_x_mobile_group	Local:	$status	$body_bytes_sent	$request_time	Proxy:	$upstream_cache_status	$upstream_status	$upstream_response_length	$upstream_response_time	Agent:	$http_user_agent	Fwd:	$http_x_forwarded_for'
    #
    # log_format main
    # '${time_local}	${remote_addr}	${host}	${request_method}	${request_uri}	${server_protocol}	'
    # '${http_referer}	${http_x_mobile_group}	'
    # 'Local:	${status}	*${connection}	${body_bytes_sent}	${request_time}	'
    # 'Proxy:	${upstream_status}	${upstream_cache_status}	'
    # '${upstream_response_length}	${upstream_response_time}	${uri}${log_args}	'
    # 'Agent:	${http_user_agent}	'
    # 'Fwd:	${http_x_forwarded_for}';
    #   ./nginx2goaccess.sh '${time_local}	${remote_addr}	${host}	${request_method}	${request_uri}	${server_protocol}	${http_referer}	${http_x_mobile_group}	Local:	${status}	*${connection}	${body_bytes_sent}	${request_time}	Proxy:	${upstream_status}	${upstream_cache_status}	${upstream_response_length}	${upstream_response_time}	${uri}${log_args}	Agent:	${http_user_agent}	Fwd:	${http_x_forwarded_for}'
    #
    # Author: Rogério Carvalho Schneider <stockrt@gmail.com>
    
    # Params
    log_format="$1"
    
    # Usage
    if [[ -z "$log_format" ]]; then
        echo "Usage: $0 '<log_format>'"
        exit 1
    fi
    
    # Variables map
    conversion_table="time_local,%d:%t_%^
    host,%v
    http_host,%v
    remote_addr,%h
    request_time,%T
    request_method,%m
    request_uri,%U
    server_protocol,%H
    request,%r
    status,%s
    body_bytes_sent,%b
    bytes_sent,%b
    http_referer,%R
    http_user_agent,%u"
    
    # Conversion
    for item in $conversion_table; do
        nginx_var=${item%%,*}
        goaccess_var=${item##*,}
        goaccess_var=${goaccess_var//_/ }
        log_format=${log_format//${$nginx_var}/$goaccess_var}
        log_format=${log_format//$$nginx_var/$goaccess_var}
    done
    log_format=$(echo "$log_format" | sed 's/${[a-z_]*}/%^/g')
    log_format=$(echo "$log_format" | sed 's/$[a-z_]*/%^/g')
    
    # Config output
    echo "
    - Generated goaccess config:
    time-format %T
    date-format %d/%b/%Y
    log_format $log_format
    "
    # EOF
    
    # sh nginx2goaccess.sh '$remote_addr $http_x_forwarded_for [$time_local] $host "$request_uri" $status "$http_referer" "$http_user_agent"'
    
    - Generated goaccess config:
    time-format %T
    date-format %d/%b/%Y
    log_format %h %^ [%d:%t %^] %v "%U" %s "%R" "%u"
    

    2.3 将转换后的内容加入到配置/usr/local/etc/goaccess.conf

    3.添加定时任务,每隔30分钟执行一次

    # vim /opt/goaccess_nginx_log.sh
    #!/bin/env /bash
    
    LANG="zh_CN.UTF-8"
    /usr/local/bin/goaccess -p /usr/local/etc/goaccess/goaccess.conf -f /var/log/nginx/access.log  -o /usr/share/nginx/html/report.html
    
    # chmod a+x /opt/goaccess_nginx_log.sh
    # crontab -e 
    # 每隔1分钟生成一次html文件
    */1 * * * * /usr/bin/bash /opt/goaccess_nginx_log.sh
    

    4.配置一个Nginx虚拟主机,将root指向/usr/share/nginx/html/,这样可以通过域名去访问html页面

    1.虚拟主机,在原有的配置加一个location

    location / {
    	root /usr/share/nginx/html/;
    	index report.html;
    }
    

    5.完善的地方

    1. nginx的access日志每天轮询,每天生成report页面

    后期考虑到每天都要对nginx的access日志进行切割,可以使用脚本的形式每天根据切割后的日志文件生成响应的report页面,然后使用浏览器访问的时候根据日期进行选择

    进一步完善/opt/goaccess_nginx_log.sh脚本即可,使用此脚本获取nginx日志分割的信息,定期删除日志等

    1. 忽略自身查看页面的访问要如何做?
    2. 显示来源IP所在地区?
  • 相关阅读:
    OAuth2.0介绍
    C#集成FastDFS断点续传
    ABP集成WIF实现单点登录
    CentOS7下搭建FastDfs(V5.11)+Keepalived分布式集群部署
    【算法】之常见的排序算法
    【开发工具
    【C/C++】之C/C++快速入门
    【数据结构】之二叉树(Java语言描述)
    【数据结构】之散列链表(Java语言描述)
    【数据结构】之队列(Java语言描述)
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/11322125.html
Copyright © 2020-2023  润新知