Full implementation of the nginx dynamic module with: - SO_BINDTODEVICE-based per-interface traffic attribution - Per-worker lock-free counters flushed to shared memory - Prometheus text and JSON scrape endpoint at configurable location - UDP-only global logtail (ipng_stats_logtail) for fire-and-forget access log streaming - $ipng_source_tag nginx variable for use in log_format/map - Histogram buckets, EWMA rate gauges, zone meta-metrics - Debian packaging (libnginx-mod-http-ipng-stats) - Robot Framework end-to-end tests via containerlab - SPDX Apache-2.0 headers on all source files
158 lines
5.9 KiB
Makefile
158 lines
5.9 KiB
Makefile
# SPDX-License-Identifier: Apache-2.0
|
|
# Makefile for nginx-ipng-stats-plugin
|
|
#
|
|
# Targets:
|
|
# build - build ngx_http_ipng_stats_module.so out-of-tree.
|
|
# pkg-deb - build a .deb via dpkg-buildpackage for the current release.
|
|
# robot-test - build .deb, then run Robot Framework end-to-end tests
|
|
# in containerlab (requires docker + containerlab).
|
|
# install-deps - install build and test dependencies via apt.
|
|
# clean - remove build artifacts and the fetched nginx source tree.
|
|
# help - print this help.
|
|
#
|
|
# Overridable variables:
|
|
# NGINX_SRC - path to an unpacked nginx source tree. If unset, the
|
|
# `build` target will apt-source one into ./build/nginx-src.
|
|
|
|
MODULE_NAME := ngx_http_ipng_stats_module
|
|
MODULE_DIR := $(CURDIR)
|
|
BUILD_DIR := $(CURDIR)/build
|
|
|
|
NGINX_SRC ?=
|
|
|
|
.PHONY: help build pkg-deb robot-test install-deps clean fetch-nginx-src
|
|
|
|
TEST ?= tests/
|
|
|
|
help:
|
|
@echo "nginx-ipng-stats-plugin — make targets"
|
|
@echo ""
|
|
@echo " make build Build $(MODULE_NAME).so out-of-tree."
|
|
@echo " make pkg-deb Build a Debian package via dpkg-buildpackage."
|
|
@echo " make robot-test Build .deb, then run Robot Framework e2e tests."
|
|
@echo " make install-deps Install build and test dependencies (apt)."
|
|
@echo " make clean Remove build artifacts."
|
|
@echo ""
|
|
@echo "Overridable:"
|
|
@echo " NGINX_SRC=<path> Use an existing nginx source tree."
|
|
@echo " TEST=<path> Run a specific .robot file (default: tests/)."
|
|
|
|
# ----------------------------------------------------------------------
|
|
# build: out-of-tree dynamic module build
|
|
# ----------------------------------------------------------------------
|
|
|
|
build: $(BUILD_DIR)/$(MODULE_NAME).so
|
|
@echo ""
|
|
@echo "Built: $(BUILD_DIR)/$(MODULE_NAME).so"
|
|
@echo ""
|
|
@echo "To try it locally without installing a .deb:"
|
|
@echo " sudo install -m 0644 $(BUILD_DIR)/$(MODULE_NAME).so /usr/lib/nginx/modules/"
|
|
@echo " echo 'load_module modules/$(MODULE_NAME).so;' | sudo tee /etc/nginx/modules-enabled/50-mod-http-ipng-stats.conf"
|
|
@echo " sudo nginx -t && sudo nginx -s reload"
|
|
|
|
$(BUILD_DIR)/$(MODULE_NAME).so: fetch-nginx-src
|
|
@set -e; \
|
|
if [ -z "$(NGINX_SRC)" ]; then \
|
|
NGX_SRC="$(BUILD_DIR)/nginx-src"; \
|
|
else \
|
|
NGX_SRC="$(NGINX_SRC)"; \
|
|
fi; \
|
|
echo "Configuring nginx in $$NGX_SRC against module at $(MODULE_DIR)"; \
|
|
cd "$$NGX_SRC" && ./configure --with-compat --add-dynamic-module=$(MODULE_DIR); \
|
|
echo "Building module"; \
|
|
$(MAKE) -C "$$NGX_SRC" -f objs/Makefile modules; \
|
|
mkdir -p $(BUILD_DIR); \
|
|
cp "$$NGX_SRC/objs/$(MODULE_NAME).so" $(BUILD_DIR)/$(MODULE_NAME).so
|
|
|
|
fetch-nginx-src:
|
|
@set -e; \
|
|
if [ -n "$(NGINX_SRC)" ]; then \
|
|
echo "Using NGINX_SRC=$(NGINX_SRC)"; \
|
|
exit 0; \
|
|
fi; \
|
|
if [ -d "$(BUILD_DIR)/nginx-src" ] && [ -f "$(BUILD_DIR)/nginx-src/configure" ]; then \
|
|
echo "Reusing $(BUILD_DIR)/nginx-src"; \
|
|
exit 0; \
|
|
fi; \
|
|
mkdir -p $(BUILD_DIR); \
|
|
if [ -d /usr/share/nginx/src ] && [ -f /usr/share/nginx/src/configure ]; then \
|
|
echo "Copying /usr/share/nginx/src (from nginx-dev) to $(BUILD_DIR)/nginx-src"; \
|
|
rm -rf $(BUILD_DIR)/nginx-src; \
|
|
cp -a /usr/share/nginx/src $(BUILD_DIR)/nginx-src; \
|
|
chmod -R u+w $(BUILD_DIR)/nginx-src; \
|
|
exit 0; \
|
|
fi; \
|
|
rm -rf $(BUILD_DIR)/apt-src; \
|
|
mkdir -p $(BUILD_DIR)/apt-src; \
|
|
echo "Fetching nginx source via \`apt source nginx\` in $(BUILD_DIR)/apt-src"; \
|
|
cd $(BUILD_DIR)/apt-src && apt source nginx; \
|
|
NGX_DIR=$$(find $(BUILD_DIR)/apt-src -maxdepth 1 -type d -name 'nginx-*' | head -n1); \
|
|
if [ -z "$$NGX_DIR" ]; then \
|
|
echo "error: could not find unpacked nginx source tree under $(BUILD_DIR)/apt-src" >&2; \
|
|
exit 1; \
|
|
fi; \
|
|
rm -rf $(BUILD_DIR)/nginx-src; \
|
|
mv "$$NGX_DIR" $(BUILD_DIR)/nginx-src; \
|
|
rm -rf $(BUILD_DIR)/apt-src
|
|
|
|
# ----------------------------------------------------------------------
|
|
# pkg-deb: build a .deb
|
|
# ----------------------------------------------------------------------
|
|
|
|
pkg-deb:
|
|
dpkg-buildpackage -us -uc -b
|
|
@mkdir -p $(BUILD_DIR)
|
|
@# dpkg-buildpackage writes artifacts to ../ — relocate them into
|
|
@# $(BUILD_DIR) so everything ephemeral lives under build/.
|
|
@for f in ../libnginx-mod-http-ipng-stats*.deb \
|
|
../libnginx-mod-http-ipng-stats*.ddeb \
|
|
../nginx-ipng-stats-plugin_*.buildinfo \
|
|
../nginx-ipng-stats-plugin_*.changes; do \
|
|
if [ -f "$$f" ]; then mv -f "$$f" $(BUILD_DIR)/; fi; \
|
|
done
|
|
@echo ""
|
|
@echo "Resulting .deb(s):"
|
|
@ls -1 $(BUILD_DIR)/*.deb 2>/dev/null || true
|
|
|
|
# ----------------------------------------------------------------------
|
|
# clean
|
|
# ----------------------------------------------------------------------
|
|
|
|
# ----------------------------------------------------------------------
|
|
# robot-test: containerlab + Robot Framework end-to-end tests
|
|
# ----------------------------------------------------------------------
|
|
|
|
tests/.venv: tests/requirements.txt
|
|
python3 -m venv tests/.venv
|
|
tests/.venv/bin/pip install -q -r tests/requirements.txt
|
|
|
|
robot-test: tests/.venv
|
|
@if [ ! -f $(BUILD_DIR)/libnginx-mod-http-ipng-stats_*.deb ]; then \
|
|
echo "error: no .deb found in $(BUILD_DIR)/. Run 'make pkg-deb' first." >&2; \
|
|
exit 1; \
|
|
fi
|
|
tests/rf-run.sh docker $(TEST)
|
|
|
|
# ----------------------------------------------------------------------
|
|
# install-deps: install build and test dependencies
|
|
# ----------------------------------------------------------------------
|
|
|
|
install-deps:
|
|
sudo apt-get update -qq
|
|
sudo apt-get install -y \
|
|
nginx-dev dpkg-dev debhelper \
|
|
python3 python3-venv \
|
|
curl
|
|
@echo ""
|
|
@echo "Build dependencies installed. For 'make robot-test' you also need:"
|
|
@echo " - docker: https://docs.docker.com/engine/install/debian/"
|
|
@echo " - containerlab: https://containerlab.dev/install/"
|
|
|
|
# ----------------------------------------------------------------------
|
|
# clean
|
|
# ----------------------------------------------------------------------
|
|
|
|
clean:
|
|
rm -rf $(BUILD_DIR) tests/.venv tests/out
|
|
-dh_clean 2>/dev/null || true
|