Prometheus监控之SNMP Exporter介绍和数据展现

发布时间 2023-04-26 09:17:51作者: 十亩菠萝地

由于技术能力有限,文章仅能进行简要分析和说明,如有不对的地方,请指正,谢谢?。

1 SNMP协议介绍

SNMP协议全称是:Simple Network Management Protocol,译为简单网络管理协议,是作为TCP/IP网络管理标准协议,为不同的设备提供统一接口,实现了网络设备之间的统一管理。

SNMP协议分为三个版本:

  1. SNMPv1是最初版本,基于团体名认证,安全性较差,返回报文的错误码较少。
  2. SNMPv2c也采用团体名认证,引入了GetBulk和Inform操作,支持更多的标准错误码信息和更多的数据类型。
  3. SNMPv3主要在安全性方面进行了增强,提供了基于USM(User Security Module)的认证加密和基于VACM(View-based Access Control Model)的访问控制。

不管哪个版本,SNMP协议由外部的网络管理系统NMS和运行在被管设备内部的SNMP Agent、被管对象和管理信息库MIB组成:

MIB数据库中的OID树状存储结构:树的节点表示被管理对象,它可以用从根开始的一条路径唯一地识别,这条路径就称为OID,如system的OID为1.3.6.1.2.1.1,interfaces的OID为1.3.6.1.2.1.2

可以从OID(如system的OID为1.3.6.1.2.1.1,interfaces的OID为1.3.6.1.2.1.2)中获取到被管理设备的当前运行状态。

2 SNMP Exporter介绍

SNMP Exporter是Prometheus的官方Exporter项目之一,可以容器运行或者二进制运行,项目地址:snmp_exporter。Exporter通过snmp.yml配置文件,将SNMP Agent的数据暴露在SNMP Exporter中,供Prometheus监控被管理设备的运行状态。

例如一个最简单的snmp.yml配置文件:

Linux:
  version: 2
  auth:
    community: snmpexport
  walk:
  - 1.3.6.1.4.1.2021.11
  get:
  - 1.3.6.1.2.1.1.3
  metrics:
  - name: sysUpTime
    oid: 1.3.6.1.2.1.1.3
    type: gauge
    help: The time (in hundredths of a second) since the network management portion
      of the system was last re-initialized. - 1.3.6.1.2.1.1.3
  - name: ssCpuUser
    oid: 1.3.6.1.4.1.2021.11.9
    type: gauge
    help: The percentage of CPU time spent processing user-level code, calculated
      over the last minute - 1.3.6.1.4.1.2021.11.9
  - name: ssCpuSystem
    oid: 1.3.6.1.4.1.2021.11.10
    type: gauge
    help: The percentage of CPU time spent processing system-level code, calculated
      over the last minute - 1.3.6.1.4.1.2021.11.10
  - name: ssCpuIdle
    oid: 1.3.6.1.4.1.2021.11.11
    type: gauge
    help: The percentage of processor time spent idle, calculated over the last minute
      - 1.3.6.1.4.1.2021.11.11

这个配置文件中有一个模块为Linux,SNMP版本为v2c,团体名称为snmpexporter,监控的OID有:1.3.6.1.4.1.2021.11和1.3.6.1.2.1.1.3,根据OID所在的树状级别,采用不同方式的查询操作(walk或get),被监控的对象有sysUpTime、ssCpuUser、ssCpuSystem、ssCpuIdle

  • walk:实际上是SNMP GETNEXT,从SNMP Agent中获取一个或多个参数的下一个参数值。
  • get:从SNMP Agent中获取一个或多个参数值。

实际上还有GetBulk操作(SNMPv1不支持),基于GETNEXT实现,相当于执行多次GetNext,但是SNMP Exporter中不具备该查询操作。

3 使用SNMP Exporter监控Linux操作系统

被监控设备操作系统版本:Oracle Linux 7.9,SELinux状态:Permissive

3.1 监控设备安装和配置SNMP

  1. 安装net-snmp
    yum install -y net-snmp
  2. 配置SNMP团体名称
    echo 'rocommunity snmpexport 192.168.1.200'>/etc/snmp/snmpd.conf
  3. 重启snmpd服务
    systemctl restart snmpd
  4. 检查snmpd服务
    systemctl status snmpd|grep -E "Active"
    输出结果参考:
    Active: active (running) since Fri 2022-05-06 10:23:56 CST; 2min 4s ago
  5. 防火墙放通snmp服务
    firewall-cmd --permanent --add-service=snmp

    firewall-cmd --add-port=161/udp --permanent

3.2 docker部署SNMP Exporter

  1. docker中下载snmp exporter镜像
    docker pull prom/snmp-exporter
  2. 创建snmp.yml配置文件目录
    mkdir /home/samroot/exporter/snmp
  3. 手动创建snmp.yml配置文件,参考《2 SNMP Exporter介绍》章节中的snmp.yml示例文件
  4. 根据snmp exporter镜像创建容器
    docker create --name snmp_to_117 -v /home/samroot/exporter/snmp/:/etc/snmp_exporter/ -p 9116:9116 prom/snmp-exporter
  5. 启动容器
    docker container start snmp_to_117
  6. 检查容器运行情况
    docker container ps|grep snmp
    输出结果参考
    f06810ffa6d6 prom/snmp-exporter "/bin/snmp_exporter …" 8 days ago Up 3 days 0.0.0.0:9116->9116/tcp snmp_to_117
  7. 防火墙放通9116端口
    firewall-cmd --permanent --add-port=9116/tcp

3.3 http访问snmp exporter,查看暴露出来的监控数据

  1. 浏览器访问snmp exporter的http端口,输入被监控设备的IP地址和模块名称,点击Submit

  1. SNMP Exporter已经从被监控设备的SNMP Agent中获取到一些设备的运行状态

4 将收集到的数据存到Prometheus中

在Prometheus的配置文件中添加SNMP Exporter的地址,即可将收集到的数据存到Prometheus中。

  1. 修改prometheus配置文件prometheus.yml
   # Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
   #采集snmp exporter监控数据
  - job_name: 'snmp'
    static_configs:
      - targets:
        - 192.168.1.117
    metrics_path: /snmp
    params:
      module: [Linux]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.1.200:9116
  1. 使配置文件生效
    curl -XPOST http://localhost:9090/-/reload
  2. 确认Prometheus能够正常采集到snmp exporter的数据

5 配合Grafana实现可视化

  1. 配置dashboard的的名称和host变量:

  2. 新建plane,配置CPU使用率监控

  3. 保存退出 

6 参考文章

什么是SNMP:https://support.huawei.com/enterprise/zh/doc/EDOC1100087025

SNMP_Exporter:https://github.com/prometheus/snmp_exporter