#!/bin/sh
#
#	High-Availability Apache/IBMhttp control script
# 
# apache (aka IBMhttpd)
#
# Description:	starts/stops apache web servers.
#
# Author:	Alan Robertson
#		Sun Jiang Dong
#
# Support:	linux-ha@lists.linux-ha.org
#
# License:	GNU General Public License (GPL)
#
# Copyright:	(C) 2002-2005 International Business Machines
#
#
# An example usage in /etc/ha.d/haresources: 
#       node1  10.0.0.170 apache::/opt/IBMHTTPServer/conf/httpd.conf
#       node1  10.0.0.170 IBMhttpd
#
# Our parsing of the Apache config files is very rudimentary.
# It'll work with lots of different configurations - but not every
# possible configuration.
#
# Patches are being accepted ;-)
#
# OCF parameters:
#  OCF_RESKEY_configfile
#  OCF_RESKEY_httpd
#  OCF_RESKEY_port
#  OCF_RESKEY_statusurl
#  OCF_RESKEY_options
#  OCF_RESKEY_testregex
#  OCF_RESKEY_client
#  OCF_RESKEY_testurl
#  OCF_RESKEY_testregex10
#  OCF_RESKEY_testconffile
#  OCF_RESKEY_testname
#  OCF_RESKEY_envfiles


: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
HA_VARRUNDIR=${HA_VARRUN}

#######################################################################
#
#	Configuration options - usually you don't need to change these
#
#######################################################################
#
IBMHTTPD=/opt/IBMHTTPServer/bin/httpd
HTTPDLIST="/sbin/httpd2 /usr/sbin/httpd2 /usr/sbin/apache2 /sbin/httpd /usr/sbin/httpd /usr/sbin/apache $IBMHTTPD"
MPM=/usr/share/apache2/find_mpm
if
  [ -x $MPM ]
then
  HTTPDLIST="$HTTPDLIST `$MPM 2>/dev/null`"
fi

# default options for http clients
# NB: We _always_ test a local resource, so it should be
# safe to connect from the local interface.
WGETOPTS="-O- -q -L --no-proxy --bind-address=127.0.0.1"
CURLOPTS="-o - -Ss -L --interface lo"

LOCALHOST="http://localhost"
HTTPDOPTS="-DSTATUS"
DEFAULT_IBMCONFIG=/opt/IBMHTTPServer/conf/httpd.conf
DEFAULT_NORMCONFIG="/etc/apache2/httpd.conf"
#
# You can also set
#	HTTPD
#	PORT
#	STATUSURL
#	CONFIGFILE
# in this section if what we're doing doesn't work for you...
#
#	End of Configuration options
#######################################################################

CMD=`basename $0`

#	The config-file-pathname is the pathname to the configuration
#	file for this web server.  Various appropriate defaults are
#	assumed if no config file is specified.  If this command is
#	invoked as *IBM*, then the default config file name is
#	$DEFAULT_IBMCONFIG, otherwise the default config file
#	will be $DEFAULT_NORMCONFIG.
usage() {
  cat <<-!
usage: $0 action

action:
	start	start the web server

	stop	stop the web server

	status	return the status of web server, run or down

	monitor  return TRUE if the web server appears to be working.
                For this to be supported you must configure mod_status
		 and give it a server-status URL.  You have to have 
		installed either curl or wget for this to work.

	meta-data	show meta data message

	validate-all	validate the instance parameters
	!
  exit $1
}

#
# run the http client
#
curl_func() {
	cl_opts="$CURLOPTS $test_httpclient_opts"
	if [ x != "x$test_user" ]; then
		echo "-u $test_user:$test_password" |
			curl -K - $cl_opts "$1"
	else
		curl $cl_opts "$1"
	fi
}
wget_func() {
	auth=""
	cl_opts="$WGETOPTS $test_httpclient_opts"
	[ x != "x$test_user" ] &&
		auth="--http-user=$test_user --http-passwd=$test_password"
	wget $auth $cl_opts "$1"
}
#
# rely on whatever the user provided
userdefined() {
	$test_httpclient $test_httpclient_opts "$1"
}

#
# find a good http client
#
findhttpclient() {
	# prefer wget (for historical reasons)
	if [ "x$CLIENT" != x ]; then
		echo "$CLIENT"
	elif which wget >/dev/null 2>&1; then
		echo "wget"
	elif which curl >/dev/null 2>&1; then
		echo "curl"
	else
		return 1
	fi
}
gethttpclient() {
	[ -z "$test_httpclient" ] &&
		test_httpclient=$ourhttpclient
	case "$test_httpclient" in
		curl|wget) echo ${test_httpclient}_func;;  #these are supported
		*) echo userdefined;;
	esac
}

