Server Monitoring with Grafana and Prometheus
Tags: devops, monitoring, docker, linux
Published: 2024-01-05 15:06:36

Server monitoring is crucial for maintaining the health and performance of your systems. In this blog post, we'll walk through a basic setup using Grafana and Prometheus to monitor your servers. Before diving into the configuration details, let's briefly outline the components we'll be using in this setup.

## What is Grafana?

Grafana is an open-source platform for monitoring and observability. It allows you to create, explore, and share interactive dashboards, enabling you to visualize and understand your metrics, logs, and other data sources easily.

## What is Prometheus?

Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability. It collects and stores time-series data, making it an excellent choice for monitoring systems and generating alerts based on predefined conditions.

## What is Node Exporter?

Node Exporter is a Prometheus exporter for hardware and OS metrics. It runs on the servers you want to monitor and collects various system-level metrics, such as CPU usage, memory usage, disk activity, and network statistics. Prometheus scrapes these metrics from Node Exporter, providing a centralized location for monitoring your infrastructure.

## Requirements

Before we begin, ensure you have Docker installed on your system. Once Docker is set up, you can use the provided `docker-compose.yml` and `prometheus.yml` files to launch the monitoring stack.

**docker-compose.yml**

```yaml
version: '3.8'

networks:
  monitoring:
    driver: bridge

volumes:
  prometheus_data: {}
  grafana_storage: {}

services:
  node-exporter:
    image: prom/node-exporter:latest
    container_name: node-exporter
    restart: unless-stopped
    expose:
      - 9100
    networks:
      - monitoring

  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: unless-stopped
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    expose:
      - 9090
    networks:
      - monitoring

  grafana:
    image: grafana/grafana:9.5.15-ubuntu
    container_name: grafana
    restart: unless-stopped
    ports:
     - '3000:3000'
    volumes:
      - 'grafana_storage:/var/lib/grafana'
    networks:
      - monitoring
```

**prometheus.yml**

```yaml
global:
  scrape_interval: 1m

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 1m
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node'
    static_configs:
      - targets: ['node-exporter:9100']
```
This file serves as the configuration for Prometheus, specifying the intervals at which it scrapes metrics and targets it monitors. In our case, it targets the Node Exporter on port 9100 since we are running that container in our Docker Compose setup.

## Setting Up Grafana and Prometheus

1. **Create a Directory and Copy YAML Files:**
```console
mkdir server-monitoring
cd server-monitoring
```

2. **Launch the Stack:**
Run the following command to start the monitoring services.
```console
docker-compose up -d
```
This command will pull the necessary Docker images and launch the Grafana, Prometheus and Node Exporter containers.

3. **Access Grafana Dashboard:**
Open your web browser and navigate to http://localhost:3000 (or IP address of your virtual machine). Log in using the default credentials (username: admin, password: admin).

After initial login you will be prompted to change the password.

4. **Configure Prometheus as a Data Source:**
In Grafana, add Prometheus as a data source by specifying the URL http://prometheus:9090.

5. **Import Node Exporter Full Dashboard:**
Grafana provides a rich collection of dashboards that can be imported to visualize various metrics. To import the Node Exporter Full dashboard, follow these steps:

- From the left pane menu, select "Dashboards."
- On the right side, click "New" and then select "Import" from the dropdown menu.
- In the "Grafana.com Dashboard" section, enter the dashboard ID 1860 and click "Load."
- Configure the Prometheus data source (if not configured already) by selecting it from the drop-down menu.
- Finally, click "Import" to add the Node Exporter Full dashboard to your Grafana instance.

This dashboard (ID 1860) is specifically designed for Node Exporter metrics and provides a comprehensive view of your server's performance.


![Node Exporter Full Dashboard in Grafana](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqjln4i1y0hptgfgfjdu.png)

## Conclusion

With Grafana and Prometheus, you now have a basic yet powerful server monitoring setup. Explore the imported Node Exporter Full dashboard and other Grafana features to gain insights into your system's performance. Feel free to customize the setup and dashboards to fit your specific monitoring needs.

Happy monitoring! 🚀