From af9ce687aa48634cf34a226f5e6ae84d3e14cc7c Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 29 Jan 2024 09:29:55 +0100
Subject: [PATCH] update _detectDockererror

---
 check_docker_stats     | 19 ++-----------------
 inc_dockerfunctions.sh | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/check_docker_stats b/check_docker_stats
index d215f2f..3c5d60d 100755
--- a/check_docker_stats
+++ b/check_docker_stats
@@ -45,7 +45,7 @@ OPTIONS:
 
   General:
   -h, --help        this help
-  -d, --debug       Debug; Flag: show all docker system infos
+  -d, --debug       Debug; Flag: show docker env values.
 
   Connect to docker:
   -p, --path        Custom directory for docker binary
@@ -127,22 +127,7 @@ _detectDockerenv
 # --- get stats data
 
 data=$( sudo -n --preserve-env docker stats --no-stream --format '{{ json . }}' )
-
-if [ -z "$data" ]; then
-  # zero output could mean:
-  # - connect to docker failed
-  # - no data because no container is running
-  # --> let's detect it...
-  infos=$( sudo -n --preserve-env docker system info --format '{{ json . }}' )
-  _detectDockererror "$infos"
-
-  typeset -i iCRunning;
-  iCRunning=$(_getString "$data" ".ContainersRunning" )
-  if [ "$iCRunning" -eq "0" ]; then
-    ph.status "$sLabel >> Do data. No container is running"
-    ph.exit
-  fi
-fi
+_detectDockererror "$data"
 
 
 # --- checks to detect maximum
diff --git a/inc_dockerfunctions.sh b/inc_dockerfunctions.sh
index 761ab31..e34e425 100644
--- a/inc_dockerfunctions.sh
+++ b/inc_dockerfunctions.sh
@@ -12,6 +12,7 @@
 # https://docs.docker.com/engine/reference/commandline/docker/
 # ----------------------------------------------------------------------
 # 2024-01-19  v1.0  <axel.hahn@unibe.ch>      init
+# 2024-01-29  v1.1  <axel.hahn@unibe.ch>      _detectDockererror
 # ======================================================================
 
 
@@ -67,10 +68,28 @@ function _debugInfos(){
 # detect error after dicker command. It stops if
 # - no content was fetched
 # - content contains key "ServerErrors"
+# param  string  output of docker command (json)
+# param  bool    flag: is output from docker system info
 function _detectDockererror(){
 
     local data="$1"
     if [ -z "$data" ] ; then
+        if [ "$2" != "1" ]; then
+            # zero output could mean:
+            # - connect to docker failed
+            # - no data because no container is running
+            # --> let's detect it...
+            infos=$( sudo -n --preserve-env docker system info --format '{{ json . }}' )
+            _detectDockererror "$infos"
+
+            typeset -i iCRunning;
+            iCRunning=$(_getString "$data" ".ContainersRunning" )
+            if [ "$iCRunning" -eq "0" ]; then
+                ph.status "No data. No container is running"
+                ph.exit
+            fi
+        fi
+
         ph.setStatus unknown
         ph.status "No data. Unable to fetch Docker information."
         ph.exit
-- 
GitLab