# test configuration good?
is_testconf_sane() {
	if [ "x$test_regex" = x -o "x$test_url" = x ]; then
		ocf_log err "test regular expression or test url empty"
		return 1
	fi
	if [ "x$test_user$test_password" != x -a \( "x$test_user" = x -o "x$test_password" = x \) ]; then
		ocf_log err "bad user authentication for extended test"
		return 1
	fi
	return 0
}
#
# read the test definition from the config
#
readtestconf() {
	test_name="$1" # we look for this one or the first one if empty
	lcnt=0
	readdef=""
	test_url="" test_regex=""
	test_user="" test_password=""
	test_httpclient="" test_httpclient_opts=""

	while read key value; do
		lcnt=$((lcnt+1))
		if [ "$readdef" ]; then
			case "$key" in
			"url") test_url="$value" ;;
			"user") test_user="$value" ;;
			"password") test_password="$value" ;;
			"client") test_httpclient="$value" ;;
			"client_opts") test_httpclient_opts="$value" ;;
			"match") test_regex="$value" ;;
			"end") break ;;
			"#"*|"") ;;
			*) ocf_log err "$lcnt: $key: unknown keyword"; return 1 ;;
			esac
		else
			[ "$key" = "test" ] &&
				[ -z "$test_name" -o "$test_name" = "$value" ] &&
				readdef=1
		fi
	done
}

source_envfiles() {
	for f; do
		[ -f "$f" -a -r "$f" ] &&
			. "$f"
	done
}

apachecat() {
	awk '
	function procline() {
		split($0,a);
		if( a[1]~/^[Ii]nclude$/ ) {
			procinclude(a[2]);
		} else {
			if( a[1]=="ServerRoot" ) {
				rootdir=a[2];
				gsub("\"","",rootdir);
			}
			print;
		}
	}
	function printfile(infile, a) {
		while( (getline<infile) > 0 ) {
			procline();
		}
		close(infile);
	}
	function allfiles(dir, cmd,f) {
		cmd="find -L "dir" -type f";
		while( ( cmd | getline f ) > 0 ) {
			printfile(f);
		}
		close(cmd);
	}
	function listfiles(pattern, cmd,f) {
		cmd="ls "pattern" 2>/dev/null";
		while( ( cmd | getline f ) > 0 ) {
			printfile(f);
		}
		close(cmd);
	}
	function procinclude(spec) {
		if( rootdir!="" && spec!~/^\// ) {
			spec=rootdir"/"spec;
		}
		if( isdir(spec) ) {
			allfiles(spec); # read all files in a directory (and subdirs)
		} else {
			listfiles(spec); # there could be jokers
		}
	}
	function isdir(s) {
		return !system("test -d \""s"\"");
	}
	{ procline(); }
	' $1 |
	sed 's/#.*//;s/[[:blank:]]*$//;s/^[[:blank:]]*//' |
	grep -v '^$'
}

#
# set parameters (as shell vars) from our apache config file
#
get_apache_params() {
  configfile=$1
  shift 1
  vars=`echo $@ | sed 's/ /,/g'`

  eval `
  apachecat $configfile | awk -v vars="$vars" '
  BEGIN{
    split(vars,v,",");
    for( i in v )
  	  vl[i]=tolower(v[i]);
  }
  {
	  for( i in v )
	  	if( tolower($1)==vl[i] ) {
			print v[i]"="$2
			delete vl[i]
			break
		}
  }
  '`
}

#
#	Return the location(s) that are handled by the given handler
#
FindLocationForHandler() {
  PerlScript='while (<>) {
	/<Location "?([^ >"]+)/i && ($loc=$1);
	'"/SetHandler +$2"'/i && print "$loc\n"; 
  }'
  apachecat $1 | perl -e "$PerlScript"
}

#
#	Check if the port is valid
#
CheckPort() {
  ocf_is_decimal "$1" && [ $1 -gt 0 ]
}

