Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using Prometheus and Grafana

Introduction

Prometheus and Grafana are powerful tools for monitoring and visualizing system metrics. Prometheus is a monitoring and alerting toolkit, while Grafana is an open-source platform for data visualization and analytics. Together, they provide a comprehensive solution for monitoring and managing your infrastructure.

Setting Up Prometheus

First, we'll install and configure Prometheus. Follow these steps:

Step 1: Download Prometheus

Download the latest version of Prometheus from the official website:

wget https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz

Step 2: Extract the Archive

Extract the downloaded archive:

tar -xvf prometheus-2.26.0.linux-amd64.tar.gz

Step 3: Configure Prometheus

Create a configuration file prometheus.yml in the extracted directory:

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

Step 4: Start Prometheus

Navigate to the Prometheus directory and start the server:

./prometheus --config.file=prometheus.yml

You can access the Prometheus web UI at http://localhost:9090.

Setting Up Grafana

Next, we'll install and configure Grafana.

Step 1: Download Grafana

Download the latest version of Grafana from the official website:

wget https://dl.grafana.com/oss/release/grafana-7.5.4.linux-amd64.tar.gz

Step 2: Extract the Archive

Extract the downloaded archive:

tar -zxvf grafana-7.5.4.linux-amd64.tar.gz

Step 3: Start Grafana

Navigate to the Grafana directory and start the server:

./bin/grafana-server

You can access the Grafana web UI at http://localhost:3000. The default login credentials are admin/admin.

Connecting Prometheus to Grafana

Now, we'll connect Prometheus as a data source in Grafana.

Step 1: Add a Data Source

In the Grafana UI, navigate to Configuration > Data Sources and click Add data source.

Step 2: Configure the Data Source

Select Prometheus from the list of data sources and configure it with the following settings:

Name: Prometheus

URL: http://localhost:9090

Click Save & Test to verify the connection.

Creating Dashboards in Grafana

Finally, we'll create a dashboard in Grafana to visualize the metrics collected by Prometheus.

Step 1: Create a New Dashboard

In the Grafana UI, navigate to Create > Dashboard and click Add new panel.

Step 2: Configure the Panel

Configure the panel with the following settings:

Query: up

Visualization: Select a visualization type (e.g., Graph).

Click Apply to save the panel.

Step 3: Save the Dashboard

Click Save dashboard to save your dashboard with a meaningful name.

Conclusion

In this tutorial, you learned how to set up Prometheus for monitoring, install Grafana for visualization, and create dashboards to visualize your metrics. These tools provide a powerful solution for monitoring and managing your Linux infrastructure.