首页 > 系统相关 >通过ngx-lua来统计nginx上的虚拟主机性能数据

通过ngx-lua来统计nginx上的虚拟主机性能数据

时间:2023-06-11 18:33:27浏览次数:53  
标签:虚拟主机 lua counter xiaomi nginx weatherapi time com market

介绍

以前我们为nginx做统计,都是通过对日志的分析来完成.比较麻烦,现在基于ngx_lua插件,开发了实时统计站点状态的脚本,解放生产力.

项目主页: https://github.com/skyeydemon/ngx-lua-stats


功能

支持分不同虚拟主机统计, 同一个虚拟主机下可以分不同的location统计.

可以统计与query-times request-time status-code speed 相关的数据.

环境依赖

nginx + ngx_http_lua_module

安装

http://wiki.nginx.org/HttpLuaModule#Installation


使用方法

添加全局字典

在nginx的配置中添加dict的初始化, 类似如下


lua_shared_dict log_dict 20M ;
lua_shared_dict result_dict 20M ;


为特定的location添加统计

只需要添加一句即可~~

将lua脚本嵌套进nginx的配置中, 例如:



server {
     listen 8080 ;
     server_name weatherapi . market . xiaomi . com ;
     access_log / home / work / nginx / logs / weatherapi . market . xiaomi . com . log milog ;
     location / {
         proxy_set_header Host $ host ;
         proxy_set_header X - Forwarded - For $ remote_addr ;
         proxy_pass http : //weatherapi.market.xiaomi.com_backend;
    
         log_by_lua_file . / site - enable / record . lua ;
     }
}

输出结果

通过配置一个server, 使得可以通过curl获取到字典里的所有结果



server {
     listen 8080 default ;
     server_name _ ;
     location / {
         return 404 ;
     }
 
     location / status {
         content_by_lua_file . / site - enable / output . lua ;
     }
 
     location / empty_dict {
         content_by_lua_file . / site - enable / empty_dict . lua ;
     }
}

可以通过如下命令获取


curl ip_addr:8080/status


清理字典

运行一段时间之后, 字典会变大. 可以通过如下接口清理


curl ip_addr:8080/empty_dict


支持的统计数据说明

目前支持统计以下数据,返回的原始数据类似于



-- -- -- -- -- -- -- -- -- -- -- -- --
key : weatherapi . market . xiaomi . com__upstream_time_10 . 0.3.32 : 8250_counter
0.375
key : weatherapi . market . xiaomi . com__upstream_time_10 . 0.3.32 : 8250_nb_counter
124
key : weatherapi . market . xiaomi . com__upstream_time_10 . 0.4.93 : 8250_counter
0.131
key : weatherapi . market . xiaomi . com__upstream_time_10 . 0.4.93 : 8250_nb_counter
123
key : weatherapi . market . xiaomi . com__upstream_time_10 . 20.12.49 : 8250_counter
0.081
key : weatherapi . market . xiaomi . com__upstream_time_10 . 20.12.49 : 8250_nb_counter
127
key : weatherapi . market . xiaomi . com__query_counter
500
key : weatherapi . market . xiaomi . com__request_time_counter
0.683
key : weatherapi . market . xiaomi . com__upstream_time_counter
0.683
key : weatherapi . market . xiaomi . com__upstream_time_10 . 20.12.59 : 8250_counter
0.096
key : weatherapi . market . xiaomi . com__upstream_time_10 . 20.12.59 : 8250_nb_counter
126
key : weatherapi . market . xiaomi . com__bytes_sent_counter
81500

其中 __ 用来分割虚拟主机(包含prefix)与后面的数据项,便于数据处理.

counter表示此值一直在累加

nb表示次数

可以得到的数据包括: query次数 request_time bytes_sent upstream_time

其中 upstream_time_10.20.12.49:8250_counter 表示到某个特定后端的upstrea_time耗时累加

upstream_time_10.20.12.49:8250_nb_counter 表示到到某个特定后端的upstrea_time次数累加


如何处理数据

