Installing Redis Exporter Bash Script

Here’s my version of writing redis_exporter installtion script.
I just found writing script fun not until my back aches, lol

#!/bin/bash
# Installing Redis Exporter

# tput commands
CLEAR=”tput clear”
DOWN=”tput cud1″
BOLD=”tput bold”
NORMAL=”tput s…


This content originally appeared on DEV Community and was authored by tj_27

Here's my version of writing redis_exporter installtion script.
I just found writing script fun not until my back aches, lol

#!/bin/bash
# Installing Redis Exporter

# tput commands
CLEAR="tput clear"
DOWN="tput cud1"
BOLD="tput bold"
NORMAL="tput sgr0"
BLACK="tput setaf 0"
RED="tput setaf 1"
GREEN="tput setaf 2"
YELLOW="tput setaf 3"
BLUE="tput setaf 4"

$CLEAR
$DOWN
# Installation confirmation
printf "You have selected to install redis_exporter.\n\n"
read -p "Do you want to continue? [ yes / no ] : " USER_INPUT
USER_INPUT=${USER_INPUT:-yes}
$DOWN

# Convert user's choice to lowercase for case-sensitive comparison
USER_INPUT_LOWER=$(echo "$USER_INPUT" | tr '[:upper:]' '[:lower:]')

# Check the user's input
if [ "$USER_INPUT_LOWER" == "yes" ]; then
    $YELLOW
    printf "Redis exporter installation confirmed.\n\n"
    $NORMAL
else
    printf "Redis exporter installation cancelled.\n\n"
    exit
fi

# Specify the name of the systemd service
SERVICE_NAME="redis_exporter"

# Check if the service file exists
if [ -e "/usr/lib/systemd/system/$SERVICE_NAME.service" ]; then
    # Check if the service is active
    if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
        $BOLD
        printf "There is an active $SERVICE_NAME. \n\n"
        $NORMAL
        # Check the version of the active redis_exporter
        REDIS_EXPORTER_PATH="/usr/local/$SERVICE_NAME/$SERVICE_NAME"
        VERSION_INFO="$($REDIS_EXPORTER_PATH --version 2>&1 | awk '{print $6}')"
        $GREEN
        printf "Active Redis ExporterVersion: $VERSION_INFO \n\n"
        $NORMAL
        printf "Do you want to remove it and replace with a new one? [ 1 / 2 ]\n\n"
        printf " 1: Remove the active redis_exporter and replace it with a new one. \n\n"
        printf " 2: Don't do anything and exit.\n\n"
        read -rp "> " ACTION
        # Check the action to do
        if [ -z "$ACTION" ]; then
            printf "Removing all redis_exporter files... \n\n"
            # Remove redis_exporter related files
            sudo systemctl stop $SERVICE_NAME
            sudo systemctl disable $SERVICE_NAME
            sudo rm /usr/lib/systemd/system/$SERVICE_NAME.service
            sudo rm -rf /usr/local/redis_exporter*
            $YELLOW
            printf "Related files removed.\n\n"
            $NORMAL
            printf "Installation will continue...\n\n"
        elif [ "$ACTION" -eq 1 ]; then
            printf "Removing all redis_exporter files... \n\n"
            # Remove redis_exporter related files
            sudo systemctl stop $SERVICE_NAME
            sudo systemctl disable $SERVICE_NAME
            sudo rm /usr/lib/systemd/system/$SERVICE_NAME.service
            sudo rm -rf /usr/local/redis_exporter*
            $YELLOW
            printf "Related files removed.\n\n"
            $NORMAL
            printf "Installation will continue...\n\n"
        elif [ "$ACTION" -eq 2 ]; then
            $DOWN
            printf "No action done.\n\n"
            exit
        else
            printf "Invalid input. Please enter 1 or 2.\n\n"
            exit 1
        fi
    else
        printf "There's a $SERVICE_NAME service that is not active. Removing related files...\n\n"
        sudo systemctl stop $SERVICE_NAME
        sudo systemctl disable $SERVICE_NAME
        sudo rm /usr/lib/systemd/system/$SERVICE_NAME.service
        sudo rm -rf /usr/local/redis_exporter*
        $YELLOW
        printf "Related files removed.\n\n"
        $NORMAL
        printf "Installation will continue...\n\n"
    fi
else
    printf "No $SERVICE_NAME service file found.\n\n"
    fi

# Curling Google to check if connected to a network
printf "Looking for a network...\n\n"
if curl google.com > /dev/null; then
    $DOWN
    $YELLOW
    printf "Network connected.\n\n"
    $NORMAL
