Nginx安装及使用nginx-vts-exporter监控Nginx状态信息

文章目录

    • 准备安装包
    • 安装
    • 指标释义,{NAMESPACE} 可以在exproter启动的时候指定
      • Server main
      • Server zones
      • Filter zones
      • Upstreams
    • 实例访问metrics,返回的数据样例

准备安装包

nginx-export 官方地址
https://github.com/hnlq715/nginx-vts-exporter

下载 Nginx-exporter
wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
下载 Nginx
wget http://nginx.org/download/nginx-1.17.7.tar.gz

下载 nginx 插件 ,记住项目下载地址,下面在配置nginx的时候需要指定
git clone git://github.com/vozlt/nginx-module-vts.git

安装

解压Nginx

tar -zxvf nginx-1.16.1.tar.gz

进入Nginx目录

/export/app/nginx-1.16.1

执行命令 ./configure 是用来生成Makefile,为下一步的编译做准备 --add-module=后面跟上插件的下载地址

./configure --prefix=/export/app/nginx --with-http_ssl_module  --add-module=/export/gitspace/nginx-module-vts/
make & make install

修改nginx.conf配置文件添加一下信息

http {
    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;
    server {
    #   vhost_traffic_status off;
        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上。
在不想统计流量的server区域禁用vhost_traffic_status,配置示例:

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

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

测试配置文件

./nginx -t
启动Nginx
./nginx
访问状态接口
http://localhost/status

解压nginx-exporter

tar -zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

查看Nginx-exporter启动参数

[root@ nginx-vts-exporter-0.10.3.linux-amd64]# ./nginx-vts-exporter -h
Usage of ./nginx-vts-exporter:
  -insecure
    	Ignore server certificate if using https (default true)
  -metrics.namespace string
    	Prometheus metrics namespace. (default "nginx")
  -nginx.scrape_timeout int
    	The number of seconds to wait for an HTTP response from the nginx.scrape_uri (default 2)
  -nginx.scrape_uri string
    	URI to nginx stub status page (default "http://localhost/status")
  -telemetry.address string
    	Address on which to expose metrics. (default ":9913")
  -telemetry.endpoint string
    	Path under which to expose metrics. (default "/metrics")
  -version
    	Print version information.

启动exporter

./nginx-vts-exporter -nginx.scrape_uri=http://localhost/status/format/json

参看采集指标

http://127.0.0.1:9913/metrics

指标释义,{NAMESPACE} 可以在exproter启动的时候指定

Server main

hostName, nginxVersion, uptimeSec 等信息
{NAMESPACE}_server_info
连接数 status [active, reading, writing, waiting, accepted, handled]
{NAMESPACE}_server_connections

Server zones

每个请求响应码的数量
{NAMESPACE}_server_requests
每个服务的字节数
{NAMESPACE}_server_bytes
每个服务的缓存
{NAMESPACE}_server_cache

Filter zones

过滤掉的请求数量
{NAMESPACE}_filter_requests
过滤掉的字节数
{NAMESPACE}_filter_bytes
各个server请求转发的平均处理时间
{NAMESPACE}_filter_responseMsec

Upstreams

upstream的请求数
{NAMESPACE}_upstream_requests
upstream的字节数
{NAMESPACE}_upstream_bytes
统计各个upstream平均响应时长,精确到每个节点
{NAMESPACE}_upstream_responseMsec

实例访问metrics,返回的数据样例

# HELP nginx_server_bytes request/response bytes
# TYPE nginx_server_bytes counter
nginx_server_bytes{direction="in",host="localhost"} 1404
nginx_server_bytes{direction="out",host="localhost"} 48184
# HELP nginx_server_cache cache counter
# TYPE nginx_server_cache counter
nginx_server_cache{host="localhost",status="bypass"} 0
nginx_server_cache{host="localhost",status="expired"} 0
nginx_server_cache{host="localhost",status="hit"} 0
nginx_server_cache{host="localhost",status="miss"} 0
nginx_server_cache{host="localhost",status="revalidated"} 0
nginx_server_cache{host="localhost",status="scarce"} 0
nginx_server_cache{host="localhost",status="stale"} 0
nginx_server_cache{host="localhost",status="updating"} 0
# HELP nginx_server_connections nginx connections
# TYPE nginx_server_connections gauge
nginx_server_connections{status="accepted"} 17
nginx_server_connections{status="active"} 2
nginx_server_connections{status="handled"} 17
nginx_server_connections{status="reading"} 0
nginx_server_connections{status="requests"} 281
nginx_server_connections{status="waiting"} 1
nginx_server_connections{status="writing"} 1
# HELP nginx_server_info nginx info
# TYPE nginx_server_info gauge
nginx_server_info{hostName="dh-neibu-20.50-docker.cn",nginxVersion="1.16.1"} 78
# HELP nginx_server_requestMsec average of request processing times in milliseconds
# TYPE nginx_server_requestMsec gauge
nginx_server_requestMsec{host="localhost"} 0
# HELP nginx_server_requests requests counter
# TYPE nginx_server_requests counter
nginx_server_requests{code="1xx",host="localhost"} 0
nginx_server_requests{code="2xx",host="localhost"} 13
nginx_server_requests{code="3xx",host="localhost"} 0
nginx_server_requests{code="4xx",host="localhost"} 0
nginx_server_requests{code="5xx",host="localhost"} 0
nginx_server_requests{code="total",host="localhost"} 13
# HELP nginx_vts_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which nginx_vts_exporter was built.
# TYPE nginx_vts_exporter_build_info gauge
nginx_vts_exporter_build_info{branch="HEAD",goversion="go1.10",revision="8aa2881c7050d9b28f2312d7ce99d93458611d04",version="0.10.3"} 1

你可能感兴趣的:(prometheus,nginx)