Skip to content

Instantly share code, notes, and snippets.

@umardx
Last active July 24, 2025 08:38
Show Gist options
  • Select an option

  • Save umardx/b1e0972f6ed29e2a043e1a8bc33be5f2 to your computer and use it in GitHub Desktop.

Select an option

Save umardx/b1e0972f6ed29e2a043e1a8bc33be5f2 to your computer and use it in GitHub Desktop.
install-node-exporter.sh
#!/bin/bash
set -e
NODE_EXPORTER_VERSION="1.8.1"
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"
BIN_PATH="/usr/local/bin/node_exporter"
LOG_DIR="/var/log/node_exporter"
LOG_FILE="$LOG_DIR/node_exporter.log"
SERVICE_FILE="/etc/systemd/system/node_exporter.service"
LOGROTATE_FILE="/etc/logrotate.d/node_exporter"
echo "[*] Starting Node Exporter setup..."
# 1. Create node_exporter user if not exists
if ! id "node_exporter" >/dev/null 2>&1; then
echo "[+] Creating user: node_exporter"
useradd --no-create-home --shell /bin/false node_exporter
fi
# 2. Check installed version
INSTALLED_VERSION=$($BIN_PATH --version 2>/dev/null | head -n1 | grep -oP '[0-9]+\.[0-9]+\.[0-9]+') || true
if [[ "$INSTALLED_VERSION" == "$NODE_EXPORTER_VERSION" ]]; then
echo "[=] Node Exporter v$NODE_EXPORTER_VERSION is already installed. Skipping binary install."
else
echo "[+] Installing Node Exporter v$NODE_EXPORTER_VERSION"
wget -q $DOWNLOAD_URL -O /tmp/node_exporter.tar.gz
tar -xzf /tmp/node_exporter.tar.gz -C /tmp
cp /tmp/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter $BIN_PATH
chown node_exporter:node_exporter $BIN_PATH
chmod +x $BIN_PATH
rm -rf /tmp/node_exporter*
fi
# 3. Create log directory and file if not exist
echo "[+] Preparing log directory"
mkdir -p $LOG_DIR
touch $LOG_FILE
chown -R node_exporter:node_exporter $LOG_DIR
# 4. Create or update systemd service file
NEEDS_RELOAD=false
EXPECTED_SERVICE_CONTENT=$(cat <<EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=$BIN_PATH
Restart=always
StandardOutput=append:$LOG_FILE
StandardError=append:$LOG_FILE
[Install]
WantedBy=default.target
EOF
)
if [[ ! -f "$SERVICE_FILE" ]] || [[ "$(cat "$SERVICE_FILE")" != "$EXPECTED_SERVICE_CONTENT" ]]; then
echo "[+] Writing systemd service file"
echo "$EXPECTED_SERVICE_CONTENT" > $SERVICE_FILE
NEEDS_RELOAD=true
else
echo "[=] Systemd service file already up to date."
fi
# 5. Reload and restart service if needed
if $NEEDS_RELOAD; then
echo "[+] Reloading systemd and restarting service"
systemctl daemon-reload
systemctl enable node_exporter
systemctl restart node_exporter
else
echo "[=] Skipping systemd reload; no changes."
systemctl enable node_exporter
systemctl start node_exporter
fi
# 6. Configure logrotate
EXPECTED_LOGROTATE_CONFIG=$(cat <<EOF
$LOG_FILE {
daily
rotate 3
compress
missingok
notifempty
copytruncate
}
EOF
)
if [[ ! -f "$LOGROTATE_FILE" ]] || [[ "$(cat "$LOGROTATE_FILE")" != "$EXPECTED_LOGROTATE_CONFIG" ]]; then
echo "[+] Writing logrotate config"
echo "$EXPECTED_LOGROTATE_CONFIG" > "$LOGROTATE_FILE"
else
echo "[=] Logrotate config already up to date."
fi
# 7. Status
echo "[✓] Node Exporter is installed and running"
systemctl status node_exporter --no-pager
#!/bin/bash
set -e
BIN_PATH="/usr/local/bin/node_exporter"
SERVICE_FILE="/etc/systemd/system/node_exporter.service"
LOG_DIR="/var/log/node_exporter"
LOGROTATE_FILE="/etc/logrotate.d/node_exporter"
USER="node_exporter"
echo "[*] Uninstalling Node Exporter..."
# 1. Stop and disable the service
if systemctl is-enabled --quiet node_exporter; then
echo "[+] Disabling systemd service"
systemctl stop node_exporter || true
systemctl disable node_exporter || true
systemctl daemon-reload
else
echo "[=] Node Exporter service not enabled."
fi
# 2. Remove systemd service file
if [[ -f "$SERVICE_FILE" ]]; then
echo "[+] Removing systemd service file"
rm -f "$SERVICE_FILE"
fi
# 3. Remove binary
if [[ -f "$BIN_PATH" ]]; then
echo "[+] Removing binary at $BIN_PATH"
rm -f "$BIN_PATH"
fi
# 4. Remove logrotate config
if [[ -f "$LOGROTATE_FILE" ]]; then
echo "[+] Removing logrotate config"
rm -f "$LOGROTATE_FILE"
fi
# 5. Remove logs
if [[ -d "$LOG_DIR" ]]; then
echo "[+] Removing logs at $LOG_DIR"
rm -rf "$LOG_DIR"
fi
# 6. Remove user
if id "$USER" &>/dev/null; then
echo "[+] Removing user: $USER"
userdel -r "$USER" 2>/dev/null || true
fi
echo "[✓] Node Exporter has been uninstalled."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment