内容目录

安装generator

  1. 克隆到zabbixserver虚机/root路径下
    git clone https://github.com/prometheus/snmp_exporter.git

  2. 安装go

wget https://gitee.com/robotneo/script/raw/master/install-go.sh && chmod +x install-go.sh && ./install-go.sh

# 查看是否成功
go env
go version
go version go1.23.3 linux/amd64

# 查看git版本
git --version
git version 1.8.3.1
  1. 构建generator
# 进入生成器目录构建二进制可执行文件
cd /root/snmp_exporter/generator

# 构建
make generator mibs

# 如果上面一直不动 基本就是网络无法下载开源项目提供的公共mib库 那就不下载 执行命令Ctrl + C直接结束下载 目的主要是构建出 generate 的二进制执行文件即可

args参数解析

  1. 配置生成器从 generator.yml 中读取简化的收集指令并把相应的配置写入 snmp.ymlsnmp_exporter 二进制执行文件仅使用 snmp.yml 文件从设备收集数据。

  2. flags参数解析

# ./generator [<flags>] <command> [<args> ...]
-m    # 生成配置 需要读取的mibs库文件目录 可同时指定多个
-g    # 生成配置 需要读取的生成器配置文件 generator.yml
-o    # 生成的配置保存路径和文件名
# ./generator [<flags>] <command> [<args> ...]
--fail-on-parse-errors    # 如果存在 MIB 解析错误,则以非空的状态退出
--snmp.mibopts            # 切换控制 MIB 解析的各种默认设置 请参阅 snmpwalk --help
--log.level=info          # 输出日志信息等级 debug, info, warn, error
--log.format=logfmt       # 输出日志格式 logfmt, json
--parse_errors            # 调试:打印 NetSNMP 输出的解析错误
--dump                    # 调试:转储已解析和准备的 MIB

监控华三交换机

下载华三交换机mib文件

下载ComwareV7版本
ComwareV7
保存到 /root/snmp_exporter/generator/h3c/mibs/switch目录

OID信息查询

华三设备指标 OID 信息查询可直接到华三官网 MIB 工具网址查询,根据对应的版本和设备型号筛选。工具网址如下:
华三oid

生成器配置文件编写

华三msr3600实践配置

生成器配置文件放在/root/snmp_exporter/generator/h3c/switch目录
命名为generator_h3c_msr3600.yml

cat generator_h3c_msr3600.yml
auths:
  h3c_auth:  # 认证模块名称
    version: 2  # snmp v2c版本
    community: public  # snmp 团体名

