forked from andrewjfreyer/monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argv
211 lines (179 loc) · 7.95 KB
/
argv
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
#!/bin/bash
# ----------------------------------------------------------------------------------------
# GENERAL INFORMATION
# ----------------------------------------------------------------------------------------
#
# Written by Andrew J Freyer
# GNU General Public License
# http://github.com/andrewjfreyer/monitor
#
# HELP FILE AND GENERAL PREFERENCES
#
# ----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------
# REPORT CURRENT VERSION
# ----------------------------------------------------------------------------------------
previous_version=$(cat ".previous_version")
#DETERMINE IF UPDATED SINCE LAST RUN
if [ "$previous_version" != "$version" ]; then
#HAVE WE REPORTED AN UPDATE BEFORE?
[ -z "$previous_version" ] && previous_version="Unknown"
#UPDATE REPORT
printf "%s\n" "> ${GREEN}updated${NC} $(basename "$0") (v. $previous_version) -> (v. $version)..."
#RECORD UPDATED VERSION
printf "%s\n" "$version" > ".previous_version"
else
#STANDARD RUN
printf "%s\n" "> ${GREEN}starting${NC} $(basename "$0") (v. $version)..."
fi
# ----------------------------------------------------------------------------------------
# HELP TEXT
# ----------------------------------------------------------------------------------------
show_help_text() {
#SHOW HELPFULE
printf "%s\n" "
monitor.sh
Andrew J Freyer, 2018
https://github.com/andrewjfreyer/monitor
GNU General Public License
usage:
monitor -h show usage information
monitor -R redact private information from logs
monitor -S silent operation (no logging)
monitor -c addr create connection to bluetooth device
monitor -C clean retained messages from MQTT broker
monitor -V print verbose/debug logging messages
monitor -v print version number
monitor -d restore to default behavior_preferences
monitor -u update 'monitor.service' to current command line settings
(excluding -u,-V, -F, and -d flags)
monitor -r repeatedly scan for arrival & departure of known devices
monitor -s report all mqtt messages to a single topic with
\$mqtt_topicpath/\$mqtt_publisher_identity (defined in MQTT preferences file)
monitor -f format MQTT topics with only letters and numbers
monitor -a report all known device scan results, not just changes
monitor -x retain mqtt status messages
monitor -b report bluetooth beacon advertisements (e.g., generic beacons, ibeacons, and so on)
monitor -t[adr] scan for known devices only on mqtt trigger messages:
a \$mqtt_topicpath/scan/ARRIVE (defined in MQTT preferences file)
d \$mqtt_topicpath/scan/DEPART (defined in MQTT preferences file)
r send ARRIVE or DEPART messages to trigger other devices to scan
monitor -D [dir] use alternative directory for configuration files
"
}
# ----------------------------------------------------------------------------------------
# PROCESS OPTIONS (technique: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash)
# ----------------------------------------------------------------------------------------
#REMOVE MANUFACTURER CACHE AND PUBLIC NAME CACHE FROM PREVIOUS SESSION
(&>/dev/null rm ".manufacturer_cache") && $PREF_VERBOSE_LOGGING && printf "%s\n" "> removing web request caches"
#NO LONGER REQUIRED; NAME CACHING SHOULD BE STICKY
#(&>/dev/null rm ".public_name_cache") && printf "%s\n" "> removing public name cache"
#PROCESS ARGV OPTIONS
OPTIND=1
#PREFERENCES
PREF_BEACON_MODE=false
PREF_TRIGGER_MODE_ARRIVE=false
PREF_TRIGGER_MODE_DEPART=false
PREF_TRIGGER_MODE_REPORT_OUT=false
PREF_BEACON_MODE=false
PREF_REPORT_ALL_MODE=false
PREF_RESTORE_DEFAULTS=false
PREF_UPDATE_SERVICE=false
PREF_REDACT=false
PREF_SHOULD_RETAIN=false
PREF_CLEAN_MQTT=false
PREF_FORMAT_MQTT=false
PREF_MQTT_REPORT_SCAN_MESSAGES=false
PREF_SERVICE_CHECK=true
PREF_CONFIG_DIR=''
PREF_DISABLE_LOGGING=false
PREF_VERBOSE_LOGGING=false
PREF_FILTER_DEBUG_LOGGING=false
PREF_MQTT_SINGLE_TOPIC_MODE=false
PREF_PERIODIC_MODE=false
while getopts "h?vfFbut:EgSRCmrsVadxD:c:" opt; do
case "$opt" in
h|\?)
show_help_text
exit 0
;;
S)
PREF_DISABLE_LOGGING=true && printf "%s\n" "> logging is disabled, although startup messages will still be shown"
;;
r) PREF_PERIODIC_MODE=true
printf "%s\n" "> ${ORANGE}warning:${NC} periodic scan mode may cause interference with 2.4GHz networks if run on a Raspberry Pi"
;;
v)
printf "%s\n" "$VERSION"
exit 0
;;
F)
PREF_FILTER_DEBUG_LOGGING=true && printf "%s\n" "> ${ORANGE}warning:${NC} filter logging is enabled. this setting is only for informational and debug purposes"
;;
V)
PREF_VERBOSE_LOGGING=true && printf "%s\n" "> ${ORANGE}warning:${NC} verbose logging is enabled. this setting is only for informational and debugging purposes"
;;
c)
#GET TARGET MAC ADDRESS
target_mac="$OPTARG"
#REMOVE PREVIOUS PAIRINGS
printf "%s\n" "> removing previous pairings to $target_mac"
(echo -e "remove $target_mac" | bluetoothctl &>/dev/null)
#DEBUG ECHO
printf "%s\n" "> creating connection to $target_mac..."
printf "%s\n" "> witihin 5 seconds, please set $target_mac to discoverable..."
#WAIT 5 SECONDS FOR DISCOVERABLE MODE
sleep 5
#CREATE CONNECTION
return_data=$(hcitool cc $target_mac 2>&1 && hcitool auth $target_mac 2>&1 && hcitool dc $target_mac 2>&1)
#ERROR REPORTING
[[ $return_data =~ .*error.* ]] && printf "%s\n" "> ${RED}error: ${NC}connection to $target_mac failed" || printf "%s\n" "> connection created to $target_mac"
exit 0
;;
C)
PREF_CLEAN_MQTT=true && printf "%s\n" "> cleaning retained messages on broker"
;;
E) PREF_MQTT_REPORT_SCAN_MESSAGES=true && printf "%s\n" "> publishing MQTT .../scan/[arrival|depart]/[start|end]"
;;
s) PREF_MQTT_SINGLE_TOPIC_MODE=true && printf "%s\n" "> publishing all MQTT presence messages to \$mqtt_topicpath/\$mqtt_publisher_identity"
;;
x) PREF_SHOULD_RETAIN=true && printf "%s\n" "> retaining mqtt status reports"
;;
R) PREF_REDACT=true && printf "%s\n" "> private information redacted from logs"
;;
d) PREF_RESTORE_DEFAULTS=true && printf "%s\n" "> restoring default settings"
;;
u) PREF_UPDATE_SERVICE=true && printf "%s\n" "> updating monitor.service"
;;
f) PREF_FORMAT_MQTT=true && $PREF_VERBOSE_LOGGING && printf "%s\n" "> only allow letters, numbers, and spaces in mqtt topic paths"
;;
b) PREF_BEACON_MODE=true && $PREF_VERBOSE_LOGGING && printf "%s\n" "> generic bluetooth beacon, ibeacon, and known beacon address reporting mode enabled"
;;
t) #DO WE INCLUDE THE REPORTING TRIGGER?
[[ $OPTARG = *r* ]] && PREF_TRIGGER_MODE_REPORT_OUT=true && $PREF_VERBOSE_LOGGING && printf "%s\n" "> trigger mode: report out MQTT arrive/depart scan triggers to other devices"
#SORT THROUGH REMAINING FILTERS
case "$OPTARG" in
r) PREF_TRIGGER_MODE_REPORT_OUT=true
;;
d|rd|dr) PREF_TRIGGER_MODE_DEPART=true && $PREF_VERBOSE_LOGGING && printf "%s\n" "> trigger mode: depart scan only on MQTT trigger"
;;
a|ra|ar) PREF_TRIGGER_MODE_ARRIVE=true && $PREF_VERBOSE_LOGGING && printf "%s\n" "> trigger mode: arrive scan only on MQTT trigger"
;;
da|ad|rda|rad|dra|ard|dar|adr) PREF_TRIGGER_MODE_ARRIVE=true && PREF_TRIGGER_MODE_DEPART==true && $PREF_VERBOSE_LOGGING && printf "%s\n" "> trigger mode: scan only (both on arrive and depart) on trigger"
;;
*) printf "%s\n" "> ${ORANGE}warning: ${NC}unknown trigger mode: $OPTARG"
;;
esac
;;
a) PREF_REPORT_ALL_MODE=true && $PREF_VERBOSE_LOGGING && printf "%s\n" "> report all scan results mode enabled"
;;
D) PREF_CONFIG_DIR=$OPTARG && printf "%s\n" "> using custom config directory [$PREF_CONFIG_DIR]"
[ ! -d "$PREF_CONFIG_DIR" ] && printf "%s\n" "> ${RED}error: ${NC}config directory [$PREF_CONFIG_DIR] doesn't exist" && exit 1
;;
*) printf "%s\n" "> unknown or depreciated argument: $opt"
esac
done
#RESET OPTION INDEX
shift $((OPTIND-1))
#SHIFT IF NECESSARY
[ "$1" = "--" ] && shift