forked from julian-heng/yabai-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_cpu.sh
executable file
·201 lines (165 loc) · 4.17 KB
/
show_cpu.sh
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash
check_apps()
{
if ! type -p sysctl osascript 2>&1 > /dev/null; then
return 1
fi
}
trim()
{
[[ "$*" ]] && {
set -f
set -- $*
printf "%s" "$*"
set +f
}
}
notify()
{
title="$(trim "${title_parts[*]}")"
subtitle="$(trim "${subtitle_parts[*]}")"
message="$(trim "${message_parts[*]}")"
[[ "${title:0:1}" == "|" ]] && \
title="${title##'| '}"
[[ "${title:-1:1}" == "|" ]] && \
title="${title%%' |'}"
[[ "${subtitle:0:1}" == "|" ]] && \
subtitle="${subtitle##'| '}"
[[ "${subtitle:-1:1}" == "|" ]] && \
subtitle="${subtitle%%' |'}"
[[ "${message:0:1}" == "|" ]] && \
message="${message##'| '}"
[[ "${message:-1:1}" == "|" ]] && \
message="${message%%' |'}"
if [[ "${stdout}" ]]; then
[[ "${title}" ]] && \
display+=("${title}")
[[ "${subtitle}" ]] && \
display+=("${subtitle}")
[[ "${message}" ]] && \
display+=("${message}")
printf "%s\\n" "${display[@]}"
else
osa_script="display notification \"${message}\" \
with title \"${title}\" \
subtitle \"${subtitle}\""
/usr/bin/env osascript <<< "${osa_script}"
fi
}
get_cpu()
{
cpu="${cpu_info[0]/@/(${cpu_info[1]}) @}"
cpu="${cpu/(R)}"
cpu="${cpu/(TM)}"
cpu="${cpu/ CPU}"
}
get_load()
{
read -r _ a b c _ <<< "${cpu_info[2]}"
load_avg="$a $b $c"
}
get_cpu_usage()
{
awk_script='
{ sum += $3 }
END {
printf "%f", sum / cores
}'
cpu_usage="$(awk -v cores="${cpu_info[1]:-1}" \
-v sum="0" \
"${awk_script}" <(ps aux))"
printf -v cpu_usage "%.*f" "1" "${cpu_usage}"
}
get_fan_temp()
{
type -p osx-cpu-temp 2>&1 > /dev/null && {
while read -r line; do
case "${line}" in
"CPU"*) temp="${line#*:}" ;;
"Fan "[0-9]*) fan="${line/'Fan '}" ;;
esac
done < <(osx-cpu-temp -f -c)
printf -v temp "%.*f" "1" "${temp/'°C'}"
fan="${fan/*at }"
fan="${fan/ RPM*}"
}
}
get_uptime()
{
boot="${cpu_info[3]/'{ sec = '}"
boot="${boot/,*}"
seconds="$(($(printf "%(%s)T" "-1") - boot))"
days="$((seconds / 60 / 60 / 24))d "
hours="$((seconds / 60 / 60 % 24))h "
mins="$((seconds / 60 % 60))m "
secs="$(((seconds % 60) % 60))s"
((${days/d*} == 0)) && unset days
((${hours/h*} == 0)) && unset hours
((${mins/m*} == 0)) && unset mins
((${secs/s} == 0)) && unset secs
uptime="${days}${hours}${mins}${secs}"
}
print_usage()
{
printf "%s\\n" "
Usage: ${0##*/} --option
Options:
[--stdout] Print to stdout
[-r|--raw] Print raw values delimited by commas
[-h|--help] Show this message
"
}
get_args()
{
while (($# > 0)); do
case "$1" in
"--stdout") stdout="true" ;;
"-r"|"--raw") raw="true" ;;
"-h"|"--help") print_usage; exit ;;
esac
shift
done
}
main()
{
! check_apps && exit 1
get_args "$@"
sysctl_args=(
"machdep.cpu.brand_string"
"hw.logicalcpu_max"
"vm.loadavg"
"kern.boottime"
)
mapfile -t cpu_info < <(sysctl -n ${sysctl_args[@]})
get_cpu
get_load
get_cpu_usage
get_fan_temp
get_uptime
[[ "${raw}" ]] && {
printf -v out "%s," \
"${cpu}" \
"${load_avg}" \
"${cpu_usage}%" \
"${temp}°C" \
"${fan} RPM"
printf -v out "%s%s" "${out}" "${uptime}"
out="$(trim "${out}")"
printf "%s\\n" "${out}"
exit 0
}
title_parts+=("${cpu:-CPU}")
[[ "${load_avg}" ]] && \
subtitle_parts+=("Load avg:" "${load_avg}")
[[ "${cpu_usage}" ]] && \
subtitle_parts+=("|" "${cpu_usage}%")
[[ "${temp}" ]] && \
subtitle_parts+=("|" "${temp}°C")
[[ "${fan}" ]] && \
subtitle_parts+=("|" "${fan} RPM")
[[ "${uptime}" ]] && \
message_parts+=("Uptime:" "${uptime}")
notify
}
[[ "${BASH_SOURCE[0]}" == "$0" ]] && \
main "$@"