title: 2.17.监控华为交换机
order: 20.5
icon: lightbulb
一、环境
主机名 | IP地址 | 系统 | 说明 |
localhost | 192.168.11.60 | Ubuntu 20.04 | docker 安装的prometheus |
192.168.11.45 | Gns3模拟器 | 思科c3640 | |
192.168.11.46 | 华为ensp模拟器 | 华为s5700 |
1、环境准备
a、docker安装prometheus
安装docker(略)
安装docker-compose(略)
mkdir /data/
cd /data
git clone https://gitee.com/linge365/docker-prometheus.git
docker-compose up -d
b、下载模拟器
思科模拟器
华为ensp模拟器
提取密码:0i1y
c、获取mibs文件
- 如果不生成snmp.yaml文件,这步略过
华为官网获取mibs文件:
https://support.huawei.com/mibtoolweb/enterpriseMibInfo/zh
第三方github仓库下载mibs文件
https://github.com/netdisco/netdisco-mibs
https://github.com/pgmillon/observium/tree/master/mibs
https://github.com/librenms/librenms/tree/master/mibs
二、监控交换机
1、交换机上启用和配置 SNMP 服务
a、通过telnet登录交换机或者去机房插console线
b、华为交换机开启和配置snmp
<Huawei> sys #进入配置模式
[Huawei] snmp-agent community read public #配置交换机的读团体名为“public”
[Huawei] snmp-agent sys-info version all #配置SNMP版本 (all是所有版本)
[Huawei] snmp-agent trap enable #启用snmp陷阱,输入Y
c、思科交换机开启snmp
SW> enable #切换到特权模式
SW# show version
SW# conf t #进入全局配置模式
SW(config)# snmp-server community public ro #打开交换机snmp服务,设置团体名称为public,只读
SW(config)# snmp-server enable traps snmp #启用snmp陷阱
SW(config)# exit
SW# show snmp
d、prometheus服务器上检查
sudo apt-get install snmp
#返回交换机所有接口
snmpwalk -v2c -c public 192.168.11.45 1.3.6.1.2.1.31.1.1.1.1
snmpwalk -v2c -c public 192.168.11.46 1.3.6.1.2.1.31.1.1.1.1
有数据返回表示正常
2、生成基础snmp.yml文件
snmp_exporter的配置文件snmp.yml需要自己通过Generator 项目编译生成
编译安装generator(二选一)
二进制安装golang(需要编译Generator 需要golang)
#下载
wget https://studygolang.com/dl/golang/go1.19.7.linux-amd64.tar.gz
#解压
tar -xf go1.19.7.linux-amd64.tar.gz -C /opt
#创建环境变量配置
cat >> /etc/profile << "EOF"
export PATH=$PATH:/opt/go/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
EOF
#使配置生效
source /etc/profile
# 查看go版本
go version
编译安装generator
mkdir /opt/generator
cd /opt/generator
sudo apt-get install unzip build-essential libsnmp-dev # Debian-based distros
git clone https://github.com/prometheus/snmp_exporter.git
cd snmp_exporter/generator
make generator
#检查
ls -l generator
创建generator.yml文件
cat >generator.yml<<"EOF"
modules:
huawei_mib: #mib名称根据实际修改,要和prometheus的配置一致
walk: #遍历OID列表,也可以是SNMP对象名称或特定实例
- sysUpTime
- interfaces
- ifXTable
- sysDescr
- sysName
version: 2 #snmp版本,需要看交换机开启了那个版本,华为交换机一般都是开启2c,如果是3的版本需要添加认证
auth:
community: public #团体字,根据实际修改。
lookups:
- source_indexes: [ifIndex]
lookup: ifAlias # ifAlias 表示网络设备接口的别名
- source_indexes: [ifIndex]
lookup: 1.3.6.1.2.1.2.2.1.2 # ifDescr 表示网络设备接口的描述
- source_indexes: [ifIndex]
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
EOF
下载mibs文件
wget https://github.com/librenms/librenms/archive/refs/heads/master.zip
unzip master.zip
mv librenms-master/mibs .
生成snmp.yml文件
export MIBDIRS=mibs #指定mibs文件夹在当前目录
./generator generate #生成snmp.yml
docker安装generator(和编译安装二选一)
创建generator.yml文件
mkdir /opt/generator
cd /opt/generator
cat >generator.yml<<"EOF"
modules:
huawei_mib: #mib名称根据实际修改,要和prometheus的配置一致
walk: #遍历OID列表,也可以是SNMP对象名称或特定实例
- sysUpTime
- interfaces
- ifXTable
- sysDescr
- sysName
version: 2 #snmp版本,需要看交换机开启了那个版本,华为交换机一般都是开启2c,如果是3的版本需要添加认证
auth:
community: public #团体字,根据实际修改。
lookups:
- source_indexes: [ifIndex]
lookup: ifAlias # ifAlias 表示网络设备接口的别名
- source_indexes: [ifIndex]
lookup: 1.3.6.1.2.1.2.2.1.2 # ifDescr 表示网络设备接口的描述
- source_indexes: [ifIndex]
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
EOF
配置解释:
模块名称 huawei_mib 通过名字可以知道是作用华为设备
walk 中, 是需要获取的指标. 其中前6个是共有mib获取到的,后面是私有mib获取到的.
version snmp协议是v2
auth 定义的团体名
looksups 通过索引查找列表
overrides 前三个指标删除,后面几项是定义了他们的数据类型.
下载mibs文件
cd /opt/generator
wget https://github.com/librenms/librenms/archive/refs/heads/master.zip
unzip master.zip
mv librenms-master/mibs/ .
docker运行generator生成snmp.yml文件
docker run --rm -v "${PWD}:/opt/" prom/snmp-generator generate
#会在当前目录下生成snmp.yml文件
3、生成监控cpu,内存的snmp.yml文件
获取oid
华为官网获取
https://support.huawei.com/mibtoolweb/enterpriseMibInfo/zh
创建generator.yml文件
cat >generator.yml<<"EOF"
modules:
huawei_mib:
walk:
- 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.5 #huawei CPU使用率,取值范围:2~100。hwEntityCpuUsage
- 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7 #huawei 内存使用率。hwEntityMemUsage
- 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.11 #huawei 交换机温度。 hwEntityTemperature
- 1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7 #huawei 风扇运行。 hwEntityFanState
EOF
docker运行generator
来生成snmp.yml文件
mv mibs/huawei/* mibs/
docker run --rm -v "${PWD}:/opt/" prom/snmp-generator generate
#会在当前目录下生成snmp.yml文件
会得到只有cpu、内存、风扇的snmp.yaml文件,如下:
huawei_mib:
walk:
- 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.11
- 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.5
- 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7
- 1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7
metrics:
- name: hwEntityTemperature
oid: 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.11
type: gauge
help: The temperature for the entity - 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.11
indexes:
- labelname: entPhysicalIndex
type: gauge
- name: hwEntityCpuUsage
oid: 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.5
type: gauge
help: The CPU usage for this entity - 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.5
indexes:
- labelname: entPhysicalIndex
type: gauge
- name: hwEntityMemUsage
oid: 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7
type: gauge
help: The memory usage for the entity - 1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7
indexes:
- labelname: entPhysicalIndex
type: gauge
- name: hwEntityFanState
oid: 1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7
type: gauge
help: This object indicates the state of the fan. - 1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7
indexes:
- labelname: hwEntityFanSlot
type: gauge
- labelname: hwEntityFanSn
type: gauge
enum_values:
1: normal
2: abnormal
和基础snmp.yaml合并
把合并好的snmp.yml文件,给snmp_exporter做配置文件。(完成)
4、二进制安装snmp_exporter(二选一)
https://github.com/prometheus/snmp_exporter/releases/
安装snmp_exporter
wget https://github.com/prometheus/snmp_exporter/releases/download/v0.20.0/snmp_exporter-0.20.0.linux-amd64.tar.gz
tar xf snmp_exporter-0.20.0.linux-amd64.tar.gz
mv snmp_exporter-0.20.0.linux-amd64 /opt/snmp_exporter
#备份原配置
cp /opt/snmp_exporter/snmp.yml{,.bak}
#把通过grenerator生成的snmp.yml配置移动到/opt/snmp_exporter/下面
mv snmp.yml /opt/snmp_exporter/snmp.yml
创建用户
useradd -M -s /usr/sbin/nologin prometheus
更改exporter文件夹权限
chown prometheus:prometheus -R /opt/snmp_exporter
创建systemd
cat > /etc/systemd/system/snmp_exporter.service << "EOF"
[Unit]
Description=Prometheus SNMP Exporter
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
Type=simple
ExecStart=/opt/snmp_exporter/snmp_exporter --config.file /opt/snmp_exporter/snmp.yml
[Install]
WantedBy=multi-user.target
EOF
启动
systemctl daemon-reload
systemctl start snmp_exporter.service
加入到开机自启动
systemctl enable snmp_exporter.service
检查
systemctl status snmp_exporter.service
启动不了检查日志
journalctl -u snmp_exporter -f
5、docker安装snmp_exporter(二选一)
使用现有的snmp.yml文件
#拉取已经写好的docker-compose.yaml文件
git clone https://gitee.com/linge365/docker-compose.git
cd docker-compose/snmp_exporter
启动
docker-compose up -d
6、添加prometheus配置
prometheus配置如下
- job_name: 'snmp'
scrape_timeout: 30s # 采集超时10s
scrape_interval: 1m # 采集频率1m
static_configs:
- targets:
- 192.168.11.45 #交换机ip,根据实际修改
- 192.168.11.46 #交换机ip,根据实际修改
metrics_path: /snmp
params:
module: [if_mib] #mib名称根据实际修改
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.11.60:9116
snmp.yml中的模块名要和prometheus配置的要一致:如下图:
重载prometheus
curl -X POST http://localhost:9090/-/reload
web检查
没有问题
7、SNMP的监控指标
指标名称 | 指标描述 | OID |
sysDescr | 系统版本描述 | 1.3.6.1.2.1.1.1 |
sysUpTime | 启动时间 | 1.3.6.1.2.1.1.3 |
sysName | 交换机系统名 | 1.3.6.1.2.1.1.5 |
ifIndex | 接口序号 | 1.3.6.1.2.1.2.2.1.1 |
ifName | 接口名 | 1.3.6.1.2.1.31.1.1.1.1 |
ifAdminStatus | 接口状态 | 1.3.6.1.2.1.2.2.1.7 |
ifHCOutOctets | 接口发出的字节数总计 | 1.3.6.1.2.1.31.1.1.1.10 |
ifHCInOctets | 接口收到的字节数总计 | 1.3.6.1.2.1.31.1.1.1.6 |
ifInBroadcastPkts | 接口收到的广播包数量统计 | 1.3.6.1.2.1.31.1.1.1.3 |
ifInErrors | 流入流量异常数统计 | 1.3.6.1.2.1.2.2.1.14 |
ifSpeed | 接口当前带宽[bps] | 1.3.6.1.2.1.2.2.1.5 |
ifMtu | MTU配置值 | 1.3.6.1.2.1.2.2.1.4 |
ifOutDiscards | 出方向流量丢包总计 | 1.3.6.1.2.1.2.2.1.19 |
ifHighSpeed | 接口背板带宽,单位为M bits per second | 1.3.6.1.2.1.31.1.1.1.15 |
ifInDiscards | 接口流入流量丢包数总计 | 1.3.6.1.2.1.2.2.1.13 |
ifInMulticastPkts | 接口收到的多播包统计 | 1.3.6.1.2.1.31.1.1.1.2 |
ifInUnknownProtos | 接口收到的未知协议包数总计 | 1.3.6.1.2.1.2.2.1.15 |
ifOutMulticastPkts | 发出多播包统计 | 1.3.6.1.2.1.31.1.1.1.4 |
ifCounterDiscontinuityTime | 1.3.6.1.2.1.31.1.1.1.19 | |
ifConnectorPresent | 1.3.6.1.2.1.31.1.1.1.17 | |
ifPromiscuousMode | 1.3.6.1.2.1.31.1.1.1.16 | |
ifLinkUpDownTrapEnable | 1.3.6.1.2.1.31.1.1.1.14 | |
ifHCOutBroadcastPkts | 1.3.6.1.2.1.31.1.1.1.13 | |
ifHCOutMulticastPkts | 1.3.6.1.2.1.31.1.1.1.12 | |
ifHCOutUcastPkts | 1.3.6.1.2.1.31.1.1.1.11 | |
ifHCInBroadcastPkts | 1.3.6.1.2.1.31.1.1.1.9 | |
ifHCInMulticastPkts | 1.3.6.1.2.1.31.1.1.1.8 | |
ifHCInUcastPkts | 1.3.6.1.2.1.31.1.1.1.7 | |
ifOutBroadcastPkts | 1.3.6.1.2.1.31.1.1.1.5 | |
ifSpecific | 1.3.6.1.2.1.2.2.1.22 | |
ifOutQLen | 1.3.6.1.2.1.2.2.1.21 | |
ifOutErrors | 1.3.6.1.2.1.2.2.1.20 | |
ifOutNUcastPkts | 1.3.6.1.2.1.2.2.1.18 | |
ifInNUcastPkts | 1.3.6.1.2.1.2.2.1.12 | |
ifLastChange | 1.3.6.1.2.1.2.2.1.9 | |
IfNumber | 网络接口的数目 | 1.3.6.1.2.1.2.1 |
ifDescr | 网络接口信息描述 | 1.3.6.1.2.1.2.2.1.2 |
ifType | 网络接口类型 | 1.3.6.1.2.1.2.2.1.3 |
ifPhysAddress | 接口的物理地址 | 1.3.6.1.2.1.2.2.1.6 |
ifOperStatus | 接口当前操作状态[up/down] | 1.3.6.1.2.1.2.2.1.8 |
ifInOctets | 接口收到的字节数 | 1.3.6.1.2.1.2.2.1.10 |
ifOutOctets | 接口发送的字节数 | 1.3.6.1.2.1.2.2.1.16 |
ifInUcastPkts | 接口收到的数据包个数 | 1.3.6.1.2.1.2.2.1.11 |
ifOutUcastPkts | 接口发送的数据包个数 | 1.3.6.1.2.1.2.2.1.17 |
8、配置告警规则
ifHCOutOctets 接口发出的字节数总计。上行
ifHCInOctets 接口收到的字节数总计,下行
ifOperStatus 接口状态
使用cat创建告警规则
cd /data/docker-prometheus
cat >> prometheus/rules/snmp.yml <<"EOF"
groups:
- name: snmp
rules:
- alert: ifHCOut
expr: sum by (instance) (rate(ifHCOutOctets[2m])) / 1024 / 1024 > 100
for: 5m
labels:
severity: warning
annotations:
summary: "交换机异常流入网络吞吐量,实例:{{ $labels.instance }}"
description: "交换机网络流入流量 > 100 MB/s,当前值:{{ $value }}"
- alert: ifHCIn
expr: sum by (instance) (rate(ifHCInOctets[2m])) / 1024 / 1024 > 100
for: 5m
labels:
severity: warning
annotations:
summary: "交换机异常流出网络吞吐量,实例:{{ $labels.instance }}"
description: "交换机网络流出流量 > 100 MB/s,当前值为:{{ $value }}"
EOF
检查
http://192.168.11.60:9090/alerts?search=
9、grafana添加Dashboard
id:
11169
或
1124
id为11169
图形展示如下图:
评论区