node_exporter使用textfile collector收集业务数据

上一篇文章讲了使用Pushgateway收集业务数据的方法,今天讲另外一种方式textfile collector

The textfile collector is similar to the Pushgateway, in that it allows exporting of statistics from batch jobs. The Pushgateway should be used for service-level metrics. The textfile module is for metrics that are tied to a machine.

textfile collector 和 Pushgateway类似,都可以收集监控数据,二者的不同是 Pushgateway是一个独立的组件、textfile collector是node_exporter的附加功能,Pushgateway适用于service-level的job,textfile collector适用于machine-level的job。

textfile collector的功能就是,让node_exporter在收集数据时,顺带把指定目录下文件的内容 也加入到metrics里面


To use it, set the --collector.textfile.directory flag on the node_exporter commandline. The collector will parse all files in that directory matching the glob *.prom using the text format.

要使用textfile collector,需要在node_exporter的启动命令中增加参数--collector.textfile.directory=dir_path

例:./node_exporter --collector.textfile.directory=/root/prometheus/prom 这样node_exporter会把/root/prometheus/prom下所有 .prom 文件的内容一并收集到metrics

.prom 文件的内容格式是这样:

# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027
http_requests_total{method="post",code="400"}    3

# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
http_request_duration_seconds_bucket{le="0.5"} 129389
http_request_duration_seconds_bucket{le="1"} 133988
http_request_duration_seconds_bucket{le="+Inf"} 144320
http_request_duration_seconds_sum 53423
http_request_duration_seconds_count 144320

# Finally a summary, which has a complex representation, too:
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 3102
rpc_duration_seconds{quantile="0.05"} 3272
rpc_duration_seconds{quantile="0.5"} 4773
rpc_duration_seconds{quantile="0.9"} 9001
rpc_duration_seconds{quantile="0.99"} 76656
rpc_duration_seconds_sum 1.7560473e+07
rpc_duration_seconds_count 2693

到prometheus页面就能查到上面的指标:

需要注意的是,如果不需要收集指标时,应该将对应的文件删除,否则node_exporter在每次收集数据时,将该文件的内容加入到metrics。(如上图,只要.prom文件没被删除或更新,就会搜到一堆不同时间戳下的重复值)

textfile collector是最简单的业务数据收集方式,不挑语言,只要有文件就行。

更多内容见官方文档

### Node Exporter Collector 配置与使用 Node ExporterPrometheus 生态系统中的一个重要组件,用于收集主机级别的各种指标数据Collector 负责具体的数据采集工作。 #### 默认 Collectors 启用情况 默认情况下,Node Exporter 会启用大部分内置的 collectors 来自动抓取系统的各项性能指标[^1]。这些默认开启的collectors涵盖了CPU、内存、磁盘IO等多个方面的重要信息。 #### 自定义 Collector 设置 如果希望调整哪些collector被激活或禁用某些不需要的collector,则可以通过命令行参数`--no-collector.<name>`来关闭特定的collector;也可以通过设置环境变量`NODE_EXPORTER_COLLECTOR_TEXTFILE_DIR=/path/to/textfile_directory`指定额外textfile collector使用的目录位置。 对于更复杂的场景,可以创建一个名为`collector.text.conf`的文件,在其中定义要加载的一组collector及其选项,并将其放置于Node Exporter启动时能够访问的位置。此配置文件支持多种语法结构,允许灵活控制各个collector的行为模式。 ```bash # Example of disabling specific collectors via command line argument when starting node_exporter service /usr/local/bin/node_exporter \ --web.listen-address=":9100" \ --no-collector.cpu \ --no-collector.filesystem \ ... ``` #### 查看当前已启用的Collectors列表 为了确认实际运行环境中究竟启用了哪些collectors,可以在浏览器地址栏输入目标服务器IP加上端口号(通常是`:9100`)并附加路径`/metrics`,这将展示所有由node_exporter暴露出来的metric名称以及它们所属的具体collector类别。 ```bash curl http://localhost:9100/metrics | grep "node_" | cut -d "_" -f 1,2 | sort | uniq -c ``` 上述命令可以帮助快速统计各类collector所贡献的metric数量分布状况。 #### 文档资源链接 官方文档提供了详细的说明关于如何进一步定制化collector行为,建议查阅最新版本的手册获取最权威指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值