#!/bin/sh
#
# $OpenBSD: bgpd,v 1.2 2010/10/23 15:52:22 sthen Exp $
#
# Script to monitor OpenBGPD peers
# (c) 2007 Michael Knudsen <mk@openbsd.org>

# client-conf.d/-options:
#       
#       env.rsock - path to bgpd control socket
#       defaults to /var/www/logs/bgpd.rsock
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)

# Magic markers (optional - used by munin-config and installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf

RSOCK=${rsock}
RSOCK=${RSOCK:-/var/www/logs/bgpd.rsock}

if [ "$1" = "autoconf" ]; then
	if [ -x "/usr/sbin/bgpctl" -a -S "${RSOCK}" ]; then
		echo yes
		exit 0
	else
		echo "no ($RSOCK not a socket)"
		exit 0
	fi
fi

if [ "$1" = "config" ]; then

	echo 'graph_args --base 1000'

	echo 'graph_title BGP peering overview'
	echo 'graph_vlabel prefixes'
	echo 'graph_category network'
	echo 'graph_info This graph shows the number of prefixes (routes) received from the currently used BGP peers.'

	/usr/sbin/bgpctl -s $RSOCK sh | sed -n '2,$p' | while read p; do
		peername=$(echo $p | cut -d ' ' -f1 | sed 's/[\.\-]/_/g')
		echo "$peername.label $peername"
	done
	exit 0
fi

/usr/sbin/bgpctl -s $RSOCK sh | sed -n '2,$p'| while read p; do
	peername=$(echo $p | cut -d ' ' -f1 | sed 's/[\.\-]/_/g')
	value=$(echo $p | awk '{ split($7,outp,"/"); print outp[1]; }')
	echo "$peername.value ${value}"
done

