#! /bin/bash

source utils.sh
let -A errors

usage() { echo "Usage: $0 [-i <interface>] [-a <address>]" 1>&2; exit 1; }
nexit(){ MSG=${1:-OK}; CODE=${2:-$STATE_OK}; echo $MSG; exit $CODE; }

while getopts "hi:a:" o; do
    case "${o}" in
	h)
	    usage
            ;;
        i)
            interface=${OPTARG}
            ;;
        a)
            address=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done

[ -z "$interface" ] && nexit "Configuration error: missing interface" $STATE_UNKNOWN
[ -z "$address" ] && nexit "Configuration error: missing address" $STATE_UNKNOWN

# Check interface
/sbin/ip link show dev $interface &>/dev/null
[ $? -ne 0 ] && errors+=("Missing VPN interface $interface.")

# Check IP
/sbin/ip address show dev $interface 2>/dev/null | grep -q "inet $address"
[ $? -ne 0 ] && errors+=("Missing VPN address $address.")

[ 0 -eq ${#errors[@]} ] && nexit

nexit "${errors[@]}" $STATE_CRITICAL
