Last active
October 14, 2025 09:20
-
-
Save minhpq331/1e235d45ad72f18cb84878bbb84d2eb9 to your computer and use it in GitHub Desktop.
Run node-exporter using docker
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 | |
| # ============================================================ | |
| # Node Exporter installation script for Ubuntu | |
| # ============================================================ | |
| set -e | |
| # Variables | |
| NODE_EXPORTER_VERSION="1.9.1" | |
| DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz" | |
| INSTALL_DIR="/usr/local/bin" | |
| USER="node_exporter" | |
| echo "=== Installing Node Exporter v${NODE_EXPORTER_VERSION} ===" | |
| # Update system and install prerequisites | |
| sudo apt update -y | |
| sudo apt install -y wget tar | |
| # Create a dedicated user | |
| if ! id -u $USER >/dev/null 2>&1; then | |
| echo "Creating user $USER..." | |
| sudo useradd --no-create-home --shell /bin/false $USER | |
| fi | |
| # Download and extract Node Exporter | |
| cd /tmp | |
| echo "Downloading Node Exporter..." | |
| wget -q $DOWNLOAD_URL | |
| echo "Extracting package..." | |
| tar xzf node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz | |
| sudo cp node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter $INSTALL_DIR/ | |
| # Set permissions | |
| sudo chown $USER:$USER $INSTALL_DIR/node_exporter | |
| # Create systemd service file | |
| echo "Creating systemd service..." | |
| sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<EOF | |
| [Unit] | |
| Description=Prometheus Node Exporter | |
| After=network.target | |
| [Service] | |
| User=${USER} | |
| Group=${USER} | |
| Type=simple | |
| ExecStart=${INSTALL_DIR}/node_exporter | |
| Restart=always | |
| [Install] | |
| WantedBy=multi-user.target | |
| EOF | |
| # Reload systemd and enable the service | |
| echo "Enabling and starting Node Exporter service..." | |
| sudo systemctl daemon-reload | |
| sudo systemctl enable node_exporter | |
| sudo systemctl start node_exporter | |
| # Verify service status | |
| sudo systemctl status node_exporter --no-pager | |
| echo "=== Installation completed successfully! ===" | |
| echo "Node Exporter is running on port 9100" | |
| echo "You can verify it by visiting: http://<your-server-ip>:9100/metrics" |
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 | |
| # Node Exporter Installation Script for Linux | |
| # This script installs Docker using convenient script, sets up the node exporter, and starts it | |
| set -e # Exit on any error | |
| echo "π Starting Node Exporter installation..." | |
| # Function to check if command exists | |
| command_exists() { | |
| command -v "$1" >/dev/null 2>&1 | |
| } | |
| # Function to check if docker compose is available | |
| docker_compose_available() { | |
| docker compose version >/dev/null 2>&1 | |
| } | |
| # Function to install docker-compose if needed | |
| install_docker_compose() { | |
| echo "π¦ Docker Compose not found. Installing latest docker-compose from GitHub..." | |
| # Get the latest release version | |
| COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -Po '"tag_name": "\K.*?(?=")') | |
| if [ -z "$COMPOSE_VERSION" ]; then | |
| echo "β Failed to get latest docker-compose version. Using fallback version." | |
| COMPOSE_VERSION="v2.20.2" | |
| fi | |
| echo "π½ Installing docker-compose version: $COMPOSE_VERSION" | |
| # Download docker-compose binary | |
| sudo curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose | |
| # Make it executable | |
| sudo chmod +x /usr/local/bin/docker-compose | |
| # Create symlink for easier access | |
| sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose | |
| echo "β docker-compose installed successfully!" | |
| } | |
| # Function to install Docker using convenient script | |
| install_docker_convenient() { | |
| echo "π¦ Docker not found. Installing Docker using convenient script..." | |
| # Download and run Docker's convenient installation script | |
| echo "π³ Downloading Docker installation script..." | |
| curl -fsSL https://get.docker.com -o get-docker.sh | |
| echo "π Running Docker installation script..." | |
| sudo sh get-docker.sh | |
| # Add current user to docker group | |
| echo "π€ Adding current user to docker group..." | |
| sudo usermod -aG docker $USER | |
| # Start and enable Docker service | |
| echo "π Starting and enabling Docker service..." | |
| sudo systemctl start docker | |
| sudo systemctl enable docker | |
| # Clean up installation script | |
| rm -f get-docker.sh | |
| echo "β Docker installed successfully using convenient script!" | |
| echo "β οΈ Please log out and log back in for group changes to take effect." | |
| } | |
| # Check if Docker is installed | |
| if ! command_exists docker; then | |
| echo "β Docker is not installed." | |
| # Install Docker using convenient script | |
| if [[ "$OSTYPE" == "linux-gnu"* ]]; then | |
| install_docker_convenient | |
| else | |
| echo "β Unsupported operating system: $OSTYPE" | |
| echo "This script supports Linux distributions only." | |
| echo "Please install Docker manually and run this script again." | |
| exit 1 | |
| fi | |
| else | |
| echo "β Docker is already installed." | |
| # Check if Docker is running | |
| if ! docker info >/dev/null 2>&1; then | |
| echo "β Docker is installed but not running." | |
| echo "Please start Docker service: sudo systemctl start docker" | |
| exit 1 | |
| fi | |
| fi | |
| # Check for Docker Compose | |
| echo "π Checking for Docker Compose..." | |
| if docker_compose_available; then | |
| echo "β Docker Compose plugin is available (docker compose)" | |
| COMPOSE_CMD="docker compose" | |
| elif command_exists docker-compose; then | |
| echo "β docker-compose binary is available" | |
| COMPOSE_CMD="docker-compose" | |
| else | |
| echo "β Neither 'docker compose' nor 'docker-compose' found." | |
| install_docker_compose | |
| COMPOSE_CMD="docker-compose" | |
| fi | |
| # Create /data/exporter directory | |
| echo "π Creating /data/exporter directory..." | |
| sudo mkdir -p /data/exporter | |
| sudo chown $USER:$USER /data/exporter | |
| # Create docker-compose.yml file | |
| echo "π Creating docker-compose.yml file..." | |
| cat > /data/exporter/docker-compose.yml << 'EOF' | |
| services: | |
| node_exporter: | |
| image: quay.io/prometheus/node-exporter:latest | |
| container_name: node_exporter | |
| command: | |
| - '--path.rootfs=/host' | |
| network_mode: host | |
| pid: host | |
| restart: unless-stopped | |
| volumes: | |
| - '/:/host:ro,rslave' | |
| EOF | |
| echo "β docker-compose.yml created successfully!" | |
| # Navigate to the exporter directory | |
| cd /data/exporter | |
| # Stop existing container if running | |
| echo "π Stopping existing node_exporter container if running..." | |
| $COMPOSE_CMD down 2>/dev/null || true | |
| # Start the node exporter | |
| echo "π Starting Node Exporter..." | |
| $COMPOSE_CMD up -d | |
| # Wait a moment for the container to start | |
| sleep 3 | |
| # Check if container is running | |
| if docker ps | grep -q node_exporter; then | |
| echo "β Node Exporter is running successfully!" | |
| echo "π Node Exporter metrics are available at: http://localhost:9100/metrics" | |
| echo "π Container status:" | |
| docker ps | grep node_exporter | |
| else | |
| echo "β Failed to start Node Exporter. Checking logs..." | |
| $COMPOSE_CMD logs | |
| exit 1 | |
| fi | |
| echo "π Installation completed successfully!" | |
| echo "" | |
| echo "π Summary:" | |
| echo " - Docker: β Installed and running" | |
| echo " - Directory: /data/exporter" | |
| echo " - Configuration: /data/exporter/docker-compose.yml" | |
| echo " - Node Exporter: β Running on port 9100" | |
| echo "" | |
| echo "π§ To manage the service:" | |
| echo " - Stop: cd /data/exporter && $COMPOSE_CMD down" | |
| echo " - Start: cd /data/exporter && $COMPOSE_CMD up -d" | |
| echo " - View logs: cd /data/exporter && $COMPOSE_CMD logs -f" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment