Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

how to get docker stats using shell script

#!/bin/bash

# This script is used to complete the output of the docker stats command.
# The docker stats command does not compute the total amount of resources (RAM or CPU)

# Get the total amount of RAM, assumes there are at least 1024*1024 KiB, therefore > 1 GiB
HOST_MEM_TOTAL=$(grep MemTotal /proc/meminfo | awk '{print $2/1024/1024}')

# Get the output of the docker stat command. Will be displayed at the end
# Without modifying the special variable IFS the ouput of the docker stats command won't have
# the new lines thus resulting in a failure when using awk to process each line
IFS=;
DOCKER_STATS_CMD=`docker stats --no-stream --format "table {{.MemPerc}}	{{.CPUPerc}}	{{.MemUsage}}	{{.Name}}"`

SUM_RAM=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
SUM_CPU=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
SUM_RAM_QUANTITY=`LC_NUMERIC=C printf %.2f $(echo "$SUM_RAM*$HOST_MEM_TOTAL*0.01" | bc)`

# Output the result
echo $DOCKER_STATS_CMD
echo -e "${SUM_RAM}%			${SUM_CPU}%		${SUM_RAM_QUANTITY}GiB / ${HOST_MEM_TOTAL}GiB	TOTAL"
Comment

PREVIOUS NEXT
Code Example
Typescript :: axios typescript 
Typescript :: nodejs express multer s3 
Typescript :: How to specify output directory in TypeScript? 
Typescript :: typerscript online compiler 
Typescript :: amcharts angular universal 
Typescript :: git rebase two commits to one 
Typescript :: how to add lint is declared but its value is never read. 
Typescript :: get random dark color 
Typescript :: google sheets mode text 
Typescript :: defining component layout next ts 
Typescript :: node js process on unhandled promise rejection 
Typescript :: state in react typescript 
Typescript :: Array.prototype.map() expects a return value from arrow function array-callback-return react 
Typescript :: cypress typescript example 
Typescript :: typescript generic object 
Typescript :: draw image html canvas 
Typescript :: factory design pattern typescript 
Typescript :: typescript err type 
Typescript :: typescript object key as enum 
Typescript :: sweetalert2 
Typescript :: Custom validation for phone-number using class-validator package 
Typescript :: how to set date axes limits in matplotlib plot 
Typescript :: <edit-config changes in this plugin conflicts with <edit-config changes in config.xml. Conflicts must be resolved before plugin can be added 
Typescript :: build with tsconfig-paths 
Typescript :: typescript type specific strings 
Typescript :: typescript var global: typeof globalThis 
Typescript :: react redux typescript 
Typescript :: git merge all previous commits on a branch 
Typescript :: typescript deep partial 
Typescript :: react typescript append to array 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =