-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
316 lines (287 loc) · 7.2 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*************************************************************************
* Copyright 2018 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 (
"flag"
"fmt"
"io"
"os"
"runtime"
"runtime/debug"
"time"
"github.com/gravwell/gravwell/v3/ingest"
"github.com/gravwell/gravwell/v3/ingest/entry"
"github.com/gravwell/gravwell/v3/ingesters/args"
"github.com/gravwell/gravwell/v3/ingesters/utils"
"github.com/gravwell/gravwell/v3/ingesters/version"
"github.com/google/gopacket"
pcap "github.com/google/gopacket/pcapgo"
)
const (
throwHintSize uint64 = 1024 * 1024 * 4
throwBlockSize int = 4096
)
var (
pcapFile = flag.String("pcap-file", "", "Path to the pcap file")
tsOverride = flag.Bool("ts-override", false, "Override the timestamps and start them at now")
simIngest = flag.Bool("no-ingest", false, "Do not ingest the packets, just read the pcap file")
srcOvr = flag.String("source-override", "", "Override source with address, hash, or integer")
ver = flag.Bool("version", false, "Print the version information and exit")
pktCount uint64
pktSize uint64
simulate bool
)
func init() {
flag.Parse()
if *ver {
version.PrintVersion(os.Stdout)
ingest.PrintVersion(os.Stdout)
os.Exit(0)
}
if *pcapFile == `` {
fmt.Printf("A PCAP file is required\n")
os.Exit(-1)
}
simulate = *simIngest
}
func main() {
debug.SetTraceback("all")
a, err := args.Parse()
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid arguments: %v\n", err)
return
}
//get a handle on the pcap file
ph, err := newPacketReader(*pcapFile, 16*1024*1024)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open pcap file: %v\n", err)
return
}
defer ph.Close()
//fire up the ingesters
igCfg := ingest.UniformMuxerConfig{
Destinations: a.Conns,
Tags: a.Tags,
Auth: a.IngestSecret,
PublicKey: a.TLSPublicKey,
PrivateKey: a.TLSPrivateKey,
LogLevel: `INFO`,
}
igst, err := ingest.NewUniformMuxer(igCfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed build our ingest system: %v\n", err)
return
}
if err := igst.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Failed start our ingest system: %v\n", err)
return
}
if err := igst.WaitForHot(a.Timeout); err != nil {
fmt.Fprintf(os.Stderr, "Timedout waiting for backend connections: %v\n", err)
return
}
//get the TagID for our default tag
tag, err := igst.GetTag(a.Tags[0])
if err != nil {
fmt.Printf("Failed to look up tag %s: %v\n", a.Tags[0], err)
os.Exit(-1)
}
entChan := make(chan []*entry.Entry, 64)
errChan := make(chan error, 1)
start := time.Now()
if !simulate {
go readPackets(ph, igst, tag, entChan, errChan)
mainLoop:
for {
select {
case err, ok := <-errChan:
if !ok {
break mainLoop
}
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
break mainLoop
case blk, ok := <-entChan:
if !ok || len(blk) == 0 {
break mainLoop
}
for i := range blk {
pktCount++
pktSize += uint64(len(blk[i].Data))
}
if err := igst.WriteBatch(blk); err != nil {
fmt.Println("failed to write entry", err)
break mainLoop
}
}
}
} else {
if err := simulatePacketRead(ph); err != nil {
fmt.Println("failed to Simulate packet read", err)
}
}
if err := igst.Sync(10 * time.Second); err != nil {
fmt.Fprintf(os.Stderr, "Failed to sync: %v\n", err)
}
if err := igst.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to close ingester: %v\n", err)
}
dur := time.Since(start)
fmt.Printf("Completed in %v (%s)\n", dur, ingest.HumanSize(pktSize))
fmt.Printf("Total Count: %s\n", ingest.HumanCount(pktCount))
fmt.Printf("Entry Rate: %s\n", ingest.HumanEntryRate(pktCount, dur))
fmt.Printf("Ingest Rate: %s\n", ingest.HumanRate(pktSize, dur))
}
func readPackets(hnd *packetHandle, igst *ingest.IngestMuxer, tag entry.EntryTag, entChan chan []*entry.Entry, errChan chan error) {
//get the src
src, err := igst.SourceIP()
if err != nil {
errChan <- err
return
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var sec int64
var lSize uint64
var ts entry.Timestamp
var first bool
var base entry.Timestamp
var diff time.Duration
var dt []byte
var ci gopacket.CaptureInfo
var blk []*entry.Entry
//get packet src
for {
if dt, ci, err = hnd.ReadPacketData(); err != nil {
if err == io.EOF {
err = nil
}
break
}
ts = entry.FromStandard(ci.Timestamp)
if !first {
first = true
base = ts
diff = entry.Now().Sub(base)
if *tsOverride {
fmt.Println("First packet starts at", ts.Add(diff).Format(time.RFC3339))
} else {
fmt.Println("First packet starts at", ts.Format(time.RFC3339))
}
}
if *tsOverride {
ts = ts.Add(diff)
}
//check if we should throw
if sec != ts.Sec || len(blk) >= throwBlockSize || lSize >= throwHintSize {
if len(blk) > 0 {
entChan <- blk
blk = nil
lSize = 0
}
}
blk = append(blk, &entry.Entry{
TS: ts,
SRC: src,
Tag: tag,
Data: dt,
})
lSize += uint64(len(dt))
sec = ts.Sec
}
if len(blk) > 0 {
entChan <- blk
}
close(entChan)
close(errChan)
fmt.Println("Last packet at", ts.Format(time.RFC3339))
}
func simulatePacketRead(hnd *packetHandle) (err error) {
var ts entry.Timestamp
var first bool
var base entry.Timestamp
var diff time.Duration
runtime.LockOSThread()
defer runtime.UnlockOSThread()
//set the bpf filter
var dt []byte
var ci gopacket.CaptureInfo
for {
if dt, ci, err = hnd.ReadPacketData(); err != nil {
if err == io.EOF {
err = nil
}
break
}
ts = entry.FromStandard(ci.Timestamp)
if !first {
first = true
base = ts
diff = entry.Now().Sub(base)
if *tsOverride {
fmt.Println("First packet starts at", ts.Add(diff).Format(time.RFC3339))
} else {
fmt.Println("First packet starts at", ts.Format(time.RFC3339))
}
}
if *tsOverride {
ts = ts.Add(diff)
}
pktSize += uint64(len(dt))
pktCount++
}
fmt.Println("Last packet at", ts.Format(time.RFC3339))
return
}
type packetHandle struct {
fi io.ReadCloser
ngMode bool
hnd *pcap.Reader
nghnd *pcap.NgReader
}
func newPacketReader(pth string, buff int) (ph *packetHandle, err error) {
var fi io.ReadCloser
var hnd *pcap.Reader
var nghnd *pcap.NgReader
if fi, err = utils.OpenBufferedFileReader(pth, buff); err != nil {
return
}
//attempt to open a standard reader
if hnd, err = pcap.NewReader(fi); err == nil {
ph = &packetHandle{
fi: fi,
hnd: hnd,
}
return
}
//failed, retry as an ng reader
if err = fi.Close(); err != nil {
return
} else if fi, err = utils.OpenBufferedFileReader(pth, buff); err != nil {
return
}
if nghnd, err = pcap.NewNgReader(fi, pcap.NgReaderOptions{}); err != nil {
return
}
ph = &packetHandle{
fi: fi,
nghnd: nghnd,
ngMode: true,
}
return
}
func (ph *packetHandle) Close() error {
return ph.fi.Close()
}
func (ph *packetHandle) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {
if ph.ngMode {
data, ci, err = ph.nghnd.ReadPacketData()
} else {
data, ci, err = ph.hnd.ReadPacketData()
}
return
}