公司新上线一个监控系统,想对每一个请求的状态进行统计(1xx,2xx,3xx,4xx,5xx,request,response)。便于知道每一个url的请求量以及成功率。经过调研发现,nginx-module-vts 可以实现我们的需求,但是改模块是第三方模板,需要对Nginx进行编译添加,然后配置开启这个功能。
由于Nginx是已经运行的状态,所以设计到如何升级编译的问题。
tengine-2.1.2
nginx-module-vts-0.1.18.tar.gz
nginx-vts-exporter-0.10.3
prometheus
nginx -V
Tengine version: Tengine/2.1.2 (nginx/1.6.2)
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/app/tengine-2.1.2 --with-http_lua_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-pcre=/soft/tengine/pcre-8.39 --with-openssl=/soft/tengine/openssl-1.0.2j
./configure ${上面 nginx -V 显示出来的编译参数,原样的放在这里} --add-module=/tol/soft/nginx-module-vts-0.1.18
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/
至此我们的Nginx编译工作就完成了,停止原先的Nginx,然后启动新编译的Nginx验证是否能够正常运行。
在 nginx.conf 中添加
http {
vhost_traffic_status_zone;
...
server {
...
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
然后 nginx -t 测试一下配置ok;nginx -s reload 载入配置
此时 访问 ip:port/status 就可以查看到统计的Nginx信息
更多配置参考,官方GitHub链接
下载nginx-exporter ,0.10.3的下载地址
解压后,里面有个 nginx-vts-exporter 二进制文件。
查看启动参数:
./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.
这里主要配置 -nginx.scrape_uri=“http://ip:port/status/format/json” 这个参数即可。
附录一下自己写的启动停止脚本。
在 prometheus.yml 中配置一下采集任务
突然在官方GitHub上面发现有输出prometheus格式的统计信息,那么直接用prometheus去访问不就可以了,减少了exporter这一过程。
请求地址 /status/format/prometheus
在prometheus.yml中的配置如下
- job_name: 'nginx-exporter'
metrics_psth: /status/format/prometheus
static_configs:
- targets: ['ip:80']
```shell
#!/bin/sh
cd `dirname $0`
BIN_DIR=`pwd`
LOG_DIR=$BIN_DIR/logs
if [ ! -d $LOG_DIR ]; then
echo "mkdir -p $LOG_DIR"
mkdir -p $LOG_DIR
fi
LOG_FILE=$LOG_DIR/nginx-exporter.log
nohup ${BIN_DIR}/nginx-vts-exporter -nginx.scrape_uri="http://nginx.neibu.koolearn.com/status/format/json" > $LOG_FILE 2>&1 &
```
```shell
#!/bin/sh
cd `dirname $0`
BIN_DIR=`pwd`
PID=`ps -ef|grep ${BIN_DIR} |grep -v grep|grep -v stop |awk '{print $2}'`
if [ ! -z "$PID" ]; then
kill -9 $PID
fi
echo "kill $PID"
```
nginx_vts_info
nginx_vts_main_connections
nginx_vts_main_shm_usage_bytes
nginx_vts_server_bytes_total
nginx_vts_server_cache_total
nginx_vts_server_request_seconds
nginx_vts_server_request_seconds_total
nginx_vts_server_requests_total
nginx_vts_upstream_bytes_total
nginx_vts_upstream_request_seconds
nginx_vts_upstream_request_seconds_total
nginx_vts_upstream_requests_total
nginx_vts_upstream_response_seconds
nginx_vts_upstream_response_seconds_total
nginx_server_info
nginx_server_connections
nginx_server_bytes
nginx_server_cache
nginx_server_requestMsec
nginx_server_requests
nginx_upstream_bytes
nginx_upstream_requestMsec
nginx_upstream_requests
nginx_upstream_responseMsec
nginx_vts_exporter_build_info