Skip to content

Fixes the caddy_site_activity plugin to properly support Caddy configurations with multiple server blocks, adds checks to prevent errors from undefined values#2

Open
mgc8 wants to merge 1 commit intomattpep:mainfrom
mgc8:fix-multiple-servers
Open

Fixes the caddy_site_activity plugin to properly support Caddy configurations with multiple server blocks, adds checks to prevent errors from undefined values#2
mgc8 wants to merge 1 commit intomattpep:mainfrom
mgc8:fix-multiple-servers

Conversation

@mgc8
Copy link
Copy Markdown

@mgc8 mgc8 commented Oct 29, 2025

The plugin was hardcoded to only look at srv0 in the Caddy configuration:

my $caddy_internal_names = $config->{apps}{http}{servers}{srv0}{logs}{logger_names};

This caused the plugin to fail or provide incomplete data when:

  • Caddy configurations use multiple named server blocks (e.g., srv0, srv1, etc.)
  • Server blocks are named differently than srv0
  • The configuration has additional servers for different ports or protocols

Changes

  1. Multi-server support
  • Iterate through all servers: Now loops through all entries in $config->{apps}{http}{servers} to build a complete mapping of internal names to hostnames
  • Applied to both config and fetch modes for consistency
  1. Hostname filtering
  • Modified count_json_log_hits(): Changed the function to accept $hostname instead of unused $statuses parameter
  • Filter log entries: Only count hits for log entries that match the specific hostname we're processing => this ensures accurate hit counts when multiple sites share the same log file or when processing multiple sites
  1. Check for undefined values
  • Added defined checks before comparing:
    • $$mapping{$_} in internal_name_to_site_name()
    • $logfile->{writer}{output} and $logfile->{encoder}{format} before string comparisons
    • Log entry fields (status, request, request{host}) before accessing them
  • Added explicit close(LOG_FILE) call
  • Skip unknown sites in fetch mode: Removed the code that printed "U" (unknown) values for unidentified sites, now just skips them like in config mode

Tested and confirmed to work under Munin 2.0.73 with no errors nor warnings

Fixes #1

  - Iterate through all servers instead of hardcoding srv0
  - Build complete hostname mapping across all server blocks
  - Add hostname filtering to count_json_log_hits() to ensure accurate per-site metrics
  - Add defensive checks for undefined values in config and log data
  - Close log file handles explicitly after reading

Signed-off-by: Mihnea-Costin Grigore <mihnea@mihnea.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

caddy_site_activity showing identical stats and "unknown" sites while ignoring servers

1 participant