Created
June 5, 2025 02:16
-
-
Save zailbreck/8b5c76544a3952f23e281dfcc3eac90d 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 Upstart service file | |
| cat <<EOF > /etc/init/node_exporter.conf | |
| description "Node Exporter" | |
| start on filesystem and net-device-up IFACE=eth0 | |
| stop on runlevel [016] | |
| respawn | |
| respawn limit 10 5 | |
| setuid node_exporter | |
| setgid node_exporter | |
| exec /usr/bin/node_exporter --web.listen-address=:9100 | |
| EOF | |
| # Start the service | |
| 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