#!/bin/sh
#
# Plugin to monitor ARP entries per interface
#
# Parameters understood:
#
#       config   (required)
#       autoconf (optional)
#
#  Made by Sven Hartge (sven AT svenhartge DOT de)
#  change to iproute by Martin89 (martin AT martin89 DOT de)
#
#


#%# family=contrib
#%# capabilities=autoconf suggest

case "$1" in
	autoconf)
		# Search for ip
		if which ip >/dev/null; then
			echo 'yes'
		else
			echo "no (missing 'ip' executable)"
		fi
		exit 0
	;;
	suggest)
		if [ ! -r /proc/net/dev ]; then
			exit 1
		fi
		awk '$1~ /^(eth|wlan|ath|ra)[0-9]+(\.[0-9]+)?/ { gsub(":", ""); gsub("\.", "_"); print $1 }' /proc/net/dev
		exit 0
	;;
	config)
		INTERFACE="$(basename $0 | sed 's/^arp_//g' | tr '_' '.')"
		if [ -z "$INTERFACE" ]; then
			exit 1
		fi
		echo "graph_title ARP/NDP entries for $INTERFACE"
		cat <<'EOM'
graph_args --base 1000 -l 0
graph_vlabel Entries
graph_category network
graph_scale no
graph_info This graph shows the number of ARP and NDP entries for a interface.
entries4.label ARP entries
entries4.info Number of ARP entries
entries6global.label NDP global entries
entries6global.info Number of NDP entries for global IPv6 address
entries6local.label NDP local entries
entries6local.info Number of NDP entries for link-local IPv6 address (fe80::/64)
EOM
		exit 0
	;;
esac

INTERFACE="$(basename $0 | sed 's/^arp_//g' | tr '_' '.')"
if [ -z "$INTERFACE" ]; then
	exit 1
fi
ip neigh show dev "$INTERFACE" | awk 'BEGIN { a=0; b=0; c=0 }
/(REACHABLE|DELAY|STALE)/ { if ($1~ /^fe80:/){c++} else{if ($1~ /^[0-9]+\./) {a++} else{b++} } }
END { print "entries4.value", a "\nentries6global.value", b, "\nentries6local.value", c }'
