The following shell script can be used to monitor Elasticsearch servers. The basic idea is that the server (IP:9200) will respond with a color code(Green, Red and Yellow). We use the color code to know the status of the server and notify the users through configured E-Mail addresses. An E-Mail will be sent to configured E-Mail addresses when the Elasticsearch server goes down i.e. when the status goes red.
Steps to Configure the Shell Script:
- Copy the shell script content to a file and name it with extension .sh
- Add the required IP addresses of the Elasticsearch servers
- Add the hostnames(used for display purpose only) of the Elasticsearch servers
- Add the required E-Mail addresses
- Configure the sleep interval which is used by the script to check the status of the servers for each n minutes. Default value is 10m which represents 10minutes
- Start the script using the command sh filename.sh or ./filename.sh (in Linux/Unix OS)
Shell Script:
#!/bin/sh #Configure the IP addresses IP=("IP1" "IP2" "IP3") #Configure the hostnames hostname=("HOSTNAME1" "HOSTNAME2" "HOSTNAME3") while true do message="" flag=0 for (( i=0; i < ${#IP[@]}; i++ )) do echo $i response=$(curl ${IP[$i]}:9200/_cluster/health) if [[ ! "$response" =~ "green" ]]; then flag=1 if [[ "$response" =~ "red" ]]; then message+="Elasticsearch Server ${hostname[$i]}(${IP[$i]}) is down\n" elif [[ "$response" =~ "yellow" ]]; then message+="Elasticsearch server ${hostname[$i]}(${IP[$i]}) shards are allocating\n" elif [[ "$response" == "" ]]; then message+="Elasticsearch process is not running in ${hostname[$i]}(${IP[$i]})\n" fi fi done if [ $flag == 1 ]; then echo "Sending Mail" #Configure the E-Mail addresses echo $message | mail -s "Pulse - Elasticssearch Server Down" EMailID1 EMailID2 echo $message fi #Time duration between the monitoring. 10m represents 10 minutes sleep 10m done
The above shell script was tested and works fine in Red Hat Linux and it should also work in Unix based systems. Make sure to enable the mail settings in the system.
Good work..keep it up 🙂
Do u have automated shell script to install three node ES cluster in shell script for ubuntu
Thanks, Good work.