Skip to content
Snippets Groups Projects
Commit f3518944 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

add check_conn - check tcp and udp connections

parent f93f964e
Branches
No related tags found
No related merge requests found
#!/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>
# ======================================================================
. `dirname $0`/inc_pluginfunctions
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(){
cat <<EOF
______________________________________________________________________
CHECK_CONN check if is a host available on a given port via tcp or udp
from local machine $( hostname -f ).
(c) Institute for Medical Education - Univerity of Bern
Licence: GNU GPL 3
______________________________________________________________________
SYNTAX:
`basename $0` 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:
`basename $0` If no param is given it checks entries in $cfgfile
`basename $0` -t "tcp/api.example.com/443"
Check a single connection
`basename $0` -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
echo "$myline" | grep -E "^(tcp|udp)/[a-z][a-z0-9\.\-]*/[0-9]*$" >/dev/null 2>&1
if [ $? -ne 0 ]; then
out="$out SKIP: INVALID ENTRY"
iWarnings=$iWarnings+1
else
>/dev/$myline
if [ $? -ne 0 ]; then
out="$out FAILED"
iErrors=$iErrors+1
else
out="$out OK"
iOK=$iOK+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
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
#
# This is an example file for check_tcp
# You need to rename it and then make your changes.
#
# lines not starting with tcp or udp will be ignored. So feel free
# to use # or ; as commented lines or add empty lines for spacing.
#
# ----------------------------------------------------------------------
# check if a needed api server is reachable
tcp/api.example.com/80
tcp/api.example.com/443
# check local mysql port
tcp/localhost/3306
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment