Created
January 16, 2025 22:55
-
-
Save gourneau/3ece1a19bf1fc6c0b29a24340e64e81b to your computer and use it in GitHub Desktop.
Install bare metal prometheus node_exporter on Redhat distros.
This file contains 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 | |
# Exit immediately if a command exits with a non-zero status. | |
set -e | |
# Variables | |
NODE_EXPORTER_VERSION="1.8.2" | |
NODE_EXPORTER_USER="node_exporter" | |
NODE_EXPORTER_BINARY="/usr/local/bin/node_exporter" | |
NODE_EXPORTER_SERVICE="/etc/systemd/system/node_exporter.service" | |
# Determine the correct binary based on system architecture | |
ARCH=$(uname -m) | |
if [[ "$ARCH" == "x86_64" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz" | |
elif [[ "$ARCH" == "aarch64" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-arm64.tar.gz" | |
elif [[ "$ARCH" == "armv5" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-armv5.tar.gz" | |
elif [[ "$ARCH" == "armv6" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-armv6.tar.gz" | |
elif [[ "$ARCH" == "armv7" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-armv7.tar.gz" | |
elif [[ "$ARCH" == "mips" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-mips.tar.gz" | |
elif [[ "$ARCH" == "mips64" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-mips64.tar.gz" | |
elif [[ "$ARCH" == "i386" || "$ARCH" == "i686" ]]; then | |
DOWNLOAD_URL="https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-386.tar.gz" | |
else | |
echo "Unsupported architecture: $ARCH" | |
exit 1 | |
fi | |
# Verify that the URL exists | |
if ! curl --output /dev/null --silent --head --fail "$DOWNLOAD_URL"; then | |
echo "Download URL does not exist: $DOWNLOAD_URL" | |
exit 1 | |
fi | |
# Ensure necessary utilities are installed | |
if ! command -v wget &>/dev/null || ! command -v tar &>/dev/null; then | |
echo "Installing required utilities: wget, tar" | |
sudo yum install -y wget tar | |
fi | |
# Create a dedicated user if not exists | |
if ! id -u "$NODE_EXPORTER_USER" >/dev/null 2>&1; then | |
echo "Creating user: $NODE_EXPORTER_USER" | |
sudo useradd -r -s /bin/false "$NODE_EXPORTER_USER" | |
fi | |
# Remove existing Node Exporter binary if reinstalling | |
if [ -f "$NODE_EXPORTER_BINARY" ]; then | |
echo "Removing existing Node Exporter binary." | |
sudo rm -f "$NODE_EXPORTER_BINARY" | |
fi | |
# Download and install Node Exporter | |
echo "Downloading and installing Node Exporter v$NODE_EXPORTER_VERSION" | |
wget -q "$DOWNLOAD_URL" -O /tmp/node_exporter.tar.gz | |
if [ ! -s /tmp/node_exporter.tar.gz ]; then | |
echo "Download failed or file is empty. Exiting." | |
exit 1 | |
fi | |
tar -xzf /tmp/node_exporter.tar.gz -C /tmp | |
if [ ! -f "/tmp/node_exporter-${NODE_EXPORTER_VERSION}.linux-arm64/node_exporter" ]; then | |
echo "Extracted binary not found. Exiting." | |
exit 1 | |
fi | |
if [ ! -d "/usr/local/bin" ]; then | |
echo "Destination directory /usr/local/bin does not exist. Creating it." | |
sudo mkdir -p /usr/local/bin | |
fi | |
sudo mv "/tmp/node_exporter-${NODE_EXPORTER_VERSION}.linux-arm64/node_exporter" "$NODE_EXPORTER_BINARY" | |
sudo chmod +x "$NODE_EXPORTER_BINARY" | |
rm -rf "/tmp/node_exporter-${NODE_EXPORTER_VERSION}.linux-arm64" /tmp/node_exporter.tar.gz | |
# Remove existing Node Exporter service file if reinstalling | |
if [ -f "$NODE_EXPORTER_SERVICE" ]; then | |
echo "Removing existing Node Exporter service file." | |
sudo rm -f "$NODE_EXPORTER_SERVICE" | |
fi | |
# Create systemd service file | |
echo "Creating systemd service file for Node Exporter" | |
cat <<EOF | sudo tee "$NODE_EXPORTER_SERVICE" | |
[Unit] | |
Description=Prometheus Node Exporter | |
Wants=network-online.target | |
After=network-online.target | |
[Service] | |
User=$NODE_EXPORTER_USER | |
Group=$NODE_EXPORTER_USER | |
Type=simple | |
ExecStart=$NODE_EXPORTER_BINARY | |
RuntimeDirectory=node_exporter | |
StateDirectory=node_exporter | |
CacheDirectory=node_exporter | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
sudo systemctl daemon-reload | |
# Check if systemctl is available | |
if ! command -v systemctl &>/dev/null; then | |
echo "Systemctl is not available on this system. Exiting." | |
exit 1 | |
fi | |
# Validate systemd service file before restarting | |
sudo systemd-analyze verify "$NODE_EXPORTER_SERVICE" || true | |
# Enable and start Node Exporter | |
sudo systemctl enable node_exporter | |
sudo systemctl restart node_exporter | |
# Verify that the service reloads without errors | |
if sudo systemctl is-active --quiet node_exporter; then | |
echo "Node Exporter service restarted successfully." | |
else | |
echo "Node Exporter service failed to restart. Check logs with: sudo journalctl -u node_exporter" | |
exit 1 | |
fi | |
# Verify the installation | |
if sudo systemctl is-active --quiet node_exporter; then | |
echo "Node Exporter is running successfully." | |
echo "Metrics available at: http://$(hostname -I | awk '{print $1}'):9100/metrics" | |
else | |
echo "Node Exporter failed to start. Check the logs with: sudo journalctl -u node_exporter" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment