#!/bin/bash
#
# File:      		check_mtu
# Author:    		Sven Duscha ( sven dot duscha at lrz dot de )
# Date:					2017-02-14
# Last change:  2017-03-02

# Default values for warning and critical
WARNVAL=2048
CRITICALVAL=1220
MTU=0
# Return values for WARN and CRIT
OK=0
WARN=1
CRIT=2
UNKNOWN=3
# Default return value RETVAL is UNKNOWN
retval=$UNKNOWN


while [[ $# -gt 1 ]]
do
key="$1"

case $key in
    -w|--warn)
    EXTENSION="$2"
    shift # past argument
    ;;
    -c|--crit)
    SEARCHPATH="$2"
    shift # past argument
    ;;
    -h|--help)
    usage
    shift # past argument
    ;;
    --default)
    DEFAULT=YES
    ;;
    *)
            # unknown option
    ;;
esac
shift # past argument or value
done

usage()
{
  echo "usage: $0 -w <WARN> -c <CRIT>"
  echo "-w <WARN> warning level for MTU size in Bytes."
  echo "-c <CRIT> critical level for MTU size in Bytes."
  echo "-h this usage help"
  exit 0
}

mtu()
{
  MTU=`dig +short rs.dns-oarc.net txt | grep "at least" | awk '{print substr($9, 1, length($9)-1)}'`
}

assess()
{
  if [ "$?" -ne 0 ]
  then
    echo "Determination of MTU size failedi.|$MTU"
    retval=$UNKNOWN
  fi

  if [[ "$MTU" -le "$CRITICAL" ]]
  then
    echo "MTU size is only $MTU Bytes"
    retval=$CRIT
  elif [[ "$MTU" -le "$WARN" ]]
  then
    echo "MTU size is $MTU Bytes"
    retval=$WARN
  else
    echo "MTU size is $MTU Bytes"
    retval=$OK
  fi
}


# Do the work by calling the functions
mtu
assess

exit $retval
