-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_cpu
executable file
·56 lines (42 loc) · 1.35 KB
/
mod_cpu
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env bash
. .dzenbarrc
MONITOR=${1}
PIPE_LINE=${2}
stat_before=$(cat /proc/stat | head -n 5)
cpu_cores=$(nproc) # number of cpu cores
sleep 0.01
getCoreLoadPercent() {
local core=${1}
before=$(echo "${stat_before}" | grep "^cpu${core} ")
after=$(echo "${stat_after}" | grep "^cpu${core} ")
user0=$(echo "${before}" | awk '{ print $2 }')
user1=$(echo "${after}" | awk '{ print $2 }')
nice0=$(echo "${before}" | awk '{ print $3 }')
nice1=$(echo "${after}" | awk '{ print $3 }')
syst0=$(echo "${before}" | awk '{ print $4 }')
syst1=$(echo "${after}" | awk '{ print $4 }')
idle0=$(echo "${before}" | awk '{ print $5 }')
idle1=$(echo "${after}" | awk '{ print $5 }')
user=$(expr ${user1} - ${user0})
nice=$(expr ${nice1} - ${nice0})
syst=$(expr ${syst1} - ${syst0})
idle=$(expr ${idle1} - ${idle0})
total=$(expr ${user} + ${nice} + ${syst} + ${idle})
used=$(expr ${user} + ${nice} + ${syst})
echo "${used} * 100 / ${total}" | bc
}
while true; do
stat_after=`cat /proc/stat | head -n 5`
output=" $(iconColor 'cpu') "
let "last = ${cpu_cores} - 1"
for ((i=0; i < ${cpu_cores}; i++)); do
output="${output}$(textColor "$(getCoreLoadPercent ${i})")%"
if [[ "${i}" != "${last}" ]]; then
output="${output} \/ "
fi
done
#echo $output
putLineToPipeFile ${MONITOR} ${PIPE_LINE} "${output}"
stat_before=${stat_after}
sleep ${CPU_UPDATE}
done