prometheus 监控 nginx

本贴最后更新于 1507 天前,其中的信息可能已经东海扬尘

用prometheus可以对nginx的server_name和upstream进行监控

监控Nginx主要用到以下三个模块:

nginx-module-vts:Nginx virtual host traffic status module,Nginx的监控模块,能够提供JSON格式的数据产出。

nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913。

Prometheus:监控Nginx-vts-exporter提供的Nginx数据,并存储在时序数据库中,可以使用PromQL对时序数据进行查询和聚合。

一、安装Prometheus

安装步骤:

  1. 安装go语言环境

解压:[root@localhost data]# tar -zxvf go1.11.9.linux-amd64.tar.gz -C /usr/local/

[root@localhost data]# vim /etc/profile

#go

GO_HMOE=/usr/local/go

export PATH=PATH:GO_HMOE/bin

[root@localhost ~]# source /etc/profile

[root@localhost ~]# go version

go version go1.11 linux/amd64

  1. 安装Prometheus

[root@localhost data]# wget https://github.com/prometheus/prometheus/releases/download/v2.9.2/prometheus-2.9.2.linux-amd64.tar.gz

[root@localhost data]# tar -zxvf prometheus-2.9.2.linux-amd64.tar.gz -C /usr/local/

[root@localhost data]# mv /usr/local/prometheus-2.9.2.linux-amd64 /usr/local/prometheus

修改配置文件:

[root@localhost data]# vim /usr/local/prometheus/prometheus.yml

image.png

启动prometheus:

配置Prometheus服务:

[root@localhost data]# vim /etc/systemd/system/prometheus.service

[Unit]

Description=Prometheus Monitoring System

Documentation=Prometheus Monitoring System

[Service]

ExecStart=/usr/local/prometheus/prometheus \

--config.file=/usr/local/prometheus/prometheus.yml \

--web.listen-address=:9090

[Install]

WantedBy=multi-user.target

[root@localhost data]# systemctl daemon-reload

[root@localhost data]# service prometheus start

[root@localhost prometheus]# netstat -anp|grep prometheus

tcp6 0 0 :::9090 :::* LISTEN 9451/prometheus

通过url打开prometheus的自带监控界面:http://192.168.226.140:9090/targets

image.png

二、nginx-module-vts模块的编译(nginx可以和Prometheus在同一台机器上面,我这边是不在一台机器上面)

  1. 查看nginx安装了哪些模块:

[root@localhost data]# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.17.9

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)

built with OpenSSL 1.0.2k-fips 26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

下载ngin-module-vts模块,并添加模块到nginx

[root@localhost data]# cd /usr/local/src

[root@localhost data]# git clone

[root@localhost data]# cd /data/nginx-1.17.9/ #进入到nginx安装包目录

[root@localhost data]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/local/src/nginx-module-vts

[root@localhost data]# make ## 编译,不要make install,不然会覆盖

看到如下信息,成功了

image.png

  1. 替换Nginx启动文件

[root@localhost data]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak ## 备份nginx启动文件

[root@localhost data]# pkill -9 nginx # 停止nginx

[root@localhost data]# cp /data/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/

cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y

  1. 修改Nginx.conf配置文件,试验安装是否成功

http {

vhost_traffic_status_zone;
vhost_traffic_status on;

vhost_traffic_status_filter_by_host on;
#设定查看Nginx状态的地址

location /nginx_status {

stub_status on;

access_log  off;

vhost_traffic_status_display;

vhost_traffic_status_display_format html;
}
}

配置解析:

1.1 打开vhost过滤:

vhost_traffic_status_filter_by_host on;

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

1.2 在统计流量的server区域开启vhost_traffic_status,配置

vhost_traffic_status on;

打开浏览器访问

http://192.168.226.148/nginx_status

image.png

三、在Prometheus机器上面,安装nginx-vts-exporter

[root@localhost data]# wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

[root@localhost data]# tar -zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

[root@localhost data]# cp nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter /usr/local/prometheus/nginx-vts-exporter/

[root@localhost data]# chmod +x /usr/local/prometheus/nginx-vts-exporter

  1. #配置nginx_vts_exporter服务

[root@localhost data]# vim /etc/systemd/system/nginx-vts-exporter.service

[Unit]

Description=nginx-vts-exporter

After=network.target

[Service]

Type=simple

User=root

# 配置nginx-vts-exporter的文件所在位置

# 配置nginx的机器ip地址

ExecStart=/usr/local/prometheus/nginx-vts-exporter -nginx.scrape_uri=http://192.168.226.148/nginx_status/format/json

Restart=on-failure

[Install]

WantedBy=multi-user.target

  1. 启动nginx_vts_exporter

[root@localhost data]# systemctl daemon-reload

[root@localhost data]# systemctl start nginx-vts-exporter

[root@localhost data]# systemctl status nginx-vts-exporter

看到如下信息,说明服务起来了

image.png

查看端口:

[root@localhost data]# netstat -anp|grep 9913

image.png

  1. 访问安装了nginx-vts-exporter的机器:http://192.168.226.140:9913/metrics

image.png

  1. 修改peometheus.yml, 加入下面的监控目标:

image.png

  1. 重启Prometheus

[root@localhost data]#service prometheus restart

四、在grafana里面配置Prometheus的连接

image.png

image.png

image.png

点击save&Test

image.png

  1. 导入nginx_stat模板

image.png

nginx-vts-stats_rev2.json.json

image.png

选择Prometheus

image.png

效果图

image.png

回帖
请输入回帖内容 ...