华为交换机
开启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"}