-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhetrixtools_install.sh
More file actions
349 lines (317 loc) · 11 KB
/
hetrixtools_install.sh
File metadata and controls
349 lines (317 loc) · 11 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
#
#
# HetrixTools Server Monitoring Agent - Install Script
# Copyright 2015 - 2025 @ HetrixTools
# For support, please open a ticket on our website https://hetrixtools.com
#
#
# DISCLAIMER OF WARRANTY
#
# The Software is provided "AS IS" and "WITH ALL FAULTS," without warranty of any kind,
# including without limitation the warranties of merchantability, fitness for a particular purpose and non-infringement.
# HetrixTools makes no warranty that the Software is free of defects or is suitable for any particular purpose.
# In no event shall HetrixTools be responsible for loss or damages arising from the installation or use of the Software,
# including but not limited to any indirect, punitive, special, incidental or consequential damages of any character including,
# without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses.
# The entire risk as to the quality and performance of the Software is borne by you, the user.
#
#
# Set PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Prefer IPv4 when fetching from GitHub, fallback to IPv6 if needed
github_wget() {
local url=${!#}
if ! wget -4 "$@"; then
echo "IPv4 request failed for $url, retrying with IPv6..."
if ! wget -6 "$@"; then
echo "ERROR: Unable to fetch $url via IPv4 or IPv6." >&2
return 1
fi
fi
return 0
}
# Branch
BRANCH="master"
# Check if first argument is branch or SID
if [ ${#1} -ne 32 ]
then
BRANCH=$1
shift
fi
# Check if install script is run by root
echo "Checking root privileges..."
if [ "$EUID" -ne 0 ]
then echo "ERROR: Please run the install script as root."
exit 1
fi
echo "... done."
# Check if the selected branch exists
if github_wget --spider -q https://raw.githubusercontent.com/hetrixtools/agent/$BRANCH/hetrixtools_agent.sh
then
echo "Installing from $BRANCH branch..."
else
echo "ERROR: Branch $BRANCH does not exist." >&2
exit 1
fi
# Fetch Server Unique ID
SID=$1
# Make sure SID is not empty
echo "Checking Server ID (SID)..."
if [ -z "$SID" ]
then echo "ERROR: First parameter missing."
exit
fi
echo "... done."
# Check if user has selected to run agent as 'root' or as 'hetrixtools' user
if [ -z "$2" ]
then echo "ERROR: Second parameter missing."
exit
fi
# Check for wget and cron/systemd availability
echo "Checking system utilities..."
command -v wget >/dev/null 2>&1 || { echo "ERROR: wget is required to run this agent." >&2; exit 1; }
USE_CRON=0
USE_SYSTEMD=0
SYSTEMCTL_AVAILABLE=0
EXISTING_CRON=0
CRON_ACTIVE=0
if command -v systemctl >/dev/null 2>&1; then
if [ -d /run/systemd/system ] || systemctl list-units >/dev/null 2>&1; then
SYSTEMCTL_AVAILABLE=1
fi
fi
if command -v crontab >/dev/null 2>&1; then
if crontab -u root -l 2>/dev/null | grep -q 'hetrixtools_agent.sh'; then
EXISTING_CRON=1
elif id -u hetrixtools >/dev/null 2>&1 && crontab -u hetrixtools -l 2>/dev/null | grep -q 'hetrixtools_agent.sh'; then
EXISTING_CRON=1
fi
CRON_ACTIVE=$EXISTING_CRON
if command -v pgrep >/dev/null 2>&1 && [ "$CRON_ACTIVE" -ne 1 ]; then
for cron_process in cron crond cronie systemd-cron fcron busybox-cron busybox-crond; do
if pgrep -x "$cron_process" >/dev/null 2>&1 || pgrep -f "$cron_process" >/dev/null 2>&1; then
CRON_ACTIVE=1
break
fi
done
fi
if [ "$CRON_ACTIVE" -ne 1 ] && [ "$SYSTEMCTL_AVAILABLE" -eq 1 ]; then
for cron_service in cron crond cronie systemd-cron fcron busybox-cron busybox-crond; do
if systemctl is-active --quiet "$cron_service"; then
CRON_ACTIVE=1
break
fi
done
if [ "$CRON_ACTIVE" -ne 1 ]; then
if systemctl list-units --type=service --state=active 2>/dev/null | grep -Ei '\bcron(ie)?\b' >/dev/null 2>&1; then
CRON_ACTIVE=1
fi
fi
fi
if [ "$CRON_ACTIVE" -ne 1 ] && [ "$SYSTEMCTL_AVAILABLE" -ne 1 ]; then
CRON_ACTIVE=1
fi
if [ "$CRON_ACTIVE" -eq 1 ]; then
USE_CRON=1
fi
fi
if [ "$USE_CRON" -ne 1 ] && [ "$SYSTEMCTL_AVAILABLE" -eq 1 ]; then
USE_SYSTEMD=1
fi
if [ "$USE_CRON" -ne 1 ] && [ "$USE_SYSTEMD" -ne 1 ]; then
echo "ERROR: Neither cron nor systemd is available to schedule the agent." >&2
exit 1
fi
echo "... done."
# Remove old agent (if exists)
echo "Checking if there's any old hetrixtools agent already installed..."
if [ -d /etc/hetrixtools ]
then
echo "Old hetrixtools agent found, deleting it..."
rm -rf /etc/hetrixtools
else
echo "No old hetrixtools agent found..."
fi
echo "... done."
# Creating agent folder
echo "Creating the hetrixtools agent folder..."
mkdir -p /etc/hetrixtools
echo "... done."
# Fetching the agent
echo "Fetching the agent..."
if ! github_wget -t 1 -T 30 -qO /etc/hetrixtools/hetrixtools_agent.sh https://raw.githubusercontent.com/hetrixtools/agent/$BRANCH/hetrixtools_agent.sh
then
echo "ERROR: Failed to download the agent script from GitHub." >&2
exit 1
fi
echo "... done."
# Fetching the config file
echo "Fetching the config file..."
if ! github_wget -t 1 -T 30 -qO /etc/hetrixtools/hetrixtools.cfg https://raw.githubusercontent.com/hetrixtools/agent/$BRANCH/hetrixtools.cfg
then
echo "ERROR: Failed to download the agent configuration from GitHub." >&2
exit 1
fi
echo "... done."
# Inserting Server ID (SID) into the agent config
echo "Inserting Server ID (SID) into agent config..."
sed -i "s/SID=\"\"/SID=\"$SID\"/" /etc/hetrixtools/hetrixtools.cfg
echo "... done."
# Check if any services are to be monitored
echo "Checking if any services should be monitored..."
if [ "$3" != "0" ]
then
echo "Services found, inserting them into the agent config..."
sed -i "s/CheckServices=\"\"/CheckServices=\"$3\"/" /etc/hetrixtools/hetrixtools.cfg
fi
echo "... done."
# Check if software RAID should be monitored
echo "Checking if software RAID should be monitored..."
if [ "$4" -eq "1" ]
then
echo "Enabling software RAID monitoring in the agent config..."
sed -i "s/CheckSoftRAID=0/CheckSoftRAID=1/" /etc/hetrixtools/hetrixtools.cfg
fi
echo "... done."
# Check if Drive Health should be monitored
echo "Checking if Drive Health should be monitored..."
if [ "$5" -eq "1" ]
then
echo "Enabling Drive Health monitoring in the agent config..."
sed -i "s/CheckDriveHealth=0/CheckDriveHealth=1/" /etc/hetrixtools/hetrixtools.cfg
fi
echo "... done."
# Check if 'View running processes' should be enabled
echo "Checking if 'View running processes' should be enabled..."
if [ "$6" -eq "1" ]
then
echo "Enabling 'View running processes' in the agent config..."
sed -i "s/RunningProcesses=0/RunningProcesses=1/" /etc/hetrixtools/hetrixtools.cfg
fi
echo "... done."
# Check if any ports to monitor number of connections on
echo "Checking if any ports to monitor number of connections on..."
if [ "$7" != "0" ]
then
echo "Ports found, inserting them into the agent config..."
sed -i "s/ConnectionPorts=\"\"/ConnectionPorts=\"$7\"/" /etc/hetrixtools/hetrixtools.cfg
fi
echo "... done."
# Killing any running hetrixtools agents
echo "Making sure no hetrixtools agent scripts are currently running..."
ps aux | grep -ie hetrixtools_agent.sh | awk '{print $2}' | xargs -r kill -9
echo "... done."
# Checking if hetrixtools user exists
echo "Checking if hetrixtools user already exists..."
if id -u hetrixtools >/dev/null 2>&1
then
echo "The hetrixtools user already exists, killing its processes..."
pkill -9 -u `id -u hetrixtools`
echo "Deleting hetrixtools user..."
userdel hetrixtools
echo "Creating the new hetrixtools user..."
useradd hetrixtools -r -d /etc/hetrixtools -s /bin/false
echo "Assigning permissions for the hetrixtools user..."
chown -R hetrixtools:hetrixtools /etc/hetrixtools
chmod -R 700 /etc/hetrixtools
else
echo "The hetrixtools user doesn't exist, creating it now..."
useradd hetrixtools -r -d /etc/hetrixtools -s /bin/false
echo "Assigning permissions for the hetrixtools user..."
chown -R hetrixtools:hetrixtools /etc/hetrixtools
chmod -R 700 /etc/hetrixtools
fi
echo "... done."
# Removing old cronjob (if exists)
echo "Removing any old hetrixtools cronjob, if exists..."
if command -v crontab >/dev/null 2>&1
then
crontab -u root -l 2>/dev/null | grep -v 'hetrixtools_agent.sh' | crontab -u root - >/dev/null 2>&1
crontab -u hetrixtools -l 2>/dev/null | grep -v 'hetrixtools_agent.sh' | crontab -u hetrixtools - >/dev/null 2>&1
fi
echo "... done."
# Removing old systemd service/timer (if exists)
if [ "$SYSTEMCTL_AVAILABLE" -eq 1 ]; then
systemctl stop hetrixtools_agent.timer >/dev/null 2>&1
systemctl disable hetrixtools_agent.timer >/dev/null 2>&1
systemctl stop hetrixtools_agent.service >/dev/null 2>&1
systemctl disable hetrixtools_agent.service >/dev/null 2>&1
systemctl daemon-reload >/dev/null 2>&1
fi
rm -f /etc/systemd/system/hetrixtools_agent.timer >/dev/null 2>&1
rm -f /etc/systemd/system/hetrixtools_agent.service >/dev/null 2>&1
# Setup the new systemd or cronjob timer to run the agent every minute
if [ "$USE_CRON" -eq 1 ]
then
# Default is running the agent as 'hetrixtools' user, unless chosen otherwise by the client when fetching the installation code from the hetrixtools website.
if [ "$2" -eq "1" ]
then
echo "Setting up the new cronjob as 'root' user..."
crontab -u root -l 2>/dev/null | { cat; echo "* * * * * bash /etc/hetrixtools/hetrixtools_agent.sh >> /etc/hetrixtools/hetrixtools_cron.log 2>&1"; } | crontab -u root - >/dev/null 2>&1
else
echo "Setting up the new cronjob as 'hetrixtools' user..."
crontab -u hetrixtools -l 2>/dev/null | { cat; echo "* * * * * bash /etc/hetrixtools/hetrixtools_agent.sh >> /etc/hetrixtools/hetrixtools_cron.log 2>&1"; } | crontab -u hetrixtools - >/dev/null 2>&1
fi
elif [ "$USE_SYSTEMD" -eq 1 ]
then
echo "Setting up systemd timer..."
if [ "$2" -eq "1" ]
then
SERVICE_USER=root
else
SERVICE_USER=hetrixtools
fi
cat > /etc/systemd/system/hetrixtools_agent.service <<EOF
[Unit]
Description=HetrixTools Agent
[Service]
Type=oneshot
User=$SERVICE_USER
ExecStart=/bin/bash /etc/hetrixtools/hetrixtools_agent.sh
EOF
cat > /etc/systemd/system/hetrixtools_agent.timer <<EOF
[Unit]
Description=Runs HetrixTools agent every minute
[Timer]
OnBootSec=1min
OnCalendar=*-*-* *:*:00 UTC
AccuracySec=1s
RandomizedDelaySec=0
Persistent=true
Unit=hetrixtools_agent.service
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload >/dev/null 2>&1
systemctl enable --now hetrixtools_agent.timer >/dev/null 2>&1
systemctl restart hetrixtools_agent.timer >/dev/null 2>&1
else
echo "ERROR: Unable to configure scheduling for the agent." >&2
exit 1
fi
echo "... done."
# Cleaning up install file
echo "Cleaning up the installation file..."
if [ -f $0 ]
then
rm -f $0
fi
echo "... done."
# Let HetrixTools platform know install has been completed
echo "Letting HetrixTools platform know the installation has been completed..."
POST="v=install&s=$SID"
wget -t 1 -T 30 -qO- --post-data "$POST" https://sm.hetrixtools.net/ &> /dev/null
echo "... done."
# Start the agent
if [ "$2" -eq "1" ]
then
echo "Starting the agent under the 'root' user..."
bash /etc/hetrixtools/hetrixtools_agent.sh > /dev/null 2>&1 &
else
echo "Starting the agent under the 'hetrixtools' user..."
sudo -u hetrixtools bash /etc/hetrixtools/hetrixtools_agent.sh > /dev/null 2>&1 &
fi
echo "... done."
# All done
echo "HetrixTools agent installation completed."