diff --git a/check_docker.sh b/check_docker.sh index 7e05f500a668512b0341629548934361d3565efe..733a444551b3ebc7e51e9d9f35a816c379ceb6e7 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'