Prometheus监控交换机

环境说明

默认已安装Prometheus服务,服务地址:192.168.56.200


一、获取交换机SNMP信息

参数
SNMP服务IP(交换机IP) 172.20.2.83
SNMP Community dfetest

二、部署 snmp_exporter

2.1 下载snmp_exporter

下载地址:https://github.com/prometheus/snmp_exporter/releases

下载完成后,上传至机器的 /usr/local 目录下,解压安装:

1
2
tar -zvxf snmp_exporter-0.15.0.linux-arm64.tar.gz
mv snmp_exporter-0.15.0.linux-arm64/ snmp_exporter

2.2 配置snmp_exporter

snmp_exporter 的配置文件需要通过 SNMP Exporter Config Generator 项目编译生成。

安装依赖

1
2
3
4
5
6
yum -y install git
yum -y install gcc gcc-g++ make net-snmp net-snmp-utils net-snmp-libs net-snmp-devel
go get github.com/prometheus/snmp_exporter/generator
cd ${GOPATH-$HOME/go}/src/github.com/prometheus/snmp_exporter/generator
go build
make mibs

修改generator.yml配置

1
vim generator.yml

modules.if_mib.auth字段配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
modules:
# Default IF-MIB interfaces table with ifIndex.
if_mib:
walk: [sysUpTime, interfaces, ifXTable]
version: 1
auth:
community: dfetest
lookups:
- source_indexes: [ifIndex]
lookup: ifAlias
- source_indexes: [ifIndex]
lookup: ifDescr
- source_indexes: [ifIndex]
# Use OID to avoid conflict with Netscaler NS-ROOT-MIB.
lookup: 1.3.6.1.2.1.31.1.1.1.1 # ifName
overrides:
ifAlias:
ignore: true # Lookup metric
ifDescr:
ignore: true # Lookup metric
ifName:
ignore: true # Lookup metric
ifType:
type: EnumAsInfo

生成snmp.yml

1
2
export MIBDIRS=mibs
./generator generate

将新生成的snmp.yml替换掉原snmp_exporter中的snmp.yml:

1
cp snmp.yml /usr/local/snmp_exporter

2.3 验证snmp_exporter

1
2
cd /usr/local/snmp_exporter
./snmp_exporter

snmp_exporter 服务端口在 9116,浏览器访问 http://192.168.56.200:9116,在 Target 输入框中填入交换机IP地址,点击 submit 按钮,验证数据是否正常采集。

2.4 设置开机自启

创建系统服务文件:

1
vim /etc/systemd/system/snmp_exporter.service
1
2
3
4
5
6
7
8
9
10
[Unit]
Description=node_exporter
After=network.target

[Service]
ExecStart=/usr/local/snmp_exporter/snmp_exporter
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动服务并设置开机自启:

1
2
3
systemctl daemon-reload
systemctl enable snmp_exporter
systemctl start snmp_exporter

三、修改Prometheus配置

进入Prometheus安装文件夹,打开配置文件:

1
2
cd /usr/local/prometheus
vim prometheus.yml

在scrape_configs标签下添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- job_name: 'snmp'
scrape_interval: 10s
static_configs:
- targets:
- 172.20.2.83 # 交换机IP地址
metrics_path: /snmp
# params:
# module: [if_mib]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.56.200:9116 # snmp_exporter 服务IP地址

完整prometheus.yml配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# my global config
global:
scrape_interval: 15s
evaluation_interval: 15s

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:

# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']

- job_name: 'Linux'
static_configs:
- targets: ['192.168.56.201:9100']
labels:
instance: Linux

- job_name: 'Windows'
static_configs:
- targets: ['192.168.56.1:9182']
labels:
instance: Windows

- job_name: 'snmp'
scrape_interval: 10s
static_configs:
- targets:
- 172.20.2.83 # 交换机IP地址
metrics_path: /snmp
# params:
# module: [if_mib]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.56.200:9116 # snmp_exporter 服务IP地址

重启Prometheus服务:

1
systemctl restart prometheus

浏览器访问 http://192.168.56.200:9090/targets 查看监控信息,确认snmp已加入。


四、配置Grafana

4.1 下载Dashboard

Grafana官方提供了很多dashboard页面。访问 https://grafana.com/grafana/dashboards 下载所需dashboard。

本文使用SNMP监控dashboard,Dashboard ID:10523

4.2 导入Dashboard

  1. 打开Grafana监控页面,进入dashboard管理页面
  2. 点击【Import】按钮
  3. 将Dashboard ID(10523)复制进去
  4. Grafana会自动识别,点击【change】按钮生成随机UID
  5. 在下方输入框选择Prometheus数据源
  6. 点击【Import】完成导入

导入成功后会自动打开Dashboard,即可看到交换机SNMP监控数据。


总结

组件 端口 说明
Prometheus 9090 监控平台
snmp_exporter 9116 SNMP数据采集
交换机 172.20.2.83 被监控目标

关键配置点:

  • generator.yml 中 community 需与交换机配置一致
  • prometheus.yml 中 relabel_configs 指向 snmp_exporter 地址
  • Grafana 通过 Dashboard ID 10523 导入SNMP监控面板