Home > other >  Go the SNMP trap
Go the SNMP trap

Time:10-08

The source code is as follows:
Package the main

Import (
"FMT"
".net "
"Github.com/cdevr/WapSNMP
"//"time", "
)

Type the Trap struct {
The Address string
Version int16
The Community string
GeneralTrap int16
SpeicalTrap int16
EnterpriseId string
Interface Values map [string] {}
}

Func ParseUdp (data byte []) (the Trap, the error) {
The trap: trap={}
Seq, err:=wapsnmp DecodeSequence (data)
if err ! Nil={
FMT. Println (err)
Return the trap, err
}

Var community string
SnmpVer:=seq [1]. (int64)
If snmpVer<={
1SnmpVer++
}
The trap, Version=int16 (snmpVer)

If snmpVer & lt; 3 {
The community=seq [2]. (string)
}

Munity=trap.Com community

Var VRSP interface [] {}

The switch seq [3]. (type) {
Case wapsnmp. UnsupportedBerType:
VRSP, _=wapsnmp. DecodeSequence (seq [3]. (wapsnmp. UnsupportedBerType))
Default:
VRSP=seq [3]. ([] interface {})
}

Var varbinds interface [] {}

If snmpVer=={
1The trap. GeneralTrap=int16 (VRSP. [3] (int64))
The trap. SpeicalTrap=int16 (VRSP. [4] (int64))
Trap. EnterpriseId=VRSP. [1] (wapsnmp. Oid). The String ()
Trap. Address=VRSP. [2] (net. IP). The String ()
Varbinds=VRSP [6]. ([] interface {})
} else {
Varbinds=VRSP. [4] ([] interface {})
}

The trap. Values=make (map [string] interface {}, len (varbinds))

For I:=1; iVaroid:=varbinds [I] ([] interface {}). [1] (wapsnmp. Oid)
Result:=varbinds [I] ([] interface {}) [2]

The trap. Values [varoid. String ()]=result
}

Return the trap, nil
}

Func main () {
FMT. Println (" hello SnmpTrap!!!!!
")The socket, err:=net..listenudp (" udp4 ", & amp; Net. UDPAddr {
IP: net. IPv4,10,22 (192100),
Port: 162,
})
if err ! Nil={
Panic (err)
}
Defer the socket. The Close ()
FMT. Println (" + + + + + + + + + + + + + + + + + + + + + + + + ")
For {
FMT. Println (" + + + + + + + + + + + + + + + + + + + + + + + + ")
Buf:=make (byte [], 2048)
Read the from, _ :=socket. ReadFromUDP (buf [:])
FMT. Println (" Get MSG from ", from the IP)
Go HandleUdp (buf [: read])
}
}

Func HandleUdp (data byte []) {
The trap, err:=ParseUdp (data)
if err ! Nil={
FMT. Println (" Err, Err. Error ())
}
FMT. Println (trap) Version, trap.Com munity, trap, EnterpriseId, trap. Address)
For k, v:=range trap. Values {
FMT. Printf (" % s=% s \ n ", k, v);
}
}

Results:
GOROOT=C:/Go
GOPATH=D:/godata_zhaoyu
"C: \ Program Files \ (x86) JetBrains \ Gogland 163.10615.22 \ bin \ runnerw exe" C:/Go \ bin \ Go exe build -o "C: \ Users \ admin \ AppData \ Local \ Temp \ build test1. Go and rungo" D:/godata_zhaoyu/test/test1. Go
"C: \ Program Files \ (x86) JetBrains \ Gogland 163.10615.22 \ bin \ runnerw exe" "C: \ Users \ admin \ AppData \ Local \ Temp \ Build test1. Go and rungo"
Hello SnmpTrap!!!!!
+ + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + +

I'd like to receive by snmptrap switch alarm information, there are two questions to ask:
1. The program is blocked, because the socket waiting to have data is written to port 162? How to make data is written to port 162?
2. This program is snmptrap receiving, is warning message of receiving switches use upd cycle, but and not into the participation in the program to set the oid, is this how to return a responsibility, how should I set up to collect information of oid

Thank you all for the great god
  • Related