modules:
  h3c_common:  # 华三公共指标模块名称
    walk:
      # 标量,交换机基础信息
      - 1.3.6.1.2.1.1.1                       # sysDescr - 设备描述
      - 1.3.6.1.2.1.1.5                       # sysName - 系统名称
      - 1.3.6.1.6.3.10.2.1.3                  # snmpEngineTime(单位秒)取代sysUpTime时间(单位百分之一秒)
      # 表量,索引为entPhysicalIndex
      - 1.3.6.1.4.1.25506.2.6.1.1.1.1.6       # hh3cEntityExtCpuUsage - 实体 CPU 实时利用率(统计周期为5秒钟)
      - 1.3.6.1.4.1.25506.2.6.1.1.1.1.8       # hh3cEntityExtMemUsage - 实体内存实时利用率百分比
      - 1.3.6.1.2.1.47.1.1.1.1.7              # entPhysicalName - 实体名称
      - 1.3.6.1.4.1.25506.2.6.1.1.1.1.19      # hh3cEntityExtErrorStatus - 实体错误状态
      - 1.3.6.1.4.1.25506.2.6.1.1.1.1.12      # hh3cEntityExtTemperature - 实体温度
    lookups:
      - source_indexes: [entPhysicalIndex]
        lookup: entPhysicalClass
      - source_indexes: [entPhysicalIndex]
        lookup: entPhysicalSerialNum
    overrides:
      entPhysicalClass:
        ignore: true
      entPhysicalSerialNum:
        ignore: true
    filters:
      static:
        - targets:
          - 1.3.6.1.2.1.47.1.1.1.1.7
          # 只获取索引列表的实体名称
          indices: ["11","12","13","16","87","88","89","90","91"]
      dynamic:
        - oid: 1.3.6.1.2.1.47.1.1.1.1.7
          targets:
            - 1.3.6.1.4.1.25506.2.6.1.1.1.1.6       # hh3cEntityExtCpuUsage - 实体 CPU 实时利用率
            - 1.3.6.1.4.1.25506.2.6.1.1.1.1.8       # # hh3cEntityExtMemUsage - 实体内存实时利用率百分比
          # 只获取实体名称取值为RPU的cpu和内存使用率
          values: ["RPU"]
        - oid: 1.3.6.1.2.1.47.1.1.1.1.7
          targets:
            - 1.3.6.1.4.1.25506.2.6.1.1.1.1.12       # hh3cEntityExtTemperature - 实体温度
          # 只获取实体名称取值为Temperature Sensor的实体温度
          values: ["Temperature Sensor"]
        - oid: 1.3.6.1.2.1.47.1.1.1.1.7
          targets:
            - 1.3.6.1.4.1.25506.2.6.1.1.1.1.19       # hh3cEntityExtErrorStatus  - 实体错误状态
          # 只获取实体名称取值为下列值的实体错误状态
          values: ["Power Supply","Fan","Unknown Power"]

  h3c_interface:
    walk:
      # 表量,索引为ifIndex
      - 1.3.6.1.2.1.2.2.1.13                  # ifInDiscards - 入方向丢包统计
      - 1.3.6.1.2.1.2.2.1.14                  # ifInErrors - 入方向错包统计
      - 1.3.6.1.2.1.2.2.1.19                  # ifOutDiscards - 出方向丢包统计
      - 1.3.6.1.2.1.2.2.1.20                  # ifOutErrors - 出方向错包统计
      - 1.3.6.1.2.1.31.1.1.1.6                # ifHCInOctets - 入方向字节统计
      - 1.3.6.1.2.1.31.1.1.1.10               # ifHCOutOctets - 出方向字节统计

    lookups:
      - source_indexes: [ifIndex]
        lookup: ifDescr
      - source_indexes: [ifIndex]
        lookup: ifAlias
      - source_indexes: [ifIndex]
        lookup: ifName
      - source_indexes: [ifIndex]
        lookup: ifPhysAddress
      - source_indexes: [ifIndex]
        lookup: ifAdminStatus
      - source_indexes: [ifIndex]
        lookup: ifOperStatus
      - source_indexes: [ifIndex]
        lookup: ifHighSpeed
      - source_indexes: [ifIndex]
        lookup: dot3StatsDuplexStatus

    overrides:
      ifDescr:
        ignore: true
      ifAlias:
        ignore: true
      ifName:
        ignore: true
      ifPhysAddress:
        ignore: true
      ifAdminStatus:
        ignore: true
      ifOperStatus:
        ignore: true
      ifHighSpeed:
        ignore: true
      dot3StatsDuplexStatus:
        ignore: true

    filters:
      dynamic:
        - oid: 1.3.6.1.2.1.2.2.1.8
          targets:
            - 1.3.6.1.2.1.31.1.1.1.6   # ifHCInOctets
            - 1.3.6.1.2.1.31.1.1.1.10  # ifHCOutOctets
            - 1.3.6.1.2.1.2.2.1.13   # ifInDiscards
            - 1.3.6.1.2.1.2.2.1.19   # ifOutDiscards
            - 1.3.6.1.2.1.2.2.1.14   # ifInErrors
            - 1.3.6.1.2.1.2.2.1.20   # ifOutErrors
          # 只获取ifOperStatus取值为up/1的接口
          values: ["1"]

  h3c_ipaddr:
    walk:
      - 1.3.6.1.2.1.4.20.1.1           # ipAdEntAddr

    lookups:
      - source_indexes: [ipAdEntAddr]
        lookup: ipAdEntIfIndex
      - source_indexes: [ipAdEntIfIndex]
        lookup: ifName
      - source_indexes: [ipAdEntIfIndex]
        lookup: ifAlias

    overrides:
      ipAdEntIfIndex:
        ignore: true
      ifName:
        ignore: true
      ifAlias:
        ignore: true

