From 02f41b360356447231b6016f03f34da0bf841c83 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 10 Feb 2025 17:34:26 +0100
Subject: [PATCH 1/3] check_conn: no text on STDERR if portcheck fails

---
 check_conn | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)

diff --git a/check_conn b/check_conn
index 7848798..efab1fe 100755
--- a/check_conn
+++ b/check_conn
@@ -9,16 +9,13 @@
 # ----------------------------------------------------------------------
 # 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
-
-export self_APPVERSION=1.1
-
+. $(dirname $0)/inc_pluginfunctions || exit 1
+export self_APPVERSION=1.2
 cfgfile=$( dirname $0 )/$( basename $0 ).cfg
-
-
 out=""
 
 # ----------------------------------------------------------------------
@@ -87,12 +84,9 @@ case "$1" in
     *)
 esac
 
-configline=$( ph.getValueWithParam "" t "$@" )
-
+configline=$( ph.getValueWithParam "" t "$@" 
 test -z "$configline" || cfgfile=""
 
-
-
 typeset -i iWarnings=0
 typeset -i iErrors=0
 typeset -i iOK=0
@@ -107,18 +101,18 @@ do
 $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
+  if ! echo "$myline" | grep -Eq "^(tcp|udp)/[a-z][a-z0-9\.\-]*/[0-9]*$" 
+  then
     out="$out SKIP: INVALID ENTRY"
     iWarnings=$iWarnings+1
   else 
-    >/dev/$myline
-    if [ $? -ne 0 ]; then
-        out="$out FAILED"
-        iErrors=$iErrors+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
-- 
GitLab


From a766dadf968f3056761a7aa622db00d46cd08777 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 10 Feb 2025 17:35:51 +0100
Subject: [PATCH 2/3] fix closing bracket

---
 check_conn | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/check_conn b/check_conn
index efab1fe..bbf9842 100755
--- a/check_conn
+++ b/check_conn
@@ -84,7 +84,7 @@ case "$1" in
     *)
 esac
 
-configline=$( ph.getValueWithParam "" t "$@" 
+configline=$( ph.getValueWithParam "" t "$@" )
 test -z "$configline" || cfgfile=""
 
 typeset -i iWarnings=0
-- 
GitLab


From f67286d3883545405d60b7ffb3e2d458839ccaf9 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 10 Feb 2025 17:39:51 +0100
Subject: [PATCH 3/3] add help

---
 docs/20_Checks/check_conn.md | 77 ++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 docs/20_Checks/check_conn.md

diff --git a/docs/20_Checks/check_conn.md b/docs/20_Checks/check_conn.md
new file mode 100644
index 0000000..87db842
--- /dev/null
+++ b/docs/20_Checks/check_conn.md
@@ -0,0 +1,77 @@
+## Check TCP or UDP connection
+
+Script: `check_conn`
+
+It is a plugin to check oe or mmultiple tcp and udp connections to localhost or a remote system.
+
+## Standalone installation
+
+From this repository ypu need next to this script:
+
+* `inc_pluginfunctions` shared function for all IML checks written in bash
+
+## Syntax
+
+```txt
+______________________________________________________________________
+
+CHECK_CONN
+v1.2
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+
+https://os-docs.iml.unibe.ch/icinga-checks/Checks/check_conn.html
+______________________________________________________________________
+
+SYNTAX:
+check_conn OPTIONS
+
+You can create a file named ./check_conn.cfg
+and add your connections there. 
+To start you can copy the file ./check_conn.cfg.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:
+
+check_conn         If no param is given it checks entries in ./check_conn.cfg
+
+check_conn -t "tcp/api.example.com/443"
+                   Check a single connection
+
+check_conn -t "tcp/api.example.com/443 tcp/localhost/3306"
+                   Check multiple connections.
+
+
+```
+
+## Examples
+
+Check a single tcp port which is currently open:
+
+```txt
+./check_conn -t "tcp/localhost/80"
+OK: tcp check - 1 checks - 1 OK; 0 warnings; 0 errors 
+tcp/localhost/80 OK
+```
+
+Check multiple connections - one port is open the other closed:
+
+```txt
+./check_conn -t "tcp/localhost/22 tcp/localhost/443"
+CRITICAL: tcp check - 2 checks - 1 OK; 0 warnings; 1 errors 
+tcp/localhost/22 FAILED
+tcp/localhost/443 OK
+```
-- 
GitLab