暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

服务端性能监控(四):nginx-module-vts监控nginx流量

重口味码农 2020-03-30
3257

简介

这个系列的第一篇中分享了使用nginx-lua-module监控Nginx的请求以便进行分析,监控请求主要是在应用层面,除了请求之外我们一般还需要监控经过nginx的流量、连接数等偏底层的数据,这些数据对分析nginx的性能比较重要下面我们看下如何对nginx流量进行监控

添加nginx-module-vts模块

1. 下载

  1. git clone git://github.com/vozlt/nginx-module-vts.git

2.编译nginx 并添加该模块

下载nginx源码,并编译。

编译前先查看当前的nginx已经安装的模块,避免重新编译时漏掉了

  1. nginx -v

带着原来的模块,再添加nginx-module-vts模块,进行编译。

  1. ./configure --add-module=/usr/local/src/nginx-module-vts

编辑nginx.conf

编辑nginx.conf,在http和server中加入配置信息。

  1. http {

  2. vhost_traffic_status_zone;

  3. vhost_traffic_status_filter_by_host on; # 根据host分别进行流量统计

  4. ...

  5. server {

  6. ...

  7. location /status {

  8. vhost_traffic_status_display;

  9. vhost_traffic_status_display_format html;

  10. }

  11. }

  12. }

注意:在不想监控的server中,可以单独关闭监控

  1. server {

  2. vhost_traffic_status off;

  3. }

访问ip:port/status即可查看到实时监控

prometheus + grafana

上图中,可以看到一个简易的面板,但想要查看详细的趋势图,还是要借助prometheus和grafana这两个神器。

首先,将ip:port/status/format/prometheus配置到prometheus的监控。

注:/format/可以转换出想要的各种数据格式,默认是html,还有json prometheus等等。

打开grafana,配置数据源。

下载图表模板,官网上针对nginx-module-vts有N种模板可用。

下载地址 https://grafana.com/grafana/dashboards?orderBy=name&direction=asc

导入模板,选择数据源后,nginx的相关数据就会展示出来了。

各个监控项的含义

