Created
June 3, 2025 03:31
-
-
Save zailbreck/26d9d1360f70c0f080b118d1863bd75f to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env bash | |
| # Exit if not run as root | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Please run as root (use sudo)." | |
| exit 1 | |
| fi | |
| # Variables | |
| DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-amd64.tar.gz" | |
| TAR_FILE="node_exporter-1.9.1.linux-amd64.tar.gz" | |
| EXTRACTED_DIR="node_exporter-1.9.1.linux-amd64" | |
| # Download file | |
| echo "Downloading Node Exporter..." | |
| curl -L -o "$TAR_FILE" "$DOWNLOAD_URL" | |
| if [ $? -ne 0 ]; then | |
| echo "Download failed. Exiting." | |
| exit 1 | |
| fi | |
| # Remove old user and group if exists | |
| if id "node_exporter" &>/dev/null; then | |
| echo "User node_exporter exists, deleting..." | |
| userdel node_exporter | |
| fi | |
| groupadd -f node_exporter | |
| useradd -g node_exporter --no-create-home --shell /bin/false node_exporter | |
| # Create required directories | |
| mkdir -p /etc/node_exporter | |
| chown node_exporter:node_exporter /etc/node_exporter | |
| # Extract package | |
| tar -xzf "$TAR_FILE" | |
| if [ $? -ne 0 ]; then | |
| echo "Extraction failed. Exiting." | |
| exit 1 | |
| fi | |
| # Install binary | |
| cp "$EXTRACTED_DIR/node_exporter" /usr/bin/ | |
| chown node_exporter:node_exporter /usr/bin/node_exporter | |
| # Cleanup downloaded files and extracted folder | |
| echo "Cleaning up temporary files..." | |
| rm -f "$TAR_FILE" | |
| rm -rf "$EXTRACTED_DIR" | |
| # Create systemd service | |
| cat <<EOF > /etc/systemd/system/node_exporter.service | |
| [Unit] | |
| Description=Node Exporter | |
| Documentation=https://prometheus.io/docs/guides/node-exporter/ | |
| Wants=network-online.target | |
| After=network-online.target | |
| [Service] | |
| User=node_exporter | |
| Group=node_exporter | |
| Type=simple | |
| Restart=on-failure | |
| ExecStart=/usr/bin/node_exporter --web.listen-address=:9100 | |
| [Install] | |
| WantedBy=multi-user.target | |
| EOF | |
| # Reload systemd, enable and start service | |
| systemctl daemon-reload | |
| systemctl enable node_exporter | |
| systemctl start node_exporter | |
| # Verify service by checking HTTP status code | |
| echo "Verifying Node Exporter..." | |
| sleep 2 | |
| STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:9100/metrics) | |
| if [ "$STATUS_CODE" -eq 200 ]; then | |
| echo "Node Exporter is running and reachable." | |
| else | |
| echo "Node Exporter verification failed. HTTP status code: $STATUS_CODE" | |
| exit 1 | |
| fi | |
| echo "Installation completed successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment