#!/bin/bash

# plugin v1.1
# made by Dju
# to check dibbler and ipv6 connectivity

DIB=$(which dibbler-client)
[ -z "$DIB" ] && DIB=/usr/sbin/dibbler-client
PING_TIMEOUT=4

. /usr/lib/nagios/plugins/utils.sh

if [ ! -x $DIB ]; then
  echo "Error: $DIB not found/executable"
  exit $STATE_UNKNOWN
fi

HOSTS="free.fr google.fr"
pinged=0
rta=""
for host in $HOSTS; do
  ping=$(ping6 -c2 -W $PING_TIMEOUT $host)
  if [ $? -eq 0 ]; then
    rta=$(echo "$ping" | awk '/^rtt min\/avg/ {print $4}' | cut -d"/" -f2)
    pinged=1
    break
  fi
done

is_running=$(pidof $(basename $DIB))

if [ -n "$is_running" -a $pinged -eq 1 ]; then
  mem=$(ps aux | egrep $(basename $DIB) | egrep -v egrep | awk '{print $6 * 1024}')
  echo "OK - dibbler and ipv6 running | mem=${mem} rta=${rta}ms"
  exit $STATE_OK
else
  MSG="CRITICAL - "
  [ -z "$is_running" ] && MSG="${MSG} dibbler is not running"
  [ $pinged -eq 0 ] && MSG="${MSG} / no ipv6 connectivity"
  echo $MSG
  exit $STATE_CRITICAL
fi

