Sign inSign up

301167/server-metrics

By 301167

•Updated 3 days ago

Sends host and container CPU, memory and disk usage to Seq every minute

Image
0

450

301167/server-metrics repository overview

⁠Server Metrics to Seq

Docker container that sends the host's CPU, load, memory and disk usage to Seq⁠ as a structured event every minute, so you can chart the trend in a Seq dashboard. Optionally it also sends CPU, memory and disk usage per running container.

⁠Using

This project uses:

/proc/stat and /proc/meminfo inside a container report the host's values, so no privileged mode is needed. The host root filesystem is mounted read-only on /host for the disk figures.

Container figures come from the Docker API. Instead of mounting /var/run/docker.sock into this container (which gives root-equivalent access to the host), compose.yaml⁠ runs a socket proxy on an internal network that only allows reading container info and stats.

⁠Usage / Commands

⁠Build or pull image

docker build -t <dockerhub-user>/server-metrics:latest .
or
docker pull <dockerhub-user>/server-metrics:latest

⁠Run container

docker run -d --name server-metrics --restart unless-stopped \
  --network logging \
  -v /:/host:ro \
  -e HOST_NAME=docker-server \
  -e SEQ_URL=http://seq:5341 \
  -e SEQ_API_KEY=myapikey \
  <dockerhub-user>/server-metrics:latest

For container metrics, use the Portainer stack below (it adds the socket proxy) or set DOCKER_URL to a socket proxy with CONTAINERS=1 that this container can reach.

⁠Portainer stack

Use compose.yaml⁠: Stacks → Add stack → paste it (or point to this repository) and set SEQ_API_KEY under Environment variables.

⁠Environment variables

VariableDefaultDescription
HOST_NAMEcontainer hostnameValue of the Host property in Seq
SEQ_URLhttp://seq:5341Seq ingestion URL
SEQ_API_KEY(empty)Seq API key; omitted when empty
INTERVAL60Seconds between measurements
WARN_PERCENT90Event level becomes Warning when CPU, memory or disk usage exceeds this
DISK_PATH/hostPath whose filesystem is measured
DOCKER_URL(empty)Docker API (socket proxy) URL, e.g. http://docker-proxy:2375; container metrics are off when empty

⁠Event properties

⁠Host

Every host event has Application = 'server-metrics' and these properties:

PropertyDescription
CpuPercentAverage CPU usage (all cores) since the previous measurement
CpuCores, CpuUsedCoresNumber of cores (capacity) and cores in use
Load1, Load5, Load15Load average
MemUsedPercent, MemUsedMB, MemTotalMBMemory used, excluding page cache
DiskUsedPercent, DiskUsedGB, DiskFreeGB, DiskTotalGBFilesystem of DISK_PATH

⁠Containers

With DOCKER_URL set, every running container gets an event with Application = 'container-metrics', the same Host and these properties:

PropertyDescription
Container, Image, ComposeProjectContainer name, image and compose/stack project (when set)
CpuPercent, CpuUsedCoresCPU usage as a share of all host cores (like the host CpuPercent) and cores in use, over about one second
MemUsedMB, MemLimitMB, MemUsedPercentMemory used excluding page cache (as docker stats), and the container limit (host memory when unlimited)
DiskUsedMBSize of the container's writable layer; volumes and the image are not included

The level is Warning when CpuPercent or MemUsedPercent exceeds WARN_PERCENT.

⁠Seq dashboard queries

select mean(CpuPercent) as CPU, mean(MemUsedPercent) as Memory from stream where Application = 'server-metrics' group by time(5m)
select min(DiskFreeGB) as DiskFree from stream where Application = 'server-metrics' group by time(1h)
select mean(CpuPercent) as CPU from stream where Application = 'container-metrics' group by Container, time(5m)
select mean(MemUsedMB) as Memory from stream where Application = 'container-metrics' group by Container, time(5m)

⁠Container dashboards

seq-container-dashboards.sh⁠ creates a Seq dashboard named Container - <name> for every running container, copying the charts of the server metrics dashboard. Re-run it after adding containers; existing dashboards are updated, not duplicated. It needs a Seq API key with the Setup permission in a file on the server and runs inside the server-metrics container:

ssh [email protected] 'docker cp ~/.seq-dashboard-key server-metrics:/tmp/seq.key &&
  docker exec -i -e SEQ_ADMIN_KEY_FILE=/tmp/seq.key server-metrics sh -s;
  docker exec server-metrics rm -f /tmp/seq.key' < seq-container-dashboards.sh

Add -e DRY_RUN=1 to only print what would be created, or -e EXCLUDE="docker-proxy server-metrics" to skip containers.

⁠Publishing

GitHub Actions (docker-publish.yml⁠) builds for linux/amd64, linux/arm64 and linux/arm/v7 and pushes to Docker Hub on every push to main, on v*.*.* tags and weekly. Set the repository secrets DOCKERHUB_USERNAME and DOCKERHUB_TOKEN.

Tag summary

Content type

Image

Digest

sha256:1f4203d9d…

Size

6.4 MB

Last updated

3 days ago

docker pull 301167/server-metrics