#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

PROGNAME=${0##*/}
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.5 $' | sed -e 's/[^0-9.]//g'`
ARGS="$*"

. $PROGPATH/utils.sh

iptables=/usr/sbin/iptables
sudo=/usr/bin/sudo
chain=INPUT
table=filter
verbose=0
warning=1
critical=1

print_usage() {
    echo "Usage: $PROGNAME -C CHAIN -t TABLE"
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}

print_help() {
  print_revision $PROGNAME $REVISION
  echo ""
  print_usage
  echo ""
  echo "This plugin tests if iptables has needed amount of rules loaded"
  echo ""

  echo "-C CHAIN"
  echo "   Chain to list. Default: $chain"
  echo "-t TABLE"
  echo "   Table to list. Default: $table"
  echo "-v"
  echo "   Enable verbose run"
  echo "--help"
  echo "   Print this help screen"
  echo "--version"
  echo "   Print version and license information"
  echo ""

  support
  exit 0
}

list_iptables() {
  # if running as root, skip sudo
  [ "$(id -u)" != 0 ] || sudo=

  $sudo $list_iptables 2>/dev/null | grep -Fc /
}

while [ $# -gt 0 ]; do
  case "$1" in
  --help)
    print_help
    exit 0
    ;;

  -h)
    print_help
    exit 0
    ;;

  --version)
    print_revision $PROGNAME $REVISION
    exit 0
    ;;

  -V)
    print_revision $PROGNAME $REVISION
    exit 0
    ;;

  -v)
    verbose=1
    ;;

  -C)
    chain=$2; shift
    ;;

  -t)
    table=$2; shift
    ;;

  -w)
    warning=$2; shift
    ;;

  -c)
    critical=$2; shift
    ;;

  *)
    echo >&2 "Unknown argument: $1"
    print_usage
    exit $STATE_UNKNOWN
    ;;
  esac
  shift
done

rc=$STATE_UNKNOWN

list_iptables="$iptables -w -n -t $table -L $chain"

count=$(list_iptables)
if [ "$count" -lt "$critical" ]; then
  rc=$STATE_CRITICAL
  state=CRITICAL
elif [ "$count" -lt "$warning" ]; then
  rc=$STATE_WARNING
  state=WARNING
else
  rc=$STATE_OK
  state=OK
fi

echo "$state: $count iptables rules in $chain chain of $table table"

exit $rc
