The Cockpit: Intro to Prometheus and Grafana
·TechSoftware Development

The Cockpit: Intro to Prometheus and Grafana

Turn raw numbers into actionable insight. Discover the modern observability stack. Learn how 'Prometheus' collects metrics from your Linux server and how 'Grafana' builds beautiful real-time dashboards. Understand the role of the 'Node Exporter' agent.

Performance Visualization: Prometheus and Grafana

Reading logs is like reading a history book. It tells you what happened. But if you want to know what is happening right now, or if you want to see a trend (like "Is my RAM usage slowly increasing every day?"), you need Metrics.

In the modern world of DevOps and Site Reliability Engineering (SRE), the "Golden Duo" is Prometheus and Grafana.

  • Prometheus: The "Librarian." It visits your servers every 15 seconds, asks for their metrics, and stores them in a time-series database.
  • Grafana: The "Artist." It takes the numbers from Prometheus and draws beautiful, real-time graphs and dashboards.

In this lesson, we will understand how to connect your Linux server to this global monitoring web.


1. The Pull Model: How Prometheus Watches

Unlike traditional tools that "Push" data to a central server, Prometheus is a Pull system.

  1. Your Linux server runs a tiny program called Node Exporter.
  2. Node Exporter publishes a simple text page at http://your-ip:9100/metrics.
  3. Prometheus "Scrapes" (visits) that page periodically.

Why Pull?

If a server goes down, the "Push" sender just stops talking. In a "Pull" system, Prometheus notices immediately: "I tried to visit Server A, but the door was locked." This makes it much more reliable for detecting downtime.


2. Node Exporter: The Linux Spy

Node Exporter is the bridge between the Linux Kernel and the monitoring world. It translates complex kernel data (from /proc and /sys) into simple numbers Prometheus can understand.

# Example metrics published by Node Exporter:
node_cpu_seconds_total{cpu="0", mode="idle"} 10234.55
node_memory_MemFree_bytes 542389120
node_filesystem_avail_bytes{mountpoint="/"} 45000000000

3. Grafana: Turning Numbers into Truth

A list of 10,000 numbers is useless to a human admin. Grafana connects to Prometheus and allows you to build a Dashboard.

In the dashboard, you can see:

  • CPU Load: A "Speedometer" that turns red if load > 80%.
  • Disk Space: A "Bar Chart" showing how many days until the disk is full.
  • Network Traffic: A "Waveform" showing when your peak hours are.

4. Practical: Starting the Node Exporter

You don't need a complex setup to start. You can run Node Exporter on any Linux box.

# Download and start Node Exporter (Standard Port 9100)
./node_exporter &

# Verify it is working locally
curl http://localhost:9100/metrics

5. Troubleshooting: The Flatline

If your Grafana graph is a straight flat line at zero:

  1. Check the Exporter: Is the node_exporter service running on the target machine?
  2. Check the Firewall: Is Port 9100 open for the Prometheus server to visit?
  3. Check the Query: In Grafana, did you select the correct "Instance" name?

6. Example: A Metric Scraper (Python)

You don't need Prometheus to read these numbers. Since Node Exporter just produces a text web page, you can write your own Python script to scrape it and send an alert if a specific number looks wrong.

import requests
import re

def scrape_local_metrics(url="http://localhost:9100/metrics"):
    """
    Manually parses Node Exporter output for a specific metric.
    """
    try:
        response = requests.get(url, timeout=2)
        content = response.text
        
        # Look for the Free Memory metric
        # node_memory_MemFree_bytes 123456
        match = re.search(r"node_memory_MemFree_bytes (\d+)", content)
        if match:
            free_bytes = int(match.group(1))
            free_gb = free_bytes / (1024**3)
            print(f"Current Free RAM: {free_gb:.2f} GB")
            
            if free_gb < 0.5:
                print("[!!!] ALERT: Critical Memory Depletion!")
        
    except Exception as e:
        print(f"Could not reach Node Exporter: {e}")

if __name__ == "__main__":
    scrape_local_metrics()

7. Professional Tip: Use 'Alertmanager'

Prometheus doesn't just store data; it can Act. You can define a rule: "If a server's CPU stays above 95% for more than 5 minutes, send a message to the Slack #ops channel." This is handled by a side-car tool called the Prometheus Alertmanager.


8. Summary

Visualization turns "Data" into "Intelligence."

  • Node Exporter collects the raw facts from the kernel.
  • Prometheus pulls and stores those facts over time.
  • Grafana makes the facts beautiful and actionable.
  • Observability is the goal: knowing the state of your system without logging in.

In the final lesson of this module, we will explore the security side of monitoring: Auditing auth.log and implementing Fail2Ban.

Quiz Questions

  1. What is the difference between a "Log" and a "Metric"?
  2. Why does Prometheus use a "Pull" architecture instead of a "Push" architecture?
  3. What information does the "Node Exporter" provide to the monitoring stack?

Continue to Lesson 6: Security Monitoring—auth.log and Fail2Ban.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn