forked from influxdata/kapacitor
/
snmptraptest.go
117 lines (102 loc) · 1.84 KB
/
snmptraptest.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package snmptraptest
import (
"sync"
"github.com/k-sone/snmpgo"
)
type Server struct {
traps []Trap
Addr string
Community string
closed bool
mu sync.Mutex
wg sync.WaitGroup
srv *snmpgo.TrapServer
}
func NewServer() (*Server, error) {
addr := "127.0.0.1:9162"
srv, err := snmpgo.NewTrapServer(snmpgo.ServerArguments{
LocalAddr: addr,
})
if err != nil {
return nil, err
}
s := &Server{
srv: srv,
Addr: addr,
Community: "public",
}
s.srv.AddSecurity(&snmpgo.SecurityEntry{
Version: snmpgo.V2c,
Community: s.Community,
})
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.srv.Serve(s)
}()
return s, nil
}
func (s *Server) Traps() []Trap {
s.mu.Lock()
defer s.mu.Unlock()
return s.traps
}
func (s *Server) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return nil
}
s.closed = true
err := s.srv.Close()
s.wg.Wait()
return err
}
func (s *Server) OnTRAP(trap *snmpgo.TrapRequest) {
s.mu.Lock()
defer s.mu.Unlock()
t := Trap{
Pdu: convertPdu(trap.Pdu),
Error: trap.Error,
}
s.traps = append(s.traps, t)
}
type Trap struct {
Pdu Pdu
Error error
}
type Pdu struct {
IsNil bool
Type snmpgo.PduType
ErrorStatus snmpgo.ErrorStatus
ErrorIndex int
VarBinds VarBinds
}
func convertPdu(pdu snmpgo.Pdu) Pdu {
if pdu == nil {
return Pdu{IsNil: true}
}
return Pdu{
Type: pdu.PduType(),
ErrorStatus: pdu.ErrorStatus(),
ErrorIndex: pdu.ErrorIndex(),
VarBinds: convertVarBinds(pdu.VarBinds()),
}
}
type VarBinds []VarBind
type VarBind struct {
Oid string
Value string
Type string
}
func convertVarBinds(vbs snmpgo.VarBinds) VarBinds {
binds := make(VarBinds, len(vbs))
for i, vb := range vbs {
binds[i] = VarBind{
Oid: vb.Oid.String(),
Value: vb.Variable.String(),
Type: vb.Variable.Type(),
}
}
return binds
}