Bash Script
Install Node Exporter
Welcome
This script downloads, installs, configures, and starts the Prometheus Node Exporter on a Linux system, ensuring it runs as a systemd service managed by a dedicated node_exporter user. It also logs all actions to a specified log file.
About Node Exporter
Node Exporter is a monitoring agent that collects and exposes hardware and OS-level metrics from Linux and Unix systems in a format that can be ingested by Prometheus. These metrics are valuable for understanding the performance and health of the underlying infrastructure on which applications and services are running.
* Please be aware, the coding on this site is meant to guide. Testing these scripts in a non-production environment is strongly encouraged. I take no responsibility for use of any scripts on this site or on my github repositories.
Additional Information: Click Here
Script:
#!/bin/bash
# Save this script to a file, for example, install_node_exporter.sh,
# make it executable with chmod +x install_node_exporter.sh,
# and then run it with ./install_node_exporter.sh.
# This script will perform all the specified tasks step-by-step.
# Minor configuration necessary, including Filename and Version. Script should work from there.
set -e
LOG_FILE="/var/tmp/node_install_log.txt"
# Architecture Type, valid entries would be after the -, e.g. amd64, arm64, 386
ARCH="arm64"
FILENAME="node_exporter-1.8.1.linux-${ARCH}"
VERSION="v1.8.1"
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/${VERSION}/${FILENAME}.tar.gz"
# Function to log the output of a command
log_and_run() {
echo "$@" | tee -a "$LOG_FILE"
"$@" | tee -a "$LOG_FILE"
}
# Change to /var/tmp/
log_and_run cd /var/tmp/
# Download node_exporter tar.gz file
log_and_run wget "$DOWNLOAD_URL"
# Extract the tar.gz file
log_and_run tar xvf "${FILENAME}.tar.gz"
# Change to the extracted directory
log_and_run cd "$FILENAME"
# Copy node_exporter to /usr/local/bin
log_and_run sudo cp /var/tmp/${FILENAME}/node_exporter /usr/local/bin
# Change to the parent directory
log_and_run cd ..
# Remove the extracted directory
log_and_run rm -Rf "$FILENAME"
# Remove the tar file
log_and_run rm -Rf "$FILENAME".tar.gz
# Create the node_exporter user
log_and_run sudo useradd --no-create-home --shell /bin/false node_exporter
# Change ownership of the node_exporter binary
log_and_run sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
# Create the node_exporter.service file
log_and_run sudo bash -c 'cat <<EOL > /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOL'
# Reload systemd manager configuration
log_and_run sudo systemctl daemon-reload
# Enable node_exporter service to start on boot
log_and_run sudo systemctl enable node_exporter
# Start node_exporter service
log_and_run sudo systemctl start node_exporter
# Display the status of the node_exporter service
log_and_run sudo systemctl status node_exporter.service