-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
411 lines (368 loc) · 16.3 KB
/
Copy pathMakefile
File metadata and controls
411 lines (368 loc) · 16.3 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# APMonitor Installation Makefile for Debian/Ubuntu Linux
PYTHON := python3
PIP := pip3
INSTALL_DIR := /usr/local/bin
CONFIG_DIR := /usr/local/etc
SERVICE_DIR := /etc/systemd/system
STATE_DIR := /var/tmp/APMonitor
OLD_STATE_DIR := /var/tmp
MRTG_WORK_DIR := /var/www/html/mrtg
NGINX_CONF_DIR := /var/www/html
CGI_DIR := /var/www/html
USER := monitoring
GROUP := monitoring
# Detect if sudo is available, use it if not root, or fail
SUDO := $(shell if [ "$$(id -u)" -eq 0 ]; then echo ""; elif command -v sudo >/dev/null 2>&1; then echo "sudo"; else echo "NOSUDO"; fi)
.PHONY: help install uninstall enable start stop restart status logs test-config test-webhooks installmrtg check-root check-sudo migrate
help:
@echo "APMonitor Installation Makefile"
@echo ""
@echo "Available targets:"
@echo " install - Install APMonitor (requires root)"
@echo " installmrtg - Install MRTG web interface on port 888 (requires root)"
@echo " uninstall - Remove APMonitor completely (requires root)"
@echo " migrate - Migrate statefiles and MRTG directories to new layout"
@echo " enable - Enable and start systemd service (requires root)"
@echo " start - Start APMonitor service (requires root)"
@echo " stop - Stop APMonitor service (requires root)"
@echo " restart - Restart APMonitor service (requires root)"
@echo " status - Show service status"
@echo " logs - Show live service logs (Ctrl+C to exit)"
@echo " test-config - Test configuration as monitoring user"
@echo " test-webhooks - Test webhook notifications"
@echo ""
@echo "Quick start:"
@echo " 1. sudo make install # Install APMonitor"
@echo " 2. sudo make installmrtg # Install MRTG web interface"
@echo " 3. Edit /usr/local/etc/apmonitor-config.yaml"
@echo " 4. sudo make enable # Start monitoring"
check-root:
@if [ "$$(id -u)" -ne 0 ]; then \
echo "Error: This target must be run as root (use sudo make ...)"; \
exit 1; \
fi
check-sudo:
@if [ "$(SUDO)" = "NOSUDO" ]; then \
echo "Error: Not running as root and sudo is not installed."; \
echo "Either run as root or install sudo: apt install sudo"; \
exit 1; \
fi
migrate: check-root
@echo "==> Migrating APMonitor statefiles and MRTG directories..."
@service_was_running=0; \
if systemctl is-active --quiet apmonitor.service; then \
echo "==> Stopping APMonitor service..."; \
systemctl stop apmonitor.service; \
service_was_running=1; \
fi; \
\
# Phase 1: rename legacy flat name -> config-derived name in OLD_STATE_DIR \
OLD_BASE=$(OLD_STATE_DIR)/apmonitor-statefile; \
NEW_BASE=$(OLD_STATE_DIR)/apmonitor-config.statefile; \
migrated=0; \
for suffix in .json .json.new .json.old .mrtg.cfg .mrtg.cfg.new .mrtg.cfg.old; do \
if [ -f "$$OLD_BASE$$suffix" ] && [ ! -f "$$NEW_BASE$$suffix" ]; then \
mv "$$OLD_BASE$$suffix" "$$NEW_BASE$$suffix"; \
echo " Phase 1 migrated: $$OLD_BASE$$suffix -> $$NEW_BASE$$suffix"; \
migrated=1; \
fi; \
done; \
if [ -d "$$OLD_BASE.rrd" ] && [ ! -d "$$NEW_BASE.rrd" ]; then \
mv "$$OLD_BASE.rrd" "$$NEW_BASE.rrd"; \
echo " Phase 1 migrated: $$OLD_BASE.rrd -> $$NEW_BASE.rrd"; \
migrated=1; \
elif [ -d "$$OLD_BASE.rrd" ] && [ -d "$$NEW_BASE.rrd" ]; then \
echo " ERROR: both $$OLD_BASE.rrd and $$NEW_BASE.rrd exist."; \
echo " Historical RRD data is in $$OLD_BASE.rrd -- manual merge required."; \
echo " To recover: rm -rf $$NEW_BASE.rrd && mv $$OLD_BASE.rrd $$NEW_BASE.rrd"; \
if [ "$$service_was_running" -eq 1 ]; then \
echo "==> Restarting APMonitor service..."; \
systemctl start apmonitor.service; \
fi; \
exit 1; \
fi; \
\
# Phase 2: move all apmonitor-*.statefile.* from OLD_STATE_DIR into STATE_DIR \
mkdir -p $(STATE_DIR); \
chmod 755 $(STATE_DIR); \
for f in $(OLD_STATE_DIR)/apmonitor-*.statefile.json \
$(OLD_STATE_DIR)/apmonitor-*.statefile.json.new \
$(OLD_STATE_DIR)/apmonitor-*.statefile.json.old \
$(OLD_STATE_DIR)/apmonitor-*.statefile.mrtg.cfg \
$(OLD_STATE_DIR)/apmonitor-*.statefile.mrtg.cfg.new \
$(OLD_STATE_DIR)/apmonitor-*.statefile.mrtg.cfg.old; do \
[ -f "$$f" ] || continue; \
dest=$(STATE_DIR)/$$(basename $$f); \
if [ ! -f "$$dest" ]; then \
mv "$$f" "$$dest"; \
echo " Phase 2 migrated: $$f -> $$dest"; \
migrated=1; \
else \
echo " WARNING: $$dest already exists, skipping $$f"; \
fi; \
done; \
for d in $(OLD_STATE_DIR)/apmonitor-*.statefile.rrd; do \
[ -d "$$d" ] || continue; \
dest=$(STATE_DIR)/$$(basename $$d); \
if [ ! -d "$$dest" ]; then \
mv "$$d" "$$dest"; \
echo " Phase 2 migrated: $$d -> $$dest"; \
migrated=1; \
else \
echo " WARNING: $$dest already exists, skipping $$d"; \
fi; \
done; \
\
# Phase 3: migrate mrtg-rrd.cgi.pl from old location inside mrtg/ to CGI_DIR \
OLD_CGI=$(MRTG_WORK_DIR)/mrtg-rrd.cgi.pl; \
NEW_CGI=$(CGI_DIR)/mrtg-rrd.cgi.pl; \
if [ -f "$$OLD_CGI" ]; then \
echo " Phase 3: found $$OLD_CGI — archiving $(MRTG_WORK_DIR) to $(MRTG_WORK_DIR).old"; \
if [ -d "$(MRTG_WORK_DIR).old" ]; then \
echo " WARNING: $(MRTG_WORK_DIR).old already exists, removing..."; \
rm -rf "$(MRTG_WORK_DIR).old"; \
fi; \
mv "$(MRTG_WORK_DIR)" "$(MRTG_WORK_DIR).old"; \
echo " Archived: $(MRTG_WORK_DIR) -> $(MRTG_WORK_DIR).old"; \
mkdir -p $(MRTG_WORK_DIR); \
chown mrtg:www-data $(MRTG_WORK_DIR); \
chmod 775 $(MRTG_WORK_DIR); \
echo " Created fresh: $(MRTG_WORK_DIR)"; \
if [ -f "$(MRTG_WORK_DIR).old/mrtg-rrd.cgi.pl" ] && [ ! -f "$$NEW_CGI" ]; then \
cp "$(MRTG_WORK_DIR).old/mrtg-rrd.cgi.pl" "$$NEW_CGI"; \
chmod 755 "$$NEW_CGI"; \
echo " Copied CGI: $(MRTG_WORK_DIR).old/mrtg-rrd.cgi.pl -> $$NEW_CGI"; \
fi; \
migrated=1; \
fi; \
\
if [ "$$migrated" -eq 0 ]; then \
echo " Nothing to migrate."; \
fi; \
if [ "$$service_was_running" -eq 1 ]; then \
echo "==> Restarting APMonitor service..."; \
systemctl start apmonitor.service; \
fi
@echo "==> Migration complete."
install: check-root migrate
@echo "==> Installing system dependencies..."
apt update
apt install -y python3 python3-pip python3-rrdtool librrd-dev python3-dev mrtg rrdtool librrds-perl libcgi-pm-perl libsnmp-dev python3-easysnmp
@echo "==> Installing Python dependencies globally..."
$(PIP) install --break-system-packages PyYAML requests pyOpenSSL urllib3 aioquic rrdtool || \
$(PIP) install PyYAML requests pyOpenSSL urllib3 aioquic rrdtool easysnmp
@echo "==> Creating monitoring user..."
id -u $(USER) >/dev/null 2>&1 || /usr/sbin/useradd -r -s /bin/bash -d /var/lib/apmonitor -m $(USER)
@echo "==> Adding monitoring user to www-data group..."
/usr/sbin/usermod -a -G www-data $(USER)
@echo "==> Creating APMonitor state directory $(STATE_DIR)..."
mkdir -p $(STATE_DIR)
chmod 755 $(STATE_DIR)
chown $(USER):$(GROUP) $(STATE_DIR)
@echo "==> Creating MRTG working directory $(MRTG_WORK_DIR)..."
mkdir -p $(MRTG_WORK_DIR)
chown mrtg:www-data $(MRTG_WORK_DIR)
chmod 775 $(MRTG_WORK_DIR)
@echo "==> Installing APMonitor script..."
install -m 755 APMonitor.py $(INSTALL_DIR)/APMonitor.py
@echo "==> Installing example configuration..."
mkdir -p $(CONFIG_DIR)
@if [ -f "$(CONFIG_DIR)/apmonitor-config.yaml" ]; then \
echo "Warning: Config file already exists at $(CONFIG_DIR)/apmonitor-config.yaml"; \
echo "Skipping config installation. To reinstall example config:"; \
echo " sudo cp example-apmonitor-config.yaml $(CONFIG_DIR)/apmonitor-config.yaml"; \
else \
install -m 640 -o $(USER) -g $(GROUP) example-apmonitor-config.yaml $(CONFIG_DIR)/apmonitor-config.yaml; \
echo "Installed example configuration to $(CONFIG_DIR)/apmonitor-config.yaml"; \
fi
@echo "==> Installing systemd service..."
@DEFAULT_EXEC="ExecStart=/bin/bash -c 'while true; do $(INSTALL_DIR)/APMonitor.py -vv $(CONFIG_DIR)/apmonitor-config.yaml --generate-mrtg-config; sleep 5; done'"; \
if [ -f "$(SERVICE_DIR)/apmonitor.service" ]; then \
echo "Service file exists — merging (preserving customized ExecStart if changed)..."; \
CURRENT_EXEC=$$(grep '^ExecStart=' $(SERVICE_DIR)/apmonitor.service | head -1); \
if [ "$$CURRENT_EXEC" = "$$DEFAULT_EXEC" ] || [ -z "$$CURRENT_EXEC" ]; then \
KEEP_EXEC=0; \
else \
KEEP_EXEC=1; \
echo " Preserving customized ExecStart: $$CURRENT_EXEC"; \
fi; \
else \
KEEP_EXEC=0; \
fi; \
echo "[Unit]" > $(SERVICE_DIR)/apmonitor.service; \
echo "Description=APMonitor Network Resource Monitor" >> $(SERVICE_DIR)/apmonitor.service; \
echo "After=network.target" >> $(SERVICE_DIR)/apmonitor.service; \
echo "" >> $(SERVICE_DIR)/apmonitor.service; \
echo "[Service]" >> $(SERVICE_DIR)/apmonitor.service; \
echo "Type=simple" >> $(SERVICE_DIR)/apmonitor.service; \
if [ "$$KEEP_EXEC" -eq 1 ]; then \
echo "$$CURRENT_EXEC" >> $(SERVICE_DIR)/apmonitor.service; \
else \
echo "$$DEFAULT_EXEC" >> $(SERVICE_DIR)/apmonitor.service; \
fi; \
echo "Restart=always" >> $(SERVICE_DIR)/apmonitor.service; \
echo "RestartSec=5" >> $(SERVICE_DIR)/apmonitor.service; \
echo "User=$(USER)" >> $(SERVICE_DIR)/apmonitor.service; \
echo "StandardOutput=journal" >> $(SERVICE_DIR)/apmonitor.service; \
echo "StandardError=journal" >> $(SERVICE_DIR)/apmonitor.service; \
echo "" >> $(SERVICE_DIR)/apmonitor.service; \
echo "[Install]" >> $(SERVICE_DIR)/apmonitor.service; \
echo "WantedBy=multi-user.target" >> $(SERVICE_DIR)/apmonitor.service; \
echo "Service file written."
@echo "==> Reloading systemd..."
systemctl daemon-reload
@echo ""
@echo "==> Installation complete!"
@echo ""
@echo "IMPORTANT: Edit $(CONFIG_DIR)/apmonitor-config.yaml before starting the service"
@echo ""
@echo "Next steps:"
@echo " 1. Install MRTG web UI: sudo make installmrtg"
@echo " 2. Edit configuration: nano $(CONFIG_DIR)/apmonitor-config.yaml"
@echo " 3. Test configuration: make test-config"
@echo " 4. Enable and start: sudo make enable"
@echo " 5. Check status: make status"
@echo " 6. View logs: make logs"
installmrtg: check-root
@echo "==> Installing MRTG web interface dependencies..."
apt update
apt install -y fcgiwrap nginx librrds-perl
@echo "==> Checking if system nginx is already running..."
@if systemctl is-active --quiet nginx; then \
echo "Warning: System nginx service is already running"; \
echo "This will conflict with our standalone nginx on port 888"; \
echo "Stopping and disabling system nginx..."; \
systemctl stop nginx; \
systemctl disable nginx; \
fi
@echo "==> Installing mrtg-rrd.cgi.pl to $(CGI_DIR)..."
mkdir -p $(CGI_DIR)
install -m 755 -o $(USER) -g $(GROUP) mrtg-rrd.cgi.pl $(CGI_DIR)/mrtg-rrd.cgi.pl
@echo "==> Setting permissions on CGI directory $(CGI_DIR)..."
chown root:$(GROUP) $(CGI_DIR)
chmod 775 $(CGI_DIR)
@echo "==> Installing nginx configuration..."
install -m 644 mrtg-nginx.conf $(NGINX_CONF_DIR)/mrtg-nginx.conf
@echo "==> Installing MRTG banner icons..."
cp images/mrtg-l.png images/mrtg-m.png images/mrtg-r.png $(MRTG_WORK_DIR)/
@echo "==> Setting up fcgiwrap socket permissions..."
@if ! systemctl is-active --quiet fcgiwrap.service; then \
echo "Starting fcgiwrap service..."; \
systemctl start fcgiwrap.service; \
systemctl enable fcgiwrap.service; \
fi
chmod 777 /var/run/fcgiwrap.socket
@echo "==> Configuring fcgiwrap to log to journald..."
mkdir -p /etc/systemd/system/fcgiwrap.service.d
@echo "[Service]" > /etc/systemd/system/fcgiwrap.service.d/logging.conf
@echo "StandardError=journal" >> /etc/systemd/system/fcgiwrap.service.d/logging.conf
@echo "==> Creating systemd service for MRTG nginx..."
@echo "[Unit]" > $(SERVICE_DIR)/apmonitor-nginx.service
@echo "Description=APMonitor MRTG Web Interface (nginx)" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "After=network.target fcgiwrap.service" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "Requires=fcgiwrap.service" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "[Service]" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "Type=forking" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "ExecStartPre=/usr/sbin/nginx -t -c $(NGINX_CONF_DIR)/mrtg-nginx.conf -p $(NGINX_CONF_DIR)" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "ExecStart=/usr/sbin/nginx -c $(NGINX_CONF_DIR)/mrtg-nginx.conf -p $(NGINX_CONF_DIR)" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "ExecReload=/usr/sbin/nginx -s reload -c $(NGINX_CONF_DIR)/mrtg-nginx.conf -p $(NGINX_CONF_DIR)" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "ExecStop=/usr/sbin/nginx -s stop -c $(NGINX_CONF_DIR)/mrtg-nginx.conf -p $(NGINX_CONF_DIR)" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "PrivateTmp=true" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "[Install]" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "WantedBy=multi-user.target" >> $(SERVICE_DIR)/apmonitor-nginx.service
@echo "==> Reloading systemd..."
systemctl daemon-reload
@echo "==> Starting apmonitor-nginx service..."
systemctl enable apmonitor-nginx.service
systemctl start apmonitor-nginx.service
@HOSTNAME=$$(hostname); \
echo ""; \
echo "==> MRTG web interface installation complete!"; \
echo ""; \
echo "Web interface is now running at:"; \
echo " http://localhost:888/"; \
echo " http://$$HOSTNAME:888/"; \
echo " http://<your-ip>:888/"; \
echo ""; \
echo "MRTG CGI interface:"; \
echo " http://localhost:888/mrtg-rrd/"; \
echo ""; \
echo "Service management:"; \
echo " Status: systemctl status apmonitor-nginx"; \
echo " Stop: systemctl stop apmonitor-nginx"; \
echo " Start: systemctl start apmonitor-nginx"; \
echo " Restart: systemctl restart apmonitor-nginx"; \
echo " Logs: journalctl -u apmonitor-nginx -f"; \
echo ""; \
echo "Note: Make sure port 888 is open in your firewall"
uninstall: check-root
@echo "==> Stopping and disabling services..."
-systemctl stop apmonitor.service
-systemctl disable apmonitor.service
-systemctl stop apmonitor-nginx.service
-systemctl disable apmonitor-nginx.service
@echo "==> Removing service files..."
rm -f $(SERVICE_DIR)/apmonitor.service
rm -f $(SERVICE_DIR)/apmonitor-nginx.service
systemctl daemon-reload
@echo "==> Removing files..."
rm -f $(INSTALL_DIR)/APMonitor.py
rm -f $(CONFIG_DIR)/apmonitor-config.yaml
rm -rf $(STATE_DIR)
rm -f $(OLD_STATE_DIR)/apmonitor-statefile.json*
rm -f $(OLD_STATE_DIR)/apmonitor-statefile.mrtg.cfg*
rm -rf $(OLD_STATE_DIR)/apmonitor-statefile.rrd
rm -f $(OLD_STATE_DIR)/apmonitor-config.statefile.json*
rm -f $(OLD_STATE_DIR)/apmonitor-config.statefile.mrtg.cfg*
rm -rf $(OLD_STATE_DIR)/apmonitor-config.statefile.rrd
rm -f $(NGINX_CONF_DIR)/mrtg-nginx.conf
rm -f $(CGI_DIR)/mrtg-rrd.cgi.pl
rm -rf $(MRTG_WORK_DIR)/*/
rm -rf $(MRTG_WORK_DIR).old
@echo "==> Removing monitoring user..."
-/usr/sbin/userdel -r $(USER)
@echo ""
@echo "==> Uninstallation complete!"
@echo ""
@echo "Note: Python dependencies (PyYAML, requests, pyOpenSSL, urllib3, easysnmp) were not removed."
@echo "To remove them manually: pip3 uninstall -y PyYAML requests pyOpenSSL urllib3 aioquic rrdtool easysnmp"
enable: check-root
@echo "==> Enabling APMonitor service..."
systemctl enable apmonitor.service
systemctl start apmonitor.service
@echo "==> Service enabled and started"
@sleep 2
@systemctl status apmonitor.service --no-pager
start: check-root
@echo "==> Starting APMonitor service..."
systemctl start apmonitor.service
@systemctl status apmonitor.service --no-pager
stop: check-root
@echo "==> Stopping APMonitor service..."
systemctl stop apmonitor.service
restart: check-root
@echo "==> Restarting APMonitor service..."
systemctl restart apmonitor.service
@sleep 2
@systemctl status apmonitor.service --no-pager
status:
@systemctl status apmonitor.service --no-pager
logs:
@echo "==> Showing APMonitor logs (Ctrl+C to exit)..."
journalctl -u apmonitor.service -f
test-config: check-sudo
@echo "==> Testing configuration as monitoring user..."
@if [ "$$(id -u)" -eq 0 ]; then \
su -s /bin/bash -c "$(INSTALL_DIR)/APMonitor.py --test-config $(CONFIG_DIR)/apmonitor-config.yaml" $(USER); \
else \
$(SUDO) -u $(USER) $(INSTALL_DIR)/APMonitor.py --test-config $(CONFIG_DIR)/apmonitor-config.yaml; \
fi
test-webhooks: check-sudo
@echo "==> Testing webhook notifications..."
@if [ "$$(id -u)" -eq 0 ]; then \
su -s /bin/bash -c "$(INSTALL_DIR)/APMonitor.py --test-webhooks -v $(CONFIG_DIR)/apmonitor-config.yaml" $(USER); \
else \
$(SUDO) -u $(USER) $(INSTALL_DIR)/APMonitor.py --test-webhooks -v $(CONFIG_DIR)/apmonitor-config.yaml; \
fi