Grafana Dashboards

LinuxBeginner
Practice Now

Introduction

In this lab, you will learn the fundamentals of creating a monitoring dashboard using Grafana. Grafana is a popular open-source platform for monitoring and observability that allows you to query, visualize, alert on, and explore your metrics no matter where they are stored.

You will start a monitoring stack from prepared configuration files. It includes:

  • Grafana: The visualization tool where you will build your dashboard.
  • Prometheus: A time-series database that will serve as the data source for Grafana.
  • Node Exporter: An agent that collects hardware and OS metrics from the host machine and exposes them for Prometheus to scrape.

Your goal is to build a simple dashboard from scratch that displays the live CPU usage of the lab environment.

Start and Explore the Monitoring Stack

In this step, you will start Grafana, Prometheus, and Node Exporter. Node Exporter exposes system metrics, Prometheus collects them, and Grafana displays them. The configuration and data source provisioning files are prepared for you.

cd /home/labex/project

Each pinned tag selects the version used in this lab. Downloads can take several minutes. Wait for each command to return to the prompt. If a pull fails, rerun that same pull; Docker reuses completed layers. Do not continue while a download is incomplete.

docker pull prom/prometheus:v3.3.0
docker pull prom/node-exporter:v1.9.1
docker pull grafana/grafana:11.6.0

docker network create creates a network where containers can resolve each other by name. docker run -d starts a container in the background; --name names it, --network joins that network, and -p maps a host port to a container port. The -v mounts attach the prepared configuration files. The :ro suffix makes these mounts read-only.

docker network create monitoring-net
docker run -d --name node-exporter --network monitoring-net -p 9100:9100 prom/node-exporter:v1.9.1
docker run -d --name prometheus --network monitoring-net -p 9090:9090 -v /home/labex/project/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro prom/prometheus:v3.3.0
docker run -d --name grafana --network monitoring-net -p 8080:3000 -v /home/labex/project/grafana/provisioning:/etc/grafana/provisioning:ro grafana/grafana:11.6.0

The curl options report HTTP failures and retry temporary startup errors up to 30 times, two seconds apart. A running container can still be initializing, so wait for the health check to succeed before continuing.

curl --fail --silent --show-error --retry 30 --retry-delay 2 --retry-connrefused --retry-all-errors http://localhost:9090/-/ready
curl --fail --silent --show-error --retry 30 --retry-delay 2 --retry-connrefused --retry-all-errors http://localhost:8080/api/health

Prometheus should report that it is ready; Grafana should return "database": "ok". Inspect the containers with:

docker ps

All three named containers should be Up. Select the Grafana lab tab. If embedded login is blocked, open the tab in a new browser tab with its open-in-browser control. Log in with username admin and password admin, then click Skip on the password-change screen. These credentials are for this temporary lab only.

Open Connections → Data sources and confirm that Prometheus is present. Its provisioning file connects to http://prometheus:9090 through the shared Docker network. Leave the stack running so Prometheus can collect multiple samples before you write the CPU query.

Create a New Dashboard in Grafana

In this step, you will create an empty dashboard and add its first visualization. A dashboard is a collection of panels arranged in a grid; each panel presents the result of a data query.

Open Dashboards in the left sidebar. Click New and select New dashboard.

On the empty dashboard, click Add visualization. In the data source picker, select Prometheus. This tells the new panel where to send its queries.

You should now see the panel editor, with the visualization preview, query editor, and display options. You will enter the CPU query in the next step.

Add Panel with PromQL Query for CPU Usage

In this step, you will add a panel to your dashboard and use a PromQL (Prometheus Query Language) query to fetch CPU usage data.

You should already be in the panel editor from the previous step.

At the bottom of the editor, you'll find the query section. The Prometheus data source should already be selected by default. Switch the query editor to Code, then enter the following PromQL query. You can copy and paste it directly.

100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

Let's break down this query:

  • node_cpu_seconds_total{mode="idle"}: This is a metric from the Node Exporter that counts the total number of seconds the CPU has been in an "idle" state.
  • rate(...[5m]): This function calculates the per-second average rate of increase of the idle time over the last 5 minutes. The result is a value between 0 and 1, representing the fraction of time the CPU was idle.
  • avg by (instance): This aggregates the results, which is useful if you have multiple CPUs or machines.
  • * 100: This converts the fractional value into a percentage (e.g., 0.95 becomes 95%).
  • 100 - ...: Finally, we subtract the idle percentage from 100 to get the active CPU usage percentage.

Click Run queries. The rate needs at least two scrapes; if the stack just started, wait about 30 seconds and run the query again. A graph should appear in the preview pane at the top, showing the CPU usage over time.

Your panel is now displaying data, but it can be improved with better labeling and formatting, which you will do in the next step.

Customize and Save the Dashboard

In this step, you will label the CPU panel, format its values as percentages, and save the dashboard. Clear labels make the result easier to interpret.

In the right-hand Panel options, set Title to exactly CPU Usage %.

Under Standard options, open Unit, search for percent, and select Misc / Percent (0-100). The query already returns values from 0 to 100, so this unit adds the percentage symbol without rescaling them.

Click Back to dashboard to keep the panel and return to the dashboard. The dashboard is not permanently saved yet.

Click Save dashboard. In the save dialog, enter the exact title My System Dashboard, then click Save. The lab uses this title to locate your saved dashboard.

Confirm that the saved dashboard contains the CPU Usage % panel with a CPU graph and percentage units.

Summary

In this lab, you have successfully built a Grafana dashboard from scratch. You started the monitoring stack from prepared configuration files and performed the following key actions:

  • Explored the environment consisting of Grafana, Prometheus, and Node Exporter containers.
  • Navigated the Grafana UI to create a new, empty dashboard.
  • Added a visualization panel and wrote a PromQL query to fetch CPU usage data from the Prometheus data source.
  • Customized the panel's title and unit formatting for better readability.
  • Saved the completed dashboard for future use.

You have now learned the fundamental workflow for creating visualizations in Grafana. You can build on this knowledge by adding more panels to your dashboard to monitor other system metrics like memory usage, disk I/O, or network traffic.