walk 下的具体指标可以指定标量或者表量,表示方式可以是 OID 或者对象名称,这里的定义代表该模块需要采集哪些指标,通常有:接口信息相关的 OID 或对象名称、设备基础信息相关的 OID

lookupsoverrides 主要利用在有索引的表量中,如下所示: lookups 中利用源索引标签生成指标标签,但标签必须是通过 OID 查找到的;还有一种用途就是如果表量有多个索引,可以根据多个索引生成可读性更好的索引标签,以达到索引标签的唯一性。

lookups:
  # 通过接口表中唯一索引标签 ifIndex 把当前查找的指标 ifAlias 作为标签插入当前表量指标中
  - source_indexes: [ifIndex]
    lookup: ifAlias
  - source_indexes: [ifIndex]
    lookup: ifName

overrides 是指定当前抓取的指标是否显示,以及抓取的指标数据类型的指定,通常结合 lookups 一起使用。比如:

overrides:
  ifAlias:
    ignore: true
  ifName:
    ignore: true
    type: DisplayString

ifAliasifName 已经作为标签插入表量指标中,那么一般情况下都是不需要再以指标的形式存在的,故可直接忽略该指标在暴露出来,因已经作为标签插入需要采集的表量指标中了。

ignore 设置 true 就代表不暴露指标, type 代表指标的数据类型。

生成华三msr3600采集配置文件

generator_h3c_msr3600.yml 配置文件已经编辑好,接下来就可以生成最终的采集配置文件,采集配置文件以 snmp 为前缀,也是为了后续好管理和识别,比如华三路由器,可以命名为 snmp_h3c_msr3600.yml

# 进入生成器文件目录
cd /root/snmp_exporter/generator/

# 执行命令采集配置,执行该命令后会最终生成采集配置文件`snmp_h3c_msr3600.yml,该文件保存到-o参数指定的目录中
./generator --no-fail-on-parse-errors generate -m h3c/mibs/switch/ -g h3c/switch/generator_h3c_msr3600.yml -o h3c/switch/snmp_h3c_msr3600.yml

# 复制采集配置文件到snmp_exporter读取配置文件的目录中
cp /root/snmp_exporter/generator/h3c/switch/snmp_h3c_msr3600.yml /etc/snmp_exporter/

# 重启snmp_exporter
systemctl restart snmp_exporter.service

监控华为交换机

下载华为交换机mib文件

华为交换机mib文件,是根据华为固件版本来更新的,
华为交换机mib文件下载

OID信息查询

华为设备指标 OID 信息查询可直接到华为官网 MIB 工具网址查询,根据对应的版本和设备型号筛选。工具网址如下:
华为OID

可以根据实际的关键字进行模糊匹配查询,或者知道对应指标的 OID 和对象名称,也可直接搜索查询到对应信息。

生成器配置文件编写

华为9306实践配置

生成器配置文件在/root/snmp_exporter/generator/huawei/switch目录
命名为generator_huawei_9306.yml

auths:
  huawei_auth:  # 认证模块名称
    version: 2  # snmp v2c版本
    community: public  # snmp 团体名

