1. 前言
工作中需要监控 Nginx 的指标,选用的指标采集器是 Categraf,特此记录下,以备后用。
此文档并未详细记录详细的操作细节,只记录了大概的操作步骤,仅供参考。
2. 采集基础指标
2.1. 暴露 Nginx 自带的指标采集接口
参考 从源码安装 Nginx 文档,得知需要在源码安装时添加配置选项才能暴露指标采集接口:
--with-http_stub_status_module
enables building the ngx_http_stub_status_module module that provides access to basic status information. This module is not built by default.
下载 Nginx 源码 后自行加上编译选项编译。
参考 Nginx 指标接口路由配置 在配置文件(nginx.conf)中的对应位置加上指标接口路由配置:
location = /basic_status {
stub_status;
}
省略已有的部分配置,在http.server下添加指标接口路由配置如下所示:
http {
server {
location = /basic_status {
stub_status;
}
}
}
启动 Nginx 服务,访问对应的指标页面 http://xxx/basic_status,就能看到 Nginx 服务的一些基础指标:
Active connections: 1
server accepts handled requests
122 122 6581
Reading: 0 Writing: 1 Waiting: 0
2.2. 使用 Categraf 采集 Nginx 基础指标
在 Categraf 探针配置文件 conf/input.nginx/nginx.toml 中加上 Nginx 指标接口 http://xxx/basic_status,上报到 prometheus,从 prometheus 页面就能查到对应的指标数据了:

不过这都是些很基础的指标数据,我们期望能获取更丰富的指标数据。
3. 采集更丰富的指标
在网上搜索了下,发现为了能采集更丰富的 Nginx 指标,还得借助 nginx-module-vts 模块。
参考 操作文档 将 nginx-module-vts 模块编译进 Nginx 可执行文件中,重启 Nginx 服务,访问 http://xxx/status,就能看到更为丰富的 Nginx 指标数据了:

3.1. 使用 Categraf 采集 Nginx vts 指标
Nginx vts 指标路径不能直接配置到 conf/input.nginx/nginx.toml 文件中,会报错,查看 Categraf 源码发现需要配置到 prometheus 采集插件配置中:

将采集 prometheus 格式数据的路径 http://xxx/status/format/prometheus 配置到 conf/input.prometheus/prometheus.toml 文件中后,重启 Categraf,从 prometheus 页面就能查到对应的指标数据了:







![[C++] VS 2022演练 - 创建和使用静态连接库(Static Lib) (详细图文)](https://img-blog.csdnimg.cn/direct/cb5bf3f881234780a89911c99d2fb553.png)











