#!/bin/sh
#
# portblock: iptables temporary portblocking control 
#
# Author:	Sun Jiang Dong (initial version)
#               Philipp Reisner (per-IP filtering)
#
# License:	GNU General Public License (GPL)
#
# Copyright:	(C) 2005 International Business Machines
#
#	  OCF parameters are as below:
#		OCF_RESKEY_protocol
#		OCF_RESKEY_portno
#		OCF_RESKEY_action
#		OCF_RESKEY_ip
#		OCF_RESKEY_tickle_dir
#		OCF_RESKEY_sync_script
#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Defaults
OCF_RESKEY_ip_default="0.0.0.0/0"

: ${OCF_RESKEY_ip=${OCF_RESKEY_ip_default}}
#######################################################################
CMD=`basename $0`
TICKLETCP=$HA_BIN/tickle_tcp

usage()
{
	cat <<END >&2
	usage: $CMD {start|stop|status|monitor|meta-data|validate-all}

	$CMD is used to temporarily block ports using iptables.

	It can be used to turn off a port before bringing
	up an IP address, and enable it after a service is started.
	To do that for samba, the following resource line can be used:

	$CMD::tcp::137,138::block		\\
	    10.10.10.20				\\
	    nmbd smbd 				\\
	    $CMD::tcp::137,138::unblock

	This will do the follwing things:

	  - DROP all incoming packets for TCP ports 137 and 138
	  - Bring up the IP alias 10.10.10.20
	  - start the nmbd and smbd services
	  - Re-enable TCP ports 137 and 138
	        (enable normal firewall rules on those ports)

	This prevents clients from getting ICMP port unreachable
	if they try to reconnect to the service after the alias is
	enabled but before nmbd and smbd are running.  These packets
	will cause some clients to give up attempting to reconnect to
	the server.

	NOTE:  iptables is linux-specific...

	An additional feature in the portblock RA is the tickle ACK function
	enabled by specifying the tickle_dir parameter. The tickle ACK 
	triggers the clients to faster reconnect their TCP connections to the 
	fail-overed server.

	Please note that this feature is often used for the floating IP fail-
	over scenario where the long-lived TCP connections need to be tickled.
	It doesn't support the cluster alias IP scenario.

	When using the tickle ACK function, in addition to the normal usage
	of portblock RA, the parameter tickle_dir must be specified in the 
	action=unblock instance of the portblock resources.
	For example, you may stack resources like below:
		portblock action=block
		services
		portblock action=unblock tickle_dir=/tickle/state/dir

	If you want to tickle all the TCP connections which connected to _one_
	floating IP but different ports, no matter how many portblock resources 
	you have defined, you should enable tickles for _one_ portblock 
	resource(action=unblock) only.
	
	The tickle_dir is a location which stores the established TCP 
	connections. It can be a shared directory(which is cluster-visible to 
	all nodes) or a local directory.
	If you use the shared directory, you needn't do any other things.
	If you use the local directory, you must also specify the sync_script
	paramater. We recommend you to use csync2 as the sync_script.
	For example, if you use the local directory /tmp/tickle as tickle_dir, 
	you could setup the csync2 as the csync2 documentation says and 
	configure your /etc/csync2/csync2.cfg like:
		group ticklegroup {
		  host node1;
		  host node2;
		  key  /etc/csync2/ticklegroup.key;
		  include /etc/csync2/csync2.cfg;
		  include /tmp/tickle;
		  auto younger;
		}
	Then specify the parameter sync_script as "csync2 -xv".

END
}

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

<longdesc lang="en">
Resource script for portblock. It is used to temporarily block ports 
using iptables. In addition, it may allow for faster TCP reconnects
for clients on failover. Use that if there are long lived TCP
connections to an HA service. This feature is enabled by setting the
tickle_dir parameter and only in concert with action set to unblock.
Note that the tickle ACK function is new as of version 3.0.2 and
hasn't yet seen widespread use.
</longdesc>
<shortdesc lang="en">Block and unblocks access to TCP and UDP ports</shortdesc>

<parameters>
<parameter name="protocol" unique="0" required="1">
<longdesc lang="en">
The protocol used to be blocked/unblocked.
</longdesc>
<shortdesc lang="en">protocol</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="portno" unique="0" required="1">
<longdesc lang="en">
The port number used to be blocked/unblocked.
</longdesc>
<shortdesc lang="en">portno</shortdesc>
<content type="integer" default="" />
</parameter>

<parameter name="action" unique="0" required="1">
<longdesc lang="en">
The action (block/unblock) to be done on the protocol::portno.
</longdesc>
<shortdesc lang="en">action</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="ip" unique="0" required="0">
<longdesc lang="en">
The IP address used to be blocked/unblocked.
</longdesc>
<shortdesc lang="en">ip</shortdesc>
<content type="string" default="${OCF_RESKEY_ip_default}" />
</parameter>

<parameter name="tickle_dir" unique="0" required="0">
<longdesc lang="en">
The shared or local directory (_must_ be absolute path) which 
stores the established TCP connections.
</longdesc>
<shortdesc lang="en">Tickle directory</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="sync_script" unique="0" required="0">
<longdesc lang="en">
If the tickle_dir is a local directory, then the TCP connection state
file has to be replicated to other nodes in the cluster. It can be
csync2 (default), some wrapper of rsync, or whatever. It takes the
file name as a single argument. For csync2, set it to "csync2 -xv".
</longdesc>
<shortdesc lang="en">Connection state file synchronization script</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="20" />
<action name="stop" timeout="20" />
<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
}


