#!/bin/bash
#
# check used traffic with vnstat
#
# BINARIES
VNSTAT_BIN=/usr/bin/vnstat
GREP_BIN=/bin/grep
PS_BIN=/bin/ps
CAT_BIN=/bin/cat
ECHO_BIN=/bin/echo
CUT_BIN=/usr/bin/cut
# OPTIONS
VNSTAT_OPTIONS="--dumpdb"
PS_OPTIONS="-e"
# vmstatd.pid
VNSTATD_PID="/var/run/vnstat.pid"
# NAGIOS results
NAGIOS_OK=0
NAGIOS_WARN=1
NAGIOS_CRIT=2
NAGIOS_UNKNOWN=3
function usage() {
${ECHO_BIN} ""
${ECHO_BIN} "USAGE:"
${ECHO_BIN} "$0 OPTION"
${ECHO_BIN} " OPTION:"
${ECHO_BIN} " d - daily traffic"
${ECHO_BIN} " m - monthly traffic"
${ECHO_BIN} " w - warn MiB"
${ECHO_BIN} " c - warn MiB"
${ECHO_BIN} " v - Verbose"
${ECHO_BIN} " h - this help"
${ECHO_BIN} ""
exit 3
}
system=0
verbose=0
warn=0
crit=0
timerange="x"
while getopts dmw:c:h option
do
case ${option} in
d)
[ ${timerange} == "x" ] && timerange="d" || usage
system=1
;;
m)
[ ${timerange} == "x" ] && timerange="m" || usage
system=1
;;
w)
warn=$OPTARG
;;
c)
crit=$OPTARG
;;
h)
system=0
;;
?)
system=0
;;
esac
done
[ ${system} == 0 ] && usage
[ ${warn} -le 0 ] && usage
[ ${crit} -le 0 ] && usage
[ ${warn} -ge ${crit} ] && usage
# check if vnstatd is running
if [ -r ${VNSTATD_PID} ]; then
vnstatd_pid=`${CAT_BIN} ${VNSTATD_PID}`
vnstatd_count=`${PS_BIN} ${PS_OPTIONS} | ${GREP_BIN} -c vnstatd`
if [ ${vnstatd_count} -ne 1 ]; then
${ECHO_BIN} "UNKNOWN: no vnstatd running"
exit ${NAGIOS_UNKNOWN}
fi
else
${ECHO_BIN} "UNKNOWN: no ${VNSTATD_PID} found"
exit ${NAGIOS_UNKNOWN}
fi
vnstat_output=`${VNSTAT_BIN} ${VNSTAT_OPTIONS} | ${GREP_BIN} "^${timerange};0"`
vnstat_rx=`${ECHO_BIN} ${vnstat_output} | ${CUT_BIN} -d\; -f4`
vnstat_tx=`${ECHO_BIN} ${vnstat_output} | ${CUT_BIN} -d\; -f5`
vnstat_sum=$((vnstat_rx+vnstat_tx))
if [ ${vnstat_sum} -ge ${warn} ]; then
if [ ${vnstat_sum} -ge ${crit} ]; then
${ECHO_BIN} "CRITICAL: ${vnstat_rx} MiB RX, ${vnstat_tx} MiB TX, ${vnstat_sum} MiB total transfer";
exit ${NAGIOS_CRIT}
fi
${ECHO_BIN} "WARNING: ${vnstat_rx} MiB RX, ${vnstat_tx} MiB TX, ${vnstat_sum} MiB total transfer";
exit ${NAGIOS_WARN}
else
${ECHO_BIN} "OKAY: ${vnstat_rx} MiB RX, ${vnstat_tx} MiB TX, ${vnstat_sum} MiB total transfer";
exit ${NAGIOS_OK}
fi