Skip to content
Snippets Groups Projects
Commit ef3347ea authored by Théo - Le Filament's avatar Théo - Le Filament
Browse files

fix CPU usage computation

parent fbc03dc1
Branches
No related tags found
No related merge requests found
......@@ -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'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment