Skip to content

Commit cbd8cc0

Browse files
committed
netstat_freebsd: add support for some IPv6 metrics
Metrics added: - ip6_transmit_packets_total - ip6_transmit_raw_packets_total - ip6_receive_packets_total - ip6_receive_fragments_total - ip6_forward_total - ip6_delivered_total Signed-off-by: Danilo Egea Gondolfo <[email protected]>
1 parent 5068195 commit cbd8cc0

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

collector/netstat_freebsd.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
#include <netinet/tcp_var.h>
3535
#include <netinet/udp.h>
3636
#include <netinet/ip_var.h>
37+
#include <netinet6/ip6_var.h>
3738
*/
3839
import "C"
3940

@@ -48,6 +49,12 @@ var (
4849
ipv4ForwardTotal = "bsdNetstatIPv4ForwardTotal"
4950
ipv4FastForwardTotal = "bsdNetstatIPv4FastForwardTotal"
5051
ipv4DeliveredTotal = "bsdNetstatIPv4DeliveredTotal"
52+
ipv6SendTotal = "bsdNetstatIPv6SendPacketsTotal"
53+
ipv6RawSendTotal = "bsdNetstatIPv6RawSendPacketsTotal"
54+
ipv6RecvTotal = "bsdNetstatIPv6RecvPacketsTotal"
55+
ipv6RecvFragmentsTotal = "bsdNetstatIPv6RecvFragmentsTotal"
56+
ipv6ForwardTotal = "bsdNetstatIPv6ForwardTotal"
57+
ipv6DeliveredTotal = "bsdNetstatIPv6DeliveredTotal"
5158

5259
counterMetrics = map[string]*prometheus.Desc{
5360
// TCP stats
@@ -80,6 +87,26 @@ var (
8087
ipv4DeliveredTotal: prometheus.NewDesc(
8188
prometheus.BuildFQName(namespace, "netstat", "ip4_delivered_total"),
8289
"IPv4 packets delivered to the upper layer (packets for this host)", nil, nil),
90+
91+
// IPv6 stats
92+
ipv6SendTotal: prometheus.NewDesc(
93+
prometheus.BuildFQName(namespace, "netstat", "ip6_transmit_packets_total"),
94+
"IPv6 packets sent from this host", nil, nil),
95+
ipv6RawSendTotal: prometheus.NewDesc(
96+
prometheus.BuildFQName(namespace, "netstat", "ip6_transmit_raw_packets_total"),
97+
"IPv6 raw packets generated", nil, nil),
98+
ipv6RecvTotal: prometheus.NewDesc(
99+
prometheus.BuildFQName(namespace, "netstat", "ip6_receive_packets_total"),
100+
"IPv6 packets received", nil, nil),
101+
ipv6RecvFragmentsTotal: prometheus.NewDesc(
102+
prometheus.BuildFQName(namespace, "netstat", "ip6_receive_fragments_total"),
103+
"IPv6 fragments received", nil, nil),
104+
ipv6ForwardTotal: prometheus.NewDesc(
105+
prometheus.BuildFQName(namespace, "netstat", "ip6_forward_total"),
106+
"IPv6 packets forwarded", nil, nil),
107+
ipv6DeliveredTotal: prometheus.NewDesc(
108+
prometheus.BuildFQName(namespace, "netstat", "ip6_delivered_total"),
109+
"IPv6 packets delivered to the upper layer (packets for this host)", nil, nil),
83110
}
84111
)
85112

@@ -141,6 +168,33 @@ func (netstatMetric *NetstatIPv4Data) GetData() (NetstatMetrics, error) {
141168
}, nil
142169
}
143170

