From fbd45da44140b1014e61ecb51ce19fdb78da7fda Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 23 Jun 2025 16:51:39 +0200
Subject: [PATCH] docker info: - check status output "(healthy)" - speedup
 output of containers

---
 check_docker_info                   | 36 ++++++++++++++++-------------
 docs/20_Checks/check_docker_info.md |  8 +++----
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/check_docker_info b/check_docker_info
index 8f936a2..6db334b 100755
--- a/check_docker_info
+++ b/check_docker_info
@@ -19,13 +19,14 @@
 # 2024-01-29  v1.4  <axel.hahn@unibe.ch>      add --target option
 # 2024-02-13  v1.5  <axel.hahn@unibe.ch>      update docker ps --format
 # 2025-02-10  v1.6  <axel.hahn@unibe.ch>      harden sourcing files
+# 2025-06-23  v1.7  <axel.hahn@unibe.ch>      check status output "(healthy)"; speedup output of containers
 # ======================================================================
 
 # shellcheck source=/dev/null
 . "$( dirname "$0" )/inc_pluginfunctions"    || exit 1
 . "$( dirname "$0" )/inc_dockerfunctions.sh" || exit 1
 
-self_APPVERSION=1.6
+self_APPVERSION=1.7
 
 # ----------------------------------------------------------------------
 # FUNCTIONS
@@ -66,8 +67,8 @@ OPTIONS:
                     multiple users run a rootless docker daemon.
 
   Flags:
-  -c, --containers  Flag: Show containers (slow)
-  -e, --errors      Flag: Show containers on error only (slow)
+  -c, --containers  Flag: Show containers
+  -e, --errors      Flag: Show containers on error only
 
 EXAMPLES:
   $_self
@@ -75,7 +76,7 @@ EXAMPLES:
 
   $_self -c
     Show Status of all containers and a list of container names with its
-    status. Warning: this feature uses docker ps --all and can be slow.
+    status.
 
   $_self -e
     Show Status of all containers. If not all containers are running you
@@ -178,20 +179,23 @@ echo "$out"
 if [ $bOptContainers -eq 1 ] && [ "$iCTotal" -gt "0" ]; then
     echo
     echo "Containers:"
-    # containers=$( sudo -n --preserve-env docker ps --all --format "{{ json . }}" --all 2>/dev/null )
-    containers=$( sudo -n --preserve-env docker ps --all --format '{"Names": "{{.Names}}", "State": "{{.State}}", "Status": "{{.Status}}" }' 2>/dev/null )
+    # containers=$( sudo -n --preserve-env docker ps --all --format '{"Names": "{{.Names}}", "State": "{{.State}}", "Status": "{{.Status}}" }' 2>/dev/null )
+    containers=$( sudo -n --preserve-env docker ps --format "table {{.Names}} | {{.State}} | {{.Status}}" --all 2>/dev/null | sed -n 2,\$p )
    
     echo "$containers" | while read -r line; do    
-      sName=$(   _getString "$line" ".Names" )
-      sState=$(  _getString "$line" ".State" )
-      sStatus=$( _getString "$line" ".Status" )
-      sIco=
-      # Icinga ui does not show these characters
-      # sIco="🔶"
-      # grep "exited"  <<< "${sState}" >/dev/null && sIco="❌"
-      # grep "running" <<< "${sState}" >/dev/null && sIco="✅"
-
-      printf "  %-14s %-40s %-20s\n" "$sIco $sState" "$sName" "$sStatus" 
+      contStatus="OK"
+      if grep -q "(" <<< "$line"; then
+        if grep -Fv "(healthy)" <<< "$line" | grep -q "("; then
+          contStatus="??"
+          # commented because within a while loop I am in a subshell
+          # ph.setStatus critical
+        fi
+      else
+        if ! grep -q "| running |" <<< "$line"; then
+          contStatus="??"
+        fi
+      fi
+      echo "$contStatus $line"
 
     done 
     echo
diff --git a/docs/20_Checks/check_docker_info.md b/docs/20_Checks/check_docker_info.md
index 044079f..3de3b8f 100644
--- a/docs/20_Checks/check_docker_info.md
+++ b/docs/20_Checks/check_docker_info.md
@@ -37,7 +37,7 @@ If DOCKER_HOST is not set then the docker socket is detected from a running dock
 ______________________________________________________________________
 
 CHECK_DOCKER_INFO
-v1.6
+v1.7
 
 (c) Institute for Medical Education - University of Bern
 Licence: GNU GPL 3
@@ -73,8 +73,8 @@ OPTIONS:
                     multiple users run a rootless docker daemon.
 
   Flags:
-  -c, --containers  Flag: Show containers (slow)
-  -e, --errors      Flag: Show containers on error only (slow)
+  -c, --containers  Flag: Show containers
+  -e, --errors      Flag: Show containers on error only
 
 EXAMPLES:
   check_docker_info
@@ -82,7 +82,7 @@ EXAMPLES:
 
   check_docker_info -c
     Show Status of all containers and a list of container names with its
-    status. Warning: this feature uses docker ps --all and can be slow.
+    status.
 
   check_docker_info -e
     Show Status of all containers. If not all containers are running you
-- 
GitLab