From ef3347ea9c2cb7234a365d8d3efca4451f155228 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o?= <theo@le-filament.com>
Date: Thu, 12 May 2022 16:00:49 +0200
Subject: [PATCH] fix CPU usage computation

---
 check_docker.sh | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/check_docker.sh b/check_docker.sh
index 7e05f50..733a444 100755
--- a/check_docker.sh
+++ b/check_docker.sh
@@ -19,8 +19,8 @@ print_help() {
 while [[ $# -gt 0 ]]; do
     case "$1" in
         -c|--cpu)
-            cpu_threshold_warning=`awk -F ':' '{print +$1}' <(echo $2)`
-            cpu_threshold_critical=`awk -F ':' '{print +$2}' <(echo $2)`
+            cpu_threshold_warning=$(/usr/bin/awk -F ':' '{print +$1}' <(echo $2))
+            cpu_threshold_critical=$(/usr/bin/awk -F ':' '{print +$2}' <(echo $2))
             shift 2
             ;;
 
@@ -30,8 +30,8 @@ while [[ $# -gt 0 ]]; do
             ;;
 
         -m|--memory)
-            mem_threshold_warning=`awk -F ':' '{print +$1}' <(echo $2)`
-            mem_threshold_critical=`awk -F ':' '{print +$2}' <(echo $2)`
+            mem_threshold_warning=$(/usr/bin/awk -F ':' '{print +$1}' <(echo $2))
+            mem_threshold_critical=$(/usr/bin/awk -F ':' '{print +$2}' <(echo $2))
             shift 2
             ;;
 
@@ -48,11 +48,11 @@ while [[ $# -gt 0 ]]; do
 done
 
 # Gathering facts.
-all_containers=`sudo /usr/bin/docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet --all`
-running_containers=`sudo /usr/bin/docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet`
-running_containers_stats=`sudo /usr/bin/docker stats --format '{{.Name}}:{{.CPUPerc}}:{{.MemPerc}}' --no-stream`
+all_containers=$(sudo /usr/bin/docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet --all)
+running_containers=$(sudo /usr/bin/docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet)
+running_containers_stats=$(sudo /usr/bin/docker stats --format '{{.Name}}:{{.CPUPerc}}:{{.MemPerc}}' --no-stream)
 
-not_running_containers=`comm -23 <(echo $all_containers | /usr/bin/tr ' ' '\n') <(echo $running_containers | /usr/bin/tr ' ' '\n')`
+not_running_containers=$(comm -23 <(echo $all_containers | /usr/bin/tr ' ' '\n') <(echo $running_containers | /usr/bin/tr ' ' '\n'))
 
 # Format data.
 output=''
@@ -72,10 +72,12 @@ done
 
 # CPU and memory statistics for running containers.
 for container_stats in ${running_containers_stats}; do
-    name=`/usr/bin/awk -F ':' '{print $1}' <(echo $container_stats)`
+    name=$(/usr/bin/awk -F ':' '{print $1}' <(echo $container_stats))
     [[ ! $name =~ ${filter} ]] && continue
-    cpu=`/usr/bin/awk -F ':' '{print +$2}' <(echo $container_stats)`
-    mem=`/usr/bin/awk -F ':' '{print +$3}' <(echo $container_stats)`
+    all_cpu=$(/usr/bin/awk -F ':' '{print +$2}' <(echo $container_stats))
+    mem=$(/usr/bin/awk -F ':' '{print +$3}' <(echo $container_stats))
+    proc_number=$(nproc --all)
+    cpu=$(echo "scale=2; ${all_cpu} / ${proc_number}" | bc)
 
     status='OK'
     [[ ${cpu%.*} -ge $cpu_threshold_warning ]] && status='WARNING'
-- 
GitLab