Skip to content

Instantly share code, notes, and snippets.

@j0ju
Created September 9, 2017 12:00
Show Gist options
  • Select an option

  • Save j0ju/17b4866f7dee6fb1e10786b0740fb846 to your computer and use it in GitHub Desktop.

Select an option

Save j0ju/17b4866f7dee6fb1e10786b0740fb846 to your computer and use it in GitHub Desktop.
collectd exec plugin bird
#!/bin/sh
set -e
#HOSTNAME="${COLLECTD_HOSTNAME:-`hostname -f`}" # RH / CentOs
HOSTNAME="${COLLECTD_HOSTNAME:-`hostname`}" # Debian/Ubuntu
INTERVAL="${COLLECTD_INTERVAL:-60}"
INTERVAL="${INTERVAL%.*}"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
PLUGIN=bird
_sleep() { # sleep variants
local t
case "$1" in
*s ) t="${1%s}" ;;
*m ) t=$(( ${1%m} * 60 )) ;;
*h ) t=$(( ${1%h} * 3600 )) ;;
*d ) t=$(( ${1%d} * 86400 )) ;;
* ) t="$1" ;;
esac
#read -t $t tmp < /dev/tty1
#sleep $t
/bin/sleep $t
}
putval() {
local key="$1"
local value="$2"
local key_instance="$3"
local plugin_instance="$4"
echo "PUTVAL ${HOSTNAME}/${PLUGIN}${plugin_instance:+-$plugin_instance}/gauge-${key_instance:+$key_instance-}$key interval=$INTERVAL N:$value"
}
# 1. itereate over all address familiies
# 2. itereate over all protocol is BGP peer Established or not
# 3. count all routes of each BGP protocol
while :; do
for familiy in ipv4 ipv6; do
birdc="birdc${familiy#ipv}"
which "$birdc" > /dev/null 2>&1 || \
birdc="birdc${familiy#ipv4}"
which "$birdc" > /dev/null 2>&1 || \
birdc="birdcl${familiy#ipv}"
## 1. is bird running?
running=0
$birdc -v -r show status > /dev/null 2>&1 && \
running=1
putval running "$running" "" "$familiy"
[ "$running" = 1 ] || \
continue
## 2. iterate of protocols of type BGP
$birdc -v -r show protocols | while read name proto table state since info; do
case "$proto" in
BGP ) ;;
* ) continue ;;
esac
established=0
if [ "$info" = "Established" ]; then
established=1
fi
putval established "$established" "$name" "$familiy"
## 3. get information about routes
#routes="$($birdc -v -r show route protocol $name | grep -v ^00 || :)"
route_count="$($birdc -v -r show route protocol $name | grep -v ^00 | wc -l || :)"
putval route_count "$route_count" "$name" "$familiy"
done
done
_sleep "$INTERVAL"
done
@j0ju
Copy link
Copy Markdown
Author

j0ju commented Sep 9, 2017

Example output for collectd:

PUTVAL example.com/bird-ipv4/gauge-running interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as2324-established interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as2324-route_count interval=60 N:5
PUTVAL example.com/bird-ipv4/gauge-bgp_as65111-established interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as65111-route_count interval=60 N:0
PUTVAL example.com/bird-ipv4/gauge-bgp_as65112-established interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as65112-route_count interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as65112-established interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as65112-route_count interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as65110-established interval=60 N:0
PUTVAL example.com/bird-ipv4/gauge-bgp_as65110-route_count interval=60 N:0
PUTVAL example.com/bird-ipv4/gauge-bgp_as65113-established interval=60 N:1
PUTVAL example.com/bird-ipv4/gauge-bgp_as65113-route_count interval=60 N:1
PUTVAL example.com/bird-ipv6/gauge-running interval=60 N:0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment