-
Notifications
You must be signed in to change notification settings - Fork 6
/
tap.go
295 lines (254 loc) · 6.35 KB
/
tap.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
//
// February 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
// Output node used to tap pipeline for troubleshooting
//
package main
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"os"
"time"
)
//
// Module implementing outputNodeModule interface.
type tapOutputModule struct {
name string
filename string
countOnly bool
rawDump bool
streamSpec *dataMsgStreamSpec
dataChannelDepth int
ctrlChan chan *ctrlMsg
dataChan chan dataMsg
}
func tapOutputModuleNew() outputNodeModule {
return &tapOutputModule{}
}
func (t *tapOutputModule) tapOutputFeederLoop() {
var stats msgStats
var hexOnly bool
//
// Period, in seconds, to dump stats if only counting.
const TIMEOUT = 10
timeout := make(chan bool, 1)
if !t.streamSpec.dataMsgStreamSpecTextBased() {
hexOnly = true
}
logctx := logger.WithFields(
log.Fields{
"name": t.name,
"filename": t.filename,
"countonly": t.countOnly,
"streamSpec": t.streamSpec,
})
// Prepare dump file for writing
f, err := os.OpenFile(t.filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND,
0660)
if err != nil {
logctx.WithError(err).Error("Tap failed to open dump file")
return
}
defer f.Close()
logctx.Info("Starting up tap")
if t.countOnly {
go func() {
time.Sleep(TIMEOUT * time.Second)
timeout <- true
}()
}
w := bufio.NewWriter(f)
for {
select {
case <-timeout:
go func() {
time.Sleep(TIMEOUT * time.Second)
timeout <- true
}()
w.WriteString(fmt.Sprintf(
"%s:%s: rxed msgs: %v\n",
t.name, time.Now().Local(), stats.MsgsOK))
w.Flush()
case msg, ok := <-t.dataChan:
if !ok {
// Channel has been closed. Our demise
// is near. SHUTDOWN is likely to be
t.dataChan = nil
continue
}
if t.countOnly {
stats.MsgsOK++
continue
}
dM := msg
description := dM.getDataMsgDescription()
if t.rawDump {
err, b := dM.produceByteStream(dataMsgStreamSpecDefault)
if err != nil {
logctx.WithError(err).WithFields(
log.Fields{
"msg": description,
}).Error("Tap failed to produce raw message")
continue
}
err, enc := dataMsgStreamTypeToEncoding(
dM.getDataMsgStreamType())
if err != nil {
logctx.WithError(err).WithFields(
log.Fields{
"msg": description,
}).Error("Tap failed to identify encoding")
continue
}
err, encst := encapSTFromEncoding(enc)
if err != nil {
logctx.WithError(err).WithFields(
log.Fields{
"msg": description,
}).Error("Tap failed to identify encap st")
continue
}
//
// We should really push this into co side of codec.
hdr := encapSTHdr{
MsgType: ENC_ST_HDR_MSG_TYPE_TELEMETRY_DATA,
MsgEncap: encst,
MsgHdrVersion: ENC_ST_HDR_VERSION,
Msgflag: ENC_ST_HDR_MSG_FLAGS_NONE,
Msglen: uint32(len(b)),
}
err = binary.Write(w, binary.BigEndian, hdr)
if err != nil {
logctx.WithError(err).WithFields(
log.Fields{
"msg": description,
}).Errorf("Tap failed to write binary hdr %+v", hdr)
continue
}
_, err = w.Write(b)
if err != nil {
logctx.WithError(err).WithFields(
log.Fields{
"msg": description,
}).Error("Tap failed to write binary message")
continue
}
continue
}
// OK. We're ready to dump something largely human readable.
errStreamType, b := dM.produceByteStream(t.streamSpec)
if errStreamType != nil {
err, b = dM.produceByteStream(dataMsgStreamSpecDefault)
if err != nil {
logctx.WithError(err).WithFields(
log.Fields{
"msg": description,
}).Error("Tap failed to dump message")
stats.MsgsNOK++
continue
}
} else if b == nil {
continue
}
stats.MsgsOK++
w.WriteString(fmt.Sprintf(
"\n------- %v -------\n", time.Now()))
w.WriteString(fmt.Sprintf("Summary: %s\n", description))
if hexOnly || errStreamType != nil {
if errStreamType != nil {
w.WriteString(fmt.Sprintf(
"Requested stream type failed: [%v]\n", errStreamType))
}
w.WriteString(hex.Dump(b))
} else {
var out bytes.Buffer
json.Indent(&out, b, "", " ")
w.WriteString(out.String())
}
w.Flush()
case msg := <-t.ctrlChan:
switch msg.id {
case REPORT:
content, _ := json.Marshal(stats)
resp := &ctrlMsg{
id: ACK,
content: content,
respChan: nil,
}
msg.respChan <- resp
case SHUTDOWN:
w.Flush()
logctx.Info("tap feeder loop, rxed SHUTDOWN")
//
// Dump detailed stats here
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
msg.respChan <- resp
return
default:
logctx.Error("tap feeder loop, unknown ctrl message")
}
}
}
}
//
// Setup a tap output module so we can see what is going on.
func (t *tapOutputModule) configure(name string, nc nodeConfig) (
error, chan<- dataMsg, chan<- *ctrlMsg) {
var err error
t.name = name
t.filename, err = nc.config.GetString(name, "file")
if err != nil {
return err, nil, nil
}
t.dataChannelDepth, err = nc.config.GetInt(name, "datachanneldepth")
if err != nil {
t.dataChannelDepth = DATACHANNELDEPTH
}
// If not set, will default to false, but let's be clear.
t.countOnly, _ = nc.config.GetBool(name, "countonly")
// Looking for a raw dump?
t.rawDump, _ = nc.config.GetBool(name, "raw")
if t.rawDump {
if t.countOnly {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"tap config: 'countonly' is incompatible with 'raw'")
return err, nil, nil
}
_, err = nc.config.GetString(name, "encoding")
if err == nil {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"tap config: 'encoding' is incompatible with 'raw'")
return err, nil, nil
}
}
nc.config.GetString(name, "encoding")
// Pick output stream type
err, t.streamSpec = dataMsgStreamSpecFromConfig(nc, name)
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"name": name,
}).Error("'encoding' option for tap output")
return err, nil, nil
}
//
// Setup control and data channels
t.ctrlChan = make(chan *ctrlMsg)
t.dataChan = make(chan dataMsg, t.dataChannelDepth)
go t.tapOutputFeederLoop()
return nil, t.dataChan, t.ctrlChan
}