-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu-usage
More file actions
executable file
·32 lines (26 loc) · 916 Bytes
/
cpu-usage
File metadata and controls
executable file
·32 lines (26 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
#
# 2020 Toazd cpu_usage.sh
#
# Purpose:
# Parse mpstat output (the last line - avg idle%) and convert it to usage%
# Requires mpstat (sysstat)
#
################################################################################
iINTERVAL=1
iREPORTS=1
sAVG_LINE=""
# map the output of mpstat into an indexed array
mapfile -s 2 -t <<< "$(mpstat $iINTERVAL $iREPORTS --dec=2)"
# we only want the last line which is the same no matter how many reports are chosen (index total - 1)
sAVG_LINE=${MAPFILE[${#MAPFILE[@]}-1]}
# remove all spaces including anything to the left of them
sAVG_LINE=${sAVG_LINE##* }
# round to 0 decimals (to avoid showing extraneous zeros *.0%), left bound the field (aligns better with icons/glyphs)
sCPU_USAGE=$(printf '%-.0f' "$(bc -l <<< 100-"${sAVG_LINE}")")%
if [[ ${sCPU_USAGE:0:1} -eq 0 ]]
then
printf '%s\n' "<1%"
else
printf '%s\n' "$sCPU_USAGE"
fi