#!/usr/bin/env bash # # Alloy Config Generator # # Interactive script that generates a Grafana Alloy configuration # file based on your environment. Asks what backends you use, what # signals to collect, and what services to monitor, then outputs # a working config.alloy ready to deploy. # # Usage: # ./alloy-config-generator.sh # ./alloy-config-generator.sh -o /etc/alloy/config.alloy # ./alloy-config-generator.sh --non-interactive --metrics --logs --prometheus-url http://mimir:9009 # # Parameters: # -o, --output FILE Write config to file (default: stdout) # --non-interactive Skip prompts, use flags and defaults # --metrics Enable host metrics collection # --logs Enable log collection # --traces Enable OTLP trace collection # --journald Enable journald log collection # --docker Enable Docker container log collection # --nginx Enable nginx log collection # --prometheus-url URL Prometheus/Mimir remote_write URL # --loki-url URL Loki push URL # --tempo-url URL Tempo OTLP endpoint (host:port) # --hostname NAME Hostname label for metrics and logs # --scrape-targets LIST Comma-separated host:port targets to scrape # --help Show usage # # Author: Phil Connor # Contact: contact@mylinux.work # Website: https://mylinux.work # License: MIT # Version: 1.0 set -euo pipefail # --- Configuration --- readonly VERSION="1.0" readonly SCRIPT_NAME="$(basename "$0")" # Defaults OUTPUT="" NON_INTERACTIVE=false ENABLE_METRICS=false ENABLE_LOGS=false ENABLE_TRACES=false ENABLE_JOURNALD=false ENABLE_DOCKER=false ENABLE_NGINX=false PROMETHEUS_URL="" LOKI_URL="" TEMPO_URL="" HOSTNAME="" SCRAPE_TARGETS="" # --- Functions --- usage() { cat </dev/null || echo "server") ask "Hostname label" "$detected_hostname" HOSTNAME echo "" # --- Backends --- echo "== Backends ==" echo "" ask "Prometheus/Mimir remote_write URL (leave empty to skip metrics)" "http://prometheus:9090/api/v1/write" PROMETHEUS_URL if [[ -n "$PROMETHEUS_URL" ]]; then ENABLE_METRICS=true fi ask "Loki push URL (leave empty to skip logs)" "http://loki:3100/loki/api/v1/push" LOKI_URL if [[ -n "$LOKI_URL" ]]; then ENABLE_LOGS=true fi ask "Tempo OTLP endpoint host:port (leave empty to skip traces)" "" TEMPO_URL if [[ -n "$TEMPO_URL" ]]; then ENABLE_TRACES=true fi echo "" # --- Metrics options --- if [[ "$ENABLE_METRICS" == true ]]; then echo "== Metrics ==" echo "" local extra_targets="" ask "Additional Prometheus scrape targets (comma-separated host:port, or empty)" "" extra_targets if [[ -n "$extra_targets" ]]; then SCRAPE_TARGETS="$extra_targets" fi echo "" fi # --- Log options --- if [[ "$ENABLE_LOGS" == true ]]; then echo "== Logs ==" echo "" ask_yn "Collect journald logs?" "y" ENABLE_JOURNALD ask_yn "Collect Docker container logs?" "n" ENABLE_DOCKER ask_yn "Collect nginx logs?" "n" ENABLE_NGINX echo "" fi } # --- Config generation functions --- generate_header() { cat <\\\\S+) (?P\\\\S+) (?P\\\\S+)\" (?P\\\\d+) (?P\\\\d+)" } stage.labels { values = { method = "", status = "", } } } EOF } generate_logs_write() { if [[ "$ENABLE_LOGS" != true ]]; then return fi cat <&2; usage ;; esac done # Set hostname default if [[ -z "$HOSTNAME" ]]; then HOSTNAME=$(hostname -s 2>/dev/null || echo "server") fi # Set backend URL defaults for non-interactive mode if [[ "$NON_INTERACTIVE" == true ]]; then [[ "$ENABLE_METRICS" == true && -z "$PROMETHEUS_URL" ]] && PROMETHEUS_URL="http://prometheus:9090/api/v1/write" [[ "$ENABLE_LOGS" == true && -z "$LOKI_URL" ]] && LOKI_URL="http://loki:3100/loki/api/v1/push" [[ "$ENABLE_TRACES" == true && -z "$TEMPO_URL" ]] && TEMPO_URL="tempo:4317" fi # Interactive mode if [[ "$NON_INTERACTIVE" != true ]]; then interactive_setup fi # Check at least one signal is enabled if [[ "$ENABLE_METRICS" != true && "$ENABLE_LOGS" != true && "$ENABLE_TRACES" != true ]]; then echo "ERROR: No signals enabled. Enable at least one of: metrics, logs, traces" >&2 exit 1 fi # Generate config if [[ -n "$OUTPUT" ]]; then generate_config > "$OUTPUT" echo "" echo "Config written to: $OUTPUT" echo "" echo "Signals enabled:" [[ "$ENABLE_METRICS" == true ]] && echo " ✓ Metrics → $PROMETHEUS_URL" [[ "$ENABLE_LOGS" == true ]] && echo " ✓ Logs → $LOKI_URL" [[ "$ENABLE_TRACES" == true ]] && echo " ✓ Traces → $TEMPO_URL" echo "" echo "Next steps:" echo " 1. Review the config: cat $OUTPUT" echo " 2. Validate syntax: alloy fmt $OUTPUT" echo " 3. Test it: alloy run $OUTPUT" echo " 4. Deploy: sudo cp $OUTPUT /etc/alloy/config.alloy && sudo systemctl restart alloy" else generate_config fi } main "$@"