-
Notifications
You must be signed in to change notification settings - Fork 10
/
statshandler.go
316 lines (274 loc) · 8.95 KB
/
statshandler.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
package main
import (
"fmt"
"log"
"os"
"sync"
"time"
gnmi_ext1 "github.com/Juniper/jtimon/gnmi/gnmi_ext"
gnmi_juniper_header_ext "github.com/Juniper/jtimon/gnmi/gnmi_juniper_header_ext"
gnmi_pb "github.com/Juniper/jtimon/gnmi/gnmi"
na_pb "github.com/Juniper/jtimon/telemetry"
proto "github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"google.golang.org/grpc/stats"
)
type statsCtx struct {
sync.Mutex // guarding following stats
startTime time.Time
totalIn uint64
totalKV uint64
totalInPayloadLength uint64
totalInPayloadWireLength uint64
totalInHeaderWireLength uint64
}
type kpiStats struct {
SensorName string
Path string
Streamed_path string
Component string
SequenceNumber uint64
ComponentId uint32
SubComponentId uint32
Timestamp uint64
notif_timestamp int64
re_stream_creation_timestamp uint64
re_payload_get_timestamp uint64
}
type statshandler struct {
jctx *JCtx
}
func (h *statshandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
return ctx
}
func (h *statshandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return ctx
}
func (h *statshandler) HandleConn(ctx context.Context, s stats.ConnStats) {
switch s.(type) {
case *stats.ConnBegin:
case *stats.ConnEnd:
default:
}
}
func (h *statshandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
h.jctx.stats.Lock()
defer h.jctx.stats.Unlock()
switch s.(type) {
case *stats.InHeader:
h.jctx.stats.totalInHeaderWireLength += uint64(s.(*stats.InHeader).WireLength)
case *stats.OutHeader:
case *stats.OutPayload:
case *stats.InPayload:
h.jctx.stats.totalInPayloadLength += uint64(s.(*stats.InPayload).Length)
h.jctx.stats.totalInPayloadWireLength += uint64(s.(*stats.InPayload).WireLength)
if *statsHandler && h.jctx.config.InternalJtimon.CsvLog != "" {
switch v := (s.(*stats.InPayload).Payload).(type) {
case *na_pb.OpenConfigData:
updateStats(h.jctx, v, false)
for idx, kv := range v.Kv {
updateStatsKV(h.jctx, false, 0)
switch kvvalue := kv.Value.(type) {
case *na_pb.KeyValue_UintValue:
if kv.Key == "__timestamp__" {
var re_c_ts uint64 = 0
var re_p_get_ts uint64 = 0
if len(v.Kv) > idx+2 {
nextKV := v.Kv[idx+1]
if nextKV.Key == "__junos_re_stream_creation_timestamp__" {
re_c_ts = nextKV.GetUintValue()
}
nextnextKV := v.Kv[idx+2]
if nextnextKV.Key == "__junos_re_payload_get_timestamp__" {
re_p_get_ts = nextnextKV.GetUintValue()
}
}
//"sensor-path", "sequence-number", "component-id", "sub-component-id", "packet-size", "p-ts", "e-ts", "re-stream-creation-ts", "re-payload-get-ts"))
h.jctx.config.InternalJtimon.csvLogger.Printf(
fmt.Sprintf("%s,%d,%d,%d,%d,%d,%d,%d,%d\n",
v.Path, v.SequenceNumber, v.ComponentId, v.SubComponentId, s.(*stats.InPayload).Length, v.Timestamp, kvvalue.UintValue, re_c_ts, re_p_get_ts))
}
}
}
case *gnmi_pb.SubscribeResponse:
stat := h.getKPIStats(v)
if stat != nil && stat.Timestamp != 0 {
path := stat.SensorName + ":" + stat.Streamed_path + ":" + stat.Path + ":" + stat.Component
h.jctx.config.InternalJtimon.csvLogger.Printf(
fmt.Sprintf("%s,%d,%d,%d,%d,%d,%d,%d,%d\n",
path, stat.SequenceNumber, stat.ComponentId, stat.SubComponentId,
s.(*stats.InPayload).Length, stat.notif_timestamp, int64(stat.Timestamp*uint64(1000000)),
int64(stat.re_stream_creation_timestamp*uint64(1000000)),
int64(stat.re_payload_get_timestamp*uint64(1000000)),
),
)
}
}
}
case *stats.InTrailer:
case *stats.End:
default:
}
}
func (h *statshandler) getKPIStats(subResponse *gnmi_pb.SubscribeResponse) *kpiStats {
var jHdrPresent bool
stats := new(kpiStats)
notfn := subResponse.GetUpdate()
if notfn == nil {
return nil
}
stats.notif_timestamp = notfn.Timestamp
extns := subResponse.GetExtension()
if extns != nil {
var extIds []gnmi_ext1.ExtensionID
for _, ext := range extns {
regExtn := ext.GetRegisteredExt()
if (regExtn.GetId()) != gnmi_ext1.ExtensionID_EID_JUNIPER_TELEMETRY_HEADER {
extIds = append(extIds, regExtn.GetId())
continue
}
jHdrPresent = true
var hdr gnmi_juniper_header_ext.GnmiJuniperTelemetryHeaderExtension
msg := regExtn.GetMsg()
err := proto.Unmarshal(msg, &hdr)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
stats.ComponentId = hdr.ComponentId
stats.SequenceNumber = hdr.SequenceNumber
stats.Path = hdr.SubscribedPath
stats.SubComponentId = hdr.SubComponentId
stats.Component = hdr.Component
stats.Streamed_path = hdr.StreamedPath
stats.SensorName = hdr.SensorName
if hdr.ExportTimestamp > 0 {
stats.Timestamp = uint64(hdr.ExportTimestamp)
}
if hdr.PayloadGetTimestamp > 0 {
stats.re_payload_get_timestamp = uint64(hdr.PayloadGetTimestamp)
}
if hdr.StreamCreationTimestamp > 0 {
stats.re_stream_creation_timestamp = uint64(hdr.StreamCreationTimestamp)
}
break
}
if !jHdrPresent {
jLog(h.jctx, fmt.Sprintf(
"Juniper header extension not present, available extensions: %v", extIds))
}
}
return stats
}
func updateStats(jctx *JCtx, ocData *na_pb.OpenConfigData, needLock bool) {
if !*statsHandler {
return
}
if needLock {
jctx.stats.Lock()
defer jctx.stats.Unlock()
}
jctx.stats.totalIn++
}
func updateStatsKV(jctx *JCtx, needLock bool, count uint64) {
if !*statsHandler {
return
}
if needLock {
jctx.stats.Lock()
defer jctx.stats.Unlock()
}
jctx.stats.totalKV = jctx.stats.totalKV + count
}
func periodicStats(jctx *JCtx) {
if !*statsHandler {
return
}
pstats := jctx.config.Log.PeriodicStats
if pstats == 0 {
return
}
headerCounter := 0
for {
tickChan := time.NewTicker(time.Second * time.Duration(pstats)).C
<-tickChan
// Do nothing if we haven't heard back anything from the device
jctx.stats.Lock()
if jctx.stats.totalIn == 0 {
jctx.stats.Unlock()
continue
}
s := fmt.Sprintf("\n")
// print header
if headerCounter%100 == 0 {
s += "+------------------------------+--------------------+--------------------+--------------------+--------------------+\n"
s += "| Timestamp | KV | Packets | Bytes | Bytes(wire) |\n"
s += "+------------------------------+--------------------+--------------------+--------------------+--------------------+\n"
}
s += fmt.Sprintf("| %s | %18v | %18v | %18v | %18v |\n", time.Now().Format(time.UnixDate),
jctx.stats.totalKV,
jctx.stats.totalIn,
jctx.stats.totalInPayloadLength,
jctx.stats.totalInPayloadWireLength)
jctx.stats.Unlock()
headerCounter++
if s != "" {
jLog(jctx, fmt.Sprintf("%s\n", s))
}
}
}
func printSummary(jctx *JCtx) {
if !*statsHandler {
return
}
endTime := time.Since(jctx.stats.startTime)
s := fmt.Sprintf("\nCollector Stats for %s:%d (Run time : %s)\n", jctx.config.Host, jctx.config.Port, endTime)
s += fmt.Sprintf("%-12v : in-packets\n", jctx.stats.totalIn)
s += fmt.Sprintf("%-12v : data points (KV pairs)\n", jctx.stats.totalKV)
s += fmt.Sprintf("%-12v : in-header wirelength (bytes)\n", jctx.stats.totalInHeaderWireLength)
s += fmt.Sprintf("%-12v : in-payload length (bytes)\n", jctx.stats.totalInPayloadLength)
s += fmt.Sprintf("%-12v : in-payload wirelength (bytes)\n", jctx.stats.totalInPayloadWireLength)
if uint64(endTime.Seconds()) != 0 {
s += fmt.Sprintf("%-12v : throughput (bytes per seconds)\n", jctx.stats.totalInPayloadLength/uint64(endTime.Seconds()))
}
s += fmt.Sprintf("\n")
jLog(jctx, fmt.Sprintf("\n%s\n", s))
// Print Summary to terminal for internal Jitmon
if jctx.config.InternalJtimon.CsvLog != "" {
fmt.Println(s)
}
}
func isCsvStatsEnabled(jctx *JCtx) bool {
if *statsHandler && jctx.config.InternalJtimon.CsvLog != "" {
return true
}
return false
}
func csvStatsLogInit(jctx *JCtx) {
if !*statsHandler && jctx.config.InternalJtimon.CsvLog == "" {
return
}
var out *os.File
var err error
csvStatsFile := "csv-stats.csv"
if jctx.config.InternalJtimon.CsvLog == "" {
jctx.config.InternalJtimon.CsvLog = csvStatsFile
}
out, err = os.OpenFile(jctx.config.InternalJtimon.CsvLog, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
log.Printf("Could not create csv stats file(%s): %v\n", csvStatsFile, err)
}
if out != nil {
flags := 0
jctx.config.InternalJtimon.csvLogger = log.New(out, "", flags)
jctx.config.InternalJtimon.csvOut = out
log.Printf("Writing stats in %s for %s:%d [in csv format]\n",
jctx.config.InternalJtimon.CsvLog, jctx.config.Host, jctx.config.Port)
}
}
func csvStatsLogStop(jctx *JCtx) {
if jctx.config.InternalJtimon.csvOut != nil {
jctx.config.InternalJtimon.csvOut.Close()
jctx.config.InternalJtimon.csvOut = nil
jctx.config.InternalJtimon.csvLogger = nil
}
}