else
    $DOWN
    printf "The server is not connected to the network. Please connect and try again.\n\n";
    exit 1
fi

echo -n "Insert the version you would like to be installed, default is [ 1.29.0 ] : "
$BOLD
$BLUE
read VERSION
$NORMAL
VERSION=${VERSION:-1.29.0}
$DOWN
$NORMAL
# Download the file
wget https://github.com/oliver006/redis_exporter/releases/download/v$VERSION/redis_exporter-v$VERSION.linux-amd64.tar.gz -P /opt

# Extract the downloaded tarball in the user directory with new name
tar -xzvf /opt/redis_exporter-v$VERSION.linux-amd64.tar.gz -C /usr/local && mv /usr/local/redis_exporter-v$VERSION.linux-amd64 /usr/local/redis_exporter

# Create a systemd service file for redis exporter
sudo cat >/usr/lib/systemd/system/redis_exporter.service<<EOF
[Unit]
Descrition=redis_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
PIDFile=/usr/local/redis_exporter/redis_exporter.pid
ExecStart=/usr/local/redis_exporter/redis_exporter -redis.password=xxPASSWORDxx -redis.addr=xxIPxx:xxPORTxx
ExecReload=/bin/kill -s HUP
ExecStop=/bin/kill -s QUIT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# Provide the redis information
$DOWN
$YELLOW
printf "Please enter the Redis information (e.g, 10.10.10.10:6379 MyPasswd123) : \n"
$NORMAL
read -r REDIS_INFO
$DOWN
IP=$(echo "$REDIS_INFOR" | awk '{print $1}' FS=":")
PORT=$(echo "$REDIS_INFO" | awk '{print $1}' | awk '{print $2}' FS=":")
PASSWORD=$(echo "$REDIS_INFO" |awk '{print $2}')

sed -i "s#xxPORTxx#$PORT#g" /usr/lib/systemd/system/redis_exporter.service
sed -i "s#xxIPxx#$IP#g" /usr/lib/systemd/system/redis_exporter.service
sed -i "s#xxPASSWORDxx#$PASSWORD#g" /usr/lib/systemd/system/redis_exporter.service

# Reload systemd and start redis exporter
sudo systemctl daemon-reload
sudo systemctl start redis_exporter.service
sudo systemctl enable redis_exporter.service
sudo systemctl status redis_exporter.service

# Cleanup downloaded file
rm -f /opt/redis_exporter*

$DOWN
if sudo systemctl is-active --quiet "$SERVICE_NAME"; then
    $DOWN
    $BOLD
    $YELLOW
    printf "======================================\n"
    $GREEN
    printf "Redis Exporter installed successfully!\n"
    $NORMAL
    $BOLD
    printf "Version: $VERSION_INFO\n"
    $YELLOW
    printf "======================================\n"
    $NORMAL
    $DOWN
else
    $DOWN
    $RED
    printf "Redis Exporter installation failed.\n\n"
    $NORMAL
    $DOWN
fi


This content originally appeared on DEV Community and was authored by tj_27


Print Share Comment Cite Upload Translate Updates
APA

tj_27 | Sciencx (2024-06-29T07:23:31+00:00) Installing Redis Exporter Bash Script. Retrieved from https://www.scien.cx/2024/06/29/installing-redis-exporter-bash-script/

MLA
" » Installing Redis Exporter Bash Script." tj_27 | Sciencx - Saturday June 29, 2024, https://www.scien.cx/2024/06/29/installing-redis-exporter-bash-script/
HARVARD
tj_27 | Sciencx Saturday June 29, 2024 » Installing Redis Exporter Bash Script., viewed ,<https://www.scien.cx/2024/06/29/installing-redis-exporter-bash-script/>
VANCOUVER
tj_27 | Sciencx - » Installing Redis Exporter Bash Script. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/29/installing-redis-exporter-bash-script/
CHICAGO
" » Installing Redis Exporter Bash Script." tj_27 | Sciencx - Accessed . https://www.scien.cx/2024/06/29/installing-redis-exporter-bash-script/
IEEE
" » Installing Redis Exporter Bash Script." tj_27 | Sciencx [Online]. Available: https://www.scien.cx/2024/06/29/installing-redis-exporter-bash-script/. [Accessed: ]
rf:citation
» Installing Redis Exporter Bash Script | tj_27 | Sciencx | https://www.scien.cx/2024/06/29/installing-redis-exporter-bash-script/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.