-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·101 lines (81 loc) · 2.46 KB
/
install.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
#!/usr/bin/env bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
install=false
# If no service is present, always start the install-routine. Otherwise, only
# do so if at least _one_ argument is passed to the script. There should be
# multiple – that validation is handled by the install-routine...
if
[ ! -e /etc/systemd/system/sysmon-mqtt.service ] || [ -v 1 ]
then
install=true
mqtt_host="${1:?"Missing MQTT-broker hostname!"}"
device_name="${2:?"Missing device name!"}"
network_adapters="${3:-}"
rtt_hosts="${4:-}"
# Ensure list of network adapters and RTT hosts remain quoted in the heredoc.
# There appears no way to achieve this using something like ${var:+\""$var"\"} —
# whatever I try, I either get no quotes or a literal '\"' in the output...
if [ -n "$network_adapters" ]; then
network_adapters=\""$network_adapters"\"
fi
if [ -n "$rtt_hosts" ]; then
rtt_hosts=\""$rtt_hosts"\"
# If network_adapters is not specified, set it to a literal "" to prevent
# rtt_hosts from being interpreted as network_adapters.
if [ -z "$network_adapters" ]; then
network_adapters=\"\"
fi
fi
fi
sysmon_url="https://github.com/thijsputman/sysmon-mqtt/raw/main/sysmon.sh"
if [ -e /etc/systemd/system/sysmon-mqtt.service ]; then
systemctl stop sysmon-mqtt
if $install; then
systemctl disable sysmon-mqtt
rm /etc/systemd/system/sysmon-mqtt.service
fi
# Assumes dependencies are only relevant on first ever install...
else
apt update
apt install -y \
bash \
gawk \
iw \
jq \
mosquitto-clients
fi
wget -O .sysmon-mqtt "$(tr -d ' ' <<< "$sysmon_url")"
chown "${SUDO_USER:-$(whoami)}:" .sysmon-mqtt
chmod +x .sysmon-mqtt
if $install; then
tee /etc/systemd/system/sysmon-mqtt.service <<- EOF > /dev/null
[Unit]
Description=Simple system monitoring over MQTT
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=120
StartLimitBurst=3
[Service]
Type=simple
Restart=on-failure
RestartSec=30
User=${SUDO_USER:-$(whoami)}
ExecStart=/usr/bin/env bash $(pwd)/.sysmon-mqtt \
$mqtt_host \
"$device_name" \
$network_adapters \
$rtt_hosts
[Install]
WantedBy=multi-user.target
EOF
# N.B. heredoc should be indented with tabs...
systemctl daemon-reload
systemctl enable sysmon-mqtt
fi
if [ ! -e /etc/systemd/system/sysmon-mqtt.service ]; then
echo 'Install failed – sysmon-mqtt service not present!' >&2
exit 1
fi
systemctl start sysmon-mqtt
exit $?