forked from Juniper/jtimon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drops.go
112 lines (97 loc) · 2.87 KB
/
drops.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
/*
* Copyright (c) 2018, Juniper Networks, Inc.
* All rights reserved.
*/
package main
import (
"bufio"
"fmt"
"strconv"
"strings"
na_pb "github.com/Juniper/jtimon/telemetry"
)
type dropData struct {
seq uint64
received uint64
drop uint64
}
func dropInit(jctx *JCtx) {
jctx.dMap = make(map[uint32]map[uint32]map[string]dropData)
}
func dropCheckCSV(jctx *JCtx) {
if !jctx.config.Log.CSVStats {
return
}
f := jctx.config.Log.FileHandle
if jctx.config.Log.FileHandle == nil {
return
}
if _, err := f.Seek(0, 0); err != nil {
return
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "sensor-path,sequence-number,component-id,sub-component-id,packet-size,p-ts,e-ts") {
tokens := strings.Split(line, ",")
//fmt.Printf("\n%s + %s + %s + %s + %s + %s + %s", tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6])
cid, _ := strconv.ParseUint(tokens[2], 10, 32)
scid, _ := strconv.ParseUint(tokens[3], 10, 32)
seqNum, _ := strconv.ParseUint(tokens[1], 10, 32)
dropCheckWork(jctx, uint32(cid), uint32(scid), tokens[0], seqNum)
}
}
}
func dropCheckWork(jctx *JCtx, cid uint32, scid uint32, path string, seq uint64) {
var last dropData
var new dropData
var ok bool
_, ok = jctx.dMap[cid]
if !ok {
jctx.dMap[cid] = make(map[uint32]map[string]dropData)
}
_, ok = jctx.dMap[cid][scid]
if !ok {
jctx.dMap[cid][scid] = make(map[string]dropData)
}
last, ok = jctx.dMap[cid][scid][path]
if !ok {
new.seq = seq
new.received = 1
new.drop = 0
jctx.dMap[cid][scid][path] = new
} else {
new.seq = seq
new.received = last.received + 1
new.drop = last.drop
if seq > last.seq && seq-last.seq != 1 {
fmt.Printf("Packet Drop: path: %-120v cid: %-5v scid: %v seq: %v-%v=%v\n", path, cid, scid, seq, last.seq, seq-last.seq)
new.drop += (seq - last.seq)
}
jctx.dMap[cid][scid][path] = new
}
}
func dropCheck(jctx *JCtx, ocData *na_pb.OpenConfigData) {
dropCheckWork(jctx, ocData.ComponentId, ocData.SubComponentId, ocData.Path, ocData.SequenceNumber)
}
func printDropDS(jctx *JCtx) {
jctx.stats.Lock()
s := fmt.Sprintf("\nDrops Distribution for %s:%d", jctx.config.Host, jctx.config.Port)
s += fmt.Sprintf("\n+----+-----+-------+----------+%s+", strings.Repeat("-", 121))
s += fmt.Sprintf("\n| CID |SCID| Drops | Received | %-120s|", "Sensor Path")
s += fmt.Sprintf("\n+----+-----+-------+----------+%s+", strings.Repeat("-", 121))
s += fmt.Sprintf("\n")
for cid, sdMap := range jctx.dMap {
for scid, pathM := range sdMap {
for path, dData := range pathM {
if path != "" {
s += fmt.Sprintf("|%5v|%4v| %6v| %8v | %-120s| \n", cid, scid, dData.drop, dData.received, path)
jctx.stats.totalDdrops += dData.drop
}
}
}
}
s += fmt.Sprintf("+----+-----+-------+----------+%s+", strings.Repeat("-", 121))
jLog(jctx, s)
jctx.stats.Unlock()
}