Shell Script | Install Influxdb and Grafana on Linux machines

 


What is a Monitoring Stack? 

A Monitoring Stack is an end-to-end setup where we try to keep a close eye on our Application. We deploy a few layers and connect them so that we can put the server metrics, logs, network traffic, resources utilisation, etc on surveillance. A well structured monitoring stack should include 3 layers, metrics collecting agent (like Telegraf), a time-series database (like InfluxDB) and a visualisation tool to view them as a meaningful information (like Grafana).

Today's Agenda

In this post, we will learn how to setup a monitoring stack for our Application. To make this easier and to save time, we have created a Shell Script that will do these steps for you by just taking few arguments. You can follow this blog to get a separate script that will configure Telegraf with just a single command.

Prerequisite

This post has been prepared for the audience who : 
  1. Have access to a Linux-based system like Ubuntu, CentOS, Redhat, etc.
  2. Have a basic understanding of Linux-based systems and their commands.
  3. And finally, are eager to learn and try something new.

Let's get started

Step 1: Create a new file to store the shell script.

Create a file with name Setup-influxdb-grafana.sh and add the below script to it.
#!/bin/bash
### SCRIPT TO INSTALL INFLUXDB AND GRAFANA ###
set -e
if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root user"
  exit 1
fi

usage() {
    echo  -e "\n###\n Monitoring Stack Installation Script
      Note:
        This script is for Ubuntu | Debian | Redhat | CentOS Server types Only.
      Purpose:
        This Script will perform the following functions:
-- Check OS type of server.
-- Install Monitoring Stack with required input plugins & dependencies.
      Usage:
        bash $0 --help
        bash $0 start
      Script Parameters:
        start                  starts the script
        --help or -h           shows this help text \n###\n"
    exit 0
}
if test "$1" = "" || test  "$1" = "--help" || test "$1" = "-h"; then
  usage
elif test "$1" = "start" ; then
  break
fi
echo -e "\n##############################################\n"

### BEGIN INSTALLATION ACCORDING TO OS TYPE ###
debian_download() {
    # resynchronize the package index files from their sources.
    sudo apt-get update
    sudo apt-get install wget -y
    # Downloading debian package of influxdb
    sudo https://dl.influxdata.com/influxdb/releases/influxdb2-2.0.7-amd64.deb
    # Install influxdb from debian package
    sudo dpkg -i influxdb2-2.0.7-amd64.deb
    # Starting The Service
    sudo systemctl start influxdb
    sudo systemctl enable influxdb
    # Downloading debian package of grafana
sudo apt-get install -y adduser libfontconfig1
    sudo wget https://dl.grafana.com/oss/release/grafana_8.0.1_amd64.deb
    # Handeling Configurations
    sudo apt-get update
    sudo apt-get install -f -y
    sudo dpkg --configure -a
    # Install grafana from debian package
    sudo dpkg -I grafana_8.0.1_amd64.deb
    # Starting The Service
    sudo systemctl start grafana-server
    sudo systemctl enable grafana-server
    sudo rm -f influxdb2_2.0.7_amd64.deb grafana_8.0.1_amd64.deb
    echo -e "All Installations Completed. \nYou are ready to go!"
}

redhat_download() {
sudo yum update
    #Installing wget.
    sudo yum install wget -y
    # Downloading rpm package of logstash
    sudo wget https://dl.influxdata.com/influxdb/releases/influxdb2-2.0.7.x86_64.rpm
    # Install logstash rpm package
    sudo yum localinstall influxdb2-2.0.7.x86_64.rpm
    # Starting The Services
    sudo systemctl start influxdb
    sudo systemctl enable influxdb
    # Downloading grafana redhat package
    wget https://dl.grafana.com/oss/release/grafana-8.0.1-1.x86_64.rpm
sudo yum install grafana-8.0.1-1.x86_64.rpm
    sudo yum install -f -y
    # Starting The Service
    sudo systemctl start grafana-server
    sudo systemctl enable grafana-server
    sudo rm -f influxdb_1.7.6_amd64.rpm grafana_8.0.1-1.x86_64.rpm
    echo -e "All Installations Completed. \nYou are ready to go!"
}

### CHECK OS OF CLIENT MACHINE ###
if [ "$(grep -Ei 'Debian|Ubuntu|Mint' /etc/*release)" ]
    then
        echo " It's a Debian based system"
        debian_download
elif [ "$(grep -Ei 'Redhat|CentOS|Fedora' /etc/*release)" ]
    then
        echo "It's a RedHat based system."
        redhat_download
else
    echo "This script doesn't support this OS."
echo "Please refer this page to find your OS type:"
echo "https://portal.influxdata.com/downloads/"
echo "https://grafana.com/grafana/download"
    exit 3
fi

#Finishing
echo  -e "\n###\n ### InfluxDB and Grafana Should now be Running. ###\n###\n"


Step 2 : Run this Shell Script to begin Installation.

This Shell Script needs to be run as a root user.

To know how this shell script works, enter the below command:

      bash Setup-influxdb-grafana.sh -help


Add the asked variables and run the Shell Script as the root user.

      sudo bash Setup-influxdb-grafana.sh start


This script is designed to run only on Linux-based machines.

Step 3 : Setup Telegraf to start metrics collection.

Install Telegraf on the server to collect metrics.
You can follow this blog to get a separate script that will configure Telegraf with just a single command.

Once All the 3 tools are up and running, you can start with configuring Telegraf plugins to start metrics collection.

That's all, you did a great job !!

If you face any issues or need any suggestions, please comment down below and hit the like button to appreciate the efforts.






Comments