1、使用 nginx-vts-exporter 监控 nginx 状态,前提条件是 nginx 要编译进 nginx-module-vts 模块
2、下载 openresty 安装包和 nginx-module-vts 模块安装包
// 下载地址:http://openresty.org/cn/download.html
# ll /usr/local/src/openresty-1.13.6.2.tar.gz
-rw-r--r-- 1 root root 4635916 Nov 26 14:09 /usr/local/src/openresty-1.13.6.2.tar.gz
// 下载地址:https://github.com/vozlt/nginx-module-vts/releases
# ll /usr/local/src/nginx-module-vts-0.1.18.tar.gz
-rw-r--r-- 1 root root 380327 Nov 26 14:13 /usr/local/src/nginx-module-vts-0.1.18.tar.gz
3、编译安装 openresty
# cd /usr/local/src
# tar xf openresty-1.13.6.2.tar.gz
# tar xf nginx-module-vts-0.1.18.tar.gz
# cd openresty-1.13.6.2
// 加上 --add-module=/usr/local/src/nginx-module-vts-0.1.18 编译选项
# ./configure --prefix=/usr/local/openresty --with-luajit --with-pcre --with-http_iconv_module --with-http_realip_module --with-http_sub_module --with-http_stub_status_module --with-stream --with-stream_ssl_module --add-module=/usr/local/src/nginx-module-vts-0.1.18
# make build install
4、nginx.conf 配置添加 vhost_traffic_status_zone;
user nobody;
worker_processes auto;
error_log syslog:server=10.1.9.40:12302;
pid logs/nginx.pid;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format graylog2_json escape=json '{ "timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host",'
'"upstream_cache_status": "$upstream_cache_status",'
'"upstream_addr": "$upstream_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent" }';
access_log logs/access.log graylog2_json;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
client_max_body_size 10m;
vhost_traffic_status_zone;
include /usr/local/openresty/nginx/conf/conf.d/*.conf;
}
5、要监控的站点添加配置 /vts_status
server
{
listen 80;
server_name www.x.cn x.cn;
access_log syslog:server=10.1.9.40:12301 graylog2_json;
location / {
return 301 https://$server_name$request_uri;
}
location /vts_status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
allow 127.0.0.1;
deny all;
}
}
6、测试请求是否能获取到数据
# curl http://127.0.0.1/vts_status/format/json
7、安装 nginx-vts-exporter
# mkdir /usr/local/nginx-vts-exporter -p
// 下载地址:https://github.com/hnlq715/nginx-vts-exporter/releases
# ll /usr/local/nginx-vts-exporter/nginx-vts-exporter
-rwxr-xr-x 1 root root 9982855 Nov 22 15:41 /usr/local/nginx-vts-exporter/nginx-vts-exporter
// 配置 nginx-vts-exporter.service
[Unit]
Description=nginx-vts-exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri=http://127.0.0.1/vts_status/format/json
Restart=on-failure
[Install]
WantedBy=multi-user.target
// 启动服务,nginx-vts-exporter 默认监听端口为 9913
# systemctl enable nginx-vts-exporter
# systemctl start nginx-vts-exporter
# systemctl status nginx-vts-exporter
8、prometheus 配置
// prometheus.yml 配置监控规则
- file_sd_configs:
- files:
- 'configs/nginx.yml'
job_name: Nginx Server
metrics_path: /metrics
relabel_configs:
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*)
target_label: __address__
replacement: $1:9913
// configs/nginx.yml 配置要监控的主机
- labels:
service: nginx
targets:
- 10.1.9.45
可以看到已经成功添加主机
9、grafana 配置
从 https://grafana.com/dashboards/2949 这里下载一个模板,导入 grafana 中即可
网友评论