|
#!/bin/bash |
|
|
|
# Define versions as variables |
|
PROMETHEUS_VERSION="3.2.0" # Default version, can be overridden |
|
NODE_EXPORTER_VERSION="1.9.0" # Default version, can be overridden |
|
|
|
# Prompt user for versions |
|
read -p "Enter Prometheus version (default: $PROMETHEUS_VERSION): " PROMETHEUS_INPUT |
|
read -p "Enter Node Exporter version (default: $NODE_EXPORTER_VERSION): " NODE_EXPORTER_INPUT |
|
|
|
# Use default if user input is empty |
|
PROMETHEUS_VERSION=${PROMETHEUS_INPUT:-$PROMETHEUS_VERSION} |
|
NODE_EXPORTER_VERSION=${NODE_EXPORTER_INPUT:-$NODE_EXPORTER_VERSION} |
|
|
|
# Detect system architecture |
|
ARCH=$(uname -m) |
|
if [[ "$ARCH" == "x86_64" ]]; then |
|
ARCH="amd64" |
|
elif [[ "$ARCH" == "aarch64" ]]; then |
|
ARCH="arm64" |
|
else |
|
echo "Unsupported architecture: $ARCH" |
|
exit 1 |
|
fi |
|
|
|
echo "Detected architecture: $ARCH" |
|
|
|
# Update the system |
|
sudo yum update -y |
|
|
|
# Create prometheus user |
|
echo "Creating prometheus user..." |
|
sudo useradd -M -U prometheus |
|
|
|
# Install Node Exporter |
|
echo "Downloading Node Exporter v$NODE_EXPORTER_VERSION for $ARCH..." |
|
wget https://github.com/prometheus/node_exporter/releases/download/v$NODE_EXPORTER_VERSION/node_exporter-$NODE_EXPORTER_VERSION.linux-$ARCH.tar.gz |
|
|
|
echo "Extracting Node Exporter..." |
|
tar -xvf node_exporter-$NODE_EXPORTER_VERSION.linux-$ARCH.tar.gz |
|
|
|
echo "Moving Node Exporter to /opt/node_exporter..." |
|
sudo mv node_exporter-$NODE_EXPORTER_VERSION.linux-$ARCH /opt/node_exporter |
|
|
|
echo "Setting ownership and permissions for Node Exporter..." |
|
sudo chown -R prometheus:prometheus /opt/node_exporter |
|
|
|
echo "Creating systemd service for Node Exporter..." |
|
sudo tee /etc/systemd/system/node_exporter.service > /dev/null <<EOF |
|
[Unit] |
|
Description=Node Exporter |
|
After=network-online.target |
|
|
|
[Service] |
|
User=prometheus |
|
Group=prometheus |
|
Restart=on-failure |
|
ExecStart=/opt/node_exporter/node_exporter |
|
|
|
[Install] |
|
WantedBy=multi-user.target |
|
EOF |
|
|
|
echo "Reloading systemd and enabling Node Exporter..." |
|
sudo systemctl daemon-reload |
|
sudo systemctl enable node_exporter |
|
sudo systemctl start node_exporter |
|
|
|
echo "Checking Node Exporter status..." |
|
sudo systemctl status node_exporter |
|
curl http://localhost:9100/metrics |
|
|
|
# Install Prometheus |
|
echo "Downloading Prometheus v$PROMETHEUS_VERSION for $ARCH..." |
|
wget https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VERSION/prometheus-$PROMETHEUS_VERSION.linux-$ARCH.tar.gz |
|
|
|
echo "Extracting Prometheus..." |
|
tar -xvf prometheus-$PROMETHEUS_VERSION.linux-$ARCH.tar.gz |
|
|
|
echo "Moving Prometheus to /opt/prometheus..." |
|
sudo mv prometheus-$PROMETHEUS_VERSION.linux-$ARCH /opt/prometheus |
|
|
|
echo "Setting ownership and permissions for Prometheus..." |
|
sudo chown -R prometheus:prometheus /opt/prometheus |
|
|
|
echo "Configuring prometheus.yml with global settings and scrapers..." |
|
sudo tee /opt/prometheus/prometheus.yml > /dev/null <<EOF |
|
global: |
|
scrape_interval: 15s |
|
evaluation_interval: 15s |
|
|
|
scrape_configs: |
|
- job_name: 'prometheus' |
|
static_configs: |
|
- targets: ['localhost:9090'] |
|
|
|
- job_name: 'node_exporter' |
|
static_configs: |
|
- targets: ['localhost:9100'] |
|
EOF |
|
|
|
echo "Creating systemd service for Prometheus..." |
|
sudo tee /etc/systemd/system/prometheus.service > /dev/null <<EOF |
|
[Unit] |
|
Description=Prometheus |
|
After=network-online.target |
|
|
|
[Service] |
|
User=prometheus |
|
Group=prometheus |
|
Restart=on-failure |
|
ExecStart=/opt/prometheus/prometheus \ |
|
--config.file=/opt/prometheus/prometheus.yml \ |
|
--storage.tsdb.path=/opt/prometheus/data \ |
|
--storage.tsdb.retention.time=30d |
|
|
|
[Install] |
|
WantedBy=multi-user.target |
|
EOF |
|
|
|
echo "Reloading systemd and enabling Prometheus..." |
|
sudo systemctl daemon-reload |
|
sudo systemctl enable prometheus |
|
sudo systemctl start prometheus |
|
|
|
echo "Checking Prometheus status..." |
|
sudo systemctl status prometheus |
|
curl http://localhost:9090/metrics |
|
|
|
echo "Installation completed." |