设为首页 收藏本站
查看: 723|回复: 0

[经验分享] 通过ngx-lua来统计nginx上的虚拟主机性能数据

[复制链接]

尚未签到

发表于 2016-12-28 09:32:00 | 显示全部楼层 |阅读模式
介绍
  以前我们为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的初始化, 类似如下
 
1
2

[color=teal !important]lua_shared_dict [color=#000000 !important]log_dict[color=#009999 !important]20M[color=#333333 !important];
[color=teal !important]lua_shared_dict [color=#000000 !important]result_dict[color=#009999 !important]20M[color=#333333 !important];



为特定的location添加统计
  只需要添加一句即可~~
将lua脚本嵌套进nginx的配置中, 例如:
 
1
2
3
4
5
6
7
8
9
10
11
12

[color=teal !important]server[color=#333333 !important]{
[color=#006fe0 !important]    [color=#000000 !important]listen[color=#009999 !important]8080[color=#333333 !important];
[color=#006fe0 !important]    [color=teal !important]server_name [color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com[color=#333333 !important];
[color=#006fe0 !important]    [color=#000000 !important]access_log[color=#006fe0 !important]/[color=#000000 !important]home[color=#006fe0 !important]/[color=#000000 !important]work[color=#006fe0 !important]/[color=#000000 !important]nginx[color=#006fe0 !important]/[color=#000000 !important]logs[color=#006fe0 !important]/[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com[color=#333333 !important].[color=teal !important]log [color=#000000 !important]milog[color=#333333 !important];
[color=#006fe0 !important]    [color=teal !important]location[color=#006fe0 !important]/[color=#333333 !important]{
[color=#006fe0 !important]        [color=teal !important]proxy_set_header [color=#000000 !important]Host[color=#333333 !important]$[color=#000000 !important]host[color=#333333 !important];
[color=#006fe0 !important]        [color=#000000 !important]proxy_set_header[color=#000000 !important]X[color=#006fe0 !important]-[color=#000000 !important]Forwarded[color=#006fe0 !important]-[color=#000000 !important]For[color=#333333 !important]$[color=#000000 !important]remote_addr[color=#333333 !important];
[color=#006fe0 !important]        [color=teal !important]proxy_pass [color=#000000 !important]http[color=#006fe0 !important]:[color=#999999 !important]//weatherapi.market.xiaomi.com_backend;
[color=#006fe0 !important]    
[color=#006fe0 !important]        [color=#000000 !important]log_by_lua_file[color=#333333 !important].[color=#006fe0 !important]/[color=#000000 !important]site[color=#006fe0 !important]-[color=#000000 !important]enable[color=#006fe0 !important]/[color=#000000 !important]record[color=#333333 !important].[color=#002d7a !important]lua[color=#333333 !important];
[color=#006fe0 !important]    [color=#333333 !important]}
[color=#333333 !important]}



输出结果
  通过配置一个server, 使得可以通过curl获取到字典里的所有结果
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

[color=teal !important]server[color=#333333 !important]{
[color=#006fe0 !important]    [color=#000000 !important]listen[color=#009999 !important]8080[color=#000000 !important]default[color=#333333 !important];
[color=#006fe0 !important]    [color=#000000 !important]server_name[color=#000000 !important]_[color=#333333 !important];
[color=#006fe0 !important]    [color=teal !important]location[color=#006fe0 !important]/[color=#333333 !important]{
[color=#006fe0 !important]        [color=#000000 !important]return[color=#009999 !important]404[color=#333333 !important];
[color=#006fe0 !important]    [color=#333333 !important]}
 
[color=#006fe0 !important]    [color=teal !important]location[color=#006fe0 !important]/[color=teal !important]status[color=#333333 !important]{
[color=#006fe0 !important]        [color=#000000 !important]content_by_lua_file[color=#333333 !important].[color=#006fe0 !important]/[color=#000000 !important]site[color=#006fe0 !important]-[color=#000000 !important]enable[color=#006fe0 !important]/[color=#000000 !important]output[color=#333333 !important].[color=#002d7a !important]lua[color=#333333 !important];
[color=#006fe0 !important]    [color=#333333 !important]}
 
[color=#006fe0 !important]    [color=teal !important]location[color=#006fe0 !important]/[color=teal !important]empty_dict[color=#333333 !important]{
[color=#006fe0 !important]        [color=#000000 !important]content_by_lua_file[color=#333333 !important].[color=#006fe0 !important]/[color=#000000 !important]site[color=#006fe0 !important]-[color=#000000 !important]enable[color=#006fe0 !important]/[color=#000000 !important]empty_dict[color=#333333 !important].[color=#002d7a !important]lua[color=#333333 !important];
[color=#006fe0 !important]    [color=#333333 !important]}
[color=#333333 !important]}



  可以通过如下命令获取
  curl ip_addr:8080/status
清理字典
  运行一段时间之后, 字典会变大. 可以通过如下接口清理
  curl ip_addr:8080/empty_dict
支持的统计数据说明
  目前支持统计以下数据,返回的原始数据类似于
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--[color=#006fe0 !important]--
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]0.3.32[color=#006fe0 !important]:[color=#009999 !important]8250_counter
[color=#009999 !important]0.375
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]0.3.32[color=#006fe0 !important]:[color=#009999 !important]8250_nb_counter
[color=#009999 !important]124
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]0.4.93[color=#006fe0 !important]:[color=#009999 !important]8250_counter
[color=#009999 !important]0.131
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]0.4.93[color=#006fe0 !important]:[color=#009999 !important]8250_nb_counter
[color=#009999 !important]123
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]20.12.49[color=#006fe0 !important]:[color=#009999 !important]8250_counter
[color=#009999 !important]0.081
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]20.12.49[color=#006fe0 !important]:[color=#009999 !important]8250_nb_counter
[color=#009999 !important]127
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__query_counter
[color=#009999 !important]500
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__request_time_counter
[color=#009999 !important]0.683
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_counter
[color=#009999 !important]0.683
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]20.12.59[color=#006fe0 !important]:[color=#009999 !important]8250_counter
[color=#009999 !important]0.096
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__upstream_time_10[color=#333333 !important].[color=#009999 !important]20.12.59[color=#006fe0 !important]:[color=#009999 !important]8250_nb_counter
[color=#009999 !important]126
[color=#000000 !important]key[color=#006fe0 !important]:[color=#000000 !important]weatherapi[color=#333333 !important].[color=#002d7a !important]market[color=#333333 !important].[color=#002d7a !important]xiaomi[color=#333333 !important].[color=#002d7a !important]com__bytes_sent_counter
[color=#009999 !important]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值以及更复杂的数据,比如:
  转自http://noops.me/?p=867

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-320489-1-1.html 上篇帖子: nginx.conf中配置上不允许访问的网站目录 下篇帖子: Nginx 0.5.31 + PHP 5.2.6(FastCGI)搭建可承受3万以上并发连接数,胜过
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表