diff --git a/collector/netstat_freebsd.go b/collector/netstat_freebsd.go index da8ef9b0..c3671785 100644 --- a/collector/netstat_freebsd.go +++ b/collector/netstat_freebsd.go @@ -34,6 +34,7 @@ import ( #include #include #include +#include */ import "C" @@ -48,6 +49,12 @@ var ( ipv4ForwardTotal = "bsdNetstatIPv4ForwardTotal" ipv4FastForwardTotal = "bsdNetstatIPv4FastForwardTotal" ipv4DeliveredTotal = "bsdNetstatIPv4DeliveredTotal" + ipv6SendTotal = "bsdNetstatIPv6SendPacketsTotal" + ipv6RawSendTotal = "bsdNetstatIPv6RawSendPacketsTotal" + ipv6RecvTotal = "bsdNetstatIPv6RecvPacketsTotal" + ipv6RecvFragmentsTotal = "bsdNetstatIPv6RecvFragmentsTotal" + ipv6ForwardTotal = "bsdNetstatIPv6ForwardTotal" + ipv6DeliveredTotal = "bsdNetstatIPv6DeliveredTotal" counterMetrics = map[string]*prometheus.Desc{ // TCP stats @@ -80,6 +87,26 @@ var ( ipv4DeliveredTotal: prometheus.NewDesc( prometheus.BuildFQName(namespace, "netstat", "ip4_delivered_total"), "IPv4 packets delivered to the upper layer (packets for this host)", nil, nil), + + // IPv6 stats + ipv6SendTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "netstat", "ip6_transmit_packets_total"), + "IPv6 packets sent from this host", nil, nil), + ipv6RawSendTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "netstat", "ip6_transmit_raw_packets_total"), + "IPv6 raw packets generated", nil, nil), + ipv6RecvTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "netstat", "ip6_receive_packets_total"), + "IPv6 packets received", nil, nil), + ipv6RecvFragmentsTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "netstat", "ip6_receive_fragments_total"), + "IPv6 fragments received", nil, nil), + ipv6ForwardTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "netstat", "ip6_forward_total"), + "IPv6 packets forwarded", nil, nil), + ipv6DeliveredTotal: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "netstat", "ip6_delivered_total"), + "IPv6 packets delivered to the upper layer (packets for this host)", nil, nil), } ) @@ -141,6 +168,33 @@ func (netstatMetric *NetstatIPv4Data) GetData() (NetstatMetrics, error) { }, nil } +type NetstatIPv6Data NetstatData + +func NewIPv6Stat() *NetstatIPv6Data { + return &NetstatIPv6Data{ + structSize: int(unsafe.Sizeof(C.struct_ipstat{})), + sysctl: "net.inet6.ip6.stats", + } +} + +func (netstatMetric *NetstatIPv6Data) GetData() (NetstatMetrics, error) { + data, err := getData(netstatMetric.sysctl, netstatMetric.structSize) + if err != nil { + return nil, err + } + + ipStats := *(*C.struct_ip6stat)(unsafe.Pointer(&data[0])) + + return NetstatMetrics{ + ipv6SendTotal: float64(ipStats.ip6s_localout), + ipv6RawSendTotal: float64(ipStats.ip6s_rawout), + ipv6RecvTotal: float64(ipStats.ip6s_total), + ipv6RecvFragmentsTotal: float64(ipStats.ip6s_fragments), + ipv6ForwardTotal: float64(ipStats.ip6s_forward), + ipv6DeliveredTotal: float64(ipStats.ip6s_delivered), + }, nil +} + func getData(queryString string, expectedSize int) ([]byte, error) { data, err := sysctlRaw(queryString) if err != nil { @@ -185,6 +239,11 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error { return err } + ipv6Stats, err := NewIPv6Stat().GetData() + if err != nil { + return err + } + allStats := make(map[string]float64) for k, v := range tcpStats { @@ -195,6 +254,10 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error { allStats[k] = v } + for k, v := range ipv6Stats { + allStats[k] = v + } + for metricKey, metricData := range counterMetrics { ch <- prometheus.MustNewConstMetric( metricData, @@ -229,6 +292,18 @@ func getFreeBSDDataMock(sysctl string) []byte { } size := int(unsafe.Sizeof(C.struct_ipstat{})) + return unsafe.Slice((*byte)(unsafe.Pointer(&ipStats)), size) + } else if sysctl == "net.inet6.ip6.stats" { + ipStats := C.struct_ip6stat{ + ip6s_localout: 1234, + ip6s_rawout: 1235, + ip6s_total: 1236, + ip6s_fragments: 1237, + ip6s_forward: 1238, + ip6s_delivered: 1240, + } + size := int(unsafe.Sizeof(C.struct_ip6stat{})) + return unsafe.Slice((*byte)(unsafe.Pointer(&ipStats)), size) } diff --git a/collector/netstat_freebsd_test.go b/collector/netstat_freebsd_test.go index a6b3e441..fb70b053 100644 --- a/collector/netstat_freebsd_test.go +++ b/collector/netstat_freebsd_test.go @@ -92,6 +92,36 @@ func TestGetIPv4Metrics(t *testing.T) { } } +func TestGetIPv6Metrics(t *testing.T) { + testSetup() + + ipv6Data, err := NewIPv6Stat().GetData() + if err != nil { + t.Fatal("unexpected error:", err) + } + + sndTotal := ipv6Data[ipv6SendTotal] + rcvTotal := ipv6Data[ipv6RecvTotal] + forwardTotal := ipv6Data[ipv6ForwardTotal] + deliveredTotal := ipv6Data[ipv6DeliveredTotal] + + if got, want := sndTotal, float64(1234); got != want { + t.Errorf("unexpected sndTotal value: want %f, got %f", want, got) + } + + if got, want := rcvTotal, float64(1236); got != want { + t.Errorf("unexpected rcvTotal value: want %f, got %f", want, got) + } + + if got, want := forwardTotal, float64(1238); got != want { + t.Errorf("unexpected forwardTotal value: want %f, got %f", want, got) + } + + if got, want := deliveredTotal, float64(1240); got != want { + t.Errorf("unexpected deliveredTotal value: want %f, got %f", want, got) + } +} + func TestNetStatCollectorUpdate(t *testing.T) { ch := make(chan prometheus.Metric, len(counterMetrics)) collector := &netStatCollector{