modules:
  huawei_common:  # 华为公共指标模块名称
    walk:
      # 交换机基础信息
      - 1.3.6.1.2.1.1.1                       # sysDescr - 设备描述
      - 1.3.6.1.2.1.1.5                       # sysName - 系统名称
      - 1.3.6.1.6.3.10.2.1.3                  # snmpEngineTime(单位秒)取代sysUpTime时间(单位百分之一秒)
      - 1.3.6.1.4.1.2011.5.25.31.1.1.6.1.4    # hwEntPowerUsedInfoPower  实体当前功率
      - 1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7   # hwEntityFanState  风扇的运行状态
      - 1.3.6.1.4.1.2011.5.25.31.1.1.18.1.6   # hwEntityPwrState  电源的状态
      - 1.3.6.1.4.1.2011.5.25.31.1.1.14.1.3   # hwSystemPowerUsedPower 已使用的功率
      - 1.3.6.1.2.1.47.1.1.1.1.11.67108867    # entPhysicalSerialNum  序列号

    lookups:
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntPowerUsedInfoBoardName
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntPowerUsedInfoBoardType
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntPowerUsedInfoBoardSlot
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntityCpuUsage
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntityUpTime
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntityTemperature
      - source_indexes: [hwSystemPowerDeviceID]
        lookup: hwSystemPowerTotalPower
    overrides:
      hwEntPowerUsedInfoBoardName:
        ignore: true
        type: DisplayString
      hwEntPowerUsedInfoBoardType:
        ignore: true
        type: DisplayString
      hwEntPowerUsedInfoBoardSlot:
        ignore: true
      hwEntityCpuUsage:
        ignore: true
      hwEntityUpTime:
        ignore: true
      hwEntityTemperature:
        ignore: true
      hwSystemPowerTotalPower:
        ignore: true

  huawei_interface:
    walk:
      - 1.3.6.1.2.1.31.1.1.1.1                # ifName
      - 1.3.6.1.4.1.2011.5.25.31.1.1.3.1.8    # hwEntityOpticalRxPower 光模块接收功率 单位 uW
      - 1.3.6.1.4.1.2011.5.25.31.1.1.3.1.9    # hwEntityOpticalTxPower 光模块发送功率 单位 uW

    lookups:
      - source_indexes: [ifIndex]
        lookup: ifAlias
      - source_indexes: [ifIndex]
        lookup: ifOperStatus
      - source_indexes: [entPhysicalIndex]
        lookup: entPhysicalName                 # 实体名称
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntityOpticalRxLowThreshold   # 光模块接收功率过低的告警门限值
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntityOpticalDirectionType    # 光模块的传输方式
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntityOpticalVenderName       # 光模块厂商名
      - source_indexes: [entPhysicalIndex]
        lookup: hwEntityOpticalTransferDistance # 光模块传输距离

    overrides:
      ifAlias:
        ignore: true
      ifOperStatus:
        ignore: true
      entPhysicalName:
        ignore: true
      hwEntityOpticalRxLowThreshold:
        ignore: true
      hwEntityOpticalDirectionType:
        ignore: true
      hwEntityOpticalVenderName:
        ignore: true
      hwEntityOpticalTransferDistance:
        ignore: true

    filters:
      dynamic:
        - oid: 1.3.6.1.2.1.2.2.1.8   # ifOperStatus
          targets:
            - 1.3.6.1.2.1.31.1.1.1.1 # ifName
          values: ["1"]

生成华为9306采集配置文件

generator_huawei_9306.yml 配置文件已经编辑好,接下来就可以生成最终的采集配置文件,采集配置文件以 snmp 为前缀,也是为了后续好管理和识别,可以命名为 snmp_huawei_9306.yml

# 进入生成器文件目录
cd /root/snmp_exporter/generator/

# 执行命令采集配置,执行该命令后会最终生成采集配置文件`snmp_huawei_9306.yml,该文件保存到-o参数指定的目录中
./generator --no-fail-on-parse-errors generate -m huawei/mibs/switch/ -g huawei/switch/generator_huawei_9306.yml -o huawei/switch/snmp_huawei_9306.yml

