From f35189442f37d158d9f13df7a750ab4825917053 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch> Date: Fri, 5 Nov 2021 17:01:56 +0100 Subject: [PATCH] add check_conn - check tcp and udp connections --- check_conn | 148 ++++++++++++++++++++++++++++++++++++++++++++ check_conn.cfg.dist | 19 ++++++ 2 files changed, 167 insertions(+) create mode 100755 check_conn create mode 100644 check_conn.cfg.dist diff --git a/check_conn b/check_conn new file mode 100755 index 0000000..0e90305 --- /dev/null +++ b/check_conn @@ -0,0 +1,148 @@ +#!/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 + +# ---------------------------------------------------------------------- diff --git a/check_conn.cfg.dist b/check_conn.cfg.dist new file mode 100644 index 0000000..2f1aa58 --- /dev/null +++ b/check_conn.cfg.dist @@ -0,0 +1,19 @@ +# ---------------------------------------------------------------------- +# +# 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 + +# ---------------------------------------------------------------------- -- GitLab