一、前提条件
- 目标SNMP设备开启SNMP并配置读写组权限
- snmptranslate、snmpwalk客户端(用于调试)
- snmp_exporter的generator环境
- 目标SNMP设备的对应mib文件
generator环境
https://github.com/prometheus/snmp_exporter/tree/master/generator
clone到本地后使用go编译为可执行文件
snmptranslate、snmpwalk
yum install net-snmp net-snmp-utils net-snmp-libs net-snmp-devel
二、步骤
- 开启SNMP功能并配置加密算法、读写组等(这里以华为TS200服务器举例)
- 将下载的对应snmp设备型号的mib文件,上传到generator环境
[root@it-webpy-master generator]# ll mibs/huawei/
total 748
-rw-r--r-- 1 root root 761950 Jan 18 14:51 HUAWEI-SERVER-iBMC-MIB.mib
- 使用snmptranslate检索需要抓取的metrics OID,这里以温度监控为例
[root@it-webpy-master generator]# snmptranslate -Tz -m mibs/huawei/HUAWEI-SERVER-iBMC-MIB.mib |grep -i temperatureReading
"temperatureReading" "1.3.6.1.4.1.2011.2.235.1.1.26.50.1.3"
#温度读数OID为1.3.6.1.4.1.2011.2.235.1.1.26.50.1.3
- 使用snmpbulkwalk获取指定OID的snmp取值,校验与实际值是否吻合
[root@it-webpy-master generator]# snmpbulkwalk -v3 -l authPriv -u "Administrator" -a SHA -A "Admin@9999" -x AES -X "Admin@9999" 172.26.3.134 1.3.6.1.4.1.2011.2.235.1.1.26.50.1.3
SNMPv2-SMI::enterprises.2011.2.235.1.1.26.50.1.3.1 = INTEGER: 240
SNMPv2-SMI::enterprises.2011.2.235.1.1.26.50.1.3.2 = INTEGER: 450
SNMPv2-SMI::enterprises.2011.2.235.1.1.26.50.1.3.3 = INTEGER: 520
通过Object读数得知,上面获取到的三个值分别为进风温度、CPU1温度、CPU2温度,下面校验一下与BMC获取的温度是否一致(这里需要注意:snmp取值/10=实际值)
经校验,snmp取值与实际温度值一致,该OID可用。
- 编辑generator.yml文件
[root@it-webpy-master generator]# cat generator.yml
modules:
huawei:
walk:
- 1.3.6.1.4.1.2011.2.235.1.1.26.50.1.3
version: 3
timeout: 30s
auth:
community: public
security_level: authPriv
username: "Administrator"
password: "Admin@9999"
auth_protocol: SHA
priv_protocol: AES
priv_password: "Admin@9999"
- 使用generator生成snmp_exporter配置文件
[root@it-webpy-master generator]# export MIBDIRS=/root/snmp_exporter-master/generator/mibs
[root@it-webpy-master generator]# cp mibs/huawei/HUAWEI-SERVER-iBMC-MIB.mib mibs/
[root@it-webpy-master generator]# ./generator generate
level=info ts=2021-01-18T07:38:37.110Z caller=net_snmp.go:142 msg="Loading MIBs" from=/root/snmp_exporter-master/generator/mibs
level=info ts=2021-01-18T07:38:37.368Z caller=main.go:52 msg="Generating config for module" module=huawei
level=info ts=2021-01-18T07:38:37.392Z caller=main.go:67 msg="Generated metrics" module=huawei metrics=1
level=info ts=2021-01-18T07:38:37.392Z caller=main.go:92 msg="Config written" file=/root/snmp_exporter-master/generator/snmp.yml
[root@it-webpy-master generator]# cat snmp.yml
# WARNING: This file was auto-generated using snmp_exporter generator, manual changes will be lost.
huawei:
walk:
- 1.3.6.1.4.1.2011.2.235.1.1.26.50.1.3
metrics:
- name: temperatureReading
oid: 1.3.6.1.4.1.2011.2.235.1.1.26.50.1.3
type: gauge
help: Information about temperature reading,the unit is 1/10 Celsius degree -
1.3.6.1.4.1.2011.2.235.1.1.26.50.1.3
indexes:
- labelname: temperatureIndex
type: gauge
version: 3
timeout: 30s
auth:
community: public
security_level: authPriv
username: Administrator
password: Admin@9999
auth_protocol: SHA
priv_protocol: AES
priv_password: Admin@9999
- 把生成的配置复制到snmp_exporter.yml中,重启snmp_exporter生效,测试读数是否正确
- 成功获取snmp读数,metrics添加完成。