linux 系统配置接收华为交换机trap

该博客介绍了如何配置华为交换机开启SNMP,并设置社区字符串和目标主机,结合Ubuntu服务器上的snmptrapd进行网络监控。当交换机接口状态变化时,通过脚本解析SNMP trap信息,利用MQTT发布到消息队列,实现网络状态的实时同步和报警。整个流程包括交换机配置、服务器端snmptrapd的安装与配置、脚本编写、MQTT服务的启动以及消息监听。
摘要由CSDN通过智能技术生成

华为交换机

开启snmp

system-view

snmp-agent

snmp-agent community read somgl@123

snmp-agent community write somgl@123

snmp-agent sys-info version all

snmp-agent target-host inform address udp-domain 10.10.3.180 udp-port 161 params securityname somgl@123 v2c

snmp-agent target-host trap address udp-domain 10.10.3.180 udp-port 162 params securityname somgl@123 v2c

snmp-agent trap enable

y

quit

save

y
 

ubuntu16.04服务器

1.安装软件

sudo apt-get install snmptrapd

2.配置

vim /etc/snmp/snmptrapd.conf


authCommunity log,execute,net public
traphandle IF-MIB::linkDown         /monitor/notification.sh
traphandle IF-MIB::linkUp         /monitor/notification.sh

3.脚本
vim /monitor/notification.sh

 
#!/bin/sh
 
read blank
read ip
switch_ip=`echo $ip | awk -F '[' '{print $2}' | awk -F ']' '{print $1}'`

while read oid val
do
if [ "$oid" = "SNMPv2-MIB::snmpTrapOID.0" ];then
    if_status=`echo $val | awk -F"link" '{print $2}'`
fi
if echo $oid | grep ifIndex;then
    if_name=`echo $val`
fi
done
 
if [ $if_status = "Up" ];then
 msg=`echo "OK! Switch($switch_ip) -- $if_name -- $if_status"`
else
 msg=`echo "Critical! Switch($switch_ip) -- $if_name -- $if_status"`
fi

date=`date +%Y-%m-%d%t%H:%M:%S`

data=`echo '{"date":"'$date'","server_ip":"'$switch_ip'","device_index":"'$if_name'","device_status":"'$if_status'"}'`

mosquitto_pub -h localhost -p 1883 -u test -P 123456 -t /nic/sync/res -m "$data"

4.查看端口
lsof -i :162

5.启动
snmptrapd -d -f -Lo

或者使用配置后台启动
snmptrapd -c /etc/snmp/snmptrapd.conf

6.启动mqtt
sudo netstat -antup | grep 1883

sudo mosquitto -c /etc/mosquitto/mosquitto.conf -d


7.监听消息
mosquitto_sub -h localhost -p 1883 -u test -P 123456  -t /#

8.然后拔插交换机上的网线


9.消息返回结果
{"date":"2022-06-15 16:22:06","server_ip":"10.10.3.70","device_index":"10","device_status":"Down"}
{"date":"2022-06-15 16:22:07","server_ip":"10.10.3.75","device_index":"6","device_status":"Up"}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

somgl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值