2023年7月17日发(作者:)
Nginx⽇志监控、⽹站访问统计⼀、nginx log分析⼯具ngxtop ngxtop是⼀个基于python的程序,可以在Python上安装。ngxtop通过实时解析nginx访问⽇志,并将结果(nginx服务器的有⽤指标)输出到终端。ngxtop解析nginx访问⽇志并输出nginx服务器的有⽤的顶级度量,可以实时判断服务器发⽣了什么。主要的功能当前有效请求总请求计数摘要按状态代码提供的总请求数(2xx,3xx,4xx,5xx)发送平均字节顶级远程地址不仅能实时监控Nginx⽇志的访问还可以对以前的⽇志进⾏排查整理。在Linux上安装ngxtop⾸先,你需要安装PIP(Python包管理系统)使⽤root凭据登录你的服务器通过安装以下包启⽤EPEL存储库wget /pub/epel/7/x86_64/e/ -ivh # 然后执⾏以下命令安装pipyum install python-pip# 最后使⽤下⾯安装ngxtoppip install ngxtopngxtop使⽤详解Usage: ngxtop [options] ngxtop [options] (print|top|avg|sum) ngxtop infoOptions: -l , --access-log access log file to parse. -f , --log-format log format as specify in log_format directive. --no-follow ngxtop default behavior is to ignore current lines in log and only watch for new lines as they are written to the access log. Use this flag to tell ngxtop to process the current content of the access log instead. -t , --interval report interval when running in follow mode [default: 2.0] -g , --group-by group by variable [default: request_path] -w , --having having clause [default: 1] -o , --order-by order of output for default query [default: count] -n , --limit limit the number of records included in report for top command [default: 10] -a ..., --a ... add exp (must be aggregation exp: sum, avg, min, max, etc.) into output -v, --verbose more verbose output -d, --debug print every line and parsed record -h, --help print this help message. --version print version information. Advanced / experimental options: -c , --config allow ngxtop to parse nginx config file for log format and location. -i , --filter filter in, records satisfied given expression are processed. -p , --pre-filter in-filter expression to check in pre-parsing phase. ngxtop实践1.使⽤ngxtop命令查看请求计数摘要,请求的URI,状态码请求的数量。$ ngxtoprunning for 411 seconds, 64332 records processed: 156.60 req/secSummary:| count | avg_bytes_sent | 2xx | 3xx | 4xx | 5xx ||---------+------------------+-------+-------+-------+-------|| 64332 | 2775.251 | 61262 | 2994 | 71 | 5 |Detailed:| request_path | count | avg_bytes_sent | 2xx | 3xx | 4xx | 5xx ||------------------------------------------+---------+------------------+-------+-------+-------+-------|| /abc/xyz/xxxx | 20946 | 434.693 | 20935 | 0 | 11 | 0 || / | 5633 | 1483.723 | 5633 | 0 | 0 | 0 || /xxxxx/xxx/xxxxxxxxxxxxx | 3629 | 6835.499 | 3626 | 0 | 3 | 0 || /xxxxx/xxx/xxxxxxxx | 3627 | 15971.885 | 3623 | 0 | 4 | 0 || /xxxxx/xxx/xxxxxxx | 3624 | 7830.236 | 3621 | 0 | 3 | 0 || /static/js/minified/ | 3031 | 1781.155 | 2104 | 927 | 0 | 0 || /static/js/minified/ | 2889 | 2210.235 | 2068 | 821 | 0 | 0 || /static/tracking/js/ | 2594 | 1325.681 | 1927 | 667 | 0 | 0 || /xxxxx/ | 2521 | 573.597 | 2520 | 0 | 1 | 0 || /xxxxx/ | 1840 | 800.542 | 1839 | 0 | 1 | 0 |2.检查顶级客户端的IP查看谁向你的Nginx服务器发出⼤量请求$ ngxtop top remote_addrrunning for 20 seconds, 3215 records processed: 159.62 req/sectop remote_addr| remote_addr | count ||-----------------+---------|| 118.173.177.161 | 20 || 110.78.145.3 | 16 || 171.7.153.7 | 16 || 180.183.67.155 | 16 || 183.89.65.9 | 16 || 202.28.182.5 | 16 || 1.47.170.12 | 15 || 119.46.184.2 | 15 || 125.26.135.219 | 15 || 125.26.213.203 | 15 |3.列出4xx或5xx响应码$ ngxtop -i 'status >= 400' print request status http_refererrunning for 2 seconds, 28 records processed: 13.95 req/secrequest, status, http_referer:| request | status | http_referer ||-----------+----------+----------------|| - | 400 | - |4.分析top -l /path/5.使⽤通⽤格式从远程服务器解析apache⽇志ngxtop -f common -l /path/$ ssh user@remote_server tail -f /var/log/apache2/ | ngxtop -f commonrunning for 20 seconds, 1068 records processed: 53.01 req/secSummary:| count | avg_bytes_sent | 2xx | 3xx | 4xx | 5xx ||---------+------------------+-------+-------+-------+-------|| 1068 | 28026.763 | 1029 | 20 | 19 | 0 |Detailed:| request_path | count | avg_bytes_sent | 2xx | 3xx | 4xx | 5xx ||------------------------------------------+---------+------------------+-------+-------+-------+-------|| /xxxxxxxxxx | 199 | 55150.402 | 199 | 0 | 0 | 0 || /xxxxxxxx/xxxxx | 167 | 47591.826 | 167 | 0 | 0 | 0 || /xxxxxxxxxxxxx/xxxxxx | 25 | 7432.200 | 25 | 0 | 0 | 0 || /xxxx/xxxxx/x/xxxxxxxxxxxxx/xxxxxxx | 22 | 698.727 | 22 | 0 | 0 | 0 || /xxxx/xxxxx/x/xxxxxxxxxxxxx/xxxxxx | 19 | 7431.632 | 19 | 0 | 0 | 0 || /xxxxx/xxxxx/ | 18 | 7840.889 | 18 | 0 | 0 | 0 || /xxxxxxxx/xxxxxxxxxxxxxxxxx | 15 | 7356.000 | 15 | 0 | 0 | 0 || /xxxxxxxxxxx/xxxxxxxx | 15 | 9978.800 | 15 | 0 | 0 | 0 || /xxxxx/ | 14 | 0.000 | 0 | 14 | 0 | 0 || /xxxxxxxxxx/xxxxxxxx/xxxxx | 13 | 20530.154 | 13 | 0 | 0 | 0 |⼆、GoAccess实时分析绘制访问⽇志⾯板GoAccess使⽤场景对于想在短时间内快速的⽣成⼩站点访问情况的统计数据,⼀般会选择使⽤sed、awk、grep三剑客,但是对于要对数据的展⽰就需要填充到已有开发⾯板中,⽐较耗时费⼒,当然对于也可以使⽤ELK/EFK专业⽇志分析平台来做,但搭建缓慢,服务笨重,不是⼩站点的⾸选,这⾥介绍⼀个使⽤Go语⾔研发的轻量级⽇志分析⼯具,性能稳定,可作为⼩站点⽇志分析统计的⾸选。GoAccess功能GoAccess随着不断地版本迭代,其⽀持的功能也越来与丰富、越来越能满⾜基本的需求,下⾯列举⼏个基础功能,更多可参见可以实时分析⼏乎所有的web⽇志格式,并且还⽀持⾃定义⽇志格式满⾜⽇常需要关注的基础指标安装和部署简单,只需要依赖⼀个ncurses库可以在命令⾏终端试试显⽰统计报告,也可以输出到静态html页⾯展⽰分析统计数据报告。⽀持扩展的模块…实践说明安装goaccess仅有需要依赖的ncurses库sudo apt-get install libncursesw5-dev当需要把Nginx访问⽇志中的geoip给统计导⼊到新的⽂件中时,需要GeoIP库的⽀持$ wget /maxmind/geoip-api-c/releases/download/v1.6.11/$ tar -xzvf $ cd GeoIP-1.6.11$ ./configure$ make$ make install完成Goaccess依赖的环境之后,开始构建goacess⼯具wget / xf && cd goaccess-1.2/./configure --enable-utf8 --enable-geoip=legacy --with-opensslmake && make installcentos7安装#centos7wget -O /etc/.d/ /repo/et -O /etc/.d/ /repo/
#安装依赖包yum -y install glib2 glib2-devel ncurses ncurses-devel GeoIP GeoIP-devel GeoIP-data
安装Goaccess 由于⽹络问题,使⽤abcdocker提供软件包下载#abcdocker提供软件包下载wget / xf goaccess-1.3./configure --prefix=/usr/local/goaccess1.3 --enable-utf8 --enable-geoip=legacy --with-opensslmake && make install
#官⽅下载wget / xf goaccess-1.3./configure --prefix=/usr/local/goaccess1.3 --enable-utf8 --enable-geoip=legacy --with-opensslmake && make install
#设置软链ln -s /usr/local/goaccess1.3 /usr/local/goaccess对goaccess做⼀些配置1.配置环境变量echo "export PATH=/usr/local/goaccess/bin:$PATH" >>/etc/profilesource /etc/profile
2.配置⽂件修改# vim /usr/local/goaccess/etc/goaccess/me-format %H:%M:%Sdate-format %d/%b/%Ylog-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
3.配置⽂件参数说明 %x 匹配 time-format 和 date-format 变量的⽇期和时间字段。⽤于使⽤时间戳来代替⽇期和时间两个独⽴变量的场景。 %t 匹配 time-format 变量的时间字段。 %d 匹配 date-format 变量的⽇期字段。 %v 根据 canonical 名称设定的服务器名称(服务区或者虚拟主机)。 %e 请求⽂档时由 HTTP 验证决定的⽤户 ID。 %h 主机(客户端IP地址,IPv4 或者 IPv6)。 %r 客户端请求的⾏数。这些请求使⽤分隔符(单引号,双引号)引⽤的部分可以被解析。否则,需要使⽤由特殊格式说明符(例如:%m, %U, %q 和 %H)组合格式去解析独⽴的字段。 注意: 既可以使⽤ %r 获取完整的请求,也可以使⽤ %m, %U, %q and %H 去组合你的请求,但是不能同时使⽤。 %m 请求的⽅法。 %U 请求的 URL。 注意: 如果查询字符串在 %U中,则⽆需使⽤ %q。但是,如果 URL 路径中没有包含任何查询字符串,则你可以使⽤ %q 查询字符串将附加在请求后⾯。 %q 查询字符串。 %H 请求协议。 %s 服务器回传客户端的状态码。 %b 回传客户端的对象的⼤⼩。 %R HTTP 请求的 "Referer" 值。 %u HTTP 请求的 "UserAgent" 值。 %D 处理请求的时间消耗,使⽤微秒计算。 %T 处理请求的时间消耗,使⽤带秒和毫秒计算。 %L 处理请求的时间消耗,使⽤⼗进制数表⽰的毫秒计算。 %^ 忽略此字段。 %~ 继续解析⽇志字符串直到找到⼀个⾮空字符(!isspace)。 ~h 在 X-Forwarded-For (XFF) 字段中的主机(客户端 IP 地址,IPv4 或者 IPv6)。我们使⽤Nginx的⽇志为例,通过解析nginx访问⽇志进⾏数据的展⽰,为了能准确的获取数据,下⾯提供⼀下测试时使⽤的nginx⽇志格式,log_format配置在的http配置段中:#配置1log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" "$request_body"';#配置2log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_cookie" "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access_ main;下⾯配置安装好GoAccess之后的配置⽂件:root@master:/webserver/nginx18# cat /etc/ |egrep -v '^$|^#'time-format %H:%M:%Sdate-format %d/%b/%Ylog-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"...# 上⾯的三⾏添加到配置⽂件中,其余的位置不需要改动下⾯对部分的⽇志格式选项进⾏说明,更多的选项的使⽤可以直接参考官⽅⽂章%t 匹配time-format格式的时间字段%d 匹配date-format格式的⽇期字段%h host(客户端ip地址,包括ipv4和ipv6)%r 来⾃客户端的请求⾏%m 请求的⽅法%U URL路径%H 请求协议%s 服务器响应的状态码%b 服务器返回的内容⼤⼩%R HTTP请求头的referer字段%u ⽤户代理的HTTP请求报头%D 请求所花费的时间,单位微秒%T 请求所花费的时间,单位秒%^ 忽略这⼀字段在配置好之后,我们启动⼀个使⽤该nginx⽇志格式的服务,配置⽂件如下:server { server_name ; listen 80; access_log logs/; location / { proxy_set_header Host $host; proxy_set_header X-Real_IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass 192.168.99.1; }}通过命令⾏进⾏访问该服务,并且启动GoAccess的命令⾏控制台模式curl cess -a -d -f ./ -p /etc/ot@master:/webserver/nginx18/logs# pwd/webserver/nginx18/logsgoaccess -a -d -f ./ -p /etc/ -o ${}/ # goaccess分析的⽂件输出到的⽬录下,通过服务直接访问goaccess -a -d -f ./ -p /etc/ -o ${}/ --real-time-html --daemonize# 也可以通过以上命令实时分析⽇志⽂件,渲染到html页⾯三、Grafana展⽰Nginx图表(基于elk)/s/klzKThVzJFvApgcBsB-_Sg四、⽹站访问流量统计Matomo(Piwik)piwik现已改名为Matomo,这是⼀套国外著名的开源⽹站统计系统,类似于百度统计、Google Analytics等系统。可以分析流量、访客数、PV、UV等信息。Matomo相当于将流量复制了⼀份进⾏分析,所以对配置的要求⽐较⾼,尽量和负载均衡所在机器的配置⼀样。
部署:/project-46/doc-389/
/lebinh/ngxtop ngxtop项⽬地址/ goaccess项⽬地址/s/hte8fb0C404ABecc1ZGf7w Flask + echarts 轻松搞定 nginx ⽇志可视化/ GoAccess 分析Nginx ⽇志,整理不错
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689542925a264755.html
评论列表(0条)