Skip to content

Commit cd0fed1

Browse files
authored
feature/cli refactor (#120)
* refactor ui codebase * add -P for specifying which policy to view
1 parent 3bccb19 commit cd0fed1

File tree

8 files changed

+794
-650
lines changed

8 files changed

+794
-650
lines changed

golang/cmd/pktvisor-cli/main.go

+17-547
Large diffs are not rendered by default.

golang/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module pktvisor
33
go 1.15
44

55
require (
6-
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
76
github.com/jroimartin/gocui v0.4.0
7+
github.com/mitchellh/mapstructure v1.4.2
88
github.com/nsf/termbox-go v0.0.0-20201124104050-ed494de23a00 // indirect
99
github.com/pkg/errors v0.9.1
1010
)

golang/go.sum

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
2-
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
31
github.com/jroimartin/gocui v0.4.0 h1:52jnalstgmc25FmtGcWqa0tcbMEWS6RpFLsOIO+I+E8=
42
github.com/jroimartin/gocui v0.4.0/go.mod h1:7i7bbj99OgFHzo7kB2zPb8pXLqMBSQegY7azfqXMkyY=
53
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
64
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
7-
github.com/ns1/pktvisor v3.0.7+incompatible h1:WIzyYHobkxJN0/9y5DmeNOxJVbOsvtejS9XPG++RpJs=
5+
github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7pjMO+rqo=
6+
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
87
github.com/nsf/termbox-go v0.0.0-20201124104050-ed494de23a00 h1:Rl8NelBe+n7SuLbJyw13ho7CGWUt2BjGGKIoreCWQ/c=
98
github.com/nsf/termbox-go v0.0.0-20201124104050-ed494de23a00/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
109
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

golang/internal/ui/dns.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
package ui
6+
7+
import "github.com/jroimartin/gocui"
8+
9+
func (u *ui) doDNSView() error {
10+
11+
maxX, _ := u.gui.Size()
12+
13+
//viewsWidth := 15
14+
viewsHeight := 7
15+
tableHeight := 8
16+
tableWidth := (maxX / 4) - 1
17+
row1Y := viewsHeight + 1
18+
row2Y := row1Y + tableHeight + 1
19+
row3Y := row2Y + tableHeight + 1
20+
//row4Y := row3Y + tableHeight + 1
21+
midCol1 := 0
22+
midCol2 := midCol1 + tableWidth + 1
23+
midCol3 := midCol2 + tableWidth + 1
24+
midCol4 := midCol3 + tableWidth + 1
25+
26+
// row 1
27+
if v, err := u.gui.SetView("qname2", midCol1, row1Y, midCol1+tableWidth, row1Y+tableHeight); err != nil {
28+
if err != gocui.ErrUnknownView {
29+
return err
30+
}
31+
v.Title = "Top QName 2"
32+
}
33+
if v, err := u.gui.SetView("qname3", midCol2, row1Y, midCol2+tableWidth, row1Y+tableHeight); err != nil {
34+
if err != gocui.ErrUnknownView {
35+
return err
36+
}
37+
v.Title = "Top QName 3"
38+
}
39+
if v, err := u.gui.SetView("nx", midCol3, row1Y, midCol3+tableWidth, row1Y+tableHeight); err != nil {
40+
if err != gocui.ErrUnknownView {
41+
return err
42+
}
43+
v.Title = "Top NX"
44+
}
45+
if v, err := u.gui.SetView("slow_in", midCol4, row1Y, midCol4+tableWidth, row1Y+tableHeight); err != nil {
46+
if err != gocui.ErrUnknownView {
47+
return err
48+
}
49+
v.Title = "Slow In"
50+
}
51+
52+
// row 2
53+
if v, err := u.gui.SetView("qtype", midCol1, row2Y, midCol1+tableWidth, row2Y+tableHeight); err != nil {
54+
if err != gocui.ErrUnknownView {
55+
return err
56+
}
57+
v.Title = "Top QTypes"
58+
}
59+
if v, err := u.gui.SetView("rcode", midCol2, row2Y, midCol2+tableWidth, row2Y+tableHeight); err != nil {
60+
if err != gocui.ErrUnknownView {
61+
return err
62+
}
63+
v.Title = "Top RCodes"
64+
}
65+
if v, err := u.gui.SetView("srvfail", midCol3, row2Y, midCol3+tableWidth, row2Y+tableHeight); err != nil {
66+
if err != gocui.ErrUnknownView {
67+
return err
68+
}
69+
v.Title = "Top SRVFAILS"
70+
}
71+
72+
if v, err := u.gui.SetView("slow_out", midCol4, row2Y, midCol4+tableWidth, row2Y+tableHeight); err != nil {
73+
if err != gocui.ErrUnknownView {
74+
return err
75+
}
76+
v.Title = "Slow Out"
77+
}
78+
79+
// row 3
80+
if v, err := u.gui.SetView("refused", midCol1, row3Y, midCol1+tableWidth, row3Y+tableHeight); err != nil {
81+
if err != gocui.ErrUnknownView {
82+
return err
83+
}
84+
v.Title = "Top REFUSED"
85+
}
86+
87+
if v, err := u.gui.SetView("top_udp_ports", midCol4, row3Y, midCol4+tableWidth, row3Y+tableHeight); err != nil {
88+
if err != gocui.ErrUnknownView {
89+
return err
90+
}
91+
v.Title = "Top DNS UDP Ports"
92+
}
93+
94+
return nil
95+
96+
}

golang/internal/ui/header.go

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
package ui
6+
7+
import (
8+
"fmt"
9+
"github.com/jroimartin/gocui"
10+
"pktvisor/pkg/client"
11+
"time"
12+
)
13+
14+
func (u *ui) updateHeader(v *gocui.View, window5m *client.StatSnapshot) {
15+
v.Clear()
16+
pcounts := window5m.Packets
17+
// there may be some unknown
18+
inOutDiff := pcounts.Total - (pcounts.In + pcounts.Out)
19+
_, _ = fmt.Fprintf(v, "Pkts %d | UDP %d (%3.1f%%) | TCP %d (%3.1f%%) | Other %d (%3.1f%%) | IPv4 %d (%3.1f%%) | IPv6 %d (%3.1f%%) | In %d (%3.1f%%) | Out %d (%3.1f%%) | Deep Samples %d (%3.1f%%)\n",
20+
pcounts.Total,
21+
pcounts.UDP,
22+
(float64(pcounts.UDP)/float64(pcounts.Total))*100,
23+
pcounts.TCP,
24+
(float64(pcounts.TCP)/float64(pcounts.Total))*100,
25+
pcounts.OtherL4,
26+
(float64(pcounts.OtherL4)/float64(pcounts.Total))*100,
27+
pcounts.Ipv4,
28+
(float64(pcounts.Ipv4)/float64(pcounts.Total))*100,
29+
pcounts.Ipv6,
30+
(float64(pcounts.Ipv6)/float64(pcounts.Total))*100,
31+
pcounts.In,
32+
(float64(pcounts.In)/float64(pcounts.Total-inOutDiff))*100,
33+
pcounts.Out,
34+
(float64(pcounts.Out)/float64(pcounts.Total-inOutDiff))*100,
35+
pcounts.DeepSamples,
36+
(float64(pcounts.DeepSamples)/float64(pcounts.Total))*100,
37+
)
38+
_, _ = fmt.Fprintf(v, "Pkt Rates Total %d/s %d/%d/%d/%d pps | In %d/s %d/%d/%d/%d pps | Out %d/s %d/%d/%d/%d pps | IP Card. In: %d | Out: %d | TCP Errors %d | OS Drops %d | IF Drops %d\n\n",
39+
pcounts.Rates.Pps_total.Live,
40+
pcounts.Rates.Pps_total.P50,
41+
pcounts.Rates.Pps_total.P90,
42+
pcounts.Rates.Pps_total.P95,
43+
pcounts.Rates.Pps_total.P99,
44+
pcounts.Rates.Pps_in.Live,
45+
pcounts.Rates.Pps_in.P50,
46+
pcounts.Rates.Pps_in.P90,
47+
pcounts.Rates.Pps_in.P95,
48+
pcounts.Rates.Pps_in.P99,
49+
pcounts.Rates.Pps_out.Live,
50+
pcounts.Rates.Pps_out.P50,
51+
pcounts.Rates.Pps_out.P90,
52+
pcounts.Rates.Pps_out.P95,
53+
pcounts.Rates.Pps_out.P99,
54+
pcounts.Cardinality.SrcIpsIn,
55+
pcounts.Cardinality.DstIpsOut,
56+
window5m.Pcap.TcpReassemblyErrors,
57+
window5m.Pcap.OsDrops,
58+
window5m.Pcap.IfDrops,
59+
)
60+
dnsc := window5m.DNS.WirePackets
61+
_, _ = fmt.Fprintf(v, "DNS Wire Pkts %d (%3.1f%%) | Rates Total %d/s %d/%d/%d/%d | UDP %d (%3.1f%%) | TCP %d (%3.1f%%) | IPv4 %d (%3.1f%%) | IPv6 %d (%3.1f%%) | Query %d (%3.1f%%) | Response %d (%3.1f%%)\n",
62+
dnsc.Total,
63+
(float64(dnsc.Total)/float64(pcounts.Total))*100,
64+
dnsc.Rates.Total.Live,
65+
dnsc.Rates.Total.P50,
66+
dnsc.Rates.Total.P90,
67+
dnsc.Rates.Total.P95,
68+
dnsc.Rates.Total.P99,
69+
dnsc.UDP,
70+
(float64(dnsc.UDP)/float64(dnsc.Total))*100,
71+
dnsc.TCP,
72+
(float64(dnsc.TCP)/float64(dnsc.Total))*100,
73+
dnsc.Ipv4,
74+
(float64(dnsc.Ipv4)/float64(dnsc.Total))*100,
75+
dnsc.Ipv6,
76+
(float64(dnsc.Ipv6)/float64(dnsc.Total))*100,
77+
dnsc.Queries,
78+
(float64(dnsc.Queries)/float64(dnsc.Total))*100,
79+
dnsc.Replies,
80+
(float64(dnsc.Replies)/float64(dnsc.Total))*100,
81+
)
82+
xact := window5m.DNS.Xact
83+
_, _ = fmt.Fprintf(v, "DNS Xacts %d | Timed Out %d | In %d (%3.1f%%) | Out %d (%3.1f%%) | In %3.1f/%3.1f/%3.1f/%3.1f ms | Out %3.1f/%3.1f/%3.1f/%3.1f ms | Qname Card. %d\n",
84+
xact.Counts.Total,
85+
xact.Counts.TimedOut,
86+
xact.In.Total,
87+
(float64(xact.In.Total)/float64(xact.Counts.Total))*100,
88+
xact.Out.Total,
89+
(float64(xact.Out.Total)/float64(xact.Counts.Total))*100,
90+
float64(xact.In.QuantilesUS.P50)/1000,
91+
float64(xact.In.QuantilesUS.P90)/1000,
92+
float64(xact.In.QuantilesUS.P95)/1000,
93+
float64(xact.In.QuantilesUS.P99)/1000,
94+
float64(xact.Out.QuantilesUS.P50)/1000,
95+
float64(xact.Out.QuantilesUS.P90)/1000,
96+
float64(xact.Out.QuantilesUS.P95)/1000,
97+
float64(xact.Out.QuantilesUS.P99)/1000,
98+
window5m.DNS.Cardinality.Qname,
99+
)
100+
startTime := time.Unix(window5m.Packets.Period.StartTS, 0)
101+
endTime := time.Unix(window5m.Packets.Period.StartTS+window5m.Packets.Period.Length, 0)
102+
_, _ = fmt.Fprintf(v, "DNS NOERROR %d (%3.1f%%) | SRVFAIL %d (%3.1f%%) | NXDOMAIN %d (%3.1f%%) | REFUSED %d (%3.1f%%) | Time Window %v to %v, Period %ds\n",
103+
dnsc.NoError,
104+
(float64(dnsc.NoError)/float64(dnsc.Replies))*100,
105+
dnsc.SrvFail,
106+
(float64(dnsc.SrvFail)/float64(dnsc.Replies))*100,
107+
dnsc.NxDomain,
108+
(float64(dnsc.NxDomain)/float64(dnsc.Replies))*100,
109+
dnsc.Refused,
110+
(float64(dnsc.Refused)/float64(dnsc.Replies))*100,
111+
startTime.Format(time.Kitchen),
112+
endTime.Format(time.Kitchen),
113+
window5m.Packets.Period.Length,
114+
)
115+
116+
}

0 commit comments

Comments
 (0)