171+
type NetstatIPv6Data NetstatData
172+
173+
func NewIPv6Stat() *NetstatIPv6Data {
174+
return &NetstatIPv6Data{
175+
structSize: int(unsafe.Sizeof(C.struct_ipstat{})),
176+
sysctl: "net.inet6.ip6.stats",
177+
}
178+
}
179+
180+
func (netstatMetric *NetstatIPv6Data) GetData() (NetstatMetrics, error) {
181+
data, err := getData(netstatMetric.sysctl, netstatMetric.structSize)
182+
if err != nil {
183+
return nil, err
184+
}
185+
186+
ipStats := *(*C.struct_ip6stat)(unsafe.Pointer(&data[0]))
187+
188+
return NetstatMetrics{
189+
ipv6SendTotal: float64(ipStats.ip6s_localout),
190+
ipv6RawSendTotal: float64(ipStats.ip6s_rawout),
191+
ipv6RecvTotal: float64(ipStats.ip6s_total),
192+
ipv6RecvFragmentsTotal: float64(ipStats.ip6s_fragments),
193+
ipv6ForwardTotal: float64(ipStats.ip6s_forward),
194+
ipv6DeliveredTotal: float64(ipStats.ip6s_delivered),
195+
}, nil
196+
}
197+
144198
func getData(queryString string, expectedSize int) ([]byte, error) {
145199
data, err := sysctlRaw(queryString)
146200
if err != nil {
@@ -185,6 +239,11 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error {
185239
return err
186240
}
187241

242+
ipv6Stats, err := NewIPv6Stat().GetData()
243+
if err != nil {
244+
return err
245+
}
246+
188247
allStats := make(map[string]float64)
189248

190249
for k, v := range tcpStats {
@@ -195,6 +254,10 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error {
195254
allStats[k] = v
196255
}
197256

257+
for k, v := range ipv6Stats {
258+
allStats[k] = v
259+
}
260+
198261
for metricKey, metricData := range counterMetrics {
199262
ch <- prometheus.MustNewConstMetric(
200263
metricData,
@@ -229,6 +292,18 @@ func getFreeBSDDataMock(sysctl string) []byte {
229292
}
230293
size := int(unsafe.Sizeof(C.struct_ipstat{}))
231294

295+
return unsafe.Slice((*byte)(unsafe.Pointer(&ipStats)), size)
296+
} else if sysctl == "net.inet6.ip6.stats" {
297+
ipStats := C.struct_ip6stat{
298+
ip6s_localout: 1234,
299+
ip6s_rawout: 1235,
300+
ip6s_total: 1236,
301+
ip6s_fragments: 1237,
302+
ip6s_forward: 1238,
303+
ip6s_delivered: 1240,
304+
}
305+
size := int(unsafe.Sizeof(C.struct_ip6stat{}))
306+
232307
return unsafe.Slice((*byte)(unsafe.Pointer(&ipStats)), size)
233308
}
234309

collector/netstat_freebsd_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,36 @@ func TestGetIPv4Metrics(t *testing.T) {
9292
}
9393
}
9494

95+
func TestGetIPv6Metrics(t *testing.T) {
96+
testSetup()
97+
98+
ipv6Data, err := NewIPv6Stat().GetData()
99+
if err != nil {
100+
t.Fatal("unexpected error:", err)
101+
}
102+
103+
sndTotal := ipv6Data[ipv6SendTotal]
104+
rcvTotal := ipv6Data[ipv6RecvTotal]
105+
forwardTotal := ipv6Data[ipv6ForwardTotal]
106+
deliveredTotal := ipv6Data[ipv6DeliveredTotal]
107+
108+
if got, want := sndTotal, float64(1234); got != want {
109+
t.Errorf("unexpected sndTotal value: want %f, got %f", want, got)
110+
}
111+
112+
if got, want := rcvTotal, float64(1236); got != want {
113+
t.Errorf("unexpected rcvTotal value: want %f, got %f", want, got)
114+
}
115+
116+
if got, want := forwardTotal, float64(1238); got != want {
117+
t.Errorf("unexpected forwardTotal value: want %f, got %f", want, got)
118+
}
119+
120+
if got, want := deliveredTotal, float64(1240); got != want {
121+
t.Errorf("unexpected deliveredTotal value: want %f, got %f", want, got)
122+
}
123+
}
124+
95125
func TestNetStatCollectorUpdate(t *testing.T) {
96126
ch := make(chan prometheus.Metric, len(counterMetrics))
97127
collector := &netStatCollector{

0 commit comments

Comments
 (0)