install-scripts/grafana-stack/install.sh

103 lines
3.6 KiB
Bash
Raw Permalink Normal View History

2024-09-04 23:08:16 +00:00
#!/bin/bash
# Function to install Loki and Promtail
install_loki_promtail() {
echo "Installing Loki and Promtail..."
# Install Loki
echo "Installing Loki..."
wget -q -O - https://repo.thanos.io/thanos.key | apt-key add -
echo "deb [arch=amd64] https://repo.thanos.io/deb all main" | tee /etc/apt/sources.list.d/thanos.list
apt-get update -y
apt-get install -y loki
# Install Promtail
echo "Installing Promtail..."
wget -q -O - https://repo.thanos.io/thanos.key | apt-key add -
echo "deb [arch=amd64] https://repo.thanos.io/deb all main" | tee /etc/apt/sources.list.d/thanos.list
apt-get update -y
apt-get install -y promtail
# Enable and start Loki and Promtail services
echo "Enabling and starting Loki and Promtail services..."
systemctl enable loki
systemctl start loki
systemctl enable promtail
systemctl start promtail
echo "Loki and Promtail installed and services started."
}
# Check for sudo/root privileges
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root. Please enter your sudo password."
exec sudo bash "$0" "$@"
fi
echo "Starting script as root."
# Step 2: Install software-properties-common, apt-transport-https, wget
echo "Step 2: Installing software-properties-common, apt-transport-https, wget..."
apt-get update -y
apt-get install -y software-properties-common apt-transport-https wget
# Step 3: Add the official Grafana GPG key
echo "Step 3: Adding the official Grafana GPG key..."
wget -q -O - https://packages.grafana.com/gpg.key | apt-key add -
# Step 4: Add the official Grafana repository for Debian
echo "Step 4: Adding the official Grafana repository..."
add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
# Step 5: Run apt update
echo "Step 5: Updating package list..."
apt-get update -y
# Step 6: Install both Grafana & Prometheus
echo "Step 6: Installing Grafana and Prometheus..."
apt-get install -y grafana prometheus
# Step 7: Edit the Grafana config file to change the default port
echo "Step 7: Changing Grafana port from 3000 to 3100..."
sed -i 's/;http_port = 3000/http_port = 3100/' /etc/grafana/grafana.ini
# Step 8: Install and enable both Grafana and Prometheus as systemctl services
echo "Step 8: Enabling and starting Grafana and Prometheus services..."
systemctl daemon-reload
systemctl enable grafana-server
systemctl start grafana-server
systemctl enable prometheus
systemctl start prometheus
# Step 9: Wait 2 minutes, then check both services are running successfully
echo "Step 9: Waiting 2 minutes to check service statuses..."
sleep 120
GRAFANA_STATUS=$(systemctl is-active grafana-server)
PROMETHEUS_STATUS=$(systemctl is-active prometheus)
if [ "$GRAFANA_STATUS" != "active" ] || [ "$PROMETHEUS_STATUS" != "active" ]; then
echo "One or both services failed to start. Retrying in 2 minutes..."
sleep 120
systemctl restart grafana-server
systemctl restart prometheus
fi
GRAFANA_STATUS=$(systemctl is-active grafana-server)
PROMETHEUS_STATUS=$(systemctl is-active prometheus)
if [ "$GRAFANA_STATUS" == "active" ] && [ "$PROMETHEUS_STATUS" == "active" ]; then
echo "Both Grafana and Prometheus are running successfully."
echo "Grafana can be accessed on port 3100."
# Prompt to install Loki and Promtail
read -p "Would you like to install Loki and Promtail as well? (yes/no): " INSTALL_LOKI_PROMTAIL
if [ "$INSTALL_LOKI_PROMTAIL" == "yes" ] || [ "$INSTALL_LOKI_PROMTAIL" == "y" ]; then
install_loki_promtail
else
echo "Skipping installation of Loki and Promtail."
fi
else
echo "Failed to start one or both services. Please check the logs for more information."
fi