Fix System Stats script (#6985)

automerge
This commit is contained in:
Sagar Dhawan
2019-11-15 13:25:40 -08:00
committed by Grimes
parent 946e937549
commit cab2232aba
2 changed files with 5 additions and 3 deletions

6
scripts/system-stats.sh Normal file → Executable file
View File

@ -11,10 +11,12 @@ cd "$(dirname "$0")/.."
source scripts/configure-metrics.sh
while true; do
# collect top twice because the first time is inaccurate
top_ouput="$(top -bn2 -d1)"
# collect the total cpu usage by subtracting idle usage from 100%
cpu_usage=$(top -bn1 | grep '%Cpu(s):' | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
cpu_usage=$(echo "${top_ouput}" | grep '%Cpu(s):' | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | tail -1 | awk '{print 100 - $1}')
# collect the total ram usage by dividing used memory / total memory
ram_total_and_usage=$(top -bn1 | grep 'MiB Mem'| sed "s/.*: *\([0-9.]*\)%* total.*, *\([0-9.]*\)%* used.*/\1 \2/")
ram_total_and_usage=$(echo "${top_ouput}" | grep '.*B Mem'| tail -1 | sed "s/.*: *\([0-9.]*\)%* total.*, *\([0-9.]*\)%* used.*/\1 \2/")
read -r total used <<< "$ram_total_and_usage"
ram_usage=$(awk "BEGIN {print $used / $total * 100}")