Added services management to makefile

This commit is contained in:
Dan Snyder 2025-05-23 11:34:39 -04:00
parent f99110a0be
commit d18ea48346
4 changed files with 64 additions and 9 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
# Prerequisites
*.service
*.d
# Object files

View File

@ -1,3 +1,5 @@
help: ## show help message
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
CC = gcc
OUT_DIR = ./build
@ -15,6 +17,15 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CCFLAGS := $(INC_FLAGS)
.PHONY: clean install build
install: build create-services install-services
build: $(TARGET)
clean:
@rm -rf bin/ $(OUT_DIR) $(SERVICE_DIR)
./bin:
@mkdir -p ./bin
@cp ./dist/* ./bin
@ -28,9 +39,52 @@ $(OUT_DIR)/%.o: $(SRC_DIRS)/%.c | $(OUT_DIR)
$(TARGET): $(OBJS) | ./bin
$(CC) $(CCFLAGS) $(OBJS) -o $@
.PHONY: all clean
all: $(TARGET)
clean:
@rm -rf bin/ $(OUT_DIR)
TEMPLATE_DIR ?= ./template
SERVICE_DIR ?= ./systemd
INSTALL_DIR ?= /etc/systemd/system
WORKDIR := $(shell pwd)
# Collect all template service files
TEMPLATES := $(shell find $(TEMPLATE_DIR) -name '*.service.in')
# Generate names of service files that will have paths substituted
SERVICES := $(patsubst $(TEMPLATE_DIR)/%.service.in, $(SERVICE_DIR)/%.service, $(TEMPLATES))
# Generate names of installed service files
INSTALLED := $(patsubst $(SERVICE_DIR)/%, $(INSTALL_DIR)/%, $(SERVICES))
# Create services directory
$(SERVICE_DIR):
@mkdir -p $@
# Convert template service into actual service file
$(SERVICE_DIR)/%.service: $(TEMPLATE_DIR)/%.service.in | $(SERVICE_DIR)
sed 's|@WORKDIR@|$(WORKDIR)|g' $< > $@
create-services: $(SERVICES)
# install service into systemd
$(INSTALL_DIR)/%.service: $(SERVICE_DIR)/%.service
install -m 644 $< $(INSTALL_DIR)/
install-services: create-services $(INSTALLED)
systemctl daemon-reload
@for srv in $(SERVICES); do \
name=$$(basename $$srv); \
systemctl enable $$name; \
systemctl start $$name; \
done
uninstall-services:
@for srv in $(INSTALLED); do \
name=$$(basename $$srv); \
echo "Stopping and uninstalling $$name..."; \
systemctl stop $$name || true; \
systemctl disable $$name || true; \
if [ -f $$srv ]; then rm -f $$srv; fi; \
done
systemctl daemon-reload
reinstall-services: uninstall-services install-services

View File

@ -3,10 +3,10 @@ Description=Prometheus Node Exporter
After=network.target # Adjust based on dependencies, if any
[Service]
ExecStart=/home/monitoring/node_exporter/node_exporter --collector.mountstats --no-collector.processes --no-collector.arp --no-collector.nfs --no-collector.nfsd --no-collector.thermal_zone
ExecStart=@WORKDIR@/bin/node_exporter --collector.mountstats --no-collector.processes --no-collector.arp --no-collector.nfs --no-collector.nfsd --no-collector.thermal_zone --web.listen-address=:9100
Restart=always # Restart if the process crashes
RestartSec=5 # Wait 5 seconds before restarting
WorkingDirectory=/home/monitoring/node_exporter
WorkingDirectory=@WORKDIR@
[Install]
WantedBy=multi-user.target

View File

@ -1,12 +1,12 @@
[Unit]
Description=Proxmox monitor/exporter for Prometheus
Description=Proxmox Process Monitor/Exporter
After=network.target # Adjust based on dependencies, if any
[Service]
ExecStart=/home/monitoring/ProxMon/bin/proxmon 9101
ExecStart=@WORKDIR@/bin/proxmon 9101
Restart=always # Restart if the process crashes
RestartSec=5 # Wait 5 seconds before restarting
WorkingDirectory=/home/monitoring/ProxMon
WorkingDirectory=@WORKDIR@
[Install]
WantedBy=multi-user.target