bash_memory_exporter

Export Linux process memory usage to Prometheus Pushgateway using a Bash script. Monitor memory stats for processes and visualize with Grafana.

Bash Memory Exporter

Bash Memory Exporter for Prometheus

This Bash script acts as a simple exporter to gather memory usage statistics from the top command on a Linux system and push them to a Prometheus Pushgateway. This allows for real-time monitoring of process memory consumption, which can then be visualized using tools like Grafana.

How it Works

The script executes ps aux to get a list of all running processes and their associated memory usage. It then parses this output to extract the process name, PID, and memory percentage. These metrics are formatted into Prometheus exposition format and sent via a POST request to the specified Pushgateway URL. The script includes placeholders for job name, hostname, exporter type, authentication credentials, and the Pushgateway address, which should be configured according to your environment.

Key Components and Configuration

  • job_name: Defines the Prometheus job name for these metrics.
  • my_hostname: Specifies the instance name, typically the hostname of the machine running the script.
  • exporter_type: Categorizes the exporter, useful for filtering in Prometheus.
  • auth_string: Authentication credentials for the Pushgateway (username:password).
  • pushgateway_url: The URL of your Prometheus Pushgateway instance.

Prometheus and Grafana Integration

By sending metrics to the Pushgateway, Prometheus can scrape these metrics at regular intervals. Grafana can then be configured to query Prometheus and display the memory usage data, providing valuable insights into system performance and resource allocation. This setup is particularly useful for monitoring ephemeral workloads or applications that might not have a long-lived process for direct Prometheus scraping.

External Resources

#!/bin/bash

# This exporter gets stats from top and sends it to pushgateway
# Prometheus will scrape the pushgateway service and consumes to prometheus
# Grafana will visualize the data

# resource:
# https://devconnected.com/monitoring-linux-processes-using-prometheus-and-grafana/

job_name="top"
my_hostname="my-ec2-instance"
exporter_type="bash"
auth_string="x:x"
pushgateway_url="pushgateway.mydomain.com"

z=$(ps aux)
while read -r z
do
   var=$(echo "$var")$(echo "exporter_memory_usage{process=\""$(echo "$z" | awk '{print $11}')"\", pid=\""$(echo "$z" | awk '{print $2}')"\"} "$4z"\\n")
done <<< "$z"
curl -X POST -u "$auth_string" -H  "Content-Type: text/plain" --data "$var" https://$pushgateway_url/metrics/job/$job_name/instance/$my_hostname/exporter_type/$exporter_type