/
check_bluetooth_battery
executable file
·245 lines (180 loc) · 5.55 KB
/
check_bluetooth_battery
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
241
242
243
244
245
#!/bin/sh
#
# See the INSTALL file for installation instructions
#
# Copyright (c) 2009-2011 ETH Zurich.
# Copyright (c) 2009-2021 Matteo Corti <matteo@corti.li>
#
# This module is free software; you can redistribute it and/or modify it
# under the terms of GNU general public license (gpl) version 3.
# See the LICENSE file for details.
VERSION=1.3.0
NAME="BLUETOOTH BATTERY"
################################################################################
# Functions
################################################################################
# Prints usage information
# Params
# $1 error message (optional)
usage() {
if [ -n "$1" ] ; then
echo "Error: $1" 1>&2
fi
echo
echo "Usage: check_bluetooth_battery [OPTIONS] -c critical -w warning"
echo
echo "Options:"
echo " -c critical"
echo " -w warning"
echo " -h, -? this help message"
echo " -V version"
echo
echo "Report bugs to: Matteo Corti <matteo@corti.li> or https://github.com/matteocorti/check_bluetooth_battery/issues"
echo
exit 3
}
################################################################################
# Exits with a critical message
# Params
# $1 error message
critical() {
echo "${NAME} CRITICAL: $1"
exit 2
}
################################################################################
# Exits with a warning message
# Param
# $1 warning message
warning() {
echo "${NAME} WARN: $1"
exit 1
}
################################################################################
# Exits with an 'unknown' status
# Param
# $1 message
unknown() {
echo "${NAME} UNKNOWN: $1"
exit 3
}
check_value() {
VALUE=$1
if ! echo "${VALUE}" | grep -q '^[0-9][0-9]*$' ; then
critical "${VALUE} is not a positive number"
fi
if [ "${VALUE}" -gt 100 ] ; then
critical "${VALUE} is larger than 100"
fi
}
################################################################################
# Checks if a given program is available and executable
# Params
# $1 program name
# Returns 1 if the program exists and is executable
check_required_prog() {
PROG=$(command -v "$1")
if [ -z "${PROG}" ] ; then
critical "cannot find $1"
fi
if [ ! -x "${PROG}" ] ; then
critical "${PROG} is not executable"
fi
}
get_level() {
NAME=$1
ioreg -r -l -n "$1" | grep '"BatteryPercent"\ =' | sed 's/[^[:digit:]]//g' | head -n 1
}
get_air_pod_level() {
NAME=$1
defaults read /Library/Preferences/com.apple.Bluetooth | grep "BatteryPercent${NAME}" | tr -d \; | awk '{print $3}'
}
check_device() {
device=$1
name=$2
description=$3
level=$(get_level "${device}")
if [ -z "${level}" ] ; then
level=$(get_air_pod_level "${device}")
fi
if [ -n "${level}" ] ; then
if [ -n "${VERBOSE}" ] ; then
echo "${description} detected (battery ${level}%)"
fi
if [ "${device}" = 'Case' ] && [ "${level}" -eq 0 ] ; then
if [ -n "${VERBOSE}" ] ; then
echo "empty ${description} detected (no battery level delivered)"
fi
else
PERFORMANCE="${PERFORMANCE} ${name}=${level};${WARNING};${CRITICAL};;"
if [ -n "${STATUS}" ] ; then
STATUS="${STATUS}, "
fi
STATUS="${STATUS}${name} ${level}%"
device_found=1
if [ "${level}" -lt "${WARNING}" ] ; then
EXITW=1
fi
if [ "${level}" -lt "${CRITICAL}" ] ; then
EXITC=1
fi
fi
else
if [ -n "${VERBOSE}" ] ; then
echo "no ${description} detected"
fi
fi
}
################################################################################
# Main
################################################################################
# process command line options
while getopts "c:w:h?Vv" opt; do
case ${opt} in
c ) CRITICAL=${OPTARG} ;;
w ) WARNING=${OPTARG} ;;
h | \? ) usage ; exit 3; ;;
v ) VERBOSE=1 ;;
V ) echo "check_bluetooth_battery version ${VERSION}"; exit 3; ;;
* ) echo "Unknown option '${opt}'"; exit 3 ;;
esac
done
shift $((OPTIND - 1))
################################################################################
# sanity checks
if [ -z "${CRITICAL}" ] ; then
usage 'no critical limit specified'
exit 3
fi
if [ -z "${WARNING}" ] ; then
usage 'no warning limit specified'
exit 3
fi
check_value "${CRITICAL}"
check_value "${WARNING}"
#######################
# Check needed programs
check_required_prog ioreg
check_device 'IOAppleBluetoothHIDDriver' 'keyboard' 'bluetooth keyboard'
check_device 'AppleBluetoothHIDKeyboard' 'keyboard' 'bluetooth keyboard'
check_device 'BNBMouseDevice' 'mouse' 'bluetooth mouse'
check_device 'BNBTrackpadDevice' 'trackpad' 'bluetooth trackpad'
check_device 'AppleHSBluetoothDevice' 'mouse' 'bluetooth mouse'
# check air pods
check_device 'Case' 'AirPodCase' 'AirPod Case'
check_device 'Left' 'LeftAirPod' 'Left AirPod'
check_device 'Right' 'RightAirPod' 'Right AirPod'
##############################################################################
# Checks
if [ -z "${device_found}" ] ; then
critical "no device detected"
fi
if [ -n "${EXITC}" ] ; then
echo "${NAME} CRITICAL - ${STATUS} |${PERFORMANCE}"
exit 2
fi
if [ -n "${EXITW}" ] ; then
echo "${NAME} WARNING - ${STATUS} |${PERFORMANCE}"
exit 1
fi
echo "${NAME} OK - ${STATUS} |${PERFORMANCE}"
exit 0