Nginx-module-vts提供了多种监控项,我们了解了监控项含义有助于帮助自己生成需要的图表。

  1. # HELP nginx_vts_info Nginx info

  2. # TYPE nginx_vts_info gauge

  3. nginx_vts_info{hostname="hbhly_21_205",version="1.16.1"} 1

  4. # HELP nginx_vts_start_time_seconds Nginx start time

  5. # TYPE nginx_vts_start_time_seconds gauge

  6. nginx_vts_start_time_seconds 1584268136.439

  7. # HELP nginx_vts_main_connections Nginx connections

  8. # TYPE nginx_vts_main_connections gauge


  9. # 区分状态的nginx连接数

  10. nginx_vts_main_connections{status="accepted"} 9271

  11. nginx_vts_main_connections{status="active"} 7

  12. nginx_vts_main_connections{status="handled"} 9271

  13. nginx_vts_main_connections{status="reading"} 0

  14. nginx_vts_main_connections{status="requests"} 438850

  15. nginx_vts_main_connections{status="waiting"} 6

  16. nginx_vts_main_connections{status="writing"} 1

  17. # HELP nginx_vts_main_shm_usage_bytes Shared memory [ngx_http_vhost_traffic_status] info

  18. # TYPE nginx_vts_main_shm_usage_bytes gauge


  19. # 内存使用量

  20. nginx_vts_main_shm_usage_bytes{shared="max_size"} 1048575

  21. nginx_vts_main_shm_usage_bytes{shared="used_size"} 24689

  22. nginx_vts_main_shm_usage_bytes{shared="used_node"} 7

  23. # HELP nginx_vts_server_bytes_total The request/response bytes

  24. # TYPE nginx_vts_server_bytes_total counter

  25. # HELP nginx_vts_server_requests_total The requests counter

  26. # TYPE nginx_vts_server_requests_total counter

  27. # HELP nginx_vts_server_request_seconds_total The request processing time in seconds

  28. # TYPE nginx_vts_server_request_seconds_total counter

  29. # HELP nginx_vts_server_request_seconds The average of request processing times in seconds

  30. # TYPE nginx_vts_server_request_seconds gauge

  31. # HELP nginx_vts_server_request_duration_seconds The histogram of request processing time

  32. # TYPE nginx_vts_server_request_duration_seconds histogram

  33. # HELP nginx_vts_server_cache_total The requests cache counter

  34. # TYPE nginx_vts_server_cache_total counter


  35. # 分Host的进出流量

  36. nginx_vts_server_bytes_total{host="10.160.21.205",direction="in"} 22921464

  37. nginx_vts_server_bytes_total{host="10.160.21.205",direction="out"} 1098196005


  38. # 分状态码的请求数量统计 1** 2** 3** 4** 5**

  39. nginx_vts_server_requests_total{host="10.160.21.205",code="1xx"} 0

  40. nginx_vts_server_requests_total{host="10.160.21.205",code="2xx"} 86809

  41. nginx_vts_server_requests_total{host="10.160.21.205",code="3xx"} 0

  42. nginx_vts_server_requests_total{host="10.160.21.205",code="4xx"} 2

  43. nginx_vts_server_requests_total{host="10.160.21.205",code="5xx"} 0

  44. nginx_vts_server_requests_total{host="10.160.21.205",code="total"} 86811


  45. # 响应时间

  46. nginx_vts_server_request_seconds_total{host="10.160.21.205"} 0.000

  47. nginx_vts_server_request_seconds{host="10.160.21.205"} 0.000


  48. # 分状态的缓存的统计

  49. nginx_vts_server_cache_total{host="10.160.21.205",status="miss"} 0

  50. nginx_vts_server_cache_total{host="10.160.21.205",status="bypass"} 0

  51. nginx_vts_server_cache_total{host="10.160.21.205",status="expired"} 0

  52. nginx_vts_server_cache_total{host="10.160.21.205",status="stale"} 0

  53. nginx_vts_server_cache_total{host="10.160.21.205",status="updating"} 0

  54. nginx_vts_server_cache_total{host="10.160.21.205",status="revalidated"} 0

  55. nginx_vts_server_cache_total{host="10.160.21.205",status="hit"} 0

  56. nginx_vts_server_cache_total{host="10.160.21.205",status="scarce"} 0

  57. nginx_vts_server_bytes_total{host="devapi.feedback.test",direction="in"} 3044526

  58. nginx_vts_server_bytes_total{host="devapi.feedback.test",direction="out"} 41257028


  59. # 分状态的连接数的统计

  60. nginx_vts_server_requests_total{host="devapi.feedback.test",code="1xx"} 0

  61. nginx_vts_server_requests_total{host="devapi.feedback.test",code="2xx"} 3983

  62. nginx_vts_server_requests_total{host="devapi.feedback.test",code="3xx"} 0

  63. nginx_vts_server_requests_total{host="devapi.feedback.test",code="4xx"} 24

  64. nginx_vts_server_requests_total{host="devapi.feedback.test",code="5xx"} 11

  65. nginx_vts_server_requests_total{host="devapi.feedback.test",code="total"} 4018

  66. nginx_vts_server_request_seconds_total{host="devapi.feedback.test"} 327.173

  67. nginx_vts_server_request_seconds{host="devapi.feedback.test"} 0.000


  68. # nginx缓存计算器,精确到状态和type

  69. nginx_vts_server_cache_total{host="devapi.feedback.test",status="miss"} 0

  70. nginx_vts_server_cache_total{host="devapi.feedback.test",status="bypass"} 0

  71. nginx_vts_server_cache_total{host="devapi.feedback.test",status="expired"} 0

  72. nginx_vts_server_cache_total{host="devapi.feedback.test",status="stale"} 0

  73. nginx_vts_server_cache_total{host="devapi.feedback.test",status="updating"} 0

  74. nginx_vts_server_cache_total{host="devapi.feedback.test",status="revalidated"} 0

  75. nginx_vts_server_cache_total{host="devapi.feedback.test",status="hit"} 0

  76. nginx_vts_server_cache_total{host="devapi.feedback.test",status="scarce"} 0

  77. nginx_vts_server_bytes_total{host="testapi.feedback.test",direction="in"} 55553573

  78. nginx_vts_server_bytes_total{host="testapi.feedback.test",direction="out"} 9667561188

  79. nginx_vts_server_requests_total{host="testapi.feedback.test",code="1xx"} 0

  80. nginx_vts_server_requests_total{host="testapi.feedback.test",code="2xx"} 347949

  81. nginx_vts_server_requests_total{host="testapi.feedback.test",code="3xx"} 31

  82. nginx_vts_server_requests_total{host="testapi.feedback.test",code="4xx"} 7

  83. nginx_vts_server_requests_total{host="testapi.feedback.test",code="5xx"} 33

  84. nginx_vts_server_requests_total{host="testapi.feedback.test",code="total"} 348020

  85. nginx_vts_server_request_seconds_total{host="testapi.feedback.test"} 2185.177

  86. nginx_vts_server_request_seconds{host="testapi.feedback.test"} 0.001

  87. nginx_vts_server_cache_total{host="testapi.feedback.test",status="miss"} 0

  88. nginx_vts_server_cache_total{host="testapi.feedback.test",status="bypass"} 0

  89. nginx_vts_server_cache_total{host="testapi.feedback.test",status="expired"} 0

  90. nginx_vts_server_cache_total{host="testapi.feedback.test",status="stale"} 0

  91. nginx_vts_server_cache_total{host="testapi.feedback.test",status="updating"} 0

  92. nginx_vts_server_cache_total{host="testapi.feedback.test",status="revalidated"} 0

  93. nginx_vts_server_cache_total{host="testapi.feedback.test",status="hit"} 0

  94. nginx_vts_server_cache_total{host="testapi.feedback.test",status="scarce"} 0

  95. nginx_vts_server_bytes_total{host="*",direction="in"} 81519563

  96. nginx_vts_server_bytes_total{host="*",direction="out"} 10807014221


  97. # 分host请求数量统计

  98. nginx_vts_server_requests_total{host="*",code="1xx"} 0

  99. nginx_vts_server_requests_total{host="*",code="2xx"} 438741

  100. nginx_vts_server_requests_total{host="*",code="3xx"} 31

  101. nginx_vts_server_requests_total{host="*",code="4xx"} 33

  102. nginx_vts_server_requests_total{host="*",code="5xx"} 44

  103. nginx_vts_server_requests_total{host="*",code="total"} 438849

  104. nginx_vts_server_request_seconds_total{host="*"} 2512.350

  105. nginx_vts_server_request_seconds{host="*"} 0.007


  106. # 分host缓存统计

  107. nginx_vts_server_cache_total{host="*",status="miss"} 0

  108. nginx_vts_server_cache_total{host="*",status="bypass"} 0

  109. nginx_vts_server_cache_total{host="*",status="expired"} 0

  110. nginx_vts_server_cache_total{host="*",status="stale"} 0

  111. nginx_vts_server_cache_total{host="*",status="updating"} 0

  112. nginx_vts_server_cache_total{host="*",status="revalidated"} 0

  113. nginx_vts_server_cache_total{host="*",status="hit"} 0

  114. nginx_vts_server_cache_total{host="*",status="scarce"} 0

  115. # HELP nginx_vts_upstream_bytes_total The request/response bytes

  116. # TYPE nginx_vts_upstream_bytes_total counter

  117. # HELP nginx_vts_upstream_requests_total The upstream requests counter

  118. # TYPE nginx_vts_upstream_requests_total counter

  119. # HELP nginx_vts_upstream_request_seconds_total The request Processing time including upstream in seconds

  120. # TYPE nginx_vts_upstream_request_seconds_total counter

  121. # HELP nginx_vts_upstream_request_seconds The average of request processing times including upstream in seconds

  122. # TYPE nginx_vts_upstream_request_seconds gauge

  123. # HELP nginx_vts_upstream_response_seconds_total The only upstream response processing time in seconds

  124. # TYPE nginx_vts_upstream_response_seconds_total counter

  125. # HELP nginx_vts_upstream_response_seconds The average of only upstream response processing times in seconds

  126. # TYPE nginx_vts_upstream_response_seconds gauge

  127. # HELP nginx_vts_upstream_request_duration_seconds The histogram of request processing time including upstream

  128. # TYPE nginx_vts_upstream_request_duration_seconds histogram

  129. # HELP nginx_vts_upstream_response_duration_seconds The histogram of only upstream response processing time

  130. # TYPE nginx_vts_upstream_response_duration_seconds histogram


  131. # 分upstream流量统计

  132. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.144.227.162:80",direction="in"} 12296

  133. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.144.227.162:80",direction="out"} 13582924

  134. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.144.227.162:80",code="1xx"} 0

  135. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.144.227.162:80",code="2xx"} 25

  136. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.144.227.162:80",code="3xx"} 0

  137. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.144.227.162:80",code="4xx"} 0

  138. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.144.227.162:80",code="5xx"} 0

  139. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.144.227.162:80",code="total"} 25

  140. nginx_vts_upstream_request_seconds_total{upstream="::nogroups",backend="10.144.227.162:80"} 1.483

  141. nginx_vts_upstream_request_seconds{upstream="::nogroups",backend="10.144.227.162:80"} 0.000

  142. nginx_vts_upstream_response_seconds_total{upstream="::nogroups",backend="10.144.227.162:80"} 1.484

  143. nginx_vts_upstream_response_seconds{upstream="::nogroups",backend="10.144.227.162:80"} 0.000

  144. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.152.218.149:80",direction="in"} 12471

  145. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.152.218.149:80",direction="out"} 11790508

  146. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.152.218.149:80",code="1xx"} 0

  147. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.152.218.149:80",code="2xx"} 24

  148. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.152.218.149:80",code="3xx"} 0

  149. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.152.218.149:80",code="4xx"} 0

  150. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.152.218.149:80",code="5xx"} 0

  151. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.152.218.149:80",code="total"} 24

  152. nginx_vts_upstream_request_seconds_total{upstream="::nogroups",backend="10.152.218.149:80"} 1.169

  153. nginx_vts_upstream_request_seconds{upstream="::nogroups",backend="10.152.218.149:80"} 0.000

  154. nginx_vts_upstream_response_seconds_total{upstream="::nogroups",backend="10.152.218.149:80"} 1.168

  155. nginx_vts_upstream_response_seconds{upstream="::nogroups",backend="10.152.218.149:80"} 0.000

  156. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.160.21.205:8081",direction="in"} 3036924

  157. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.160.21.205:8081",direction="out"} 33355357

  158. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8081",code="1xx"} 0

  159. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8081",code="2xx"} 3971

  160. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8081",code="3xx"} 0

  161. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8081",code="4xx"} 24

  162. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8081",code="5xx"} 11

  163. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8081",code="total"} 4006

  164. nginx_vts_upstream_request_seconds_total{upstream="::nogroups",backend="10.160.21.205:8081"} 326.427

  165. nginx_vts_upstream_request_seconds{upstream="::nogroups",backend="10.160.21.205:8081"} 0.000

  166. nginx_vts_upstream_response_seconds_total{upstream="::nogroups",backend="10.160.21.205:8081"} 300.722

  167. nginx_vts_upstream_response_seconds{upstream="::nogroups",backend="10.160.21.205:8081"} 0.000

  168. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.160.21.205:8082",direction="in"} 55536408

  169. nginx_vts_upstream_bytes_total{upstream="::nogroups",backend="10.160.21.205:8082",direction="out"} 9650089427

  170. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8082",code="1xx"} 0

  171. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8082",code="2xx"} 347912

  172. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8082",code="3xx"} 31

  173. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8082",code="4xx"} 7

  174. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8082",code="5xx"} 33

  175. nginx_vts_upstream_requests_total{upstream="::nogroups",backend="10.160.21.205:8082",code="total"} 347983

  176. nginx_vts_upstream_request_seconds_total{upstream="::nogroups",backend="10.160.21.205:8082"} 2183.271

  177. nginx_vts_upstream_request_seconds{upstream="::nogroups",backend="10.160.21.205:8082"} 0.001

  178. nginx_vts_upstream_response_seconds_total{upstream="::nogroups",backend="10.160.21.205:8082"} 2180.893

  179. nginx_vts_upstream_response_seconds{upstream="::nogroups",backend="10.160.21.205:8082"} 0.001

通过这些指标,可以精确地统计到经过nginx的流量,进而对nginx的性能进行监控。

本文首发于重口味博客(http://www.gscoder.cn)


文章转载自重口味码农,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论