-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
291 lines (251 loc) · 6.57 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
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <[email protected]>
*
* 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 (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"sync"
"time"
"github.com/gravwell/gravwell/v3/ingest"
"github.com/gravwell/gravwell/v3/ingest/entry"
"github.com/gravwell/gravwell/v3/ingest/log"
"github.com/gravwell/gravwell/v3/ingesters/utils"
"github.com/gravwell/gravwell/v3/ingesters/version"
)
const (
defaultConfigLoc = `/opt/gravwell/etc/macosLog.conf`
ingesterName = `macosLog`
PERIOD = time.Second
READ_PERIOD = time.Second
)
var (
confLoc = flag.String("config-file", defaultConfigLoc, "Location for configuration file")
stderrOverride = flag.String("stderr", "", "Redirect stderr to a shared memory file")
ver = flag.Bool("version", false, "Print the version information and exit")
lg *log.Logger
igst *ingest.IngestMuxer
)
func init() {
flag.Parse()
if *ver {
version.PrintVersion(os.Stdout)
ingest.PrintVersion(os.Stdout)
os.Exit(0)
}
var fp string
var err error
if *stderrOverride != `` {
fp = filepath.Join(`/dev/shm/`, *stderrOverride)
}
cb := func(w io.Writer) {
version.PrintVersion(w)
ingest.PrintVersion(w)
}
if lg, err = log.NewStderrLoggerEx(fp, cb); err != nil {
fmt.Fprintf(os.Stderr, "Failed to get stderr logger: %v\n", err)
os.Exit(-1)
}
}
func main() {
debug.SetTraceback("all")
// config setup
cfg, err := GetConfig(*confLoc)
if err != nil {
lg.FatalCode(0, "Failed to get configuration: %v\n", err)
return
}
if len(cfg.Global.Log_File) > 0 {
fout, err := os.OpenFile(cfg.Global.Log_File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
lg.FatalCode(0, "Failed to open log file %s: %v", cfg.Global.Log_File, err)
}
if err = lg.AddWriter(fout); err != nil {
lg.Fatal("Failed to add a writer: %v", err)
}
if len(cfg.Global.Log_Level) > 0 {
if err = lg.SetLevelString(cfg.Global.Log_Level); err != nil {
lg.FatalCode(0, "Invalid Log Level \"%s\": %v", cfg.Global.Log_Level, err)
}
}
}
tag := cfg.Global.Tag_Name
conns, err := cfg.Global.Targets()
if err != nil {
lg.FatalCode(0, "Failed to get backend targets from configuration: %v\n", err)
return
}
lmt, err := cfg.Global.RateLimit()
if err != nil {
lg.FatalCode(0, "Failed to get rate limit from configuration: %v\n", err)
return
}
// create ingest connection(s)
id, ok := cfg.Global.IngesterUUID()
if !ok {
lg.FatalCode(0, "Couldn't read ingester UUID\n")
}
igCfg := ingest.UniformMuxerConfig{
IngestStreamConfig: cfg.Global.IngestStreamConfig,
Destinations: conns,
Tags: []string{tag},
Auth: cfg.Global.Secret(),
LogLevel: cfg.Global.LogLevel(),
VerifyCert: !cfg.Global.InsecureSkipTLSVerification(),
IngesterName: ingesterName,
IngesterVersion: version.GetVersion(),
IngesterUUID: id.String(),
IngesterLabel: cfg.Global.Label,
RateLimitBps: lmt,
Logger: lg,
CacheDepth: cfg.Global.Cache_Depth,
CachePath: cfg.Global.Ingest_Cache_Path,
CacheSize: cfg.Global.Max_Ingest_Cache,
CacheMode: cfg.Global.Cache_Mode,
LogSourceOverride: net.ParseIP(cfg.Global.Log_Source_Override),
}
igst, err = ingest.NewUniformMuxer(igCfg)
if err != nil {
lg.Fatal("Failed build our ingest system: %v\n", err)
return
}
defer igst.Close()
if err := igst.Start(); err != nil {
lg.Fatal("Failed start our ingest system: %v\n", err)
return
}
if err := igst.WaitForHot(cfg.Global.Timeout()); err != nil {
lg.FatalCode(0, "Timedout waiting for backend connections: %v\n", err)
return
}
// prepare the configuration we're going to send upstream
err = igst.SetRawConfiguration(cfg)
if err != nil {
lg.FatalCode(0, "Failed to set configuration for ingester state messages\n")
}
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
var src net.IP
if cfg.Global.Source_Override != `` {
// global override
src = net.ParseIP(cfg.Global.Source_Override)
if src == nil {
lg.FatalCode(0, "Global Source-Override is invalid")
}
}
t, err := igst.GetTag(cfg.Global.Tag_Name)
if err != nil {
lg.Fatal("Failed to resolve tag \"%s\": %v\n", cfg.Global.Tag_Name, err)
}
go run(t, src, &wg, ctx)
// listen for signals so we can close gracefully
utils.WaitForQuit()
cancel()
if err := igst.Sync(time.Second); err != nil {
lg.Error("Failed to sync: %v\n", err)
}
if err := igst.Close(); err != nil {
lg.Error("Failed to close: %v\n", err)
}
}
func run(tag entry.EntryTag, src net.IP, wg *sync.WaitGroup, ctx context.Context) {
for {
cmd := exec.Command("log", "stream", "--style=json")
out, err := cmd.StdoutPipe()
if err != nil {
lg.Fatal("Failed to get stdoutpipe: %v\n", err)
}
err = cmd.Start()
if err != nil {
lg.Error("Failed to start log: %v\n", err)
time.Sleep(PERIOD)
continue
}
for {
ents, err := decode(out)
if err != nil {
lg.Error("Failed to decode: %v\n", err)
break
}
for _, v := range ents {
v.SRC = src
v.TS = entry.Now()
v.Tag = tag
}
if err = igst.WriteBatchContext(ctx, ents); err != nil {
if err == context.Canceled {
return
}
lg.Error("Sending message: %v", err)
}
}
cmd.Process.Kill()
}
}
var buf []byte
var first = true
func decode(r io.Reader) ([]*entry.Entry, error) {
if first {
b := make([]byte, 1024)
for {
n, err := r.Read(b)
if err != nil {
return nil, err
}
if n > 0 {
buf = append(buf, b[:n]...)
}
if len(buf) >= 3 {
// pop off the leading [{\n
buf = buf[3:]
first = false
break
}
time.Sleep(READ_PERIOD)
}
}
var ents []*entry.Entry
for {
b := make([]byte, 1024)
n, err := r.Read(b)
if err != nil {
return nil, err
}
buf = append(buf, b[:n]...)
e := bytes.Split(buf, []byte("\n},{\n"))
if len(e) <= 1 {
time.Sleep(READ_PERIOD)
continue
}
// consume all but the last piece
for i := 0; i < len(e)-1; i++ {
d := []byte{'{'}
d = append(d, e[i]...)
d = append(d, '}')
var o bytes.Buffer
err := json.Compact(&o, d)
if err != nil {
return nil, err
}
ents = append(ents, &entry.Entry{
Data: o.Bytes(),
})
}
buf = e[len(e)-1]
break
}
return ents, nil
}