From 3ce24d6e8c3fe7793f785b903f8a9c78dc47a7aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o?= <theo@le-filament.com>
Date: Tue, 10 May 2022 17:43:28 +0200
Subject: [PATCH] improve output format of check_docker

---
 check_docker.sh | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/check_docker.sh b/check_docker.sh
index 51ad6a6..83e0a8d 100755
--- a/check_docker.sh
+++ b/check_docker.sh
@@ -63,11 +63,11 @@ mem_scale=";${mem_threshold_warning};${mem_threshold_critical};0;100"
 
 # Running containers.
 for container in ${running_containers}; do
-    output+=" OK: ${container} status is running"
+    output+="OK: ${container} status is running\n"
 done
 # Not running containers.
 for container in ${not_running_containers}; do
-    output+=" NOK: ${container} status is not running"
+    output+="NOK: ${container} status is not running\n"
 done
 
 # CPU and memory statistics for running containers.
@@ -80,15 +80,15 @@ for container_stats in ${running_containers_stats}; do
     status='OK'
     [[ ${cpu%.*} -ge $cpu_threshold_warning ]] && status='WARNING'
     [[ ${cpu%.*} -ge $cpu_threshold_critical ]] && status='CRITICAL'
-    output+=" ${status}: ${name} cpu is ${cpu}%"
+    output+="${status}: ${name} cpu is ${cpu}%\n"
 
     status='OK'
     [[ ${mem%.*} -ge $mem_threshold_warning ]] && status='WARNING'
     [[ ${mem%.*} -ge $mem_threshold_critical ]] && status='CRITICAL'
-    output+=" ${status}: ${name} memory is ${mem}%"
+    output+="${status}: ${name} memory is ${mem}%\n"
 
-    perf+=" ${name}_cpu=${cpu}%${cpu_scale}"
-    perf+=" ${name}_mem=${mem}%${mem_scale}"
+    perf+="${name}_cpu=${cpu}%${cpu_scale} "
+    perf+="${name}_mem=${mem}%${mem_scale} "
 done
 
 # Nagios OK status.
@@ -99,5 +99,5 @@ grep --quiet 'WARNING:' <(echo $output) && exit_code=1
 grep --quiet 'NOK:\|CRITICAL:' <(echo $output) && exit_code=2
 
 # Plugin output.
-echo "${output#' '}|${perf#' '}"
+echo "${output%'\n'}|${perf%' '}"
 exit $exit_code
-- 
GitLab