#!/usr/local/bin/bash # # check_netstat_combined - Nagios netstat check script (perfdata only) # version 1.0 15.11.2012 # by Mikanoshi - iam@mikanoshi.name, http://en.mikanoshi.name # tested on FreeBDS 8.3 # # This Nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. # It may be used, redistributed and/or modified under the terms of the # GNU General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt). STATE_OK=0 STATE_WARNING=1 STATE_CRITICAL=2 STATE_UNKNOWN=3 GET_CONN=FALSE GET_PORTS=FALSE NETSTAT="/usr/local/libexec/nagios/netstat" AWK=`which awk 2>/dev/null` function usage { printf "\nUsage: check_netstat_combined [-c] [-p ]\n" printf " -c Connections per second (requested, accepted, failed, established)\n" printf " -p Comma-separated list of ports, e.g.:\n check_netstat_combined -p 80,8080,21,25\n" } while getopts p:ch argcase "$@" do case $argcase in c) GET_CONN=TRUE;; p) GET_PORTS=TRUE PORTS=($OPTARG);; h) usage exit;; esac done if [[ $GET_CONN = "FALSE" && $GET_PORTS = "FALSE" ]] ; then usage exit $STATE_UNKNOWN fi printf "OK - data received |" if [ $GET_CONN = "TRUE" ] ; then $NETSTAT -s 2>/dev/null | awk ' /connection requests/ { printf " conn_requested="$1"c;;;" } /connection accepts/ { printf " conn_accepted="$1"c;;;" } /bad connection/ { printf " conn_failed="$1"c;;;" } /connections established/ { printf " conn_established="$1"c;;;\n" }' fi if [ $GET_PORTS = "TRUE" ] ; then $NETSTAT -nptcp 2>/dev/null | egrep -v 'Active|Address|sockets' | $AWK '{print ($4)}' | cut -d. -f 5 | sort | uniq -ci | sort | \ $AWK -v ports="$PORTS" 'BEGIN { split(ports, portsarr, ",") } { for (i in portsarr) if (portsarr[i] == $2) printf " port%s=%s;;;",$2,$1 }' printf "\n" fi exit $STATE_OK