Skip to content

Commit

Permalink
use ipprotocols library
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvil committed Oct 14, 2024
1 parent 96d4a75 commit 39c7387
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
21 changes: 7 additions & 14 deletions collectors/traffic_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/tomvil/countries"
"github.com/tomvil/go-ipprotocols"
wgc "github.com/tomvil/wanguard_exporter/client"
)

Expand Down Expand Up @@ -66,19 +67,6 @@ type Talker struct {
Value int
}

var protocol = map[int]string{
1: "ICMP",
2: "IGMP",
6: "TCP",
17: "UDP",
47: "GRE",
50: "ESP",
51: "AH",
58: "ICMPv6",
88: "EIGRP",
89: "OSPF",
}

func NewTrafficCollector(wgclient *wgc.Client) *TrafficCollector {
prefix := "wanguard_traffic_"
return &TrafficCollector{
Expand Down Expand Up @@ -197,7 +185,12 @@ func collectTopTrafficByIPProtocol(desc *prometheus.Desc, ch chan<- prometheus.M

for i := 1; i <= len(ipProtocolTop.Top); i++ {
k := strconv.Itoa(i)
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, float64(ipProtocolTop.Top[k].Value), protocol[ipProtocolTop.Top[k].IPProtocol])
protocolName, err := ipprotocols.GetProtocolName(ipProtocolTop.Top[k].IPProtocol)
if err != nil {
log.Errorln("failed to get protocol name for protocol number: ", ipProtocolTop.Top[k].IPProtocol)
continue
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, float64(ipProtocolTop.Top[k].Value), protocolName)
}

defer wsync.Done()
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/tomvil/wanguard_exporter

go 1.17
go 1.21.5

require (
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.26.0
github.com/tomvil/countries v0.0.0-20220104165753-f0d74c0c9799
github.com/tomvil/go-ipprotocols v0.0.0-20241014123012-278a27f8a73e
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tomvil/countries v0.0.0-20220104165753-f0d74c0c9799 h1:WsFmU9SbaxwRxrCkI45yTjGJ4NRInkoE6C2URi68gbU=
github.com/tomvil/countries v0.0.0-20220104165753-f0d74c0c9799/go.mod h1:DGwxkfl84qe5kzX5D6fljA6V+MEk8PvdVGT1MnDl5Js=
github.com/tomvil/go-ipprotocols v0.0.0-20241014123012-278a27f8a73e h1:ZKGEs1IO5ZuJvoI7yfw92nRoiwwh5XiQXqnDl9CZqfU=
github.com/tomvil/go-ipprotocols v0.0.0-20241014123012-278a27f8a73e/go.mod h1:U0UPn/og7dbWylgkF76EPY1gkkNo0IDnAK3dmzigLK0=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down

0 comments on commit 39c7387

Please # to comment.