-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
264 lines (240 loc) · 6.67 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
/*************************************************************************
* Copyright 2017 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"
"log"
"net"
"os"
"path/filepath"
"runtime/debug"
"time"
"github.com/gravwell/gravwell/v3/ingest"
"github.com/gravwell/gravwell/v3/ingest/config"
"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"
)
const (
initBuffSize = 4 * 1024 * 1024
maxBuffSize = 128 * 1024 * 1024
)
var (
inFile = flag.String("i", "", "Input file to process (specify - for stdin)")
ver = flag.Bool("version", false, "Print version and exit")
verbose = flag.Bool("v", false, "Print every step")
status = flag.Bool("status", false, "Output ingest rate stats as we go")
srcOvr = flag.String("source-override", "", "Override source with address, hash, or integer")
fmtF = flag.String("import-format", "", "Set the import file format manually")
tagOvr = flag.String("tag-override", "", "Override the import file tags")
rebaseTime = flag.Bool("rebase-timestamp", false, "Rewrite timestamps so the most recent entry is at the current time. (Warning: may be slow with large files!)")
noEvs = flag.Bool("no-evs", false, "Do not include enumerated values in imported data")
nlBytes = []byte("\n")
count uint64
totalBytes uint64
dur time.Duration
srcOverride net.IP
timeDelta time.Duration // if we're rebasing, this is the adjustment added to each entry's TS
format string
)
func init() {
flag.Parse()
if *ver {
version.PrintVersion(os.Stdout)
ingest.PrintVersion(os.Stdout)
os.Exit(0)
}
}
func main() {
debug.SetTraceback("all")
if *inFile == "" {
log.Fatal("Input file path required")
}
a, err := args.Parse()
if err != nil {
log.Fatalf("Invalid arguments: %v\n", err)
}
if len(a.Tags) != 1 {
log.Fatal("File oneshot only accepts a single tag")
}
if *srcOvr != `` {
if srcOverride, err = config.ParseSource(*srcOvr); err != nil {
log.Fatalf("Invalid source override")
}
}
if *rebaseTime && *inFile == `-` {
log.Fatal("Cannot rebase time when reading from stdin!")
}
if format, err = utils.GetImportFormat(*fmtF, *inFile); err != nil {
log.Fatalf("%v, please set -import-format", err)
}
//fire up a uniform muxer
igst, err := ingest.NewUniformIngestMuxer(a.Conns, a.Tags, a.IngestSecret, a.TLSPublicKey, a.TLSPrivateKey, "")
if err != nil {
log.Fatalf("Failed to create new ingest muxer: %v\n", err)
}
if err := igst.Start(); err != nil {
log.Fatalf("Failed to start ingest muxer: %v\n", err)
}
if err := igst.WaitForHot(a.Timeout); err != nil {
log.Fatalf("Failed to wait for hot connection: %v\n", err)
}
if len(a.Conns) > 0 {
//sleep so that all connections can get a crack at negotiating tags
time.Sleep(500 * time.Millisecond)
}
//get a handle on the input file with a wrapped decompressor if needed
var fin io.ReadCloser
if *inFile == "-" {
fin = os.Stdin
} else {
fin, err = utils.OpenBufferedFileReader(*inFile, 8192)
if err != nil {
log.Fatalf("Failed to open %s: %v\n", *inFile, err)
}
if format == `` {
format = filepath.Ext(*inFile)
}
}
var ir utils.ReimportReader
ir, err = utils.GetImportReader(format, fin, utils.NewIngestTagHandler(igst))
if err != nil {
igst.Close()
log.Fatal(err)
}
if *noEvs {
ir.DisableEVs()
}
if *rebaseTime {
var newest *entry.Entry
var ent *entry.Entry
for {
if ent, err = ir.ReadEntry(); err != nil {
if err == io.EOF {
err = nil
}
break
}
if newest == nil || ent.TS.After(newest.TS) {
newest = ent
}
}
if err != nil {
log.Fatalf("Couldn't read full input file: %v", err)
}
timeDelta = time.Now().Sub(newest.TS.StandardTime())
fmt.Printf("timeDelta = %v\n", timeDelta)
// Now reset the reader
fin, err = utils.OpenBufferedFileReader(*inFile, 8192)
if err != nil {
log.Fatalf("Failed to open %s: %v\n", *inFile, err)
}
ir, err = utils.GetImportReader(format, fin, utils.NewIngestTagHandler(igst))
if err != nil {
igst.Close()
log.Fatal(err)
}
}
if *tagOvr != `` {
tag, err := igst.NegotiateTag(*tagOvr)
if err != nil {
igst.Close()
log.Fatalf("Failed to negotiate the override tag %s: %v", *tagOvr, err)
}
ir.OverrideTags(tag)
}
//go ingest the file
if err := doIngest(ir, igst); err != nil {
log.Fatalf("Failed to ingest file: %v\n", err)
}
if err = igst.Sync(a.Timeout); err != nil {
log.Fatalf("Failed to sync ingest muxer: %v\n", err)
}
if err := igst.Close(); err != nil {
log.Fatalf("Failed to close the ingest muxer: %v\n", err)
}
if err := fin.Close(); err != nil {
log.Fatalf("Failed to close the input file: %v\n", err)
}
fmt.Printf("Completed in %v (%s)\n", dur, ingest.HumanSize(totalBytes))
fmt.Printf("Total Count: %s\n", ingest.HumanCount(count))
fmt.Printf("Entry Rate: %s\n", ingest.HumanEntryRate(count, dur))
fmt.Printf("Ingest Rate: %s\n", ingest.HumanRate(totalBytes, dur))
}
func doIngest(ir utils.ReimportReader, igst *ingest.IngestMuxer) (err error) {
//if not doing regular updates, just fire it off
if !*status {
err = doImport(ir, igst)
return
}
errCh := make(chan error, 1)
tckr := time.NewTicker(time.Second)
defer tckr.Stop()
go func(ch chan error) {
ch <- doImport(ir, igst)
}(errCh)
loop:
for {
lastts := time.Now()
lastcnt := count
lastsz := totalBytes
select {
case err = <-errCh:
fmt.Println("\nDONE")
break loop
case _ = <-tckr.C:
dur := time.Since(lastts)
cnt := count - lastcnt
bts := totalBytes - lastsz
fmt.Printf("\r%s %s ",
ingest.HumanEntryRate(cnt, dur),
ingest.HumanRate(bts, dur))
}
}
return
}
func doImport(ir utils.ReimportReader, igst *ingest.IngestMuxer) (err error) {
var ent *entry.Entry
src := srcOverride
if src == nil {
if src, err = igst.SourceIP(); err != nil {
return
}
}
start := time.Now()
for {
if ent, err = ir.ReadEntry(); err != nil {
if err == io.EOF {
err = nil
}
break
}
if srcOverride != nil {
ent.SRC = srcOverride
}
if ent.SRC == nil {
ent.SRC = src
}
if timeDelta != 0 {
ent.TS = ent.TS.Add(timeDelta)
}
if err = igst.WriteEntry(ent); err != nil {
break
}
if *verbose {
fmt.Println(ent.TS, ent.Tag, ent.SRC, string(ent.Data))
}
count++
totalBytes += uint64(len(ent.Data))
}
dur = time.Since(start)
return
}