-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
129 lines (113 loc) · 4.12 KB
/
output.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
// @license
// Copyright (C) 2024 Dinko Korunic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"fmt"
"sort"
"strings"
"time"
"github.com/cilium/ebpf"
jsoniter "github.com/json-iterator/go"
)
const (
Bps float64 = 1.0
Kbps = 1000 * Bps
Mbps = 1000 * Kbps
Gbps = 1000 * Mbps
Tbps = 1000 * Gbps
)
// processMap generates statEntry objects from an ebpf.Map using the provided start time.
//
// Parameters:
//
// m *ebpf.Map - the eb
func processMap(m *ebpf.Map, start time.Time) ([]statEntry, error) {
var (
key counterStatkey
val counterStatvalue
)
dur := time.Since(start).Seconds()
stats := make([]statEntry, 0, m.MaxEntries())
iter := m.Iterate()
// build statEntry slice converting data where needed
for iter.Next(&key, &val) {
stats = append(stats, statEntry{
SrcIP: bytesToAddr(key.Srcip.In6U.U6Addr8),
DstIP: bytesToAddr(key.Dstip.In6U.U6Addr8),
Proto: protoToString(key.Proto),
SrcPort: key.SrcPort,
DstPort: key.DstPort,
Bytes: val.Bytes,
Packets: val.Packets,
Bitrate: 8 * float64(val.Bytes) / dur,
})
}
sort.Slice(stats, func(i, j int) bool {
return stats[i].Bitrate > stats[j].Bitrate
})
return stats, iter.Err()
}
// formatBitrate formats the bitrate value into a human-readable string.
//
// It takes a float64 parameter representing the bitrate and returns a string.
func formatBitrate(b float64) string {
switch {
case b < Kbps:
return fmt.Sprintf("%.2f bps", b)
case b < 10*Kbps:
return fmt.Sprintf("%.2f Kbps", b/Kbps)
case b < 10*Mbps:
return fmt.Sprintf("%.2f Mbps", b/Mbps)
case b < 10*Gbps:
return fmt.Sprintf("%.2f Gbps", b/Gbps)
case b < 10*Tbps:
return fmt.Sprintf("%.2f Tbps", b/Tbps)
}
return fmt.Sprintf("%.2fTbps", b/Tbps)
}
// outputPlain generates a plain text representation of the given statEntry slice.
//
// It takes a slice of statEntry structs as its parameter and formats them into a string
// containing information about each entry's bitrate, packets, bytes, protocol, source IP,
// source port, and destination IP and port. The resulting string is then printed to the
// console.
//
// The function does not return anything.
func outputPlain(m []statEntry) {
var sb strings.Builder
for _, v := range m {
sb.WriteString(fmt.Sprintf("bitrate: %v, packets: %d, bytes: %d, proto: %v, src: %v:%v, dst: %v:%v\n",
formatBitrate(v.Bitrate), v.Packets, v.Bytes, v.Proto, v.SrcIP, v.SrcPort, v.DstIP, v.DstPort))
}
fmt.Printf("%v", sb.String())
}
// outputJSON marshals the given slice of statEntry structs into a JSON string and prints it.
//
// The function takes a slice of statEntry structs as a parameter.
// It uses the jsoniter library to configure the JSON encoder to be compatible with the standard library.
// The slice is then marshaled into a JSON string using the Marshal function of the jsoniter package.
// The resulting JSON string is printed using the Printf function from the fmt package.
// The function does not return any value.
func outputJSON(m []statEntry) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
out, _ := json.Marshal(m)
fmt.Printf("%v\n", string(out))
}