Translate

Friday, February 14, 2014

Elastic Search Init.d Script

#!/usr/bin/env bash
#
# elasticsearch
#
# chkconfig:   - 57 47
# description: elasticsearch
# processname: elasticsearch
# config:      /usr/local/etc/elasticsearch/elasticsearch.yml
ES_MIN_MEM=256m  
ES_MAX_MEM=2g
# Source networking configuration
if [ -f /etc/sysconfig/network ]; then source /etc/sysconfig/network; fi

# Exit if networking is not up
[ "$NETWORKING" = "no" ] && exit

PIDFILE='/var/run/elasticsearch/10_225_139_31_cloud_opsource_net.pid'
ES_INCLUDE='/usr/local/etc/elasticsearch/elasticsearch-env.sh'
CHECK_PID_RUNNING=$(ps ax | grep 'java' | grep -e "es.pidfile=$PIDFILE" | sed 's/^\s*\([0-9]*\)\s.*/\1/')

start() {
    if [ -f $PIDFILE ]; then
      # PIDFILE EXISTS -- ES RUNNING?
      echo -e "\033[31;1mPID file found in $PIDFILE, elasticsearch already running?\033[0m"
      es_pid="$(cat $PIDFILE)"
      pid_running="$( ps ax | grep 'java' | grep $es_pid )"

      if [ ! -z "$pid_running" ] ; then
        # EXIT IF ES IS ALREADY RUNNING
     echo -e "\033[31;1mPID $es_pid still alive, already running...\033[0m"
     return 1
      fi
    fi
   
    echo -e "\033[1mStarting elasticsearch...\033[0m"
    su - elasticsearch -m -c "ES_INCLUDE=$ES_INCLUDE /usr/local/bin/elasticsearch -p $PIDFILE"

    return $?
}

stop() {
    if [[ -f $PIDFILE ]]; then
      echo -n -e "\033[1mStopping elasticsearch...\033[0m"

      # REMOVE PIDFILE AND EXIT IF PROCESS NOT RUNNING
      if [ ! $CHECK_PID_RUNNING ]; then
        echo -e "\033[1mPID file found, but no matching process running?\033[0m"
        echo    "Removing PID file..."
        su elasticsearch -m -c "rm $PIDFILE"
        exit 0
      fi

      # KILL PROCESS
      su elasticsearch -m -c "kill $(cat $PIDFILE)"
      r=$?
      timeout=0
      while [ -f $PIDFILE  ]; do
        echo -n '.'
        (( timeout++ ))
        if [ $timeout -gt '15' ]; then return; fi
        sleep 1
      done; echo
      return $r
    else
      echo -e "\033[1mNo PID file found -- elasticsearch not running?\033[0m"
    fi
}

restart() {
    stop
    timeout=30
    while ps aux | grep 'java' | grep -e "es.pidfile"; do
      echo -n '.'
      (( timeout-- ))
      if [ $timeout -lt '1' ]; then return; fi
      sleep 1
    done;
    start
}

status() {
  # GOT PIDFILE?
  [ -f $PIDFILE ] && pid=$(cat $PIDFILE)

  # RUNNING
  if [[ $pid && -d "/proc/$pid" ]]; then
    version=$(curl -s 'http://localhost:9200' | /opt/chef/embedded/bin/ruby -rubygems -e 'require "json"; print JSON.parse(STDIN.read)["version"]["number"]')
    echo -e "\033[1;37;46melasticsearch $version running with PID $pid\033[0m"
    # VERBOSE
    if [[ $pid && $1 == '-v' || $1 == '--verbose' ]]; then
      curl -s 'http://localhost:9200/_cluster/nodes/10-225-139-31.cloud.opsource.net?os&process&jvm&network&transport&settings&pretty' | \
      /opt/chef/embedded/bin/ruby -rubygems -e '
        begin
          require "json"; h = JSON.parse(STDIN.read); id, node = h["nodes"].first;
          def e(name, value); puts %Q|\e[1;36m#{(name.to_s+":").ljust(20)}\e[0m #{value || "N/A" rescue "N/A"}|; end
          e "HTTP Address",  node["http_address"]
          e "Node Name",     node["name"]
          e "Cluster Name",  h["cluster_name"]
          e "Started",       Time.at(node["jvm"]["start_time"].to_i/1000)
          e "JVM",           "#{node["jvm"]["vm_name"]} (#{node["jvm"]["version"]})"
          e "Memory Total",  node["os"]["mem"]["total"]
          e "Open Files",    node["process"]["max_file_descriptors"]
          e "Configuration", node["settings"]["config"]
        rescue
          puts "Metadata cannot be retrieved."
        end
      '
    fi
    # INCORRECT PID?
    if [ $pid != $CHECK_PID_RUNNING ]; then
      echo -e "\033[1;31;40m[!] Incorrect PID found in $PIDFILE: $pid\033[0m"
      return 1
    fi
    return 0
  fi

  # NOT RUNNING
  if [[ ! $pid || ! -d "/proc/$pid" ]]; then
    echo -e "\033[1;33;40melasticsearch not running\033[0m"
    return 3
  fi

  # STALE PID FOUND
  if [[ ! -d "/proc/$pid" && -f $PIDFILE ]]; then
    echo -e "\033[1;31;40m[!] Stale PID found in $PIDFILE\033[0m"
    return 1
  fi
}


case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        status $2
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|status [-v]|}"
        exit 1
esac

exit $?

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete

About Me

My photo
Greetings Friend! I am Linux scholar, trying to learn as much I can and share it with you. I am in mid of my Professional Career. Doing Good. :)

Followers