Skip to content

Commit ff735b5

Browse files
committed
Improve JSON and regular output, especially when comm or pid are empty.
1 parent 0c4c745 commit ff735b5

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

output.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ import (
3333
)
3434

3535
const (
36-
Bps float64 = 1.0
37-
Kbps = 1000 * Bps
38-
Mbps = 1000 * Kbps
39-
Gbps = 1000 * Mbps
40-
Tbps = 1000 * Gbps
41-
KernelComm = "[kernel]"
36+
Bps float64 = 1.0
37+
Kbps = 1000 * Bps
38+
Mbps = 1000 * Kbps
39+
Gbps = 1000 * Mbps
40+
Tbps = 1000 * Gbps
4241
)
4342

4443
// processMap generates statEntry objects from an ebpf.Map using the provided start time.
@@ -121,7 +120,11 @@ func outputPlain(m []statEntry) {
121120
}
122121

123122
if *useKProbes {
124-
sb.WriteString(fmt.Sprintf(", pid: %d, comm: %v", v.Pid, v.Comm))
123+
sb.WriteString(fmt.Sprintf(", pid: %d", v.Pid))
124+
125+
if v.Comm != "" {
126+
sb.WriteString(fmt.Sprintf(", comm: %v", v.Comm))
127+
}
125128
}
126129

127130
sb.WriteString("\n")
@@ -157,9 +160,5 @@ func comm2String(bs []int8) string {
157160
// trim excess NULLs
158161
b = bytes.Trim(b, "\x00")
159162

160-
if len(b) == 0 {
161-
return KernelComm
162-
}
163-
164163
return string(b)
165164
}

types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ type statEntry struct {
1010
SrcIP netip.Addr `json:"srcIp"`
1111
DstIP netip.Addr `json:"dstIp"`
1212
Proto string `json:"proto"`
13-
Comm string `json:"comm"`
13+
Comm string `json:"comm,omitempty"`
1414
Bytes uint64 `json:"bytes"`
1515
Packets uint64 `json:"packets"`
1616
Bitrate float64 `json:"bitrate"`
17-
Pid int32 `json:"pid"`
17+
Pid int32 `json:"pid,omitempty"`
1818
SrcPort uint16 `json:"srcPort"`
1919
DstPort uint16 `json:"dstPort"`
2020
}

0 commit comments

Comments
 (0)