blob: ff3ddb106fdb48a2d1a3e9f5702fe7499a2d9877 (
plain)
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
|
#!/bin/sh
# grab previous stats
cache="/tmp/cpuload"
[ ! -f "$cache" ] || touch "$cache"
# get the currents cpu stats variables
curr_stats=$(head -n 1 /proc/stat)
prev_stats=$(cat "$cache")
# get the stats
curr_idle=$(echo "$curr_stats" | awk ' { print $5 } ')
prev_idle=$(echo "$prev_stats" | awk ' { print $5 } ')
curr_sum=$(echo "$curr_stats" | awk '{ print ($2+$3+$4+$5+$6+$7+$8+$9+$10) }')
prev_sum=$(cat "$cache" | awk '{ print ($2+$3+$4+$5+$6+$7+$8+$9+$10) }')
if [ -n "$prev_sum" ] ; then
# get the delta by subtracting the the current total from the previous total
delta=$(($curr_sum - $prev_sum))
# calculate idle delta
idle=$(($curr_idle - $prev_idle))
# caclulate working time
used=$(($delta - $idle))
# caculate usage
cpu_usage=$((100 * $used / $delta))
echo "$cpu_usage%"
fi
echo "$curr_stats" > "$cache"
|