AWStats

From Leo's Notes
Last edited on 23 May 2023, at 18:08.

AWStats is an open source Perl-based web application that generates web analytics from access logs.

Running in Docker to parse Traefik access logs

Use the Docker image built by pabra at: https://github.com/pabra/docker_awstats

Create a docker-compose.yml file and configure the AWSTATS_CONF_LOGFORMAT environment variable to match the Traefik access log syntax.

version: "3"
services:
  awstats:
    build:
      context: docker_awstats
      dockerfile: Dockerfile
    networks:
      - traefik
    volumes:
      - /volumes/awstats:/var/lib/awstats
      - /var/log/traefik:/logs:ro
    environment:
      - "TZ=MST7MDT,M3.2.0,M11.1.0"
      - 'AWSTATS_CONF_LOGFORMAT=%host %other %other %time1 %methodurl %code %bytesd %refererquot %uaquot'
      - 'AWSTATS_CONF_SITEDOMAIN="example.com"'
      - 'AWSTATS_CONF_HOSTALIASES="example.com"'

    labels:
      - traefik.enable=true
      - traefik.docker.network=traefik
      - traefik.http.middlewares.awstats-https-redirect.redirectscheme.scheme=https
      # http
      - traefik.http.routers.awstats.entrypoints=http
      - 'traefik.http.routers.awstats.rule=Host(`awstats.example.com`)'
      - traefik.http.routers.awstats.middlewares=awstats-https-redirect
      # https
      - traefik.http.routers.awstats-https.entrypoints=https
      - 'traefik.http.routers.awstats-https.rule=Host(`awstats.example.com`)'
      - traefik.http.routers.awstats-https.tls=true
      # - traefik.http.routers.awstats-https.tls.certresolver=letsencrypt
      - traefik.http.routers.awstats-https.service=awstats-https
      - traefik.http.services.awstats-https.loadbalancer.server.port=80

networks:
  internal:
    internal: true
  traefik:
    name: traefik
    external: true

To process existing Traefik logs, enter the container and then run:

# /usr/lib/awstats/cgi-bin/awstats.pl -update -config=example.com -LogFile="zcat /logs/access.log-rotated.gz |"

If you only have a single access.log file for all your hosts, you may tack on an extra grep to filter out only log entries that you're interested in. For example:

# /usr/lib/awstats/cgi-bin/awstats.pl -update -config=example.com -LogFile="zcat /logs/access.log-rotated.gz | grep -E 'example|my-webapp' | "