Created
May 25, 2025 10:52
-
-
Save TheNerdMan/4c2f4a34ca7bc7d7ac493c11db19df3f to your computer and use it in GitHub Desktop.
install-node-exporter.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
NODE_EXPORTER_VERSION="1.8.0" | |
NODE_EXPORTER_USER="node_exporter" | |
NODE_EXPORTER_DIR="/opt/node_exporter" | |
TARBALL="node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz" | |
URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/${TARBALL}" | |
echo "Downloading node_exporter ${NODE_EXPORTER_VERSION}..." | |
curl -L -O "$URL" | |
echo "Extracting tarball..." | |
tar -xzf "$TARBALL" | |
echo "Moving binary to /usr/local/bin..." | |
sudo cp "node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter" /usr/local/bin/ | |
sudo chown root:root /usr/local/bin/node_exporter | |
echo "Creating node_exporter user if it doesn't exist..." | |
sudo useradd --no-create-home --shell /usr/sbin/nologin "$NODE_EXPORTER_USER" || true | |
echo "Creating systemd service..." | |
cat <<EOF | sudo tee /etc/systemd/system/node_exporter.service > /dev/null | |
[Unit] | |
Description=Node Exporter | |
Wants=network-online.target | |
After=network-online.target | |
[Service] | |
User=$NODE_EXPORTER_USER | |
Group=$NODE_EXPORTER_USER | |
Type=simple | |
ExecStart=/usr/local/bin/node_exporter | |
[Install] | |
WantedBy=default.target | |
EOF | |
echo "Setting permissions..." | |
sudo mkdir -p "$NODE_EXPORTER_DIR" | |
sudo chown "$NODE_EXPORTER_USER":"$NODE_EXPORTER_USER" "$NODE_EXPORTER_DIR" | |
echo "Reloading systemd and starting node_exporter..." | |
sudo systemctl daemon-reexec | |
sudo systemctl daemon-reload | |
sudo systemctl enable --now node_exporter | |
echo "Installation complete. node_exporter should now be running on port 9100." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment