# Day 77: Grafana Alerting (Feb 11, 2024)

Grafana is a powerful open-source platform for monitoring and observability, allowing you to visualize and analyze your metrics data. One of its key features is the ability to set up alerts based on your metrics data. In this tut[oria](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)l, we'll guide you through the process of setting up alerting in Grafana using Prometheus as the data source. Specifically, we'll create an alert that triggers when the number of Docker containers is greater than 0.

## Prerequisites

Before you begin, make sure you have the following components set up:

1. **Prometheus:** Ensure that Prometheus is installed and configured to scrape Docker metrics. Prometheus will act as the data source for Grafana[.](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)
    
2. [**G**](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)**rafana:** Ins[tall](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) Grafana on your system and configure Prometheus as a data source. Th[is i](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)s usually done by specifying Prometheus URL in Grafana's data source settings.
    

I have covered everything in [this](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) blog.

## Step 1: Accessi[ng G](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)rafana Dashboard

1. Open your web browser and navigate to your Grafana instance.
    
2. Log in to Grafana using your credentials.
    

## Step 2: Adding Prometheus Data Sourc[e](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)

1. In the Grafana dashboard, click on t[he gea](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)r icon on the left sidebar to open [the](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) "Settings" menu.
    
2. Select "Data Sources" and click on the "[Add](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) your first data source" button.
    
3. Choose [Prome](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)theus from the list of available data [sou](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)rces.
    
4. Configure the Prometheus data source by providing the URL and any required authenticatio[n de](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)tails.
    
5. Click "Save & Test" to verify the connection to Prometheus.
    

## [Step](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) 3: Creating a Docker Containers Panel

1. Return to the Gr[afan](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)a dashboard and click the "+" icon on the left sidebar to add a new panel.
    
2. Choose Prometheus as [the](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) data source.
    
3. Write a PromQL query to retrieve the numbe[r of D](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)ocker containers. For example, the query c[ould](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) be `engine_daemon_container_states_containers{state="running"}`.
    
4. Customize the panel as need[ed,](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) adjusting visualization options and l[abel](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)s.
    
5. Save the panel by clicking "Apply" or "Save Dashboard" at the top.
    

## Step 4: Setting Up Alerts

1. With the Docker Containers panel selected, click o[n th](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)e panel title and choose "Edit."
    
2. In the panel editor, navigate to the "[Aler](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)t" tab.
    
3. Click on "Create Alert."
    
4. Configure the conditions fo[r the](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) alert. In this case, set [the](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) condition to trigger when the "Value is greater than 0."
    
5. Specify the alert details, [suc](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)h as the name, message, and severity.
    
6. Set up n[otif](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)ication channels (e.g., [emai](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)l, Slack, etc.) in the "Notifications" tab.
    
7. Click "Apply" or "Save Dashboard" to save the changes.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705936965114/8bd56649-15df-45ad-8f6b-eb28e727edf6.png?auto=compress,format&format=webp align="left")

## Step 5: Te[stin](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)g the Alert

1. To test the alert, intentionally create a situation [wher](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)e the number of Docker containers is greater than 0. This could involve starting a [Dock](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)er container.
    
2. Monitor the Grafana dashboard for the [alert](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus) status.
    
3. Verify that no[tifi](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)cations are sent to the configured channels.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705936981781/5cf8c3e8-70dc-450f-bcfc-1e957eb4a231.png?auto=compress,format&format=webp align="left")

Congratulations! You have successfully set up alerting in Grafana based on Docker container metrics from Pr[omet](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)heus. This ensures that you are promptly notified w[hene](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)ver the number of Docker containers exceeds the defined thresho[ld.](https://arjunmenon.hashnode.dev/day-75-monitoring-docker-logs-using-grafana-and-prometheus)
