#! /bin/bash ES_COUNT_PATH=/tmp/es_count LOG_PATH=/var/log/m2/m2_es_status.log debug() { DATE=`date` echo "$DATE - $1" >> $LOG_PATH echo $1 } # check if elasticsearch is running es_running=`netstat -tulpn | grep 9200 | grep java | wc -l` if [ "X$es_running" != "X0" ]; then # check elasticsearch status total_shards=`rm -fr $ES_COUNT_PATH && elasticsearch count &> $ES_COUNT_PATH && grep total $ES_COUNT_PATH | grep -o "[0-9]"` successful_shards=`rm -fr $ES_COUNT_PATH && elasticsearch count &> $ES_COUNT_PATH && grep successful $ES_COUNT_PATH | grep -o "[0-9]"` re='^[0-9]+$' if ! [[ $successful_shards =~ $re ]] ; then debug "Can't check Elasticsearch status" debug "Run the following command to check result:" debug "rm -fr $ES_COUNT_PATH && elasticsearch count &> $ES_COUNT_PATH && grep successful $ES_COUNT_PATH | grep -o \"[0-9]\"" else if [ "$successful_shards" != "$total_shards" ]; then debug "Failed Elasticsearch shards detected!" debug "Total shards: $total_shards" debug "Active shards: $successful_shards" debug "Restarting and resyncing Elasticsearch" service elasticsearch restart debug "Waiting for Elasticsearch to start" for i in `seq 1 60`; do echo -n "." sleep 1 curl "http://localhost:9200/?pretty" &> /dev/null if [ "X$?" == "X0" ]; then debug "" curl "http://localhost:9200/?pretty" debug "Elasticsearch started" debug "Resyncing Elasticsearch" elasticsearch resync break fi done else debug "Elasticsearch status is ok" fi fi else debug "Elasticsearch is not running" fi