Shell Script | Install Logstash on Linux machines

Shell Script |  Install Logstash on Linux machines


What is Logstash? 

Logstash is an open-source, lightweight, free, server-side data processing pipeline. It stands for the "L" in the ELK Stack, with the help of which we can pull data from diverse sources using input plugins. We can then apply numerous transformations and enhancements using filter plugins, and finally, ship the data to various destinations using output plugins. Logstash provides the advantage of selecting, mixing and utilizing different inputs, filters and outputs according to our requirements to work in a consistent manner.

Today's Agenda

In this post, we will learn how to install Logstash on Linux machines. This script becomes really helpful in cases where we have multiple servers on which we need Logstash to be installed. Also, it will detect the Server OS version and run the installation accordingly.

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 Java installed on the system (Java 8 or Java 11).
  3. Have a basic understanding of Linux-based systems and their commands.
  4. 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 the name SetupLoggingAgent-Logstash.sh and add the below script to it.

#!/bin/
bash

if [ "${1}" == "-h" ] || [ "${1}" == "-help" ] || [ "${1}" == "" ]
    then
        echo "HELP SECTION FOR THIS SCRIPT!"
        echo "This script needs user with root permissions to get executed."

        echo -e "Logstash Installation Script\n Purpose: \n This Script will perform the following functions:
-- Install Logstash with required input parsers & output.

      Usage:
        FOR HELP:
            bash SetupLoggingAgent-Logstash.sh -help
            bash
SetupLoggingAgent-Logstash.sh -h
            bash
SetupLoggingAgent-Logstash.sh
        To BEGIN INSTALLATION:
            bash
SetupLoggingAgent-Logstash.sh -start"
exit 0
fi

if [ "${1}" == "-start" ]
    then

    sudo -n true
    if [ $? -ne 0 ]
        then
            echo "Permission Denied! Try executing this script with root user permissions or try with sudo."
            echo "For more information about this file use -h or -help along with the above command."
            exit 3
    fi

    set -e

    ###########################################################
    ##    JAVA installation on the client machine
    ###########################################################

    JAVA_version_check_debian() {
    java -version
    if [ $? -ne 0 ]
        then
            # Installing Java 8 if it's not installed
            sudo apt-get install openjdk-8-jre-headless -y
        # Checking if java installed is less than version 8. If yes, installing Java 8. As logstash & Elasticsearch require Java 8 or later.
        elif [ "`java -version 2> /tmp/version && awk '/version/ { gsub(/"/, "", $NF); print ( $NF < 1.8 ) ? "YES" : "NO" }' /tmp/version`" == "YES" ]
            then
                sudo apt-get install openjdk-8-jre-headless -y
    fi
    }

    JAVA_version_check_redhat() {
        java -version
        if [ $? -ne 0 ]
            then
                #Installing Java 8 if it's not installed
                sudo yum install jre-1.8.0-openjdk -y
            # Checking if java installed is less than version 8. If yes, installing Java 8. As logstash & Elasticsearch require Java 8 or later.
            elif [ "`java -version 2> /tmp/version && awk '/version/ { gsub(/"/, "", $NF); print ( $NF < 1.8 ) ? "YES" : "NO" }' /tmp/version`" == "YES" ]
                then
                    sudo yum install jre-1.8.0-openjdk -y
        fi
    }

###########################################################
    ##    install logstash on the client machine
    ###########################################################

    debian_logstash() {
        # resynchronize the package index files from their sources.
        sudo apt-get update
        # Downloading Debian package of logstash
        sudo wget https://artifacts.elastic.co/downloads/logstash/logstash-7.13.2-amd64.deb
        # Install logstash Debian package
        sudo dpkg -i logstash-7.13.2-amd64.deb
        # Starting The Service
        sudo systemctl start logstash
        sudo systemctl enable logstash

        sudo rm -f ./logstash-7.13.2-amd64.deb
        echo -e "All Installations Completed. \nYou are ready to go!"

    }

    redhat_logstash() {
        #Installing wget.
        sudo yum install wget -y
        # Downloading rpm package of logstash
        sudo wget https://artifacts.elastic.co/downloads/logstash/logstash-7.13.2-x86_64.rpm
        # Install logstash rpm package
        sudo rpm -ivh logstash-7.13.2-x86_64.rpm
        # Starting The Services
        sudo service logstash start

        sudo rm -f ./logstash-7.13.2-x86_64.rpm
        echo -e "All Installations Completed. \nYou are ready to go!"

    }

  ###########################################################
    ##    CHECK OS VERSION OF THE CLIENT SYSTEM
    ###########################################################

    if [ "$(grep -Ei 'debian|Ubuntu|mint' /etc/*release)" ]
        then
            echo " It's a Debian based system"
            JAVA_version_check_debian
            debian_logstash
    elif [ "$(grep -Ei 'fedora|redhat|centos' /etc/*release)" ]
        then
            echo "It's a RedHat based system."
            JAVA_version_check_redhat
            redhat_logstash
    else
        echo "This script doesn't support this OS."
echo "Please follow this link to find other installation options: https://www.elastic.co/downloads/logstash"
        exit 3
    fi
fi

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 SetupLoggingAgent-Logstash.sh -help


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

      sudo bash SetupLoggingAgent-Logstash.sh -start


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