编程技术分享平台

网站首页 > 技术教程 正文

基于prometheus+grafana体系监控nginx服务第一部分

xnh888 2024-09-11 11:00:49 技术教程 26 ℃ 0 评论

概述

Prometheus 服务本地已经启动了,接下来,需要安装并运行 Exporter,它的主要作用是持续输出监控的组件信息并格式化,同时提供 Http 接口供 Prometheus 服务来抓取。Exporter 也是通过 GO 语言编写的,Prometheus GitHub 已经为我们提供了很多实用的 Exporter,直接拿来使用即可。

今天主要分享下怎么用prometheus+grafana去监控nginx服务。


一、编译安装nginx

nginx_vts_exporter依赖nginx-module-vts模块,安装此模块无需任何其他依赖。模块与Nginx的版本兼容性如下:

1.11.x (last tested: 1.11.10)
1.10.x (last tested: 1.10.3)
1.8.x (last tested: 1.8.0)
1.6.x (last tested: 1.6.3)
1.4.x (last tested: 1.4.7)

1、下载源码nginx和nginx-module-vts moudle

因为是演示,所以就简单写一下步骤了。

wget http://nginx.org/download/nginx-1.12.1.tar.gz
git clone git://github.com/vozlt/nginx-module-vts.git

2、安装组件

yum install -y openssl libssl-dev libxml2-dev libxslt-dev libgd2-xpm libgd2-xpm-dev libgeoip-dev libperl-dev patch

3、添加nginx-http-sysguard模块

从官网上下载这个模块:

https://github.com/alibaba/nginx-http-sysguard/archive/master.zip

解压到/opt/nginx-1.12.1/中,然后打补丁

cd /opt/nginx-1.12.1
patch -p1 < ./nginx-http-sysguard-master/nginx_sysguard_1.3.9.patch

4、编译安装

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-http_ssl_module --with-http_realip_module --with-http_addition_module \
--with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module \
--with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module \
--with-http_auth_request_module --with-http_slice_module --with-http_v2_module --add-module=/opt/nginx-1.12.1/nginx-http-sysguard-master \
--with-stream --add-module=../nginx-module-vts
make &&make install

5、修改nginx配置

添加监控接口/status和sysguard做压测

http {
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
...
server {
 ...
 sysguard on;
 sysguard_load load=0.01 action=/loadlimit;
 sysguard_mem swapratio=20% action=/swaplimit;
 location /loadlimit {
 return 404;
 }
 location /swaplimit {
 return 503;
 }
 location /status {
 vhost_traffic_status_display;
 vhost_traffic_status_display_format html;
 }
}
}

配置建议:

1)打开vhost过滤:

vhost_traffic_status_filter_by_host on;

开启此功能,在Nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上。

2)在不想统计流量的server区域禁用vhost_traffic_status,配置示例:

server {
...
vhost_traffic_status off;
...
}

假如nginx没有规范配置server_name或者无需进行监控的server上,那么建议在此vhost上禁用统计监控功能。否则会出现“127.0.0.1”,hostname等的域名监控信息。

6、监控数据的查看

安装完vts模块后,可以通过nginx status接口进行监控数据的查看


二、部署nginx-vts-exporter

exporter会收集nginx性能指标的JSON格式数据,并汇总后暴露监控接口给Prometheus

1、下载当前最新版本的软件包:

wget -c https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

2、解压后运行:

nohup ./nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri http://172.16.10.123:8000/status/format/json &

推荐exporter和nginx安装在同一台机器上,如果不在同一台主机,把scrape_uri改为nginx主机的地址。

nginx_vts_exporter的默认端口号:9913,对外暴露监控接口http://xxx:9913/metrics.


三、Nginx的监控数据类型

nginx-vts-exporter的数据类型命名空间默认以“nginx”开头,主要有如下9个:

HELP是对监控条目的解释,TYPE的格式是:监控条目名称+Prometheus数据类型:

# HELP nginx_server_bytes request/response bytes
# TYPE nginx_server_bytes counter
# HELP nginx_server_cache cache counter
# TYPE nginx_server_cache counter
# HELP nginx_server_connections nginx connections
# TYPE nginx_server_connections gauge
# HELP nginx_server_requestMsec average of request processing times in milliseconds
# TYPE nginx_server_requestMsec gauge
# HELP nginx_server_requests requests counter,可以区分状态码
# TYPE nginx_server_requests counter
# HELP nginx_upstream_bytes request/response bytes
# TYPE nginx_upstream_bytes counter
# HELP nginx_upstream_requestMsec average of request processing times in milliseconds
# TYPE nginx_upstream_requestMsec gauge
# HELP nginx_upstream_requests requests counter,可以区分状态码
# TYPE nginx_upstream_requests counter
# HELP nginx_upstream_responseMsec average of only upstream/backend response processing times in milliseconds
# TYPE nginx_upstream_responseMsec gauge

篇幅有限,关于nginx服务被监控端部署方面就介绍到这了,后面介绍监控端的一些配置,感兴趣的朋友可以关注下~

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表