Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add neighbor state #3191

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions collector/arp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,20 @@ type arpCollector struct {
fs procfs.FS
deviceFilter deviceFilter
entries *prometheus.Desc
states *prometheus.Desc
logger *slog.Logger
}

var neighborStatesMap = map[uint16]string{
unix.NUD_INCOMPLETE: "incomplete",
unix.NUD_REACHABLE: "reachable",
unix.NUD_STALE: "stale",
unix.NUD_DELAY: "delay",
unix.NUD_PROBE: "probe",
unix.NUD_FAILED: "failed",
unix.NUD_PERMANENT: "permanent",
}

func init() {
registerCollector("arp", defaultEnabled, NewARPCollector)
}
Expand All @@ -59,6 +70,11 @@ func NewARPCollector(logger *slog.Logger) (Collector, error) {
"ARP entries by device",
[]string{"device"}, nil,
),
states: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "arp", "states"),
"ARP states by device",
[]string{"device", "state"}, nil,
),
logger: logger,
}, nil
}
Expand All @@ -73,40 +89,53 @@ func getTotalArpEntries(deviceEntries []procfs.ARPEntry) map[string]uint32 {
return entries
}

func getTotalArpEntriesRTNL() (map[string]uint32, error) {
func getArpEntriesRTNL() (map[string]uint32, map[string]map[string]uint32, error) {
conn, err := rtnl.Dial(nil)
if err != nil {
return nil, err
return nil, nil, err
}
defer conn.Close()

// Neighbors will also contain IPv6 neighbors, but since this is purely an ARP collector,
// restrict to AF_INET.
neighbors, err := conn.Neighbours(nil, unix.AF_INET)
if err != nil {
return nil, err
return nil, nil, err
}

// Map of interface name to ARP neighbor count.
entries := make(map[string]uint32)
// Map of map[InterfaceName]map[StateName]int
states := make(map[string]map[string]uint32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if I were to implement this, I would keep the neighbor states as their native uint16 until ready to emit the metrics. This would be more memory efficient and also result in fewer map lookups. In other words, make this a map[string]map[uint16]uint32 and only resolve the states to string labels when emitting the metrics to the channel.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good idea, we won't store all those strings now, also fewer lookups as you said. Thanks for your careful review.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can, resolve the conversation if the new commit is ok.


for _, n := range neighbors {
// Skip entries which have state NUD_NOARP to conform to output of /proc/net/arp.
if n.State&unix.NUD_NOARP == 0 {
entries[n.Interface.Name]++
if n.State&unix.NUD_NOARP == unix.NUD_NOARP {
continue
}

entries[n.Interface.Name]++

if _, ok := states[n.Interface.Name]; !ok {
states[n.Interface.Name] = make(map[string]uint32)
}

states[n.Interface.Name][neighborStatesMap[n.State]]++
}

return entries, nil
return entries, states, nil
}

func (c *arpCollector) Update(ch chan<- prometheus.Metric) error {
var enumeratedEntry map[string]uint32
var (
enumeratedEntry map[string]uint32
enumStates map[string]map[string]uint32
)

if *arpNetlink {
var err error

enumeratedEntry, err = getTotalArpEntriesRTNL()
enumeratedEntry, enumStates, err = getArpEntriesRTNL()
if err != nil {
return fmt.Errorf("could not get ARP entries: %w", err)
}
Expand All @@ -125,6 +154,13 @@ func (c *arpCollector) Update(ch chan<- prometheus.Metric) error {
}
ch <- prometheus.MustNewConstMetric(
c.entries, prometheus.GaugeValue, float64(entryCount), device)

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

return nil
Expand Down