Cassandra Service Script

Without an existing cassandra service script, I decided to go ahead and create one, to make things a little easier to manage, and to make the whole experience a little more user friendly :) The script includes a few nodetool basics, such as repair, cleanup, info, netstats etc. And will log the start and end times in its own log for repair and cleanup, allowing you to see how long the process takes without having the trawl through all the cassandra logs to find a start and end time (very useful for us when it takes over 5 hours to complete a repair).

Here is the script, simply copy the content into /etc/init.d/cassandra and make it executable :)

#!/bin/bash
#
# Author: Brooke Bryan
#
#
# Description:	Cassandra Server.
# Processname: cassandra
# Config: /usr/local/share/cassandra/conf/cassandra.yaml

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

prog="Cassandra"
pidfile="/var/run/cassandra.pid"
progbin="/usr/local/share/cassandra/bin"
lock="/var/lock/subsys/cassandra"
logfile="/var/log/cassandra/service.log"

WriteLog()
{
    echo "`date`: $@" >> $logfile
}

LogInfo()
{
    echo "$@"
    WriteLog "INFO: $@"
}

LogWarning()
{
    echo "$@"
    WriteLog "WARNING: $@"
}

start()
{
    if [ -f $pidfile ] && checkpid `cat $pidfile`; then
        action "$prog is already running." /bin/false
        exit 0
    fi

    WriteLog "Starting $prog"

    daemon "$progbin/cassandra" -p $pidfile >> $logfile 2>&1
    usleep 500000
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        touch "$lock"
        action "Starting $prog" /bin/true
    else
        action "Starting $prog" /bin/false
    fi

    WriteLog "Started $prog"

    return $RETVAL
}

stop()
{
    $progbin/nodetool -h localhost disablethrift
    $progbin/nodetool -h localhost disablegossip
    $progbin/nodetool -h localhost drain
    WriteLog "Stopping $prog"
    CASSIEPID=`cat "$pidfile"  2>/dev/null `
    if [ -n "$CASSIEPID" ]; then
        /bin/kill "$CASSIEPID" >/dev/null 2>&1
        ret=$?
        if [ $ret -eq 0 ]; then
            STOPTIMEOUT=60
            while [ $STOPTIMEOUT -gt 0 ]; do
                /bin/kill -0 "$CASSIEPID" >/dev/null 2>&1 || break
                sleep 1
                let STOPTIMEOUT=${STOPTIMEOUT}-1
            done
            if [ $STOPTIMEOUT -eq 0 ]; then
                echo "Timeout error occurred trying to stop $prog Daemon"
                ret=1
                action $"Stopping $prog: " /bin/false
                LogInfo "Timeout error occurred trying to stop $prog Daemon pid($CASSIEPID)"
            else
                rm -f "$lock"
                action $"Stopping $prog: " /bin/true
                WriteLog "INFO: $prog Daemon Stopped pid($CASSIEPID)"
            fi
        else
            action $"Stopping $prog: " /bin/false
            WriteLog "WARNING: $prog Daemon Stop Failed pid($CASSIEPID)"
        fi
    else
        ret=1
        action $"Stopping $prog: " /bin/false
    fi
    return $ret
}

restart()
{
    LogInfo "Restart Initiated"
    stop
    start
}

ring()
{
    $progbin/nodetool -h localhost ring
}

info()
{
    $progbin/nodetool -h localhost info
}

netstats()
{
    $progbin/nodetool -h localhost netstats
}

repair()
{
    LogInfo "Starting Repair"
    $progbin/nodetool -h localhost repair
    LogInfo "Completed Repair"
}

cleanup()
{
    LogInfo "Starting Cleanup"
    $progbin/nodetool -h localhost cleanup
    LogInfo "Completed Cleanup"
}

version()
{
    $progbin/nodetool -h localhost version
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status cassandra
    ;;
  restart)
    restart
    ;;
  ring)
    ring
    ;;
  info)
    info
    ;;
  netstats)
    netstats
    ;;
  repair)
    repair
    ;;
  cleanup)
    cleanup
    ;;
  version)
    version
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|ring|info|netstats|repair|cleanup|version}"
    exit 1
esac

exit $?
Share/Bookmark
?php bloginfo(