#!/bin/sh # # Description: Manages a memcached server as an OCF High-Availability # resource under Heartbeat/LinuxHA control # # OCF parameters: # OCF_RESKEY_memcached - Path to executable. # OCF_RESKEY_start_opt - Startup options. # OCF_RESKEY_pid_file - Path to PID file. # OCF_RESKEY_user - Which user to run the server as. # OCF_RESKEY_port - Which TCP port to listen on. # OCF_RESKEY_max_memory - Maximum cache size in megabytes. # OCF_RESKEY_max_conns - Maximum number of connections. ############################################################################### # Initialization: PROG=memcached . /usr/lib64/heartbeat/ocf-shellfuncs unset LC_ALL; export LC_ALL unset LANGUAGE; export LANGUAGE SH=/bin/bash usage() { cat <<-! >&1 usage: $0 start|stop|status|monitor|methods $0 manages a memcached server as an HA resource. The 'start' operation starts the server. The 'stop' operation stops the server. The 'status' operation reports whether the server is up. The 'monitor' operation reports whether the server is running. The 'methods' operation reports on the methods $0 supports. ! return $OCF_ERR_ARGS } meta_data() { sed "s/__PROG__/$PROG/g" < 1.0 Resource script for __PROG__. It manages a server as an HA resource. __PROG__ resource agent Path to __PROG__ command. __PROG__ Start options. Do not include the -c, -m, -p or -P options here; there are specific settings for those. start_opt Which TCP port to listen on. Port number Amount of memory to use for cache, in megabytes. Max Memory Maximum number of connections allowed. Max Connections Path to PID file. PID File Which user to run the server as. User END } # # Run the given command in the Resource owner environment... # runasowner() { su $AS_USER -c ". ~${AS_USER}/.bash_profile; $*" } # # methods: What methods/operations do we support? # prog_methods() { echo start echo stop echo status echo monitor echo methods } #prog_start: Starts the server prog_start() { if prog_status then ocf_log info "$PROG is already running." return $OCF_SUCCESS fi if [ -x $EXECUTABLE ] then COMMANDLINE="$EXECUTABLE -d $START_OPT" if [ -n "$MAX_MEMORY" ]; then COMMANDLINE="$COMMANDLINE -m $MAX_MEMORY" fi if [ -n "$MAX_CONNS" ]; then COMMANDLINE="$COMMANDLINE -c $MAX_CONNS" fi if [ -n "$PORT" ]; then COMMANDLINE="$COMMANDLINE -p $PORT -U $PORT" fi if [ -n "$PID_FILE" ]; then COMMANDLINE="$COMMANDLINE -P $PID_FILE" fi if [ -n "$AS_USER" ]; then COMMANDLINE="$COMMANDLINE -u $AS_USER" fi echo $COMMANDLINE if $COMMANDLINE > /dev/null 2>&1 then # Probably started..... ocf_log info "$PROG started." else ocf_log err "Can't start $PROG." return $OCF_ERR_GENERIC fi else ocf_log err "$EXECUTABLE not found!" return $OCF_ERR_GENERIC fi if ! prog_status then sleep 5 if ! prog_status then echo "ERROR: $PROG is not running!" return $OCF_ERR_GENERIC fi fi return $OCF_SUCCESS } #prog_stop: Stops the server prog_stop() { if ! prog_status then #Already stopped return $OCF_SUCCESS fi if [ ! -f $PID_FILE ]; then echo "ERROR: no PID file found" return $OCF_ERR_GENERIC fi kill `cat $PID_FILE` if prog_status then sleep 1 kill -9 `cat $PID_FILE` if prog_status then echo "ERROR: server not terminating!" return $OCF_ERR_GENERIC fi fi rm -f $PID_FILE return $OCF_SUCCESS } # # prog_status: Is the server up? # prog_status() { STATS="`send_command stats`" case "$STATS" in STAT*) return $OCF_SUCCESS ;; "") return $OCF_NOT_RUNNING ;; *) return $OCF_ERR_GENERIC esac } # # sends a command to the server # send_command() { echo "`(echo "$1"; echo quit) | nc localhost $PORT`" } # # prog_monitor # prog_monitor() { if ! prog_status then ocf_log info "$PROG is down" return $OCF_NOT_RUNNING fi return $OCF_SUCCESS } # # 'main' starts here... # if [ $# -ne 1 ] then usage exit 1 fi US=`id -u -n` if [ $US != root ] then ocf_log err "$0 must be run as root" exit 1 fi EXECUTABLE=${OCF_RESKEY_memcached:-/usr/local/fbprojects/$PROG/bin/$PROG} START_OPT=${OCF_RESKEY_start_opt} PORT=${OCF_RESKEY_port:-11000} AS_USER=${OCF_RESKEY_user:-nobody} MAX_MEMORY=${OCF_RESKEY_max_memory:-12288} MAX_CONNS=${OCF_RESKEY_max_conns:-1048576} PID_FILE=${OCF_RESKEY_pid_file:-/tmp/.memcached.pid.$PORT} if [ ! -x $EXECUTABLE ] then ocf_log err "Can't run $EXECUTABLE" exit $OCF_ERR_GENERIC fi # What kind of method was invoked? case "$1" in start) prog_start exit $?;; stop) prog_stop exit $?;; status) if prog_status then ocf_log info "$PROG is up" exit $OCF_SUCCESS else ocf_log info "$PROG is down" exit $OCF_NOT_RUNNING fi exit $?;; monitor) prog_monitor exit $?;; methods) prog_methods exit $?;; meta_data) meta_data exit $OCF_SUCCESS;; esac usage