-
Notifications
You must be signed in to change notification settings - Fork 28
/
AutomateDiagnostics.sh
78 lines (64 loc) · 2.58 KB
/
AutomateDiagnostics.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
# source: https://raw.githubusercontent.com/noaht8um/CWCAutomateDiagnostics/master/AutomateDiagnostics.sh
# Exit if not macOS
if [ "$(uname)" != "Darwin" ]; then
exit 1
fi
# Arcane way to parse JSON natively on Macs with AppleScript
# https://paulgalow.com/how-to-work-with-json-api-data-in-macos-shell-scripts
convertFromJson() {
JSON="$1" osascript -l 'JavaScript' \
-e 'const env = $.NSProcessInfo.processInfo.environment.objectForKey("JSON").js' \
-e "JSON.parse(env).$2"
}
# Read state file for info
data=$(cat /usr/local/ltechagent/state)
status=$(launchctl list | grep com.labtechsoftware.LTSvc)
if [ -z "$status" ]; then
launchctl stop com.labtechsoftware.LTSvc
launchctl start com.labtechsoftware.LTSvc
status=$(launchctl list | grep com.labtechsoftware.LTSvc)
if [ -z "$status" ]; then
statusName="Stopped"
else
statusName="Running"
fi
else
statusName="Running"
fi
old_version=$(convertFromJson "$data" 'version')
/usr/local/ltechagent/ltupdate
# Read state file for info
data=$(cat /usr/local/ltechagent/state)
new_version=$(convertFromJson "$data" 'version')
if [ "$old_version" != "$new_version" ]; then
update="Updated from $old_version to $new_version"
else
update="Already updated to $new_version"
fi
server_addr=$(convertFromJson "$data" 'last_good_server_url')
version=$(convertFromJson "$data" 'version')
id=$(convertFromJson "$data" 'computer_id')
clientid=$(convertFromJson "$data" 'client_id')
online=$(convertFromJson "$data" 'is_signed_in')
# Format lastcontact time
sec=$(printf "%02d\n" $(convertFromJson "$data" 'last_contact.sec'))
min=$(printf "%02d\n" $(convertFromJson "$data" 'last_contact.min'))
hour=$(convertFromJson "$data" 'last_contact.hour')
day_of_month=$(convertFromJson "$data" 'last_contact.day_of_month')
month=$(convertFromJson "$data" 'last_contact.month')
year=$(convertFromJson "$data" 'last_contact.year')
lastcontact=$(echo "$month/$day_of_month/$year $hour:$min:$sec")
# collect agent logs
lterrors=""
log_file="/usr/local/ltechagent/agent.log"
if [ -f "$log_file" ]; then
lterrors_str=$(sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g' -e 's/"/\"/g' -e "s/'/\'/g" "$log_file")
lterrors=$(echo "$lterrors_str" | base64)
fi
json=$(
cat <<EOF
{"svc_ltservice": {"Status": "$statusName", "User": "com.labtechsoftware.LTSvc", "Start Mode": "Auto"}, "version": "$version", "clientid": $clientid,"online": $online,"id": $id,"lterrors": "$lterrors", "update": "$update", "server_addr": "$server_addr", "lastcontact": "$lastcontact"}
EOF
)
printf "!---BEGIN JSON---! "
echo $json