#
#	Because this is the normal usage, we consider "block"
#	resources to be pseudo-resources -- that is, their status can't
#	be reliably determined through external means.
#	This is because we expect an "unblock" resource to come along
#	and disable us -- but we're still in some sense active...
#

#active_grep_pat {udp|tcp} portno,portno
active_grep_pat()
{
  w="[ 	][ 	]*"
  any="0\\.0\\.0\\.0/0"
  echo "^DROP${w}${1}${w}--${w}${any}${w}${3}${w}multiport${w}dports${w}${2} "
}

#chain_isactive  {udp|tcp} portno,portno ip
chain_isactive()
{
  PAT=`active_grep_pat "$1" "$2" "$3"`
  $IPTABLES -n -L INPUT | grep "$PAT" >/dev/null
}

saen
      asterisk_extra_params="-F -p"
    else
      asterisk_extra_params="-F"
    fi

    ocf_run ${OCF_RESKEY_binary} -G $OCF_RESKEY_group -U $OCF_RESKEY_user \
                -C $OCF_RESKEY_config \
                $OCF_RESKEY_additional_parameters \
                $asterisk_extra_params
    rc=$?
    if [ $rc -ne 0 ]; then
        ocf_log err "Asterisk PBX start command failed: $rc"
        exit $OCF_ERR_GENERIC
    fi

    # Spin waiting for the server to come up.
    # Let the CRM/LRM time us out if required
    while true; do
        asterisk_monitor
        rc=$?
        [ $rc -eq $OCF_SUCCESS ] && break
        if [ $rc -ne $OCF_NOT_RUNNING ]; then
            ocf_log err "Asterisk PBX start failed"
            exit $OCF_ERR_GENERIC
        fi
        sleep 2
    done

    ocf_log info "Asterisk PBX started"
    return $OCF_SUCCESS
}

asterisk_stop() {
    local pid
    local astcanary_pid
    local rc

    asterisk_status
    rc=$?
    if [ $rc -eq $OCF_NOT_RUNNING ]; then
        ocf_log info "Asterisk PBX already stopped"
        return $OCF_SUCCESS
    fi

    # do a "soft shutdown" via the asterisk command line first
    asterisk_rx 'core stop now'

    asterisk_status
    rc=$?
    if [ $rc -eq $OCF_NOT_RUNNING ]; then
        ocf_log info "Asterisk PBX stopped"
        return $OCF_SUCCESS
    fi

    # If "core stop now" didn't succeed, try SIGTERM
    pid=`cat $ASTRUNDIR/asterisk.pid`
    ocf_run kill -s TERM $pid
    rc=$?
    if [ $rc -ne 0 ]; then
        ocf_log err "Asterisk PBX couldn't be stopped"
        exit $OCF_ERR_GENERIC
    fi

    # stop waiting
    shutdown_timeout=15
    if [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
        shutdown_timeout=$((($OCF_RESKEY_CRM_meta_timeout/1000)-5))
    fi
    count=0
    while [ $count -lt $shutdown_timeout ]; do
        asterisk_status
        rc=$?
        if [ $rc -eq $OCF_NOT_RUNNING ]; then
            break
        fi
        count=`expr $count + 1`
        sleep 1
        ocf_log debug "Asterisk PBX still hasn't stopped yet. Waiting ..."
    done

    asterisk_status
    rc=$?
    if [ $rc -ne $OCF_NOT_RUNNING ]; then
        # SIGTERM didn't help either, try SIGKILL
        ocf_log info "Asterisk PBX failed to stop after ${shutdown_timeout}s using SIGTERM. Trying SIGKILL ..."
        ocf_run kill -s KILL $pid
    fi

    # After killing asterisk, stop astcanary
    if ocf_is_true "$OCF_RESKEY_realtime"; then
      astcanary_pid=`pgrep -d " " -f "astcanary $ASTRUNDIR/alt.asterisk.canary.tweet.tweet.tweet"`
      if [ "$astcanary_pid" ]; then
        for i in $astcanary_pid; do ocf_run kill -s KILL $astcanary_pid; done
      fi
    fi

    ocf_log info "Asterisk PBX stopped"
    return $OCF_SUCCESS
}

#######################################################################

case "$1" in
  meta-data)    meta_data
                exit $OCF_SUCCESS;;
  usage|help)   usage
                exit $OCF_SUCCESS;;
esac


# Anything except meta-data and help must pass validation
asterisk_validate || exit $?

# Now that validate has passed and we can be sure to be able to read
# the config file, set convenience variables
ASTRUNDIR=`grep astrundir $OCF_RESKEY_config | awk '/^astrundir/ {print $3}'`
ASTLOGDIR=`grep astlogdir $OCF_RESKEY_config | awk '/^astlogdir/ {print $3}'`

# What kind of method was invoked?
case "$1" in
  start)        asterisk_start;;
  stop)         asterisk_stop;;
  status)       asterisk_status;;
  monitor)      asterisk_monitor;;
  validate-all) ;;
  *)            usage
                exit $OCF_ERR_UNIMPLEMENTED;;
esac
                                                                                                                                                                              ./usr/lib/ocf/resource.d/heartbeat/AudibleAlarm                                                     0000755 0000000 0000000 00000010065 12307433060 020317  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}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

#####################################################################