You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Prometheus SNMP Exporter将传感器位置标签关联到温度指标

问题描述

我正在使用SNMP Exporter从APC实验室设备(基于PowerNet MIB)采集温度指标,需要通过memSensorsStatusModuleNumber和memSensorsStatusSensorNumber这两个索引字段,将memSensorsStatusSensorLocation的位置标签关联到memSensorsTemperature指标上,让温度指标带上对应的位置标签。

当前配置与采集结果

Prometheus配置(相关部分)

- job_name: labtemp_snmp
  static_configs:
    - targets:
      - 10.163.123.87
  scrape_interval: 15s
  metrics_path: /snmp
  params:
    module: [labtemp]
  relabel_configs:
   - source_labels: [__address__]
     target_label: __param_target
   - source_labels: [__param_target]
     target_label: instance
   - target_label: __address__
     replacement: 127.0.0.1:9116

SNMP Exporter配置

modules:
  labtemp:
    walk:
      - memSensorsStatusTable
    version: 1

当前采集指标示例

手动访问http://localhost:9116/snmp?target=10.163.123.87&module=labtemp得到的部分指标:

# HELP memSensorsStatusSensorLocation The location of the sensor. - 1.3.6.1.4.1.318.1.1.10.4.2.3.1.4
# TYPE memSensorsStatusSensorLocation gauge
memSensorsStatusSensorLocation{memSensorsStatusModuleNumber="0",memSensorsStatusSensorLocation="Cold Aisle C1",memSensorsStatusSensorNumber="1"} 1
memSensorsStatusSensorLocation{memSensorsStatusModuleNumber="0",memSensorsStatusSensorLocation="Hot Aisle C2",memSensorsStatusSensorNumber="2"} 1
...
# HELP memSensorsTemperature The sensor's current temperature reading - 1.3.6.1.4.1.318.1.1.10.4.2.3.1.5
# TYPE memSensorsTemperature gauge
memSensorsTemperature{memSensorsStatusModuleNumber="0",memSensorsStatusSensorNumber="1"} 26
memSensorsTemperature{memSensorsStatusModuleNumber="0",memSensorsStatusSensorNumber="2"} 33
...

期望指标格式

希望温度指标带上位置标签,示例如下:

memSensorsTemperature{memSensorsStatusSensorLocation="Cold Aisle C1",memSensorsStatusModuleNumber="0",memSensorsStatusSensorNumber="1"} 26
...

相关MIB信息

memSensorsStatusEntry的索引为memSensorsStatusModuleNumber和memSensorsStatusSensorNumber:

memSensorsStatusEntry OBJECT-TYPE
   SYNTAX MemSensorsStatusEntry
   ACCESS not-accessible
   STATUS mandatory
   DESCRIPTION
      "The unit to get data from."
   INDEX { memSensorsStatusModuleNumber, memSensorsStatusSensorNumber }
   ::= { memSensorsStatusTable 1 }

另外memSensorsConfigTable中包含与上述索引对应的memSensorsLocation数据:

PowerNet-MIB::memSensorsLocation.0.1 = STRING: "Cold Aisle C1"
PowerNet-MIB::memSensorsLocation.0.2 = STRING: "Hot Aisle C2"
PowerNet-MIB::memSensorsLocation.1.1 = STRING: "Hot Aisle B8"
PowerNet-MIB::memSensorsLocation.1.2 = STRING: "Cold Aisle C12"
...
解决方案

这个需求必须在SNMP Exporter配置中处理——Prometheus本身无法跨指标关联标签,只能在数据采集阶段(SNMP Exporter层面)将位置标签直接附加到温度指标上。

修改SNMP Exporter的labtemp模块配置,使用lookups功能实现标签关联:

modules:
  labtemp:
    walk:
      - memSensorsStatusTable
      - memSensorsConfigTable  # 新增:需要采集该表获取位置数据
    version: 1
    lookups:
      # 通过索引匹配,将memSensorsLocation的值作为标签附加到温度指标
      - source_indexes: [memSensorsStatusModuleNumber, memSensorsStatusSensorNumber]
        lookup: memSensorsLocation
        dest_label: memSensorsStatusSensorLocation
        drop_source: false  # 保留原位置指标,不需要可设为true

配置说明

  1. 添加memSensorsConfigTable到walk列表:位置数据存储在该表中,必须先采集才能进行关联。
  2. 配置lookups规则:
    • source_indexes:指定用于匹配的索引字段,即两个表共有的memSensorsStatusModuleNumber和memSensorsStatusSensorNumber。
    • lookup:指定要获取的目标字段,即memSensorsLocation(来自memSensorsConfigTable)。
    • dest_label:指定要添加到温度指标上的标签名称,这里与原位置指标的标签名保持一致。
    • drop_source:若不需要保留原memSensorsStatusSensorLocation指标,可设为true,否则设为false保留。

修改配置后重启SNMP Exporter,重新采集指标即可看到memSensorsTemperature带上对应的位置标签。

内容的提问来源于stack exchange,提问作者Vlad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 11:07:18