basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    ManualHomelabLogs

    Vector: log pipeline and splitting Traefik’s access log by service

    Vector reads logs from files and containers, parses and filters lines, and writes them to files or ships them to storage. Paired with Traefik, it reads the shared access.log, drops noisy agent traffic, and splits events by service for CrowdSec.

    When CrowdSec reads the shared access.log directly, frequent POST /api/agents calls from your own monitoring agent can trigger HTTP-flood detection and ban your own server, while acquisition metrics lump every site onto one line. The overall Traefik, CrowdSec, Loki, and rotation layout is covered in Traefik logs; this manual focuses on Vector and its pipeline.

    How the pipeline works

    Vector is a log and metrics collection and routing agent written in Rust. Its Alpine image stays within tens of megabytes of RAM (mem_limit: 48M is plenty in production).

    The entire pipeline lives in one YAML file as a directed graph built from three block types:

    sources ─→ transforms ─→ sinks
    Section Role Example types
    sources Where events come from file (log files), docker_logs, journald, syslog, http_server
    transforms Parsing, filtering, routing remap (field manipulation in VRL), filter (dropping lines), route (branching)
    sinks Where results go file (files on disk), loki, elasticsearch, clickhouse, console

    Blocks wire together through inputs: each transform and sink names the block feeding it. One output can fan out to multiple sinks — writing per-service files for CrowdSec and shipping to Loki in parallel.

    A route block splits the stream into branches by condition, creating outputs named <name>.<branch>. Anything that matches no condition goes to the built-in <name>._unmatched output so nothing gets silently lost.

    Splitting Traefik’s access log by service

    The pipeline reads JSON lines from /logs/access.log, drops monitoring agent noise, and writes each service’s events to its own file under /out/:

    /logs/access.log ─→ traefik_raw (file)
                     ─→ parse_json (remap: parse_json!)
                     ─→ global_filter (filter: drop agent noise)
                     ─→ split_services (route: by RequestHost)
                          ├─ .notes      ─→ /out/notes.log
                          ├─ .monitor    ─→ /out/monitor.log
                          ├─ .panel      ─→ /out/panel.log
                          └─ ._unmatched ─→ /out/other.log
    /opt/traefik/vector/vector.yaml
    data_dir: /var/lib/vector

    Directory for read checkpoints. Mount it on a named volume; otherwise recreating the container wipes the checkpoints and Vector rereads access.log from the beginning.

    sources:
      traefik_raw:
        type: file
        include:
          - /logs/access.log
        read_from: beginning

    Reads access.log. read_from: beginning applies only to files without a saved checkpoint — such as a fresh access.log after rotation. Vector identifies files by a checksum of their first bytes (checksum fingerprint), so renaming doesn’t confuse it.

    default:read_from beginning

    transforms:
      parse_json:
        type: remap
        inputs:
          - traefik_raw
        source: |
          . = parse_json!(.message)

    Each line arrives as a string in .message. The VRL function parse_json! parses the JSON and promotes Traefik’s fields (RequestHost, RequestMethod, RequestPath) to the root of the event. The exclamation mark aborts on malformed JSON so broken lines don’t travel further down the pipeline.

      global_filter:
        type: filter
        inputs:
          - parse_json
        condition: |
          !(.RequestHost == "monitor.example.org" && .RequestMethod == "POST" && starts_with(string!(.RequestPath), "/api/agents"))

    A surgical filter drops frequent monitoring-agent POST requests. Keep the condition narrow — host, method, and path prefix together: CrowdSec never sees dropped lines, so a broad filter would hide an attack on the same domain.

      split_services:
        type: route
        inputs:
          - global_filter
        route:
          notes: '.RequestHost == "example.org"'
          monitor: '.RequestHost == "monitor.example.org"'
          panel: '.RequestHost == "panel.example.org"'
    Key part

    One branch per service based on .RequestHost. Explicit route rules with static filenames are safer than templating paths from the Host header: any client can send an arbitrary Host, and a template would let junk requests spawn files on disk.

    sinks:
      notes_file:
        type: file
        inputs: [split_services.notes]
        path: /out/notes.log
        encoding: { codec: json }
    
      monitor_file:
        type: file
        inputs: [split_services.monitor]
        path: /out/monitor.log
        encoding: { codec: json }
    
      panel_file:
        type: file
        inputs: [split_services.panel]
        path: /out/panel.log
        encoding: { codec: json }
    
      fallback_file:
        type: file
        inputs: [split_services._unmatched]
        path: /out/other.log
        encoding: { codec: json }

    Sinks write JSON lines into /out/*.log. The split_services._unmatched branch sends direct-IP hits and unknown hostnames into other.log so CrowdSec still catches scanners.

    Next, point CrowdSec (acquis.d/traefik.yaml) at the split files instead of access.log:

    filenames:
      - /opt/traefik/data/logs/split/*.log
    labels:
      type: traefik

    In cscli metrics show acquisition, every file gets its own row, showing per-service traffic and parser hits at a glance.

    Container in the Traefik stack and log rotation

    Keep Vector in the same docker-compose.yaml as Traefik since both work with ./data/logs.

    services:
      vector:
        image: timberio/vector:latest-alpine
        mem_limit: 48M
        memswap_limit: 64M
        container_name: vector
        restart: unless-stopped
        volumes:
          - ./data/logs:/logs:ro
          - ./vector/vector.yaml:/etc/vector/vector.yaml:ro
          - ./data/logs/split:/out
          - vector_data:/var/lib/vector
    
    volumes:
      vector_data:

    vector.yaml is bind-mounted as a single file (Single-file mounts): if an editor on the host saves via a temporary file and rename, the container keeps seeing the old inode. After editing, run docker compose up -d --force-recreate vector, not restart. Create ./data/logs/split beforehand if it doesn’t exist yet — otherwise Docker creates it owned by root (A missing volume path).

    Rotate split/*.log separately from access.log. Vector keeps its output files open and doesn’t reopen them on USR1 the way Traefik does, so /etc/logrotate.d/vector-split-logs needs copytruncate (copy to archive, then truncate the active file in place). The complete logrotate config and rotation layout for traefik.log, access.log, and split/*.log are in Traefik logs.

    When Vector is needed and when it isn’t

    Situation What to use
    One or two sites behind Traefik, no noisy agents Let CrowdSec read access.log directly — skip Vector
    Multiple services, noisy clients, or per-site CrowdSec metrics Vector with filter and route writing to split/*.log
    Shipping logs to Loki or another backend alongside filtering Vector replaces a separate collector like Promtail: attach a second sink to parse_json or global_filter

    If it doesn’t work

    The vector container enters a restart loop right after editing vector.yaml

    Cause
    The config has a YAML syntax error, a typo in an inputs block name, or an invalid VRL expression.
    Fix
    Inspect docker logs vector and validate the file before restarting with vector validate (see “How to check”).

    After updating the image, duplicates appear in split/*.log and CrowdSec bans for old events

    Cause
    data_dir lived inside the container’s writable layer and vanished on recreate, so access.log was read again from line 1.
    Fix
    Mount /var/lib/vector on a named volume vector_data. If the container is already running without a volume, copy its checkpoints first using step 4 in Traefik logs.

    The vector container is running, yet split/ is empty or the files stop growing

    Cause
    Vector is watching the wrong path (for example, an empty directory instead of ./data/logs), or Traefik is set to common format instead of json so parse_json! drops every line.
    Fix
    Match the /logs bind mount against include, check docker logs vector for parse errors, and verify accesslog.format: json in Traefik’s config.

    Requests to a newly added site end up in other.log

    Cause
    No branch was added for the new domain in split_services and no sink in sinks, so events fell through to _unmatched.
    Fix
    Add a route rule and a file sink for the domain, validate the config, and recreate vector.

    Editing vector.yaml on the host didn’t change how Vector behaves

    Cause
    vector.yaml is bind-mounted as a single file: the editor replaced it with a new inode, or the container wasn’t restarted.
    Fix
    Recreate the container with docker compose up -d --force-recreate vector (Single-file mounts).

    split/*.log files grow forever, or Vector stops writing to them after rotation

    Cause
    /etc/logrotate.d/vector-split-logs is missing or lacks copytruncate — Vector holds file descriptors open and keeps writing to the renamed file.
    Fix
    Add a config with copytruncate in /etc/logrotate.d/vector-split-logs and test it with logrotate -d.

    How to check

    Validate YAML syntax, inputs wiring, and VRL expressions before restarting the container1:

    docker exec vector vector validate --no-environment /etc/vector/vector.yaml

    Open one of your services and confirm events reach the right file under split/ and CrowdSec reads them:

    ls -la /opt/traefik/data/logs/split/
    tail -n 3 /opt/traefik/data/logs/split/notes.log
    docker exec crowdsec cscli metrics show acquisition

    Not verified yet

    1. whether vector validate –no-environment works inside the timberio/vector:latest-alpine image ↑