From b94b7b0c64db2e5f15c3f47e021b5699f3d33acd Mon Sep 17 00:00:00 2001 From: Oliver Gwyther Date: Sun, 2 Feb 2025 14:54:22 +0000 Subject: [PATCH] Create install.sh --- grafana-stack-new/install.sh | 217 +++++++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 grafana-stack-new/install.sh diff --git a/grafana-stack-new/install.sh b/grafana-stack-new/install.sh new file mode 100644 index 0000000..cf61d3b --- /dev/null +++ b/grafana-stack-new/install.sh @@ -0,0 +1,217 @@ +#!/bin/bash + +# Default versions +DEFAULT_PROMETHEUS_VERSION="3.1.0" +DEFAULT_NODE_EXPORTER_VERSION="1.8.2" + +# Function to display menu and get user choices +function show_menu() { + echo "Please select the software you would like to install (multiple selections allowed):" + echo "1. Prometheus" + echo "2. Node Exporter" + echo "3. Grafana" + echo "4. Exit" + read -p "Enter your choice(s) as a comma-separated list (e.g., 1,2,3) [default: 4]: " choice +} + +# Function to display progress bar +function show_progress { + local total=$1 + local current=$2 + local percent=$((current * 100 / total)) + printf "\r[%-70s] %d%%" "$(printf '#%.0s' $(seq 1 $percent))" "$percent" +} + +# Function to download and install Prometheus +function install_prometheus() { + read -p "Enter the desired Prometheus version (e.g., v2.41.0) [default: $DEFAULT_PROMETHEUS_VERSION]: " PROMETHEUS_VERSION + PROMETHEUS_VERSION=${PROMETHEUS_VERSION:-$DEFAULT_PROMETHEUS_VERSION} + + echo "Chosen Prometheus version: $PROMETHEUS_VERSION" + DOWNLOAD_DIR="/tmp/prometheus-install" + PROMETHEUS_USER="prometheus" + PROMETHEUS_GROUP="prometheus" + PROMETHEUS_DIR="/usr/local/bin/prometheus" + CONFIG_DIR="/etc/prometheus" + DATA_DIR="/var/lib/prometheus" + + echo "Creating download directory..." + mkdir -p $DOWNLOAD_DIR + + echo "Downloading Prometheus binary..." + cd $DOWNLOAD_DIR + wget https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VERSION/prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz + + echo "Extracting Prometheus binary..." + tar xvfz prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz + + echo "Copying Prometheus binary to $PROMETHEUS_DIR..." + cp prometheus-$PROMETHEUS_VERSION.linux-amd64/prometheus $PROMETHEUS_DIR + echo "Copying promtool to /usr/local/bin..." + cp prometheus-$PROMETHEUS_VERSION.linux-amd64/promtool /usr/local/bin/ + + echo "Creating necessary directories for Prometheus data and config..." + mkdir -p $CONFIG_DIR $DATA_DIR + + echo "Creating Prometheus configuration file in $CONFIG_DIR..." + cat < $CONFIG_DIR/prometheus.yml +global: + scrape_interval: 15s + +scrape_configs: + - job_name: 'prometheus' + static_configs: + - targets: ['localhost:9090'] + + - job_name: 'node_exporter' + static_configs: + - targets: ['localhost:9100'] +EOF + + echo "Creating $PROMETHEUS_USER user and group..." + groupadd --system $PROMETHEUS_GROUP + useradd -s /sbin/nologin --system -g $PROMETHEUS_GROUP $PROMETHEUS_USER + + echo "Setting ownership of directories and files..." + chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP $CONFIG_DIR $DATA_DIR + chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP /usr/local/bin/prometheus + chown -R $PROMETHEUS_USER:$PROMETHEUS_GROUP /usr/local/bin/node_exporter + + echo "Creating systemd service for Prometheus..." + cat < /etc/systemd/system/prometheus.service +[Unit] +Description=Prometheus Time Series Database +Wants=network-online.target +After=network-online.target + +[Service] +User=$PROMETHEUS_USER +Group=$PROMETHEUS_GROUP +Type=simple +ExecStart=/usr/local/bin/prometheus \ + --config.file=$CONFIG_DIR/prometheus.yml \ + --storage.tsdb.path=$DATA_DIR + +Restart=always +[Install] +WantedBy=default.target +EOF + + echo "Reloading systemd daemon..." + systemctl daemon-reload + echo "Enabling Prometheus service..." + systemctl enable prometheus.service + echo "Starting Prometheus service..." + systemctl start prometheus.service + echo "Checking status of Prometheus service..." + systemctl status prometheus.service +} + +# Function to download and install Node Exporter +function install_node_exporter() { + read -p "Enter the desired Node Exporter version (e.g., v1.6.0) [default: $DEFAULT_NODE_EXPORTER_VERSION]: " NODE_EXPORTER_VERSION + NODE_EXPORTER_VERSION=${NODE_EXPORTER_VERSION:-$DEFAULT_NODE_EXPORTER_VERSION} + + echo "Chosen Node Exporter version: $NODE_EXPORTER_VERSION" + DOWNLOAD_DIR="/tmp/node-exporter-install" + PROMETHEUS_USER="prometheus" + PROMETHEUS_GROUP="prometheus" + BINARY_DIR="/usr/local/bin" + + echo "Creating download directory..." + mkdir -p $DOWNLOAD_DIR + + echo "Downloading Node Exporter binary..." + cd $DOWNLOAD_DIR + wget https://github.com/prometheus/node_exporter/releases/download/v$NODE_EXPORTER_VERSION/node_exporter-$NODE_EXPORTER_VERSION.linux-amd64.tar.gz + + echo "Extracting Node Exporter binary..." + tar xvfz node_exporter-$NODE_EXPORTER_VERSION.linux-amd64.tar.gz + + echo "Copying Node Exporter binary to $BINARY_DIR..." + cp node_exporter-$NODE_EXPORTER_VERSION.linux-amd64/node_exporter $BINARY_DIR + + echo "Creating systemd service for Node Exporter..." + cat < /etc/systemd/system/node_exporter.service +[Unit] +Description=Node Exporter +Wants=network-online.target +After=network-online.target + +[Service] +User=$PROMETHEUS_USER +Group=$PROMETHEUS_GROUP +Type=simple +ExecStart=/usr/local/bin/node_exporter + +Restart=always +[Install] +WantedBy=default.target +EOF + + echo "Reloading systemd daemon..." + systemctl daemon-reload + echo "Enabling Node Exporter service..." + systemctl enable node_exporter.service + echo "Starting Node Exporter service..." + systemctl start node_exporter.service + echo "Checking status of Node Exporter service..." + systemctl status node_exporter.service +} + +# Function to install Grafana +function install_grafana() { + echo "Installing Grafana using the official PPA..." + + echo "Importing Grafana repository keyring..." + wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add - + + echo "Adding Grafana repository..." + echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list + + echo "Updating package list..." + sudo apt-get update + + echo "Installing Grafana..." + sudo apt-get install -y grafana + + echo "Enabling Grafana service..." + sudo systemctl enable grafana-server + echo "Starting Grafana service..." + sudo systemctl start grafana-server + echo "Checking status of Grafana service..." + sudo systemctl status grafana-server +} + +# Main script execution +show_menu + +# Convert the input choice to an array +IFS=',' read -r -a choices <<< "$choice" + +# Execute selected installations with progress bar +for i in "${!choices[@]}"; do + case ${choices[$i]} in + 1) + echo "Installing Prometheus..." + install_prometheus + ;; + 2) + echo "Installing Node Exporter..." + install_node_exporter + ;; + 3) + echo "Installing Grafana..." + install_grafana + ;; + 4) + echo "Exiting..." + exit 0 + ;; + *) + echo "Invalid choice: ${choices[$i]}. Skipping." + ;; + esac +done + +echo "Installation completed successfully!"