Binary Talks

Update 1.1: UDP Packet Loss Check Plugin for Nagios

logo_nagiosI’ve changed the small UDP packet loss script yesterday so that it’s now showing the total requests, requests sent to an unknown port and receive errors per check interval. That means, if you’d choose a check interval of 5 minutes, the script shows the amount of the previous mentioned for this time frame. It now creates two temporary files in a directory which you may define via -t/–path-tmp. The file is automatically deleted when it’s size got over 32Kb.
Feel free to grab it from MonitoringExchange (they’ve changed their name due to legal problems with Nagios Enterprises and because of Nagios’ new fork Icinga), via svn or copy’n'paste below.

user@host ~ $ svn co https://svn.matejunkie.com/svn/nagios-plugins/stable/check_udp-packet-loss/ check_udp-packet-loss/
#!/bin/sh
 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
PROGNAME=`basename $0`
VERSION="Version 1.1,"
AUTHOR="2009, Mike Adolphs (http://www.matejunkie.com/)"
 
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
 
path_tmp="/tmp"
 
print_version() {
    echo "$VERSION $AUTHOR"
}
 
print_help() {
    print_version $PROGNAME $VERSION
    echo ""
    echo "$PROGNAME is a Nagios plugin to monitor the UDP packet"
    echo "loss via netstat. It writes a temporary file to a configurable"
	echo "directory and compares it's values against the ones from the check"
	echo "before."
	echo "That means the actual result shows the amount of overall packets,"
	echo "received, receive errors and/or packets recieved to unknown port"
	echo "for the last check interval you've chosen."
    echo ""
    echo "$PROGNAME check_udp-packet-loss.sh"
    echo ""
    echo "Options:"
    echo "  --warning|-w)"
    echo "    Sets a warning level for packet receive errors. Default is:"
    echo "    off"
    echo "  --critical|-c)"
    echo "    Sets a critical level for packet receive errors. Default is:"
    echo "    off"
    echo "  --path-tmp|-t)"
    echo "    Sets the path for the temporary file. Default is: /tmp"
    exit $ST_UK
}
 
while test -n "$1"; do
    case "$1" in
        --help|-h)
            print_help
            exit $ST_UK
            ;;
        --version|-v)
            print_version $PROGNAME $VERSION
            exit $ST_UK
            ;;
        --path-tmp|-t)
            path_tmp=$2
            shift
            ;;
        --warning|-w)
            warn=$2
            shift
            ;;
        --critical|-c)
            crit=$2
            shift
            ;;
        *)
            echo "Unknown argument: $1"
            print_help
            exit $ST_UK
            ;;
    esac
    shift
done
 
check_filesize() {
    if [ -f $path_tmp/udp-errors.log ]
    then
        size=`du $path_tmp/udp-errors.log | awk '{print $1}'`
	if [ $size -gt 32 ]
        then
            rm $path_tmp/udp-errors.log
            touch $path_tmp/udp-errors.log
        fi
    else
        touch $path_tmp/udp-errors.log
    fi   
}
 
get_packet_loss() {
	date_beg=`date`
	echo "+++ $date_beg +++" >> $path_tmp/udp-errors.log
    netstat -us|grep 'packets received' >> $path_tmp/udp-errors.log
    netstat -us|grep 'to unknown port' >> $path_tmp/udp-errors.log
    netstat -us|grep 'packet receive errors' >> $path_tmp/udp-errors.log
	date_end=`date`
	echo "--- $date_end ---" >> $path_tmp/udp-errors.log
	tac /tmp/udp-errors.log | awk '/---/{p=1} p{print} /+++/{p=0;if \
(count++==1) exit}' > $path_tmp/udp-errors.log.tmp
}
 
chk_packet_loss() {
	p_receive_1st=`tac $path_tmp/udp-errors.log.tmp | grep -m1 'packets \
received' | awk '{print $1}'`
	p_receive_2nd=`tac $path_tmp/udp-errors.log.tmp | grep 'packets \
received' | sort -r | grep -m1 'packets received' | awk '{print $1}'`
	p_unknown_1st=`tac $path_tmp/udp-errors.log.tmp | grep -m1 'to unknown \
port' | awk '{print $1}'`
	p_unknown_2nd=`tac $path_tmp/udp-errors.log.tmp | grep 'to unknown \
port' | sort -r | grep -m1 'to unknown port' | awk '{print $1}'`
	p_errors_1st=`tac $path_tmp/udp-errors.log.tmp | grep -m1 'receive \
errors' | awk '{print $1}'`
	p_errors_2nd=`tac $path_tmp/udp-errors.log.tmp | grep 'receive errors' \
| sort -r | grep -m1 'receive errors' | awk '{print $1}'`
 
	if [ ! $p_receive_2nd = 0 ]
	then
		p_receive=`expr $p_receive_2nd - $p_receive_1st`
	else
		p_receive=0
	fi
 
	if [ ! "$p_unknown_2nd" = 0 ]
	then
		p_unknown=`expr $p_unknown_2nd - $p_unknown_1st`
	else
		p_unknown=0
	fi
 
	if [ ! "$p_errors_2nd" = 0 ]
	then
		p_errors=`expr $p_errors_2nd - $p_errors_1st`
	else
		p_errors=0
	fi
}
 
val_wcdiff() {
    if [ ${warn} -gt ${crit} ]
    then
        wcdiff=1
    fi
}
 
check_filesize
get_packet_loss
chk_packet_loss
 
if [ -n "$warn" -a -n "$crit" ]
then
    val_wcdiff
    if [ "$wcdiff" = 1 ]
    then
        echo "The warning level should be lower than the critical level. \
Please adjust them."
        exit $ST_UK
    fi
fi
 
if [ -n "$warn" -a -n "$crit" ]
then
    if [ "$p_unknown" -ge "$warn" -a "$p_unknown" -lt "$crit" ]
    then
        echo "WARNING - $p_receive total packets received, $p_unknown \ 
went to unknown port, $p_errors receive errors! | \
'received'=$p_receive 'unknown_port'=$p_unknown 'errors'=$p_errors"
        exit $ST_WR
    elif [ "$p_unknown" -ge "$crit" ]
    then
        echo "CRITICAL - $p_receive total packets received, $p_unknown \
went to unknown port, $p_errors receive errors! | \
'received'=$p_receive 'unknown_port'=$p_unknown 'errors'=$p_errors"
        exit $ST_CR
    else
        echo "OK - $p_receive total packets received, $p_unknown \
went to unknown port, $p_errors receive errors! | \
'received'=$p_receive 'unknown_port'=$p_unknown 'errors'=$p_errors"
        exit $ST_OK
    fi
else
    echo "OK - $p_receive total packets received, $p_unknown went to unknown \
port, $p_errors receive errors! | 'received'=$p_receive \
'unknown_port'=$p_unknown 'errors'=$p_errors"
    exit $ST_OK
fi
Share and Enjoy:
  • del.icio.us
  • Digg
  • Slashdot
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Reddit
  • Yigg
  • Netvibes
  • MisterWong
  • Facebook
  • HackerNews
  • Identi.ca
  • FriendFeed
  • NewsVine

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

*Required Fields