项目地址:https://github.com/prometheus/snmp_exporter
1. 环境部署
1.1 参考snmp_exporter官方安装指导,安装依赖环境
# Debian-based distributions.
sudo apt-get install unzip build-essential libsnmp-dev p7zip-full # Debian-based distros
# Redhat-based distributions.
sudo yum install gcc gcc-g++ make net-snmp net-snmp-utils net-snmp-libs net-snmp-devel # RHEL-based distros
1.2 安装go环境,方法参考官方文档 https://go.dev/doc/install
2. 制作需要snmp采集的配置文件snmp.yml
2.1 拉取Git snmp_exporter,也可以手动下载
go get github.com/prometheus/snmp_exporter/generator
2.2 编译程序
# 进入generator目录,如果是手动下载就进入generator目录
cd ${GOPATH-$HOME/go}/src/github.com/prometheus/snmp_exporter/generator
go build
这一步需要下载go依赖包,如果是离线环境部署,就先在有网络的机器上下载,然后把goPATH下的pkg/mod/cache/download目录复制过来在运行go build
2.3 设置mib文件的文件夹位置,前面的一个mibs就是文件夹,后面是公共文件夹,也加上,避免出错
export MIBDIRS=mibs:/usr/share/snmp/mibs
2.4 配置初始文件generator.yml,格式参考官方的,有几个注意点:
(1) module_nameke可以改成自定义名称,但是后面要用到
(2) walk里面填写需要遍历的节点,如下图,这个地方很重要,许多网上的教程没有说。这个根据自己需要填写
image.png
(3) version和auth,版本和认证方式根据自己的情况改
modules:
module_name: # The module name. You can have as many modules as you want.
walk: # List of OIDs to walk. Can also be SNMP object names or specific instances.
- 1.3.6.1.2.1.2 # Same as "interfaces"
- sysUpTime # Same as "1.3.6.1.2.1.1.3"
- 1.3.6.1.2.1.31.1.1.1.6.40 # Instance of "ifHCInOctets" with index "40"
version: 2 # SNMP version to use. Defaults to 2.
# 1 will use GETNEXT, 2 and 3 use GETBULK.
max_repetitions: 25 # How many objects to request with GET/GETBULK, defaults to 25.
# May need to be reduced for buggy devices.
retries: 3 # How many times to retry a failed request, defaults to 3.
timeout: 5s # Timeout for each individual SNMP request, defaults to 5s.
auth:
# Community string is used with SNMP v1 and v2. Defaults to "public".
community: public
# v3 has different and more complex settings.
# Which are required depends on the security_level.
# The equivalent options on NetSNMP commands like snmpbulkwalk
# and snmpget are also listed. See snmpcmd(1).
username: user # Required, no default. -u option to NetSNMP.
security_level: noAuthNoPriv # Defaults to noAuthNoPriv. -l option to NetSNMP.
# Can be noAuthNoPriv, authNoPriv or authPriv.
password: pass # Has no default. Also known as authKey, -A option to NetSNMP.
# Required if security_level is authNoPriv or authPriv.
auth_protocol: MD5 # MD5, SHA, SHA224, SHA256, SHA384, or SHA512. Defaults to MD5. -a option to NetSNMP.
# Used if security_level is authNoPriv or authPriv.
priv_protocol: DES # DES, AES, AES192, or AES256. Defaults to DES. -x option to NetSNMP.
# Used if security_level is authPriv.
priv_password: otherPass # Has no default. Also known as privKey, -X option to NetSNMP.
# Required if security_level is authPriv.
context_name: context # Has no default. -n option to NetSNMP.
# Required if context is configured on the device.
lookups: # Optional list of lookups to perform.
# The default for `keep_source_indexes` is false. Indexes must be unique for this option to be used.
# If the index of a table is bsnDot11EssIndex, usually that'd be the label
# on the resulting metrics from that table. Instead, use the index to
# lookup the bsnDot11EssSsid table entry and create a bsnDot11EssSsid label
# with that value.
- source_indexes: [bsnDot11EssIndex]
lookup: bsnDot11EssSsid
drop_source_indexes: false # If true, delete source index labels for this lookup.
# This avoids label clutter when the new index is unique.
# It is also possible to chain lookups or use multiple labels to gather label values.
# This might be helpful to resolve multiple index labels to a proper human readable label.
# Please be aware that ordering matters here.
# In this example, we first do a lookup to get the `cbQosConfigIndex` as another label.
- source_indexes: [cbQosPolicyIndex, cbQosObjectsIndex]
lookup: cbQosConfigIndex
# Using the newly added label, we have another lookup to fetch the `cbQosCMName` based on `cbQosConfigIndex`.
- source_indexes: [cbQosConfigIndex]
lookup: cbQosCMName
overrides: # Allows for per-module overrides of bits of MIBs
metricName:
ignore: true # Drops the metric from the output.
regex_extracts:
Temp: # A new metric will be created appending this to the metricName to become metricNameTemp.
- regex: '(.*)' # Regex to extract a value from the returned SNMP walks's value.
value: '$1' # The result will be parsed as a float64, defaults to $1.
Status:
- regex: '.*Example'
value: '1' # The first entry whose regex matches and whose value parses wins.
- regex: '.*'
value: '0'
type: DisplayString # Override the metric type, possible types are:
# gauge: An integer with type gauge.
# counter: An integer with type counter.
# OctetString: A bit string, rendered as 0xff34.
# DateAndTime: An RFC 2579 DateAndTime byte sequence. If the device has no time zone data, UTC is used.
# DisplayString: An ASCII or UTF-8 string.
# PhysAddress48: A 48 bit MAC address, rendered as 00:01:02:03:04:ff.
# Float: A 32 bit floating-point value with type gauge.
# Double: A 64 bit floating-point value with type gauge.
# InetAddressIPv4: An IPv4 address, rendered as 1.2.3.4.
# InetAddressIPv6: An IPv6 address, rendered as 0102:0304:0506:0708:090A:0B0C:0D0E:0F10.
# InetAddress: An InetAddress per RFC 4001. Must be preceded by an InetAddressType.
# InetAddressMissingSize: An InetAddress that violates section 4.1 of RFC 4001 by
# not having the size in the index. Must be preceded by an InetAddressType.
# EnumAsInfo: An enum for which a single timeseries is created. Good for constant values.
# EnumAsStateSet: An enum with a time series per state. Good for variable low-cardinality enums.
# Bits: An RFC 2578 BITS construct, which produces a StateSet with a time series per bit.
2.5 生成配置文件snmp.yml
./generator generate
3. 运行snmp_exporter
3.1 下载程序 https://github.com/prometheus/snmp_exporter/releases,把上一步生成的snmp.yml文件复制过来
./snmp_exporter
3.2 测试
访问 http://localhost:9116/snmp?module=if_mib&target=1.2.3.4
curl 'http://localhost:9116/snmp?module=if_mib&target=1.2.3.4'
1.2.3.4
是要snmp采集的设备地址或域名, if_mib
是在 snmp.yml
中定义的模块名称