diff --git a/.gitignore b/.gitignore index d514f10..c7092c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Prerequisites +*.service *.d # Object files diff --git a/Makefile b/Makefile index 3551e7a..cb776ba 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/systemd/prometheus-export.service b/template/node-exporter.service.in similarity index 52% rename from systemd/prometheus-export.service rename to template/node-exporter.service.in index f23bc9f..5b631ba 100644 --- a/systemd/prometheus-export.service +++ b/template/node-exporter.service.in @@ -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 diff --git a/systemd/proxmon.service b/template/proxmon-exporter.service.in similarity index 61% rename from systemd/proxmon.service rename to template/proxmon-exporter.service.in index b3de9c8..1c136a3 100644 --- a/systemd/proxmon.service +++ b/template/proxmon-exporter.service.in @@ -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