-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
202 lines (182 loc) · 5.78 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*************************************************************************
* Copyright 2023 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"sync"
"time"
"github.com/gosnmp/gosnmp"
"github.com/gravwell/gravwell/v4/ingest/entry"
"github.com/gravwell/gravwell/v4/ingest/log"
"github.com/gravwell/gravwell/v4/ingest/processors"
"github.com/gravwell/gravwell/v4/ingesters/base"
"github.com/gravwell/gravwell/v4/ingesters/utils"
)
const (
defaultConfigLoc = `/opt/gravwell/etc/snmp.conf`
defaultConfigDLoc = `/opt/gravwell/etc/snmp.conf.d`
ingesterName = `SNMP Ingester`
appName = `snmp`
testTimeout time.Duration = 3 * time.Second
)
// TrapRecord gets built up from the received trap,
// then encoded to JSON for ingest.
type TrapRecord struct {
Sender string
Community string `json:",omitempty"`
ContextEngineID string `json:",omitempty"`
ContextName string `json:",omitempty"`
TrapOID string `json:",omitempty"`
Variables []SnmpVariable
}
// SnmpVariable represents one OID->value mapping
// from the trap. A trap may contain multiple
// variables.
type SnmpVariable struct {
OID string
Value interface{}
Type gosnmp.Asn1BER
TypeString string
}
func main() {
var wg sync.WaitGroup
var cfg *cfgType
ibc := base.IngesterBaseConfig{
IngesterName: ingesterName,
AppName: appName,
DefaultConfigLocation: defaultConfigLoc,
DefaultConfigOverlayLocation: defaultConfigDLoc,
GetConfigFunc: GetConfig,
}
ib, err := base.Init(ibc)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get configuration %v\n", err)
return
} else if err = ib.AssignConfig(&cfg); err != nil || cfg == nil {
fmt.Fprintf(os.Stderr, "failed to assign configuration %v %v\n", err, cfg == nil)
return
}
igst, err := ib.GetMuxer()
if err != nil {
ib.Logger.FatalCode(0, "failed to get ingest connection", log.KVErr(err))
return
}
defer igst.Close()
ib.AnnounceStartup()
exitCtx, exitFn := context.WithCancel(context.Background())
var traps []*gosnmp.TrapListener
for name, lcfg := range cfg.Listener {
var tag entry.EntryTag
var proc *processors.ProcessorSet
if tag, err = igst.GetTag(lcfg.Tag_Name); err != nil {
ib.Logger.FatalCode(0, "failed to get established tag",
log.KV("tag", lcfg.Tag_Name),
log.KV("listener", name), log.KVErr(err))
} else if proc, err = cfg.Preprocessor.ProcessorSet(igst, lcfg.Preprocessor); err != nil {
ib.Logger.FatalCode(0, "preprocessor failure",
log.KV("listener", name), log.KVErr(err))
}
l := gosnmp.NewTrapListener()
l.Params = &gosnmp.GoSNMP{
Transport: "udp",
Version: lcfg.getSnmpVersion(),
Timeout: time.Duration(2) * time.Second,
Retries: 3,
ExponentialTimeout: true,
MaxOids: gosnmp.MaxOids,
Community: lcfg.Community,
//Logger: gosnmp.NewLogger(glog.New(os.Stdout, "", 0)),
}
if l.Params.Version == gosnmp.Version3 {
l.Params.SecurityParameters = &gosnmp.UsmSecurityParameters{
UserName: lcfg.Username,
AuthenticationProtocol: lcfg.getAuthProto(),
AuthenticationPassphrase: lcfg.Auth_Passphrase,
PrivacyProtocol: lcfg.getPrivacyProto(),
PrivacyPassphrase: lcfg.Privacy_Passphrase,
//Logger: gosnmp.NewLogger(glog.New(os.Stdout, "", 0)),
}
l.Params.MsgFlags = lcfg.getMsgFlags()
l.Params.SecurityModel = gosnmp.UserSecurityModel
}
traps = append(traps, l)
// Set up the callback
cb := func(s *gosnmp.SnmpPacket, u *net.UDPAddr) {
if s == nil || u == nil {
return
}
if l.Params.Version == gosnmp.Version3 && 0x3&s.MsgFlags != 0x3&l.Params.MsgFlags {
ib.Logger.Warn("dropping trap due to invalid msgflags",
log.KV("received-flags", 0x3&s.MsgFlags),
log.KV("expected-flags", 0x3&l.Params.MsgFlags),
log.KV("client", u.IP.String()))
return
} else if l.Params.Version == gosnmp.Version2c && l.Params.Community != "" && s.Community != l.Params.Community {
ib.Logger.Warn("dropping trap due to invalid community",
log.KV("received-community", s.Community),
log.KV("received-community", l.Params.Community),
log.KV("client", u.IP.String()))
return
}
ent := entry.Entry{
TS: entry.Now(),
SRC: u.IP,
Tag: tag,
}
r := TrapRecord{
Sender: u.IP.String(),
Community: s.Community,
}
for i := range s.Variables {
if s.Variables[i].Name == ".1.3.6.1.6.3.1.1.4.1.0" {
r.TrapOID, _ = s.Variables[i].Value.(string)
}
v := SnmpVariable{
OID: s.Variables[i].Name,
Value: s.Variables[i].Value,
Type: s.Variables[i].Type,
TypeString: s.Variables[i].Type.String(),
}
r.Variables = append(r.Variables, v)
}
var err error
if ent.Data, err = json.Marshal(r); err != nil {
// Skip it, I guess
return
}
proc.ProcessContext(&ent, exitCtx)
}
l.OnNewTrap = cb
wg.Add(1)
go func(bind string) {
defer wg.Done()
l.Listen(bind)
}(lcfg.Bind_String)
}
ib.Debug("Running\n")
//listen for signals so we can close gracefully
utils.WaitForQuit()
ib.AnnounceShutdown()
exitFn()
// Close all the traps so goroutines exit
for i := range traps {
traps[i].Close()
}
// wait for graceful shutdown
wg.Wait()
if err := igst.Sync(time.Second); err != nil {
ib.Logger.Error("failed to sync", log.KVErr(err))
}
if err := igst.Close(); err != nil {
ib.Logger.Error("failed to close", log.KVErr(err))
}
}