#!/bin/sh # # Description: Manages a memcached server as an OCF High-Availability # resource under Heartbeat/LinuxHA control # # OCF parameters: # OCF_RESKEY_pid_file - Path to PID file. # OCF_RESKEY_port - Which TCP port to listen on. # OCF_RESKEY_timeout - Time in seconds to timeout monitoring ############################################################################### # Initialization: PROG=memcached #. /usr/lib64/heartbeat/ocf-shellfuncs # Location of shellfuncs in Redhat 6 . /usr/lib/ocf/lib/heartbeat/ocf-shellfuncs # Set a sane default PORT="11211" # Pull in RH's sysconfig variables (override $PORT) . /etc/sysconfig/memcached unset LC_ALL; export LC_ALL unset LANGUAGE; export LANGUAGE SH=/bin/bash usage() { cat <<-! >&1 usage: $0 start|stop|status|monitor|methods|meta-data $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. The 'meta-data' operation returns metadata for this service ! 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 Which TCP port memcached is running on. (Autodetected from Redhat sysconfig) Port number For monitoring the memcached, have netcat timeout after N seconds Monitoring Timeout Path to PID file. PID File 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 echo meta-data } #prog_start: Starts the server prog_start() { if prog_status then ocf_log info "$PROG is already running." return $OCF_SUCCESS fi service $PROG start RET=$? if [ $RET == 0 ] then # Probably started..... ocf_log info "$PROG started." else ocf_log err "Can't start $PROG." 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 service $PROG stop if prog_status then sleep 1 #kill -9 `cat $PID_FILE` killall -9 memcached 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 -w 3 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 NC=`which nc` if [ ! -x $NC ]; then ocf_log err "I need the nc program installed!" exit 1 fi #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_pidfile:-/var/run/memcached/memcached.pid} # 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