forked from k-sone/snmpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trapserver.go
62 lines (54 loc) · 1.16 KB
/
trapserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"log"
"os"
"github.com/k-sone/snmpgo"
)
type TrapListener struct {
logger *log.Logger
}
func (l *TrapListener) OnTRAP(trap *snmpgo.TrapRequest) {
// simple logging
if trap.Error == nil {
l.logger.Printf("%v %v", trap.Source, trap.Pdu)
} else {
l.logger.Printf("%v %v", trap.Source, trap.Error)
}
}
func NewTrapListener() *TrapListener {
return &TrapListener{logger: log.New(os.Stdout, "", log.LstdFlags)}
}
func main() {
server, err := snmpgo.NewTrapServer(snmpgo.ServerArguments{
LocalAddr: "127.0.0.1:162",
})
if err != nil {
log.Fatal(err)
}
// V2c
err = server.AddSecurity(&snmpgo.SecurityEntry{
Version: snmpgo.V2c,
Community: "public",
})
if err != nil {
log.Fatal(err)
}
// V3
err = server.AddSecurity(&snmpgo.SecurityEntry{
Version: snmpgo.V3,
UserName: "MyName",
SecurityLevel: snmpgo.AuthPriv,
AuthPassword: "aaaaaaaa",
AuthProtocol: snmpgo.Sha,
PrivPassword: "bbbbbbbb",
PrivProtocol: snmpgo.Aes,
SecurityEngineId: "8000000004736e6d70676f",
})
if err != nil {
log.Fatal(err)
}
err = server.Serve(NewTrapListener())
if err != nil {
log.Fatal(err)
}
}