#!/bin/bash
#
#
#	ClusterNotify OCF RA.
#	Starts crm_mon in background which logs cluster status as
#	html to the specified file.
#
# Copyright (c) 2004 SUSE LINUX AG, Lars Marowsky-Bré                   All Rights Reserved.
# Copyright (c) 2012 Grueninger GmbH, Andreas Grueninger                    All Rights Reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
# OCF instance parameters:
#	OCF_RESKEY_user
#	OCF_RESKEY_pidfile
#	OCF_RESKEY_extra_options

################### Test
# cf-tester -v -n ClusterNotify-1 \
#    -o extra_options="-ls -m 10.50.235.1" -o pidfile=/opt/ha/var/run/test.pid \
#    clusternode/opt/ha/lib/ocf/resource.d/lgl/ClusterNotify
###################

#######################################################################
# Initialization:
: ${OCF_FUNCTIONS=${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs}
. ${OCF_FUNCTIONS}
: ${__OCF_ACTION=$1}

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

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

<longdesc lang="en">
This is a ClusterNotify Resource Agent.
It outputs current cluster status to an SNMP manager or as DBUS events.
</longdesc>
<shortdesc lang="en">Runs corosync-notifyd in the background, recording the cluster status to an SNMP manager or as DBUS events</shortdesc>

<parameters>

<parameter name="user" unique="0">
<longdesc lang="en">
The user we want to run corosync-notifyd as
</longdesc>
<shortdesc lang="en">The user we want to run corosync-notifyd as</shortdesc>
<content type="string" default="root" />
</parameter>

<parameter name="extra_options" unique="0">
<longdesc lang="en">
Additional options to pass to corosync-notifyd.  Eg. -lsm &lt;SNMP manager&gt;
</longdesc>
<shortdesc lang="en">Extra options</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="pidfile" unique="1">
<longdesc lang="en">
PID file location to ensure only one instance is running
</longdesc>
<shortdesc lang="en">PID file</shortdesc>
<content type="string" default="/tmp/ClusterNotify_${OCF_RESOURCE_INSTANCE}.pid" />
</parameter>

</parameters>

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

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

ClusterNotify_usage() {
	cat <<END
usage: $0 {start|stop|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}
ClusterNotify_killAll() {
   local pid
   pid=`pgrep -f "$1"`
   if [ "x$pid" != "x" ]; then
      kill -9 $pid 
   fi
   return $OCF_SUCCESS
}

ClusterNotify_getPID() {
   local line
   local words
   ClusterNotify_pid=-1

   ocf_log debug "line <- ps -ef -o pid,args|grep \"${ClusterNotify_cmdline}\"|grep -v grep"
   line=`ps -ef -o pid,args|grep "${ClusterNotify_cmdline}"|grep -v grep`
   if [ "$i{line}x" != "x" ]; then
      IFS=" " && words=($line)
      if [ "${words[0]}"x != ""x ]; then
         ClusterNotify_pid=${words[0]}
      fi
   fi
}

ClusterNotify_start() {
   local cmd_prefix=""
   local cmd_suffix=""
   if [ ! -z $OCF_RESKEY_user ]; then
      su - $OCF_RESKEY_user -c "${ClusterNotify_cmdline}"
   else
      ${ClusterNotify_cmdline}
   fi
   ClusterNotify_getPID
   if [ $ClusterNotify_pid = -1 ]; then
      return $OCF_ERR_GENERIC
   else
      echo $ClusterNotify_pid>$OCF_RESKEY_pidfile
      return $OCF_SUCCESS     
   fi
}

ClusterNotify_stop() {
   local pid
   if [ -f $OCF_RESKEY_pidfile ]; then
      pid=`cat $OCF_RESKEY_pidfile`
      if [ "x$pid" != "x" ]; then
          kill -s 9 $pid
          rm -f $OCF_RESKEY_pidfile
      fi
   fi
#   ClusterNotify_killAll
   return $OCF_SUCCESS   
}

ClusterNotify_monitor() {
   local pid
   if [ -f $OCF_RESKEY_pidfile ]; then
      pid=`cat $OCF_RESKEY_pidfile`
      if [ "x$pid" != "x" ]; then
         ClusterNotify_getPID
         if [ "$ClusterNotify_pid" != "$pid" ]; then
            rm -f $OCF_RESKEY_pidfile
         fi
         kill -s 0 $pid >/dev/null 2>&1; rc=$?
         case $rc in
            0) return $OCF_SUCCESS;;
            1) return $OCF_NOT_RUNNING;;
            *) return $OCF_ERR_GENERIC;;
         esac
      fi
   fi
   return $OCF_NOT_RUNNING
}

ClusterNotify_validate() {
# Existence of the user
    if [ ! -z $OCF_RESKEY_user ]; then
      getent passwd "$OCF_RESKEY_user" >/dev/null
      if [ $? -eq 0 ]; then
          : Yes, user exists. We can further check his permission on crm_mon if necessary
      else
          ocf_log err "The user $OCF_RESKEY_user does not exist!"
          return $OCF_ERR_ARGS
      fi
    fi

# Pidfile better be an absolute path
    case $OCF_RESKEY_pidfile in
      /*) ;;
      *) ocf_log warn "You should have pidfile($OCF_RESKEY_pidfile) of absolute path!" ;;
    esac
    
    echo "Validate OK"
    return $OCF_SUCCESS
}

if [ $# -ne 1 ]; then
    ClusterNotify_usage
    return $OCF_ERR_ARGS
fi

: ${OCF_RESKEY_pidfile:="/tmp/ClusterNotify_${OCF_RESOURCE_INSTANCE}.pid"}

ClusterNotify_pid=0
ClusterNotify_cmdline="${HA_SBIN_DIR}/corosync-notifyd $OCF_RESKEY_extra_options"

case $__OCF_ACTION in
   meta-data)	meta_data
			    return $OCF_SUCCESS
                ;;
   start)	ClusterNotify_start
            ;;
   stop)	ClusterNotify_stop
            ;;
   monitor)	ClusterNotify_monitor
            ;;
   validate-all)  ClusterNotify_validate
                  ;;
   usage|help)	ClusterNotify_usage
		        return $OCF_SUCCESS
                ;;
   *)		ClusterNotify_usage
	        return $OCF_ERR_UNIMPLEMENTED
			;;
esac

exit $?