Install and Configure Prometheus Script Exporter

Prometheus Script Exporter


What is Prometheus Script Exporter?

Prometheus Script Exporter can be used to expose all those metrics that other exporters cannot get for you. You can configure these exporters according to your requirements and get custom data. But this exporter only exposes data to Prometheus in the form of 0 and 1.

Note: If you have not yet installed Prometheus, please follow Setup Prometheus for Monitoring to get started.

Today's Agenda

In this post, we will learn how to install and configure Prometheus Script Exporter to expose custom metrics to Prometheus for visualization.

Prerequisite

This post has been prepared for the audience who :
  1. Have access to a Linux-based system although the steps are almost similar in other platforms like CentOS or MacOS.
  2. Have a basic understanding of Linux based systems and their commands.
  3. Have Setup Prometheus for Monitoring.

Let's get started


Run the below shell script with the root user on the server. You will see Script Exporter running as a service on port 9100 in your machine.
Note: You can update the version of the Script Exporter in the shell script in shaded regions.

#!/bin/bash


set -e
if [[ $EUID -ne 0 ]]; then
  echo "Permission Denied! This script must be run as root"
  exit 1
fi

## Download script-exporter package and extract
wget https://github.com/ricoberger/script_exporter/releases/download/v2.0.1/script_exporter-v2.0.1-linux-amd64


mv script_exporter-*.*-amd64 script_exporter

mv script_exporter /usr/local/bin
chown root:root /usr/local/bin/script_exporter
chmod 755 /usr/local/bin/script_exporter

mkdir -p /etc/script_exporter

## Keep the value of the name key and script the same.
## You can similarly create various scripts under the name block for different scripts.

cat > /etc/script_exporter/script3.sh << EOF
#!/bin/bash

if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
exit 0
else
exit 1
fi

EOF


cat > /etc/script_exporter/config.yaml << EOF
scripts:
- name: 'success'
  script: sleep 5

- name: 'failure'
  script: sleep 2 && exit 1


- name: 'script3'
  script: /etc/script_exporter/script3.sh

EOF

chown -R root:root /etc/script_exporter
chmod -R 644 /etc/script_exporter

## Create script-exporter service
cat > /etc/systemd/system/script_exporter.service << EOF
[Unit]
Description=ScriptExporter

[Service]
User=root
ExecStart=/usr/local/bin/script_exporter -config.file /etc/script_exporter/config.yaml

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start script_exporter
systemctl enable script_exporter

You can also create a new user for Script Exporter to make the necessary changes in the script.

If you want to know how you can create a new user in Linux, please go through our post on Setup Prometheus for Monitoring posts and follow Step 1 of the post.

Finally, run the below command to check if Script Exporter has started exposing the system metrics at port 9469.


$ curl localhost:9469/metrics

OUTPUT


Prometheus Script Exporter running on port 9469
Prometheus Script Exporter running on port 9469

To verify your scripts are running and providing correct results run the commands mentioned in below screenshots.

Script Exporter exposing metrics for script "success"
Script Exporter exposing metrics for script "success"

Script Exporter exposing metrics for script "failure"
Script Exporter exposing metrics for script "failure"

Script Exporter exposing metrics for script "script3"
Script Exporter exposing metrics for script "script3"

For more details, you can also refer to the Github repository of Script Exporter for Prometheus.

Please follow our post Configure Exporters in Prometheus to understand how to connect Blackbox Exporter with Prometheus to start getting the system metrics.

That's all, you did a great job !!

If you face any issues or need any suggestions, please comment down below and hit the like button to appreciate the efforts.





Comments