# 复制采集配置文件到snmp_exporter读取配置文件的目录中
cp /root/snmp_exporter/generator/huawei/switch/snmp_huawei_9306.yml /etc/snmp_exporter/

# 重启snmp_exporter
systemctl restart snmp_exporter.service

snmp_exporter配置

配置snmp_exporter启动参数,--config.file=/etc/snmp_exporter/snmp*.yml,启动时加载以snmp开头的采集配置文件

vim /usr/lib/systemd/system/snmp_exporter.service
[Unit]
Description=snmp_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
ExecStart=/etc/snmp_exporter/snmp_exporter --web.listen-address=:9116 --config.file=/etc/snmp_exporter/snmp*.yml
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
Restart=always

[Install]
WantedBy=multi-user.target

启动服务:

systemctl daemon-reload
systemctl restart snmp_exporter.service

prometheus配置

  • 上面已完成华三msr3600以及华为9306的数据指标收集,接下来就需要把这一批华三msr3600以及单台华为9306的指标数据都纳入到 Prometheus 时序数据库中,那么可以按照如下配置:
cat /etc/prometheus/prometheus.yml
scrape_configs:
  - job_name: 'h3c_msr3600'
    scrape_interval: 60s
    scrape_timeout: 30s
    metrics_path: /snmp
    params:
      auth: [h3c_auth]
      module: [h3c_common,h3c_interface,h3c_ipaddr]
    file_sd_configs:
      - files:
        - /etc/prometheus/targets/h3c-*.yml
        refresh_interval: 2m
    relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label: __address__
      replacement: 192.168.99.4:9116

  - job_name: 'huawei_9306'
    scrape_interval: 60s
    scrape_timeout: 30s
    metrics_path: /snmp
    params:
      auth: [huawei_auth]
      module: [huawei_common,huawei_interface]
    file_sd_configs:
      - files:
        - /etc/prometheus/targets/huawei-*.yml
        refresh_interval: 2m
    relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label: __address__
      replacement: 192.168.99.4:9116

cat targets/h3c-msr3600.yml
- labels:
    department: xxx  # 自定义的标签,非必选
  targets:
    - x.x.x.x  # 网络设备管理IP

cat /etc/prometheus/targets/huawei-9306.yml
- labels:
    department: xxx
  targets:
    - y.y.y.y
  • 通过 file_sd_configs 的方式做服务发现,主要是配置交换机管理 IP ,在目录 /etc/prometheus/targets 新建文件 h3c-msr3600.yml ,然后匹配该目录中 h3c-msr3600 的配置文件,这里主要是为了在大批量交换机场景下方便配置文件的扫描和发现。

  • 热加载prometheus的配置

# 在 Prometheus 服务器中执行热加载接口
curl -X POST localhost:9090/-/reload
# 健康检查 应用于检查 Prometheus 运行状况
curl -X GET localhost:9090/-/healthy

指标数据

prometheus中查看指标名称,是否都采集存储到了prometheus tsdb

# h3c_common模块共8个指标
sysDescr                     - 设备描述
sysName                      - 系统名称
snmpEngineTime               - 启动时间
hh3cEntityExtCpuUsage        - 实体 CPU 实时利用率(统计周期为5秒钟)
hh3cEntityExtMemUsage        - 实体内存实时利用率百分比
entPhysicalName              - 实体名称
hh3cEntityExtErrorStatus     - 实体错误状态
hh3cEntityExtTemperature     - 实体温度

# h3c_interface模块共6个指标
ifInDiscards   - 入方向丢包统计
ifInErrors     - 入方向错包统计
ifOutDiscards  - 出方向丢包统计
ifOutErrors    - 出方向错包统计
ifHCInOctets   - 入方向字节统计
ifHCOutOctets  - 出方向字节统计

