Skip to content

Instantly share code, notes, and snippets.

@vietdhtiki
Last active July 16, 2025 04:52
Show Gist options
  • Save vietdhtiki/6b82ab1858955601755085dad5664f22 to your computer and use it in GitHub Desktop.
Save vietdhtiki/6b82ab1858955601755085dad5664f22 to your computer and use it in GitHub Desktop.
node.sh
#!/bin/bash
# filepath: node.sh
# Usage: curl -s https://raw.githubusercontent.com/your-repo/node.sh | bash -s <ORG>
ORG="${1}"
NODE_EXPORTER_VERSION="1.9.1"
NODE_EXPORTER_ARCH="amd64"
NODE_EXPORTER_USER="node_exporter"
NODE_EXPORTER_BIN_PATH="/${ORG}/node-exporter/bin/node_exporter"
NODE_EXPORTER_DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-${NODE_EXPORTER_ARCH}.tar.gz"
if [ -x "$NODE_EXPORTER_BIN_PATH" ]; then
CURRENT_VERSION=$($NODE_EXPORTER_BIN_PATH --version 2>&1 | grep version | awk '{print $3}')
else
CURRENT_VERSION=""
fi
if [[ "$CURRENT_VERSION" == "$NODE_EXPORTER_VERSION" ]]; then
echo "Node Exporter version $NODE_EXPORTER_VERSION is already installed."
exit 0
fi
if ! id -u $NODE_EXPORTER_USER >/dev/null 2>&1; then
sudo useradd --system --no-create-home --shell /sbin/nologin $NODE_EXPORTER_USER
fi
sudo mkdir -p /${ORG}/node-exporter/bin
sudo mkdir -p /${ORG}/node-exporter
sudo chown -R $NODE_EXPORTER_USER: /${ORG}/node-exporter
cd /tmp
wget -O node_exporter.tar.gz "$NODE_EXPORTER_DOWNLOAD_URL"
tar -xzf node_exporter.tar.gz
sudo cp "node_exporter-${NODE_EXPORTER_VERSION}.linux-${NODE_EXPORTER_ARCH}/node_exporter" "$NODE_EXPORTER_BIN_PATH"
sudo chown $NODE_EXPORTER_USER: "$NODE_EXPORTER_BIN_PATH"
sudo chmod u+x "$NODE_EXPORTER_BIN_PATH"
sudo chmod 755 "$NODE_EXPORTER_BIN_PATH"
sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=$NODE_EXPORTER_USER
Group=$NODE_EXPORTER_USER
Type=simple
ExecStart=$NODE_EXPORTER_BIN_PATH
Restart=on-failure
RestartSec=5s
ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
echo "Node Exporter has been installed and started."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment