DEV Community

Cover image for Installing Prometheus Bash Script
tj_27
tj_27

Posted on

Installing Prometheus Bash Script

Installing monitors like Grafana would be very helpful for us. As one of the most used datasource, I decided to make a script on how to install prometheus.

#!/bin/bash
# Installing Prometheus

if [ -f "/usr/local/prometheus" ]; then
    echo "Prometheus already exists ..."
    exit
fi


# 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

printf "Installing Prometheus\n\n"
# Curling Google to check if connected to a network
printf "Looling 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

$DOWN

echo -n "Insert the version you would like to be installed, default is [ 2.49.1 ] : "
$BOLD
$BLUE
read VERSION
$NORMAL
VERSION=${VERSION:-2.49.1}
$DOWN
$NORMAL
if [ ! -f "prometheus-$VERSION.linux-amd64.tar.gz" ]; then
    # Download prometheus
    wget https://github.com/prometheus/prometheus/releases/download/v$VERSION/prometheus-$VERSION.linux-amd64.tar.gz -P /opt
fi

# extract the file
tar -xzvf /opt/prometheus-$VERSION.linux-amd64.tar.gz -C /usr/local
# make it s soft link
ln -s /usr/local/prometheus-$VERSION.linux-amd64 /usr/local/prometheus

# create a prometheus service file
cat >/usr/lib/systemd/system/prometheus.service<<EOF
[Unit]
Description=prometheus
After=network.target

[Service]
Type=simple
PIDFile=/var/run/prometheus.pid
WorkingDirectory=/usr/local/prometheus/prometheus --web.listen-address=0.0.0.0:9090 --storage.tsdb.path=/data/prometheus --web.enable-admin-api --storage.tsdb.retention=7d
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF
$DOWN
# start the prometheus
sudo systemctl start prometheus.service
# check the status
sudo systemctl status prometheus.service

# see the listening sockets
netstat -tnlp |grep 9090
$DOWN
if sudo systemctl is-active --quiet prometheus.service; then
    $DOWN
    $BOLD
    $YELLOW
    printf "=========================================\n"
    $GREEN
    printf "Prometheus installed successfully!\n"
    $NORMAL
    $BOLD
    $YELLOW
    printf "=========================================\n"
    $NORMAL
    $DOWN
else
    $DOWN
    $RED
    printf "Prometheus installation failed.\n\n"
    $NORMAL
    $DOWN
fi
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay