-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacstat.sh
executable file
·241 lines (214 loc) · 6.02 KB
/
macstat.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
#SPDX-License-Identifier: GPL-2.0-only
# Copyright 2020 Johannes Eigner <[email protected]>
# Display important system statistics of macOS
# print help text
function help
{
cat <<HelpText
Usage: $(basename "${0}") [-bch]
Display important system statistics of macOS
-b Display bars
-c Coloured output
-h Display help
HelpText
}
# configuration
# currently we need to specify the device for measuring hdd usage
readonly cfgMacHddUsageDev="/dev/disk1s1"
# colors
readonly COL_WARN='\033[1;33m' # Warnings in yellow
readonly COL_ERR='\033[0;31m' # Errors in red
readonly COL_NC='\033[0m' # No Color
# measuremnt configuration
cfgDescription=("CPU" "RAM" "HDD space" "HDD inode" "Batt" "Batt Keyboard" "Batt Mouse" "TM Backup Age")
cfgFunction=(cpu ram hddUsage hddInode battery battKeyboard battMouse tmBackupAge)
cfgWarn=(80 70 75 20 20 20 20 86400)
cfgError=(90 80 85 30 10 10 10 172800)
function cpu
{
# cpu percentage
# Note: CPU usage is scaled per thread. So a CPU with n threads will report
# up to n * 100% on full usage.
local numThreads
numThreads=$(sysctl -n hw.ncpu)
local cpuTotal
cpuTotal=$(ps -A -o %cpu | awk '{s+=$1} END {print s}' | tr ',' '.')
echo "${cpuTotal}/${numThreads}" | bc
}
function ram
{
# ram percentage
# vm_stat or memory_pressure?
local ramFreePercent
ramFreePercent=$(memory_pressure | grep "System-wide memory free percentage" | cut -d: -f2 | tr -d %)
echo $(( 100 - ramFreePercent ))
}
function hddUsage
{
df | grep ${cfgMacHddUsageDev} | awk '{print $5}' | tr -d %
}
function hddInode
{
df | grep ${cfgMacHddUsageDev} | awk '{print $8}' | tr -d %
}
function battery
{
# main battery
# pmset -g batt | grep "InternalBattery" | awk '{print %3}'
# Return empty value for no battery. As I have no MacBook I can not test
# this functionality.
echo ""
}
function battMouse
{
ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2
}
function battKeyboard
{
ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i keyboard -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2
}
function tmBackupAge
{
local lastTmBackupDate
lastTmBackupDate=$(defaults read "/Library/Preferences/com.apple.TimeMachine.plist" Destinations | sed -n '/SnapshotDates/,$p' | grep -v 'StableLocalSnapshotDate' | grep -e '[0-9]' | awk -F '"' '{print $2}' | sort | tail -n1)
# convert to unix time for calculation
lastTmBackupDate=$(date -j -f "%Y-%m-%d %H:%M:%S %z" "${lastTmBackupDate}" +"%s")
local currentDate
currentDate=$(date +"%s")
echo "$((currentDate - lastTmBackupDate))"
}
# do all measurements
function measure
{
for idx in ${!cfgFunction[*]}
do
value[$idx]=$(${cfgFunction[idx]})
done
}
# Printing functions
# TODO calculate from cfgDescription
descLen=13
# Print usage bar
# printBar <value>
function printBar
{
local val
local fillLen
local freeLen
val="$1"
fillLen="$(( barLen * val / 100 ))"
freeLen="$(( barLen - fillLen ))"
printf " ["
if [[ $fillLen -gt 0 ]]; then
printf "%0.s#" $(seq 1 $fillLen)
fi
printf "%0.s " $(seq 1 $freeLen)
printf "]"
}
# Print measured percentage value
# printPercent <description> <value>
function printPercent
{
if [[ -n $2 ]]; then
printf "%-${descLen}s: $3%3d%%" "$1" "$2"
if [[ ${bars} -eq 1 ]]; then
printBar "$2"
fi
if [[ -n "$3" ]]; then
echo -ne "${COL_NC}"
fi
printf "\n"
fi
}
# Print measured time span value
# printPercent <description> <time span in seconds>
function printTimeSpan
{
if [[ -n $2 ]]; then
local span
local days
local hours
local mins
span=$2
days="$(( span / 60 / 60 / 24 ))"
hours="$(( ( span / 60 /60 ) % 24 ))"
mins="$(( ( span / 60 ) % 60 ))"
printf "%-${descLen}s: $3" "$1"
if [[ ${days} -gt 0 ]]; then
echo -n "${days}d"
fi
if [[ ${hours} -gt 0 || ${days} -gt 0 ]]; then
printf "%02dh" "${hours}"
fi
printf "%02dm" "${mins}"
if [[ -n "$3" ]]; then
echo -ne "${COL_NC}"
fi
printf "\n"
fi
}
function print
{
for idx in ${!cfgFunction[*]}
do
txtCol=""
if [[ ${color} -eq 1 ]]; then
# check for warnings/error
if [[ "${cfgError[$idx]}" -ge "${cfgWarn[$idx]}" ]]; then
# higher values are problematic
if [[ "${value[$idx]}" -ge "${cfgError[$idx]}" ]]; then
txtCol="${COL_ERR}"
elif [[ "${value[$idx]}" -ge "${cfgWarn[$idx]}" ]]; then
txtCol="${COL_WARN}"
fi
else
# lower values are problematic
if [[ "${value[$idx]}" -le "${cfgError[$idx]}" ]]; then
txtCol="${COL_ERR}"
elif [[ "${value[$idx]}" -le "${cfgWarn[$idx]}" ]]; then
txtCol="${COL_WARN}"
fi
fi
fi
# print it
if [[ "${value[$idx]}" -gt 100 ]]; then
printTimeSpan "${cfgDescription[idx]}" "${value[$idx]}" "${txtCol}"
else
printPercent "${cfgDescription[idx]}" "${value[$idx]}" "${txtCol}"
fi
done
}
# main
function main
{
# parse options
bars=0
color=0
while getopts bch opt; do
case $opt in
b)
bars=1
local colLen
colLen=$(tput cols)
barLen=$(( colLen - descLen - 9 ))
;;
c)
color=1
;;
h)
help
exit 0
;;
*)
echo "Invalid argument"
help
exit 1
;;
esac
done
measure
print
exit 0
}
main "$@"