因为采集到的数据大多都是counter值,需要监控系统支持对于delta的处理.目前我们公司的perf-counter监控系统支持简单运算。所以这个处理起来比较简单,对于没有这种系统的同学来说,需要自己处理数据,得到delta值以及更复杂的数据,比如:



标签:虚拟主机,lua,counter,xiaomi,nginx,weatherapi,time,com,market
From: https://blog.51cto.com/u_6186189/6458516

相关文章

  • Lua 语言 15 分钟快速入门
    --单行注释--[[  [多行注释]--]]-----------1.变量&控制流----------num=23--数字都是双精度str='aspythonstring'--像Python一样不可变str="aspythonuse"--可以双引号str=[[    像Python的多行注释可用于    表示多行字符串一样 ......
  • nginx+tomcat+memcached (msm)实现 session同步复制
    这里重点强调如何实现linux服务器上服务器session共享,软件安装不再赘述。 首先我们需要对cookie和session的工作机制非常了解,如果不了解其中的原理,就算配置成功,也毫无意义。换了工作换了环境,重新配置起来 仍然需要重头来过,事倍功半。    cookie是怎样工作的? 例如,我们创......
  • Nginx流量复制/AB测试/协程
    在实际开发中经常涉及到项目的升级,而该升级不能简单的上线就完事了,需要验证该升级是否兼容老的上线,因此可能需要并行运行两个项目一段时间进行数据比对和校验,待没问题后再进行上线。这其实就需要进行流量复制,把流量复制到其他服务器上,一种方式是使用如tcpcopy引流;另外我们还可以使......
  • nginx-gridfs Benchmarking Raw Results
    RawDataSpreadsheetwithtestresults(ODFformat)Thesefollowinglinksshowtherawoutputfromthebenchmarkingutilities.GridFSOverNetworkThistestscenarioshowsperformanceforHTTPrequestsoveragigabitEthernetLANconnection.MongoDBand......
  • nginx-ssl
    阿里云sslhttps://cloud.tencent.com/developer/article/1444888?from=15425&areaSource=102001.1&traceId=hr6wuqWG00xJ9twXuLoOAapkaddopenssl-devopensslmkdir/usr/local/nginx/conf/sslcd/usr/local/nginx/conf/sslopensslgenrsa-outmy.key-des31024......
  • k8s 1.26.5 Ingress-nginx 的高可用部署
    1.安装部署ingress-nginx本次部署使用了高可用的形式,会在每个node节点做亲和性(master不部署),让每一个pod都部署上去,然后加入NGINX去过负载,这样我们之后用NGINX的80端口访问域名就可以了。主机地址端口k8s-node01192.168.80.48nginx启动端口:3080,负载均衡端口:根据ingre......
  • Nginx用作反向代理服务器使用!
        Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Nginx是由IgorSysoev为俄罗斯访问量第二的Rambler.ru站点开发的,它已经在该站点运行超过三年了。Igor将源代码以类BSD许可证的形式发布。Nginx超越Apache的高性......
  • Linux系统下配置Nginx服务器
    Nginx是一个高性能的开源HTTP和反向代理服务器,也可以作为电子邮件(SMTP/POP3/IMAP)代理服务器、负载均衡器和HTTP缓存服务器,使用在安装Nginx之前,需要安装一些其他软件依赖,如gcc、pcre、zlib和openssl。1、yum installgcc-ygcc是GNUCompilerCollection的简称,包含编译器和其他编......
  • nginx-clojure-0.6.0 集成nginx 1.25.0 构建的解决方法
    今天也说过关于nginx-clojure-0.6.0集成nginx1.2.50构建是有问题的,以下是解决方法实际问题staticdeclarationof‘ngx_http_close_request’followsnon-staticdeclaration原因nginx-clojure复制了nginx源码中对于nginx的处理函数(ngx_http_clojure_mem.c文件)......
  • lua对url字符进行编解码
      localfunctionurlEncode(s)s=string.gsub(s,"([^%w%.%-])",function(c)returnstring.format("%%%02X",string.byte(c))end)returnstring.gsub(s,"","+")endlocalfunctionurlDecode(s)s=strin......