bash_cpu_exporter
Monitor Linux processes and CPU usage with the Bash CPU Exporter. Send stats to Pushgateway for Prometheus and visualize with Grafana.
Bash CPU Exporter
Bash CPU Exporter Overview
This Bash script acts as a custom exporter to gather system statistics, specifically CPU usage for running processes, and send them to a Prometheus Pushgateway. By leveraging standard Linux commands like ps
and utilities like awk
, it extracts process information and formats it into a Prometheus-readable text format. This data can then be scraped by Prometheus and visualized in Grafana, providing real-time insights into system performance and resource consumption.
How the Bash CPU Exporter Works
The exporter script initializes by defining key variables such as the Prometheus job name, hostname, exporter type, authentication credentials for the Pushgateway, and the Pushgateway URL itself. It then executes ps aux
to get a comprehensive list of running processes. The output is piped into a while read
loop, where each line is processed. For every process, it extracts the process name and PID, and then formats the CPU usage percentage into a Prometheus metric line. Finally, it uses curl
to POST these metrics to the specified Pushgateway endpoint, making them available for Prometheus to collect.
Key Components and Configuration
The script is designed for simplicity and direct integration. Key configuration parameters include:
job_name
: Identifies the Prometheus job.my_hostname
: The instance name for the metrics.exporter_type
: A label to categorize the exporter (e.g., "bash").auth_string
: Basic authentication credentials for the Pushgateway (username:password).pushgateway_url
: The address of your Prometheus Pushgateway instance.
The core logic relies on parsing the output of ps aux
. The awk
command is used to extract specific fields, and the output is constructed to match the Prometheus exposition format, including labels for process name and PID.
Integrating with Prometheus and Grafana
To effectively use this Bash CPU Exporter, you need a running Prometheus instance configured to scrape metrics from your Pushgateway. Ensure your Prometheus configuration includes a scrape job targeting the Pushgateway. Once Prometheus collects the data, you can set up dashboards in Grafana to visualize the CPU usage of individual processes, track trends, and set up alerts for abnormal resource consumption. This setup provides a lightweight yet powerful solution for monitoring your Linux systems.
Example Usage and Script
Below is the Bash script that performs the CPU monitoring and Pushgateway submission:
#!/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=$var$(awk '{print "exporter_cpu_usage{process=\""$11"\", pid=\""$2"\"}", $3z}');
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