Skip to content
Aditya Bhalsod
← DevOps
DevOpsBeginner

Following One Metric From Metricbeat Through Logstash to Grafana

Tracing memory usage through Metricbeat-ELK-Grafana, from the Metricbeat module to a daily Elasticsearch index and a provisioned dashboard, plus what to fix before real use.

  • #docker
  • #docker-compose
  • #metricbeat
  • #logstash
  • #grafana
Contents
  1. Hop one: Metricbeat samples it every ten seconds
  2. Hop two: Logstash passes it through
  3. Hop three: Elasticsearch stores it
  4. Hop four: Grafana finds it on startup
  5. What I would change first
  6. Where this setup stops

Metricbeat-ELK-Grafana is one Compose file, three small config folders and a Grafana dashboard. The README calls it a minimal local stack: Metricbeat sends host metrics to Logstash, Logstash writes them to Elasticsearch, and Grafana reads them back.

The quickest way to understand a pipeline like this is to follow one value through it. I picked memory usage, the field system.memory.used.pct behind the Memory Usage panel, and traced it through the files one hop at a time. Along the way I note what I would change before trusting the stack with anything real.

Hop one: Metricbeat samples it every ten seconds

YAML
# metricbeat/modules.d/system.yml
- module: system
  metricsets:
    - cpu
    - memory
    - network
    - process
    - process_summary
    - filesystem
  period: 10s
  processes: ['.*']
  enabled: true

memory is one of six metricsets, and all of them run every 10 seconds. The processes regex matches every process on the machine, so the process metricset reports each one on every cycle. The output side lives in metricbeat.yml:

YAML
# metricbeat/metricbeat.yml
output.logstash:
  hosts: ["logstash:5044"]

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

logstash is the Compose service name, so no IP address appears anywhere. The two processors attach host details, and cloud details when there are any, to each event before it leaves.

Hop two: Logstash passes it through

Ruby
input {
  beats {
    port => 5044
  }
}

filter {
  # minimal parsing; keep as-is for Metricbeat
}

output {
  elasticsearch {
    hosts => ["http://elasticsearch:9200"]
    index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
  }
  stdout { codec => rubydebug }
}

The filter block is empty, so the event goes through untouched. The only decision is in the output: the index name is the beat name plus the date. For Metricbeat that is a daily index, metricbeat-YYYY.MM.dd. The stdout line prints every event to Logstash's log, which is useful for proving data flows and noisy after that.

Hop three: Elasticsearch stores it

Elasticsearch runs as a single node with a 512 MB heap and a named volume, esdata. To check that the metric arrived, the import guide suggests listing the indices:

Bash
curl localhost:9200/_cat/indices

Nothing in the repo loads Metricbeat's index template. The README lists template setup as a next step. Kibana is in the stack and metricbeat.yml has a setup.kibana block, but the Compose command never runs metricbeat setup. So Elasticsearch guesses the field types, and the dashboard is written for that: its two terms panels query system.filesystem.mount_point.keyword and process.name.keyword, the sub-fields dynamic mapping creates for strings.

Hop four: Grafana finds it on startup

YAML
# grafana/provisioning/datasources/datasource.yml
datasources:
  - name: Elasticsearch
    type: elasticsearch
    url: http://elasticsearch:9200
    isDefault: true
    jsonData:
      timeField: "@timestamp"
      esVersion: 7

A second file, provisioning/dashboards/dashboard.yml, tells Grafana to scan /var/lib/grafana/dashboards every ten seconds, and Compose mounts ./grafana/dashboards at that path. The dashboard JSON is therefore loaded on startup, which makes the manual import steps in DASHBOARD_IMPORT_GUIDE.md look redundant.

The Memory Usage panel is a query against metricbeat-*: the average of system.memory.used.pct, grouped by a date histogram on @timestamp. That is the whole journey.

What I would change first

  1. Turn security on. Elasticsearch and Kibana both set xpack.security.enabled=false, ports 9200 and 5601 are published, and Grafana's admin password is admin in both the Compose file and the import guide. Enable security, move secrets out of the file and publish ports on 127.0.0.1 only.
  2. Make the versions agree. Every image is pinned, which is good. They disagree, though: Elasticsearch, Kibana and Metricbeat are 8.4.1, Logstash is 7.7.0, the README says 7.17 for all four, and the datasource says esVersion: 7.
  3. Cap memory. Only Elasticsearch has a heap setting. No service has a container memory limit, and Logstash and Kibana have no heap setting either.
  4. Point Metricbeat at the host. Compose mounts /proc, /sys/fs/cgroup and / under /hostfs, but nothing in the config mentions /hostfs. The process and filesystem panels may describe the container, not the host.
  5. Drop the debug output. Remove the stdout line once data flows. Nothing expires old daily indices, and the README's stop command, docker-compose down -v, deletes the whole data volume.
  6. Check two panels. CPU Usage has its only query marked "hide": true, and the CPU and load panels hard-code the datasource UID P31C819B24CF3C3C7, while datasource.yml never sets a uid.

Where this setup stops

For watching one machine on a laptop, this is enough. Logstash adds a moving part with no job here: with an empty filter, Metricbeat could write to Elasticsearch directly. It earns its place only when you need to parse or route events, and the filter block is where that work would go. Past that point the list above is the minimum, and I would add a retention policy before the disk fills.

Found this useful? Share it with your team.

Keep reading

More from DevOps and beyond