This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu_usage
executable file
·71 lines (60 loc) · 2 KB
/
cpu_usage
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
set -e
# Setup default values
full_format="${full_format:-%usage}"
# format_above_threshold is not set
# format_above_degraded_threshold is not set
path="${path:-/proc/stat}"
max_threshold="${max_threshold:-95}"
degraded_threshold="${degraded_threshold:-90}"
if ! [ -e "$path" ]
then
echo "cant read cpu usage"
exit 0
fi
# Set full text format string and color
fulltext_format="$full_format"
color="#ffffff"
extra_json+=",\"color\":\"$color\""
# Compute cpuX placeholder value from procfile (current) and variable (previous)
# user: normal processes executing in user mode
# nice: niced processes executing in user mode
# system: processes executing in kernel mode
# idle: twiddling thumbs
# iowait: waiting for I/O to complete
# irq: servicing interrupts
# softirq: servicing softirqs
while read -a curr_cpu
do
id="${curr_cpu[0]}"
# Set prev_cpuX from $cpuX or $currr_cpuX if unset
eval "$(echo "read -a prev_cpu < <(echo "\${$id:-${curr_cpu[*]}}")")"
# Compute cpuX placeholder value
prev_total="$((prev_cpu[1] + prev_cpu[2] + prev_cpu[3] + prev_cpu[4]))"
curr_total="$((curr_cpu[1] + curr_cpu[2] + curr_cpu[3] + curr_cpu[4]))"
idle="$((curr_cpu[4] - prev_cpu[4]))"
total="$((curr_total - prev_total))"
usage=0
if [[ $total -ne 0 ]]
then
usage="$((((1000 * (total - idle) / total + 5) / 10)))"
fi
eval "$(echo "_$id='$(printf "%02d%%" "$usage")'")"
# Propagate cpu stat
extra_json+=",\"$id\":\"${curr_cpu[*]}\""
done < <(grep -E "^cpu[0-9]* " "$path")
# Compute usage placeholder value
_usage="$_cpu"
# Check for threshold and degraded threshold to update formatted string
if [[ $cpu -ge $max_threshold ]]
then
color="${color_bad:-#ff0000}"
fulltext_format="${format_above_threshold-$full_format}"
elif [[ $cpu -ge $degraded_threshold ]]
then
color="${color_degraded:-#ffff00}"
fulltext_format="${format_above_degraded_threshold-$full_format}"
fi
# Update json and set current cpu stat
eval "$(echo "fulltext=\"${fulltext_format//%/\$_}\"")"
printf '{"full_text":"%s"%s}\n' "$fulltext" "$extra_json"