-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog-metric-data.go
171 lines (150 loc) · 3.52 KB
/
log-metric-data.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
package main
import (
"fmt"
"log"
"net"
"net/netip"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
type TrafficType int
func (t TrafficType) String() string {
if t == 0 {
return "virtual"
}
if t == 1 {
return "subnet"
}
if t == 2 {
return "exit"
}
if t == 3 {
return "physical"
}
return "invalidTrafficType"
}
const (
VirtualTraffic TrafficType = iota
SubnetTraffic
ExitTraffic
PhysicalTraffic
)
type LogEntry struct {
Src string
Dst string
TrafficType TrafficType
Proto uint8
CountType string
}
func hostOnly(s string) string {
host, _, err := net.SplitHostPort(s)
if err != nil {
return "-"
}
ip := net.ParseIP(host)
if ip == nil {
return "-"
}
return ip.String()
}
func (l *LogEntry) String() string {
return fmt.Sprintf(`%s_%s_%d_%d_%s`, l.Src, l.Dst, l.TrafficType, l.Proto, l.CountType)
}
type MapLogEntryToValue map[LogEntry]uint64
type LogMetricData struct {
data MapLogEntryToValue
}
func (m *LogMetricData) Init() {
m.data = make(MapLogEntryToValue)
}
func (m *LogMetricData) SaveNewData(apiResponse APILogResponse) {
log.Printf("getNewLogData(): %d new messages", len(apiResponse.Logs))
mc := []int{0, 0, 0, 0}
for _, msg := range apiResponse.Logs {
mc[0] += len(msg.VirtualTraffic)
for _, cc := range msg.VirtualTraffic {
m.Update(&cc, VirtualTraffic)
}
mc[1] += len(msg.SubnetTraffic)
for _, cc := range msg.SubnetTraffic {
m.Update(&cc, SubnetTraffic)
}
mc[2] += len(msg.ExitTraffic)
for _, cc := range msg.ExitTraffic {
m.Update(&cc, ExitTraffic)
}
mc[3] += len(msg.PhysicalTraffic)
for _, cc := range msg.PhysicalTraffic {
m.Update(&cc, PhysicalTraffic)
}
}
log.Printf("getNewLogData(): counts Virtual:%d | Subnet: %d | Exit: %d | Physical: %d",
mc[0], mc[1], mc[2], mc[3])
log.Printf("getNewLogData(): Number of LogMetricData entries: %d", len(m.data))
}
// Update based on the data from a new log entry (counts)
func (m *LogMetricData) Update(cc *ConnectionCounts, tt TrafficType) {
le := LogEntry{
hostOnly(cc.Src),
hostOnly(cc.Dst),
tt,
cc.Proto,
"",
}
le.CountType = "TxPackets"
m.data[le] += cc.TxPackets
le.CountType = "RxPackets"
m.data[le] += cc.RxPackets
le.CountType = "TxBytes"
m.data[le] += cc.TxBytes
le.CountType = "RxBytes"
m.data[le] += cc.RxBytes
}
func toNetIp(addrString string) (*netip.Addr, error) {
addr, err := netip.ParseAddr(addrString)
if err != nil {
return nil, err
}
return &addr, nil
}
// Given a metric name and the actual metric,
// add the latest values collected to the metric
func (m *LogMetricData) AddCounter(metricName string, cv *prometheus.CounterVec, namesByAddr map[netip.Addr]string) {
add := func(le LogEntry, value uint64) {
s := le.Src
d := le.Dst
if namesByAddr != nil {
sNetIp, err := toNetIp(s)
if err == nil {
if h, ok := namesByAddr[*sNetIp]; ok {
s = h
}
}
dNetIp, err := toNetIp(d)
if err == nil {
if h, ok := namesByAddr[*dNetIp]; ok {
d = h
}
}
}
cv.WithLabelValues(
s,
d,
le.TrafficType.String(),
fmt.Sprintf("%d", le.Proto)).Add(float64(value))
}
for le, value := range m.data {
if strings.Contains(metricName, "tx_bytes") && le.CountType == "TxBytes" {
add(le, value)
}
if strings.Contains(metricName, "rx_bytes") && le.CountType == "RxBytes" {
add(le, value)
}
if strings.Contains(metricName, "tx_packets") && le.CountType == "TxPackets" {
add(le, value)
}
if strings.Contains(metricName, "rx_packets") && le.CountType == "RxPackets" {
add(le, value)
}
}
}