Monitoring Network Switches with Prometheus and SNMP Exporter
This guide walks through collecting metrics from a network switch via SNMP, exposing them with the Prometheus SNMP Exporter, scraping from Prometheus (server at 192.168.56.200), and visualizing in Grafana.
- Switch SNMP agent: 172.20.2.83
- SNMP community (v2c): dfetest
1. Verify SNMP access to the switch
Confirm basic SNMP connectivity from the Prometheus host:
# On the Prometheus server
snmpwalk -v2c -c dfetest 172.20.2.83 sysUpTime.0
If the command returns a sysUpTime value, the agent is reachable and the community is corrrect.
2. Install the SNMP Exporter
Download and place the exporter under /usr/local. Adjust the architecture and version as needed.
# Example for Linux amd64
export VER=0.25.0
cd /usr/local
curl -LO https://github.com/prometheus/snmp_exporter/releases/download/v${VER}/snmp_exporter-${VER}.linux-amd64.tar.gz
# Extract and standardize the directory name
tar -xzf snmp_exporter-${VER}.linux-amd64.tar.gz
rm -f snmp_exporter-${VER}.linux-amd64.tar.gz
mv snmp_exporter-${VER}.linux-amd64 snmp_exporter
3. Generate an snmp.yml using the Config Generator
The SNMP Exporter relies on an snmp.yml generated from MIBs and module definitions. Build the generator and produce the configuration.
3.1 Install build prerequisites
yum -y install git gcc gcc-c++ make net-snmp net-snmp-utils net-snmp-libs net-snmp-devel
3.2 Build the generator and MIBs
# Ensure Go is installed and GOPATH is set (Go 1.18+)
# Build the generator
GO111MODULE=on go get github.com/prometheus/snmp_exporter/generator
cd ${GOPATH:-$HOME/go}/src/github.com/prometheus/snmp_exporter/generator
go build
make mibs
3.3 Configure generator.yml
Edit generator.yml to include a module for standard interface counters using the device’s community string. If your device only suports SNMPv1, set version: 1 accordingly.
# generator.yml (excerpt)
modules:
if_mib:
walk: [sysUpTime, interfaces, ifXTable]
version: 2
auth:
community: dfetest
lookups:
- source_indexes: [ifIndex]
lookup: ifAlias
- source_indexes: [ifIndex]
lookup: ifDescr
- source_indexes: [ifIndex]
lookup: 1.3.6.1.2.1.31.1.1.1.1 # ifName
overrides:
ifAlias: { ignore: true }
ifDescr: { ignore: true }
ifName: { ignore: true }
ifType: { type: EnumAsInfo }
3.4 Generate snmp.yml
# From the generator directory
export MIBDIRS=mibs
./generator generate
# Deploy to the exporter directory
cp -f snmp.yml /usr/local/snmp_exporter/
4. Run and validate the SNMP Exporter
Start the exporter to serve metrics on port 9116.
cd /usr/local/snmp_exporter
./snmp_exporter --web.listen-address=":9116" --config.file="/usr/local/snmp_exporter/snmp.yml"
Open http://192.168.56.200:9116 in a browser, enter 172.20.2.83 into the Target field, submit, and confirm metrics are returned.
5. Set up a systemd service for the SNMP Exporter
Create a unit file to manage the exporter as a service.
cat >/etc/systemd/system/snmp_exporter.service <<'UNIT'
[Unit]
Description=Prometheus SNMP Exporter
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/snmp_exporter/snmp_exporter --web.listen-address=:9116 --config.file=/usr/local/snmp_exporter/snmp.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable snmp_exporter
systemctl start snmp_exporter
6. Configure Prometheus to scrape the switch via the exporter
Edit /usr/local/prometheus/prmoetheus.yml and add an SNMP scrape job. The relabel rules pass the device IP as the target parameter to the exporter and replace the scrape address with the exporter’s listener.
# /usr/local/prometheus/prometheus.yml (full example)
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: []
rule_files: []
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-switch
scrape_interval: 10s
metrics_path: /snmp
static_configs:
- targets:
- 172.20.2.83 # Switch IP
# Uncomment to restrict to a specific generator module
# 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 host:port
Reload Prometheus:
systemctl restart prometheus
Visit http://192.168.56.200:9090/targets and verify the snmp-switch job is up.
7. Import a Grafana dashboard
- Open Grafana and go to Dashboards → Import.
- Enter dashboard ID 10523 (SNMP monitoring dashboard from grafana.com).
- Click Change to generate a unique UID if prompted.
- Choose the Prometheus data source used above.
- Click Import and open the dashboard to view the SNMP metrics for 172.20.2.83.