# h3c_ipaddr模块共1个指标
ipAdEntAddr   - 显示这个表项的地址信息所属的IP地址
# huawei_common模块共8个指标
sysDescr                     - 设备描述
sysName                      - 系统名称
snmpEngineTime               - 启动时间
hwEntPowerUsedInfoPower      - 实体当前功率
hwEntityFanState             - 风扇的运行状态
hwEntityPwrState             - 电源的状态
hwSystemPowerUsedPower       - 已使用的功率
entPhysicalSerialNum         - 序列号

# h3c_interface模块共6个指标
ifName
hwEntityOpticalRxPower  - 光模块接收功率 单位 uW
hwEntityOpticalTxPower  - 光模块发送功率 单位 uW

一些查看指标示例

# 查看设备接口是否有半双工状态,3是全双工,2是半双工
count(ifHCInOctets{dot3StatsDuplexStatus="2"}) by (instance,ifName)

# 查看哪个设备接口适配成100Mb
count(ifHCInOctets{ifHighSpeed="100"}) by (instance,ifName)

# 查看设备接口最近1天是否有丢包和错包
rate(ifInDiscards[1d]) > 0
rate(ifOutDiscards[1d]) > 0
rate(ifInErrors[1d]) > 0
rate(ifOutErrors[1d]) > 0

# 查看设备运行时间排序
sort_desc(max(snmpEngineTime) by (instance,job)) / (3600*24*7)

# 一天中GigabitEthernet0/0接口最大的下行流量
max_over_time(irate(ifHCInOctets{job="h3c_msr3600",ifName="GigabitEthernet0/0"}[5m]) [1d:]) *8/1024/1024

grafana面板

基于华三msr3600做了一个简单的可视化面板,可以根据个人自定义的指标对模版进行修改和扩展,我只是简单的作为一个展示,在此不会过多阐述 Grafana 面板的细节,这要说的话就是另外一个话题,我分享的面板文件:

MSR3600监控.json

多模块并发

多模块并发,通过 SNMP Exporter 最新版本( 0.25.0 )实现多模块并发,降低采集响应时间。

首先需要添加 SNMP Exporter 运行参数:

cat /usr/lib/systemd/system/snmp_exporter.service
[Unit]
Description=snmp_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
ExecStart=/etc/snmp_exporter/snmp_exporter --web.listen-address=:9116 --config.file=/etc/snmp_exporter/snmp*.yml --snmp.module-concurrency=3
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
Restart=always

[Install]
WantedBy=multi-user.target

启动参数中添加: --snmp.module-concurrency
因为snmp_exporter采集配置文件有3个模块(common、interface、ipaddr)去采集不同的指标,可以分多个模块去执行采集,那么一定程度上可以加快相应采集时间。

最后修改日期: 2024年12月22日

留言

请教一个问题,请问华三的实体需要过滤匹配这些ID呢?indices: [“11″,”12″,”13″,”16″,”87″,”88″,”89″,”90″,”91”]

snmpwalk -v2c -c 团体名 ip地址 1.3.6.1.2.1.47.1.1.1.1.7 # entPhysicalName – 实体名称 SNMPv2-SMI::mib-2.47.1.1.1.1.7.11 = STRING: “RPU” SNMPv2-SMI::mib-2.47.1.1.1.1.7.12 = STRING: “Power Supply” SNMPv2-SMI::mib-2.47.1.1.1.1.7.13 = STRING: “Unknown Power” SNMPv2-SMI::mib-2.47.1.1.1.1.7.16 = STRING: “Fan” SNMPv2-SMI::mib-2.47.1.1.1.1.7.87 = STRING: “CPU” SNMPv2-SMI::mib-2.47.1.1.1.1.7.88 = STRING: “Memory” SNMPv2-SMI::mib-2.47.1.1.1.1.7.89 = STRING: “Temperature Sensor” SNMPv2-SMI::mib-2.47.1.1.1.1.7.90 = STRING: “Temperature Sensor” 需要过滤的实体名称每个设备不一样,可以用以上命令查看你要监控哪些实体名称

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。