Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Integrating Grafana with Shell Scripts

Introduction to Grafana Integration

Grafana is a powerful open-source platform for monitoring and observability. It provides beautiful visualizations, dashboards, and alerts. Integrating Grafana with shell scripts allows you to visualize custom metrics, automate data collection, and enhance your monitoring capabilities.

Setting Up Grafana

Before integrating Grafana with shell scripts, you need to set up Grafana on your server. Follow these steps:


# Add the Grafana APT repository
sudo apt-get install -y software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"

# Install Grafana
sudo apt-get update
sudo apt-get install -y grafana

# Start and enable the Grafana service
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
                

Creating Custom Metrics with Shell Scripts

You can create custom metrics using shell scripts and expose them to Grafana using Prometheus as a data source. Here's an example shell script that generates a custom metric:


#!/bin/bash

# Custom metric for example counter
echo "# HELP example_counter A simple counter for demonstration purposes"
echo "# TYPE example_counter counter"
echo "example_counter $(($RANDOM % 100))"
                

Save this script as custom_metric.sh and make it executable:


chmod +x custom_metric.sh
                

Exposing Metrics to Prometheus

To expose the custom metrics to Prometheus, you can use a simple HTTP server. Here's an example using Python's built-in HTTP server:


#!/bin/bash

# Start a simple HTTP server to expose metrics
while true; do
  ./custom_metric.sh > /tmp/metrics
  sleep 10
done &
python3 -m http.server --directory /tmp 9100
                

Save this script as serve_metrics.sh and make it executable:


chmod +x serve_metrics.sh
./serve_metrics.sh
                

This script runs custom_metric.sh every 10 seconds and serves the metrics on port 9100.

Configuring Prometheus to Scrape Custom Metrics

Configure Prometheus to scrape the custom metrics. Add the following job configuration to your prometheus.yml file:


scrape_configs:
  - job_name: 'custom_metrics'
    static_configs:
      - targets: ['localhost:9100']
                

Restart Prometheus to apply the configuration changes:


sudo systemctl restart prometheus
                

Adding Prometheus as a Data Source in Grafana

Log in to your Grafana instance (default URL: http://localhost:3000). Follow these steps to add Prometheus as a data source:

  1. Go to the Grafana homepage.
  2. Click on Configuration (gear icon) in the left sidebar and select Data Sources.
  3. Click on Add data source.
  4. Select Prometheus from the list of available data sources.
  5. Configure the Prometheus data source:
    • Name: Prometheus
    • URL: http://localhost:9090
  6. Click Save & Test to ensure Grafana can connect to Prometheus.

Creating a Dashboard in Grafana

Now, create a dashboard to visualize the custom metrics:

  1. Go to the Grafana homepage.
  2. Click on Create (plus icon) in the left sidebar and select Dashboard.
  3. Click Add new panel.
  4. In the Query section, select Prometheus as the data source and enter the following query: example_counter.
  5. Configure the visualization settings as needed.
  6. Click Apply to save the panel.
  7. Repeat the steps to add more panels to the dashboard if needed.
  8. Click Save to save the dashboard and give it a name.

Conclusion

Integrating Grafana with shell scripts allows you to visualize custom metrics and enhance your monitoring capabilities. By using Prometheus as a data source and creating dashboards in Grafana, you can gain valuable insights into your system's performance and make data-driven decisions.