buildlocalurl() {
  [ "x$Listen" != "x" ] &&
	echo "http://${Listen}" ||
	echo "${LOCALHOST}:${PORT}"
}
#
#	Get all the parameters we need from the Apache config file
#
GetParams() {
  ConfigFile=$1
  if [ ! -f $ConfigFile ]; then
  	return 1
  fi
  get_apache_params $ConfigFile ServerRoot PidFile Port Listen
  case $PidFile in
    /*)	;;
    [[:alnum:]]*)	PidFile=$ServerRoot/$PidFile;;
    *)	PidFile=$HA_VARRUNDIR/${httpd_basename}.pid;;
  esac

  for p in "$PORT" "$Port" 80; do
    if CheckPort "$p"; then
      PORT="$p"
      break
    fi
  done
 
  echo $Listenignore the instance, the info we need is already in $vars
  rc=$OCF_SUCCESS
  if
    output=`runasdb2 $db2adm/db2stop force`
  then
    : DB2 stopped OK
    ocf_log info "DB2 UDB instance $1 stopped: $output"
  else
    case $output in

      SQL1032N*|*"No start database manager command"*)
		ocf_log info "$output";;

      *)	ocf_log err "DB2 UDB instance $1 stop failed: $output"
		rc=$OCF_ERR_GENERIC;;
    esac
  fi
  logasdb2 $db2db2 terminate
  logasdb2 $db2bin/db2_kill
  pids=`our_db2_ps | grep db2jd | cut -d' ' -f1`
  for j in $pids
  do
    runasdb2 kill -9 $j
  done
  return $rc
}


#
# db2_status: is the given db2 instance running?
#
db2_status() {
  # We ignore the instance, the info we need is already in $vars
  pscount=`runasdb2 $db2bin/db2_local_ps | cut -c9- |  grep ' db2[^ ]' | wc -l`
  test $pscount -ge 5
}

our_db2_ps() {
  ps -u $db2admin | grep db2
}


db2_dblist() {
  runasdb2 $db2db2 list database directory	\
  |	grep -i 'Database name.*=' | sed 's%.*= *%%'
}


#
# db2_monitor: Can the given db2 instance do anything useful?
#
db2_monitor() {
  # We ignore the instance, the info we need is already in $vars
  for DB in `db2_dblist`
  do

  CMD="	if $db2db2 connect to $DB;
	then 
	  $db2db2 select \* from sysibm.sysversions ; rc=\$?;
	  $db2db2 disconnect $DB;
	else
          rc=\$?;
	fi;
	exit \$rc"

    : Running this command: $CMD
    if
      output=`runasdb2 $CMD`
    then
      : Command succeeded!
    else
      ocf_log err "DB2 UDB instance $1 DB $DB is not working"
      ocf_log err "DB2 UDB message: $output"
      return $OCF_ERR_GENERIC
    fi
  done
  ocf_log info "All DBs in DB2 UDB instance $1 appear to be working"
  return $OCF_SUCCESS
}

#
#	'main' starts here...
#

if
  ( [ $# -ne 1 ] )
then
  usage
  exit $OCF_ERR_ARGS
fi

# These operations don't require OCF instance parameters to be set
case "$1" in
  meta-data)	meta_data
		exit $OCF_SUCCESS;;

  usage) 	usage
		exit $OCF_SUCCESS;;

  methods)	db2_methods
		exit $?;;

  *);;
esac

if 
  [ -z "$OCF_RESKEY_instance" ]
then
  ocf_log err "Please set OCF_RESKEY_instance to the database instance !"
  exit $OCF_ERR_ARGS
fi

instance=$OCF_RESKEY_instance
db2admin=${OCF_RESKEY_admin:-$instance}

US=`id -u -n`
US=`echo $US`
if
  [ $US != root -a $US != $db2admin ]
then
  ocf_log err "$0 must be run as root or $db2admin"
  exit $OCF_ERR_PERM
fi

#
#	Grab common db2 information...
#
if
  db2info $instance 
then
  : DB2 info is OK!
else
  exit $OCF_ERR_GENERIC
fi


# What kind of method was invoked?
case "$1" in

  start)	db2_start $instance
		exit $?;;

  stop)		db2_stop $instance
		exit $?;;

  status)	if
		  db2_status $instance
		then
		  echo DB2 UDB instance $instance is running
		  exit $OCF_SUCCESS
		else
		  echo DB2 UDB instance $instance is stopped
		  exit 3  # status: stopped exit code is 3
		fi
		;;

  monitor)	
		if
		  db2_status $instance
		then
		  db2_monitor $instance
		else
		  exit $OCF_NOT_RUNNING
		fi
		exit $?;;

  validate-all)	# OCF_RESKEY_instance has already checked within db2info(),
		# just exit successfully here.
		exit $OCF_SUCCESS;;

  *)		db2_methods
		exit $OCF_ERR_UNIMPLEMENTED;;
esac
                                                         ./usr/lib/ocf/resource.d/heartbeat/AudibleAlarm                                                     0000755 0000000 0000000 00000010110 11460230763 020312  0                                                                                                    ustar   root                            root                                                                                                                                                                                                                   #!/bin/sh
#
# Startup script for the Audible Alarm
#
# author: Kirk Lawson <lklawson@heapy.com> 
#         Horms <horms@vergenet.net>
#
# description: sets an audible alarm running by beeping at a set interval
# processname: alarm
# config: /etc/AudibleAlarm/AudibleAlarm.conf - not yet implemented
#
#	  OCF parameters are as below:
#		OCF_RESKEY_nodelist
#		
# License: GNU General Public License (GPL)

#######################################################################
# Source function library.
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs

#######################################################################
PIDFILE=${HA_VARRUN}/heartbeat-bell
#For testing
#PIDFILE=/tmp/heartbeat-bell

# What host are we running on?
us=`uname -n`

usage() {
	echo "Usage: $0 {start|stop|restart|status|monitor|meta-data|validate-all}"
	echo "  The node list is an optional space delimited"
	echo "  list of hosts that should never sound the alarm."
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="AudibleAlarm">
<version>1.0</version>

<longdesc lang="en">
Resource script for AudibleAlarm. It sets an audible alarm running by beeping 
at a set interval. 
</longdesc>
<shortdesc lang="en">Emits audible beeps at a configurable interval</shortdesc>

<parameters>
<parameter name="nodelist" unique="0">
<longdesc lang="en">
The node list that should never sound the alarm.
</longdesc>
<shortdesc lang="en">Node list</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="10" />
<action name="stop" timeout="10" />
<action name="restart" timeout="10" />
<action name="status" depth="0" timeout="10" interval="10" />
<action name="monitor" depth="0" timeout="10" interval="10" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="5" />
</actions>
</resource-agent>
END
}

audiblealarm_start () {
	ocf_log info "$0: Starting"
    	if [ -f $PIDFILE ]; then
        	PID=`head -n 1 $PIDFILE`
		if [ -n "$PID" ]; then
		  ocf_log info "$0: Appears to already be running, killing [$PID]"
		  kill $PID > /dev/null
		fi
	fi
	# Use () to create a subshell to make the redirection be synchronized.
	( while [ 1 ]; do 
		sleep 1  #Sleep first, incase we bail out
		echo -ne "\a" > /dev/console 
		# Uncomment this line to cause floppy drive light
		# to flash (requires fdutils package).
		# /usr/bin/floppycontrol --pollstate > /dev/null
		#
		# To avoid issues when called by lrmd, redirect stdout->stderr.
	done &
	if echo $! >  $PIDFILE; then
		:
	else
		ocf_log info "$0: Could not write to pid file \"$PIDFILE\", bailing"
		kill $!
		return $OCF_ERR_GENERIC
	fi) >&2

	return $?
}

audiblealarm_stop () {
	ocf_log info "$0: Shutting down"
  	if [ -f $PIDFILE ]; then
		PID=`head -n 1 $PIDFILE`
		# ocf_log info "$0: Appears to already be running, killing [$PID]"
		# commented by Phost, since the confusion in the log.

		if [ -n "$PID" ]; then
		# Donnot remove PIDFILE in case the `kill` fails.
		  kill $PID > /dev/null && rm -f $PIDFILE
		fi	
	fi

	return $?
}

audiblealarm_restart () {
	audiblealarm_stop 
	audiblealarm_start 

	return $?
}

audiblealarm_status () {
  	if [ -f $PIDFILE ]; then
		PID=`head -n 1 $PIDFILE`
		if [ -n "$PID" ]; then
			echo running
			return $OCF_SUCCESS
		fi
	fi

	echo stopped
	return $OCF_NOT_RUNNING
}

audiblealarm_validate_all () {
# Is there anything to check?
	echo "Validate OK"
	return $OCF_SUCCESS
}
if [ $# -ne 1 ]; then
  usage
  exit $OCF_ERR_ARGS
fi

case "$1" in
   meta-data)		
	meta_data
	exit $OCF_SUCCESS
	;;
   start)
	for arg in $OCF_RESKEY_nodelist
 	  do
	    if [ "$us" = "$arg" ]; then
	      # We should not start because we are on a host
	      # listed in our argument list.
              exit $OCF_SUCCESS
	    fi
	  done
	audiblealarm_start
	;;
  stop)
	audiblealarm_stop
	;;
  restart)
	audiblealarm_restart 
	;;
  status|monitor)
	audiblealarm_status 
	;;
  validate-all)
	audiblealarm_validate_all
	;;
  usage)
	usage 
	exit $OCF_SUCCESS
	;;

  *)
	usage
	exit $OCF_ERR_ARGS
	;;
esac

exit $?
                                                                                                                                                                                                                                                                                                                                                                                                                                                        ./usr/lib/ocf/resource.d/heartbeat/apache                                                           0000755 0000000 0000000 00000047067 11460230763 017236  0                                                                                                    ustar   root                            root                                                                                                                                                                                                                   #!/bin/sh
#
#	High-Availability Apache/IBMhttp control script
# 
# apache (aka IBMhttpd)
#
# Description:	starts/stops apache web servers.
#
# Author:	Alan Robertson
#		Sun Jiang Dong
#
# Support:	linux-ha@lists.linux-ha.org
#
# License:	GNU General Public License (GPL)
#
# Copyright:	(C) 2002-2005 International Business Machines
#
#
# An example usage in /etc/ha.d/haresources: 
#       node1  10.0.0.170 apache::/opt/IBMHTTPServer/conf/httpd.conf
#       node1  10.0.0.170 IBMhttpd
#
# Our parsing of the Apache config files is very rudimentary.
# It'll work with lots of different configurations - but not every
# possible configuration.
#
# Patches are being accepted ;-)
#
# OCF parameters:
#  OCF_RESKEY_configfile
#  OCF_RESKEY_httpd
#  OCF_RESKEY_port
#  OCF_RESKEY_statusurl
#  OCF_RESKEY_options
#  OCF_RESKEY_testregex
#  OCF_RESKEY_client
#  OCF_RESKEY_testurl
#  OCF_RESKEY_testregex10
#  OCF_RESKEY_testconffile
#  OCF_RESKEY_testname
#  OCF_RESKEY_envfiles


: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
HA_VARRUNDIR=${HA_VARRUN}

#######################################################################
#
#	Configuration options - usually you don't need to change these
#
#######################################################################
#
IBMHTTPD=/opt/IBMHTTPServer/bin/httpd
HTTPDLIST="/sbin/httpd2 /usr/sbin/httpd2 /usr/sbin/apache2 /sbin/httpd /usr/sbin/httpd /usr/sbin/apache $IBMHTTPD"
MPM=/usr/share/apache2/find_mpm
if
  [ -x $MPM ]
then
  HTTPDLIST="$HTTPDLIST `$MPM 2>/dev/null`"
fi

# default options for http clients
# NB: We _always_ test a local resource, so it should be
# safe to connect from the local interface.
WGETOPTS="-O- -q -L --no-proxy --bind-address=127.0.0.1"
CURLOPTS="-o - -Ss -L --interface lo"

LOCALHOST="http://localhost"
HTTPDOPTS="-DSTATUS"
DEFAULT_IBMCONFIG=/opt/IBMHTTPServer/conf/httpd.conf
DEFAULT_NORMCONFIG="/etc/apache2/httpd.conf"
#
# You can also set
#	HTTPD
#	PORT
#	STATUSURL
#	CONFIGFILE
# in this section if what we're doing doesn't work for you...
#
#	End of Configuration options
#######################################################################

CMD=`basename $0`

#	The config-file-pathname is the pathname to the configuration
#	file for this web server.  Various appropriate defaults are
#	assumed if no config file is specified.  If this command is
#	invoked as *IBM*, then the default config file name is
#	$DEFAULT_IBMCONFIG, otherwise the default config file
#	will be $DEFAULT_NORMCONFIG.
usage() {
  cat <<-!
usage: $0 action

action:
	start	start the web server

	stop	stop the web server

	status	return the status of web server, run or down

	monitor  return TRUE if the web server appears to be working.
                For this to be supported you must configure mod_status
		 and give it a server-status URL.  You have to have 
		installed either curl or wget for this to work.

	meta-data	show meta data message

	validate-all	validate the instance parameters
	!
  exit $1
}

#
# run the http client
#
curl_func() {
	cl_opts="$CURLOPTS $test_httpclient_opts"
	if [ x != "x$test_user" ]; then
		echo "-u $test_user:$test_password" |
			curl -K - $cl_opts "$1"
	else
		curl $cl_opts "$1"
	fi
}
wget_func() {
	auth=""
	cl_opts="$WGETOPTS $test_httpclient_opts"
	[ x != "x$test_user" ] &&
		auth="--http-user=$test_user --http-passwd=$test_password"
	wget $auth $cl_opts "$1"
}
#
# rely on whatever the user provided
userdefined() {
	$test_httpclient $test_httpclient_opts "$1"
}

#
# find a good http client
#
findhttpclient() {
	# prefer wget (for historical reasons)
	if [ "x$CLIENT" != x ]; 