Skip to content
Snippets Groups Projects
check_conn 3.32 KiB
#!/usr/bin/env bash
# ======================================================================
#
# Check connection ... is a host available on a given port via tcp or udp
#
# requirements:
# - none
#
# ----------------------------------------------------------------------
# 2021-11-05  v0.0  <axel.hahn@iml.unibe.ch>
# 2023-07-27  v1.1  <axel.hahn@unibe.ch>      update help page
# 2025-02-10  v1.2  <axel.hahn@unibe.ch>      no text on STDERR if portcheck fails
# ======================================================================


. $(dirname $0)/inc_pluginfunctions || exit 1
export self_APPVERSION=1.2
cfgfile=$( dirname $0 )/$( basename $0 ).cfg
out=""

# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------

function getChecks(){
  while [ $# -gt 0 ]; do
    test -r "$1" && grep -E "^(tcp|udp)/" "$1" 2>&1
    shift 1
  done
}


function showHelp(){
    local _self; _self=$(basename $0)
    cat <<EOF
$( ph.showImlHelpHeader )

SYNTAX:
$_self OPTIONS

You can create a file named $cfgfile
and add your connections there. 
To start you can copy the file $cfgfile.dist

OPTIONS:

    -h or --help   show this help.
    -t CONNECTION  test given connection; This param ignores entries
                   in the config file. For multiple connection tests
                   quote the parameter value and set spaces between
                   the connections.

PARAMETERS:

    CONNECTION     Connection in the same syntax like in the config:
                   tcp/host/port
                   udp/host/port
EXAMPLES:

$_self         If no param is given it checks entries in $cfgfile

$_self -t "tcp/api.example.com/443"
                   Check a single connection

$_self -t "tcp/api.example.com/443 tcp/localhost/3306"
                   Check multiple connections.

EOF
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

# --- check required tools
# ph.require bc top


# --- check param -h
case "$1" in
    "--help"|"-h")
        showHelp
        exit 0
        ;;
    *)
esac

configline=$( ph.getValueWithParam "" t "$@" )
test -z "$configline" || cfgfile=""

typeset -i iWarnings=0
typeset -i iErrors=0
typeset -i iOK=0
typeset -i iCount=0

for myline in $( echo $configline; getChecks $cfgfile )
do

  iCount=$iCount+1
  
  out="$out
$myline"

  # --- syntax check of config entry
  if ! echo "$myline" | grep -Eq "^(tcp|udp)/[a-z][a-z0-9\.\-]*/[0-9]*$" 
  then
    out="$out SKIP: INVALID ENTRY"
    iWarnings=$iWarnings+1
  else 
    if timeout 1 bash -c "> /dev/$myline " 2>/dev/null
    then
        out="$out OK"
        iOK=$iOK+1
    else
        out="$out FAILED"
        iErrors=$iErrors+1
    fi
  fi
done

# ----- output
if [ $iCount -eq 0 ]; then
ph.setStatus "unknown"
ph.status "tcp check - NO checks"
test $iCount    -eq 0 && echo "
No config entry was found. Create a file named $cfgfile
and add your connections i.e.
tcp/www.example.com/443"
else
    test $iWarnings -gt 0 && ph.setStatus "warning"
    test $iErrors   -gt 0 && ph.setStatus "critical"
    ph.status "tcp check - $iCount checks - $iOK OK; $iWarnings warnings; $iErrors errors $out"
fi

# ----- CLEANUP AND BYE!

ph.exit

# ----------------------------------------------------------------------