Skip to content

Commit

Permalink
Move to count of states
Browse files Browse the repository at this point in the history
  • Loading branch information
eugercek committed Dec 6, 2024
1 parent b1004d8 commit 74f1c6f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions collector/arp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewARPCollector(logger *slog.Logger) (Collector, error) {
states: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "arp", "states"),
"ARP states by device",
[]string{"device", "ip", "state"}, nil,
[]string{"device", "state"}, nil,
),
logger: logger,
}, nil
Expand All @@ -96,7 +96,7 @@ func getTotalArpEntries(deviceEntries []procfs.ARPEntry) map[string]uint32 {
return entries
}

func getArpEntriesNTRL() (map[string]uint32, map[string]neighborState, error) {
func getArpEntriesNTRL() (map[string]uint32, map[string]map[string]int, error) {
conn, err := rtnetlink.Dial(nil)
if err != nil {
return nil, nil, err
Expand All @@ -109,20 +109,25 @@ func getArpEntriesNTRL() (map[string]uint32, map[string]neighborState, error) {
}

ifIndexEntries := make(map[uint32]uint32)
ifIndexStates := make(map[uint32]neighborState)
ifIndexStates := make(map[uint32]map[string]int)

for _, n := range neighbors {
// Neighbors will also contain IPv6 neighbors, but since this is purely an ARP collector,
// restrict to AF_INET. Also skip entries which have state NUD_NOARP to conform to output
// of /proc/net/arp.
if n.Family == unix.AF_INET && n.State&unix.NUD_NOARP == 0 {
ifIndexEntries[n.Index]++
ifIndexStates[n.Index] = neighborState{ip: n.Attributes.Address.String(), state: neighborStatesMap[n.State]}

_, ok := ifIndexStates[n.Index]
if !ok {
ifIndexStates[n.Index] = make(map[string]int)
}
ifIndexStates[n.Index][neighborStatesMap[n.State]]++
}
}

enumEntries := make(map[string]uint32)
enumStates := make(map[string]neighborState)
enumStates := make(map[string]map[string]int)

// Convert interface indexes to names.
for ifIndex, entryCount := range ifIndexEntries {
Expand All @@ -144,7 +149,7 @@ func getArpEntriesNTRL() (map[string]uint32, map[string]neighborState, error) {
func (c *arpCollector) Update(ch chan<- prometheus.Metric) error {
var (
enumeratedEntry map[string]uint32
enumStates map[string]neighborState
enumStates map[string]map[string]int
)

if *arpNetlink {
Expand All @@ -171,9 +176,11 @@ func (c *arpCollector) Update(ch chan<- prometheus.Metric) error {
c.entries, prometheus.GaugeValue, float64(entryCount), device)

if *arpNetlink {
s := enumStates[device]
ch <- prometheus.MustNewConstMetric(
c.states, prometheus.GaugeValue, 1, device, s.ip, s.state)
states := enumStates[device]
for state, count := range states {
ch <- prometheus.MustNewConstMetric(
c.states, prometheus.GaugeValue, float64(count), device, state)
}
}
}

Expand Down

0 comments on commit 74f1c6f

Please sign in to comment.