Skip to content
Snippets Groups Projects
check_netstat 1.83 KiB
#!/bin/bash
# ======================================================================
#
# Check NETSTAT and show connections
#
# requirements:
# - netstat
#
# ----------------------------------------------------------------------
# 2020-07-08  v1.0  <axel.hahn@iml.unibe.ch>
# ======================================================================


. `dirname $0`/inc_pluginfunctions


# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

# --- check required tools
ph.require netstat

# --- check param -h
if [ "$1" = "-h" ]; then
    echo "
    usage: $0 [ -w value -c value -h ]

        -w  Warning count  (optional)
        -c  Critical count (optional; must be larger than warning)
        -h  this help

    If there is no -w and -c then the result is always OK.
    "
    exit 0
fi

# --- set optional limits
typeset -i iWarnLimit=`     ph.getValueWithParam 0 w "$@"`
typeset -i iCriticalLimit=` ph.getValueWithParam 0 c "$@"`


# --- count all connections
tmpfile1=`mktemp`
tmpfile=`mktemp`

(netstat -wltun; netstat -wtun) | awk '{ print $6 }' | grep -E '(ESTABLISHED|LISTEN|TIME_WAIT)' >$tmpfile1
typeset -i iTotal=`wc -l $tmpfile1 | awk '{ print $1 }'`

if [ $iWarnLimit -gt 0 -a $iCriticalLimit -gt 0 ]; then
    ph.setStatusByLimit $iTotal $iWarnLimit $iCriticalLimit
fi 

ph.status "NETSTAT - count of connections: $iTotal"


# --- add performance data
cat $tmpfile1 | sort | uniq -c >$tmpfile
# result is something like that:
#      5 ESTABLISHED
#     10 LISTEN
#     77 TIME_WAIT

ph.perfadd "established" "`grep ESTABLISHED $tmpfile | awk '{ print $1 }' `"
ph.perfadd "listen"      "`grep LISTEN      $tmpfile | awk '{ print $1 }' `"
ph.perfadd "time-wait"   "`grep TIME_WAIT   $tmpfile | awk '{ print $1 }' `"


# --- bye
rm -f $tmpfile $tmpfile1
ph.exit