-
Hahn Axel (hahn) authoredHahn Axel (hahn) authored
check_netstat 2.69 KiB
#!/bin/bash
# ======================================================================
#
# Check NETSTAT and show connections
#
# requirements:
# - netstat
#
# ----------------------------------------------------------------------
# 2020-07-08 v1.0 <axel.hahn@iml.unibe.ch>
# 2022-10-25 v1.1 <axel.hahn@unibe.ch> shell fixes; no tmpfiles; IML look
# 2023-08-22 v1.2 <axel.hahn@unibe.ch> update help; small shell fixes
# ======================================================================
. $( dirname $0 )/inc_pluginfunctions
export self_APPVERSION=1.2
# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------
function showHelp(){
local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )
Count number of network connections.
SYNTAX:
$_self [ -w value -c value -h ]
-w VALUE warning level for connectios total
-c VALUE critical level
-h or --help show this help.
PARAMETERS:
None.
EXAMPLES:
$_self Show count of current connections
If there is no -w and -c then the result is always OK.
$_self -w 400 -c 500
Show count of connections incl. limits for warning
and critical.
EOF
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
# --- check required tools
ph.require netstat
# --- check param -h
case "$1" in
"--help"|"-h")
showHelp
exit 0
;;
*)
esac
# --- set optional limits
typeset -i iWarnLimit; iWarnLimit=$( ph.getValueWithParam 0 w "$@")
typeset -i iCriticalLimit; iCriticalLimit=$( ph.getValueWithParam 0 c "$@")
# --- count all connections
typeset -i iTotal=0
typeset -i iEst=0
typeset -i iListen=0
typeset -i iWait=0
netdata=$( (netstat -wltun; netstat -wtun) | awk '{ print $6 }' | grep -E '(ESTABLISHED|LISTEN|TIME_WAIT)' )
iTotal=$( echo "$netdata" | wc -l | 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
data2=$( echo "$netdata" | sort | uniq -c )
# result is something like that:
# 5 ESTABLISHED
# 10 LISTEN
# 77 TIME_WAIT
echo "$data2"
iEst=$( echo "$data2" | grep ESTABLISHED | awk '{ print $1 }' )
iListen=$( echo "$data2" | grep LISTEN | awk '{ print $1 }' )
iWait=$( echo "$data2" | grep TIME_WAIT | awk '{ print $1 }' )
ph.perfadd "established" "$iEst"
ph.perfadd "listen" "$iListen"
ph.perfadd "time-wait" "$iWait"
# --- bye
ph.exit