#!/bin/bash
#
#       OCF resource agent for Kamailio for pacemaker
#

# Copyright (c) 2013 FREQUENTIS AG,
#                    Authors: Stefan Wenk
#                             Rainer Brestan
#
# 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 input parameters:
#  OCF_RESKEY_binary
#  OCF_RESKEY_conffile
#  OCF_RESKEY_pidfile
#  OCF_RESKEY_monitoring_ip
#  OCF_RESKEY_listen_address
#  OCF_RESKEY_port
#  OCF_RESKEY_proto
#  OCF_RESKEY_sipsak
#  OCF_RESKEY_kamctlrc
#  OCF_RESKEY_kamuser

# Initialization:

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

#######################################################################
# Defaults

RESKEY_binary_default="/usr/sbin/kamailio"
RESKEY_conffile_default="/etc/kamailio/kamailio.cfg"
RESKEY_pidfile_default="/var/run/kamailio.pid"
RESKEY_monitoring_ip_default=127.0.0.1
RESKEY_port_default=5060
RESKEY_proto_default="udptcp"
RESKEY_sipsak_default="/usr/bin/sipsak"
RESKEY_kamctlrc_default="/etc/kamailio/kamctlrc"
RESKEY_kamuser_default=""

#######################################################################
: ${OCF_RESKEY_binary=${RESKEY_binary_default}}
: ${OCF_RESKEY_conffile=${RESKEY_conffile_default}}
: ${OCF_RESKEY_pidfile=${RESKEY_pidfile_default}}
: ${OCF_RESKEY_monitoring_ip=${RESKEY_monitoring_ip_default}}
: ${OCF_RESKEY_port=${RESKEY_port_default}}
: ${OCF_RESKEY_proto=${RESKEY_proto_default}}
: ${OCF_RESKEY_sipsak=${RESKEY_sipsak_default}}
: ${OCF_RESKEY_kamctlrc=${RESKEY_kamctlrc_default}}
: ${OCF_RESKEY_kamuser=${RESKEY_kamuser_default}}

#######################################################################
usage() {
	cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}
Expects to have a fully populated OCF RA-compliant environment set.
END
}

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

<longdesc lang="en">
 Resource agent for the Kamailio SIP proxy/registrar.
 Multiple instances are possible when using following parameter combinations:

 Parameters for Kamailio instance 1:
    listen_address=192.168.159.128
    monitoring_ip=192.168.159.128
    proto=udptcp
    port=5060 

 Parameters for Kamailio instance 2:
    listen_address=192.168.159.128
    monitoring_ip=192.168.159.128
    proto=udp
    port=5070
    conffile=/etc/kamailio/kamailio2.cfg
    kamctlrc=""
    pidfile="/var/run/kamailio2.pid"

 Only one instance can be monitored via the command "kamctl monitor"
 because the kamctl tool of kamailio 4.x is not designed for multiple
 instances. Therefore, the provided kamctrlrc file path needs to be 
 empty for instance 2, 3 ...

Parameters for a third Kamailio instance:
    listen_address=192.168.159.128
    monitoring_ip=192.168.159.128
    proto=tcp
    port=5080
    conffile=/etc/kamailio/kamailio3.cfg
    kamctlrc=""
    pidfile="/var/run/kamailio3.pid"
</longdesc>

<shortdesc lang="en">Resource agent for Kamailio</shortdesc>

<parameters>
  <parameter name="binary" unique="0" required="0">
   <longdesc lang="en">The kamailio binary</longdesc>
   <shortdesc lang="en">The kamailio  binary</shortdesc>
   <content type="string" default="${RESKEY_binary_default}" />
  </parameter>

  <parameter name="conffile" unique="0" required="0">
   <longdesc lang="en">
    The kamailio configuration file name with full path. 
    For example, "/etc/kamailio/kamailio.cfg" , which is the default value.
    Make sure to use unique names in case of having multiple instances.
   </longdesc>
   <shortdesc lang="en">Configuration file name with full path</shortdesc>
   <content type="string" default="${RESKEY_conffile_default}" />
  </parameter>

  <parameter name="pidfile" unique="0" required="0">
   <longdesc lang="en">
     The kamailio PID file. Make sure to use unique PID file names in case of multiple instances.
   </longdesc>
   <shortdesc lang="en">PID file</shortdesc>
   <content type="string" default="${RESKEY_pidfile_default}" />
  </parameter>

  <parameter name="monitoring_ip" unique="0" required="0">
  <longdesc lang="en">
   SIP IP Address of the kamailio instance used for SIP OPTIONS polling monitoring.
   Usually the same IP address value as for parameter listen_address should be
   provided.

   In order to respond with a 200 OK response to the SIP OOPTION requests, 
   the kamailio.cfg file needs to contain following section:
   Note: The following "kamailio.cfg" code sniplet is part of an XML section. 
         Therefore it contains two &amp; characters, which need to be replaced 
         with two ampersand characters within "kamailio.cfg":

   if (is_method("OPTIONS") &amp;&amp; ($ru=~"sip:monitor@.*")) {
        ##
        ## If the method is an OPTIONS we are simply going to respond
        ## with a 200 OK.  
        # xlog("L_INFO", "Method is an OPTIONS, probably just monitoring\n");   
        sl_send_reply("200", "Kamailio is alive");
        exit;
    }

  </longdesc>
  <shortdesc lang="en">Monitoring IP address used for SIP OPTIONS polling.</shortdesc>
   <content type="string" default="${RESKEY_monitoring_ip_default}" />
  </parameter>

  <parameter name="listen_address" unique="0" required="1">
  <longdesc lang="en">
    SIP IP address the kamailio will listen on.
  </longdesc>
  <shortdesc lang="en">Listening SIP address</shortdesc>
  <content type="string" />
  </parameter>

  <parameter name="port" unique="0" required="0">
   <longdesc lang="en">
    SIP port for the kamailio instance.
   </longdesc>
   <shortdesc lang="en">SIP Port</shortdesc>
   <content type="string" default="${RESKEY_port_default}" />
  </parameter>

  <parameter name="proto" unique="0" required="0">
   <longdesc lang="en">
    The protocol used for SIP proto  =  udp|tcp|udptcp. 
   </longdesc>
   <shortdesc lang="en">protocol</shortdesc>
   <content type="string" default="${RESKEY_proto_default}" />
  </parameter>

  <parameter name="sipsak" unique="0" required="0">
   <longdesc lang="en">
    The installation path of the sipsak tool, which is used 
    for monitoring Kamailio via SIP OPTIONS polling. 
   </longdesc>
   <shortdesc lang="en">protocol</shortdesc>
   <content type="string" default="${RESKEY_sipsak_default}" />
  </parameter>

  <parameter name="kamctlrc" unique="0" required="0">
   <longdesc lang="en">
    The location of the "kamctlrc" file for the Kamailio instance.
    The file "kamctlrc" is the Kamailio configuration file for its "kamctl" control tool.

    This parameter only needs to be provided in case of using multiple Kamailio server
    instances on a single cluster node:

    In case that the parameter "kamctlrc" is not empty, this ressource agent monitors 
    the health state of the Kamailio server via the command "kamctl monitor 1". This 
    setting is recommended in case of using a single Kamailio server instance.

    In case that the parameter "kamctlrc" is empty, the ressource agent does not 
    monitor the health state of the Kamailio server instance via the "kamctl" command.

    Please note that the "kamctl" control command of Kamailio 4.x does not support 
    running multiple Kamailio instances on one host. Nevertheless this resource agent 
    does allow multiple Kamailio instances per host. The result of the "kamctl" 
    limitation in terms of number of Kamailio server instances is that the health 
    check via "kamctl monitor 1" can be configured for a single Kamailio instance
    only.

    Please refer to the long description of this resoure agent for an example
    of parameter combinations in case that multiple instances are to be 
    configured per cluster node.

   </longdesc>
   <shortdesc lang="en">protocol</shortdesc>
   <content type="string" default="${RESKEY_kamctlrc_default}" />
  </parameter>

  <parameter name="kamuser" unique="0" required="0">
   <longdesc lang="en">
    The user account for kamailio process to run with.
    Uses the current user, if not specified or empty.
    There is no check, if running kamailio with the specified user account is possible.
   </longdesc>
   <shortdesc lang="en">kamailio user</shortdesc>
   <content type="string" default="${RESKEY_kamuser_default}" />
  </parameter>
</parameters>

 <actions>
  <action name="start" timeout="60" />
  <action name="stop" timeout="30" />
  <action name="status" timeout="30" interval="10" />
  <action name="monitor" timeout="30" interval="10" />
  <action name="meta-data" timeout="5" />
  <action name="validate-all" timeout="5" />
  <action name="notify" timeout="5" />
 </actions>
</resource-agent>
END

    exit $OCF_SUCCESS
}

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

###
#Check if a process with given PID is running
# Parameter 1: PID
###
isRunning_PID()
{
    kill -s 0 "$1" > /dev/null 2>&1
}

###
#Check if an instance with given command line is running
# Parameter 1: command line.
###
isRunning_cmd()
{
    pkill -s 0 "$1" > /dev/null 2>&1
}

###
# Formats the result of a command.
#
# Parameter 1: Exit status.
# Parameter 2: Standard output (stdout).
# Parameter 3: Error output (stderr).
#     Returns: Formatted result.
kamailio_format_result() {
    local exitstatus="$1"
    local value="$2"
    local error="$3"

    echo -n "exit status: ${exitstatus}"
    if [ -n "$value" ]; then
        echo -n ", value: ${value}"
    fi

    if [ -n "$error" ]; then
        echo -n ", error: ${error}"
    fi
    echo
}

###
# Put the command line, how the kamailio process is started according 
# to the configured parameters, into the variable "kam_cmd".
###

kamailio_cmd()
{
    case ${OCF_RESKEY_proto} in
    udp)  listen_param="-T -l udp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l udp:127.0.0.1:${OCF_RESKEY_port}"
           ;;
    tcp)  listen_param="-l tcp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l tcp:127.0.0.1:${OCF_RESKEY_port}"
           ;;
    udptcp)  listen_param1="-l udp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l udp:127.0.0.1:${OCF_RESKEY_port}"
              listen_param2="-l tcp:${OCF_RESKEY_listen_address}:${OCF_RESKEY_port} -l tcp:127.0.0.1:${OCF_RESKEY_port}"
              listen_param="${listen_param1} ${listen_param2}"
           ;;
    *)  listen_param="-T"
           ;;
    esac

    kam_cmd="${OCF_RESKEY_binary} -P ${OCF_RESKEY_pidfile} -f ${OCF_RESKEY_conffile} $listen_param"
}

###
# Gets the PID for the running Kamailio instance.
#
#     Returns: The variable $PID contains the found PID value or an empty string.
# Exit Status: Zero if the PID file was found and this process run under
#              the command line parameters of our instance.
#              1) if the PID file is not present and no process running under
#                 our command line options is active.
#              2) in all other fatal cases, which we classify in the followig 
#                 as OCF_ERR_genering. These are folloing cases:
#                a) The PID file contains a PID value which does no match to 
#                   to our instance
#                b) The PID contains a empty string in its first line
#                c) The PID file contains some text and some processeses
#                   from our instance are still active

kamailio_get_pid() {
    if [ -f ${OCF_RESKEY_pidfile} ]; then
        PID=`head -n 1 $OCF_RESKEY_pidfile`
        if [ ! -z "$PID" ]; then
            #Cross check if the PID file really contains a process of our kamailio instance:
            kamailio_cmd
            CROSSPID=`pgrep -o -f "${kam_cmd}"`
            if [ x"$PID" == x"$CROSSPID" ]; then
                #ocf_log debug "Found kamailio process PID with value: $PID."
                return 0
            fi
            #ocf_log debug "PID file does not contain a PID of a $OCF_RESKEY_binary process!"
            return 2
        fi

        #PID file does not contain a valid PID
        rm -f ${OCF_RESKEY_pidfile}
        return 2
    fi


    # No PID file found!
    # Check if still a process exists even though we don't have the PID any longer:
    kamailio_cmd
    pgrep -f "${kam_cmd}"
    if [ $? -eq 0 ]; then
        ocf_log info "PID file does not contain a valid PID, but kamailio process is still active"
        return 2
    fi

    ocf_log info "No PID file found and our kamailio instance is not active"
    return 1
}

kamailio_status() {
    local not_running_log_level="warn"
    local errorfile error output

    if [ "$__OCF_ACTION" = "start" ]; then
        not_running_log_level="debug"
    fi

    kamailio_get_pid >/dev/null
    RET=$?
    if [ $RET -ne 0 ]; then
        if [ $RET -eq 2 ]; then
            ocf_log $not_running_log_level "PID file does not contain a PID of a ${OCF_RESKEY_binary} process!"
            return $OCF_ERR_GENERIC
        fi
        return $OCF_NOT_RUNNING
    fi

    PID=`head -n 1 $OCF_RESKEY_pidfile`
    isRunning_PID "$PID"
    RET=$?
    if [ "$RET" -ne 0 ]; then
        ocf_log $not_running_log_level "PID from $PID from ${OCF_RESKEY_pidfile} not running"
        rm -f ${OCF_RESKEY_pidfile}
        return $OCF_NOT_RUNNING
    fi

    rc=0
    # In case that OCF_RESKEY_kamctlrc we perfom a health check via "kamctl monitor 1"
    if [ ! -z ${OCF_RESKEY_kamctlrc} ]; then
        # PID is running now but it is not save to check via kamctl without care, because 
        # the implementation analysis in the case that we kill all running processes
        # shows that in case that the fifo cannot be read, then kamctl blocks. This needs
        # to be avoided.
        # In order to be on the safe side, we run this check therefore under "timeout" control:
        rc=1
        timeout 3 kamctl monitor 1 |grep "Up since" ; rc=$?
    fi

    if [ $rc -ne 0 ]; then
        ocf_log $not_running_log_level "Kamailio is not up according to kamctl monitor!"
        return $OCF_NOT_RUNNING
    fi

    errorfile=`mktemp`
    case ${OCF_RESKEY_proto} in
    udp)  output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport udp>/dev/null 2>>$errorfile`
          result=$?
          ;;
    tcp)  output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport tcp>/dev/null 2>>$errorfile`
          result=$?
          ;;
    udptcp) output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport tcp>/dev/null 2>>$errorfile`
            result=$?
            if [ $result -eq 0 ]; then
                output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport udp>/dev/null 2>>$errorfile`
                result=$?
            fi
            ;;
    *)  output=`$OCF_RESKEY_sipsak -s sip:monitor@$OCF_RESKEY_monitoring_ip:${OCF_RESKEY_port} -H localhost --transport udp>/dev/null 2>>$errorfile`
        result=$?
        ;;
    esac

    error=`cat $errorfile`
    rm -f $errorfile

    if [ $result -ne 0 ]; then
        ocf_log $not_running_log_level "Kamailio is running, but not functional as sipsak ${OCF_RESKEY_proto} failed with $(kamailio_format_result $result "$output" "$error")"
        return $OCF_ERR_GENERIC
    fi

    return $OCF_SUCCESS
}

kamailio_monitor() {
    kamailio_status
}

kamailio_start() {
    local errorfile error output

    if
        kamailio_status
    then
        ocf_log info "kamailio already running."
        return $OCF_SUCCESS
    fi

    kamailio_cmd
    if [ "$OCF_RESKEY_kamuser" != "" ]; then
        kam_cmd="su -s /bin/bash $OCF_RESKEY_kamuser -c \"$kam_cmd\""
    fi

    ocf_log info "start kamailio with $kam_cmd."
    errorfile=`mktemp`
    output=$(eval ${kam_cmd} 2>>$errorfile)
    result=$?
    error=`cat $errorfile`
    rm -f $errorfile

    if [ $result -eq 0 ]; then
        result=1
        while [ $result -ne 0 ]; do
            sleep 1
            kamailio_get_pid >/dev/null
            result=$?
        done

        ocf_log info "kamailio instance PID=$PID started."
        # check with monitor operation if running correctly
        result=$OCF_ERR_GENERIC
        while [ $result -ne $OCF_SUCCESS ]; do
            sleep 1
            kamailio_monitor
            result=$?
            ocf_log info "monitor in start returned $result"
        done
        ocf_log info "kamailio started successful."

    else
        ocf_log err "kamailio instance could not be started, $(kamailio_format_result $result "$output" "$error")"
        result=$OCF_ERR_GENERIC
    fi

    return $result
}

kamailio_stop() {
    local TRIES=0
    result=$OCF_SUCCESS

    kamailio_cmd

    ocf_log info "Stopping kamailio by sending SIGTERM to ${kam_cmd}"
    pkill -SIGTERM -x -f "${kam_cmd}"
    if [ $? -eq 1 ]; then
        # already stopped. no processes found
        rm -f ${OCF_RESKEY_pidfile}
        return $result
    fi

    if [ "$OCF_RESKEY_CRM_meta_timeout" != "" ]; then
        KAMAILIO_STOP_TIMEOUT=$(( ($OCF_RESKEY_CRM_meta_timeout/1000) - 7 ))
    else
        KAMAILIO_STOP_TIMEOUT=20
    fi

    while isRunning_cmd "${kam_cmd}" && [ "$TRIES" -lt "${KAMAILIO_STOP_TIMEOUT}" ]
    do
        sleep 1
        ocf_log info "kamailio ${kam_cmd} is still running after SIGTERM"
        ((TRIES++))
    done

    isRunning_cmd "${kam_cmd}"
    RET=$?

    if [ "$RET" -eq 0 ]; then
        ocf_log info "Killing ${kam_cmd} with SIGKILL"
        TRIES=0
        pkill -SIGKILL -x -f "${kam_cmd}" > /dev/null 2>&1

        while isRunning_cmd "${kam_cmd}" && [ "$TRIES" -lt 3 ]
        do
            sleep 1
            ocf_log info "kamailio ${kam_cmd} is still running after SIGKILL"
            ((TRIES++))
        done

        isRunning_cmd "${kam_cmd}"
        RET=$?
        if [ "$RET" -eq 0 ]; then
            ocf_log fatal "kamailio is still running even after SIGKILL"
            result=$OCF_ERR_GENERIC
        fi
    else
        ocf_log info "${kam_cmd} has stopped."
    fi

    rm -f ${OCF_RESKEY_pidfile}
    return $result

}

kamailio_validate_all() {
    # Check if kamailio configuration is valid before starting the server

    if [ ! -f $OCF_RESKEY_binary ]; then
       ocf_log err "File OCF_RESKEY_binary [${OCF_RESKEY_binary}] does not exist!"
       return $OCF_NOT_INSTALLED
    fi
  
    out=$($OCF_RESKEY_binary -c 2>&1 > /dev/null)
    retcode=$?
    if [ "$retcode" -ne '0' ]; then
        ocf_log info "Not starting kamailio: $OCF_RESKEY_binary does not start!"
        return $OCF_ERR_CONFIGURED
    fi

    case $OCF_RESKEY_monitoring_ip in
    "")    ocf_log err "Required parameter OCF_RESKEY_monitoring_ip is missing!"
           return $OCF_ERR_CONFIGURED
           ;;
    [0-9]*.[0-9]*.[0-9]*.[0-9]*)     : OK
            ;;
    *)     ocf_log err "Parameter OCF_RESKEY_monitoring_ip [$OCF_RESKEY_monitoring_ip] is not an IP address!"
           return $OCF_ERR_CONFIGURED
           ;;
    esac 

    case $OCF_RESKEY_listen_address in
    "")    ocf_log err "Required parameter $OCF_RESKEY_listen_address is missing!"
           return $OCF_ERR_CONFIGURED
           ;;
    [0-9]*.[0-9]*.[0-9]*.[0-9]*)    : OK
           ;;
    *)     ocf_log err "Parameter OCF_RESKEY_listen_address [$OCF_RESKEY_listen_address] not an IP address!"
           return $OCF_ERR_CONFIGURED
           ;;
    esac 

    if [ ! -f ${OCF_RESKEY_sipsak} ]; then
        ocf_log err "sipsak [${OCF_RESKEY_sipsak}] does not exist!"
        return $OCF_NOT_INSTALLED
    fi

    if [ ! -z ${OCF_RESKEY_kamctlrc} ]; then
        if [ ! -f ${OCF_RESKEY_kamctlrc} ]; then
            ocf_log err "kamctlrc file [${kamctlrc}] does not exist!"
            return $OCF_NOT_INSTALLED
        fi
    else
        ocf_log debug "No monitoring via kamctl monitor because the parameter [kamctlrc] is empty."
    fi

    if [ ! -f ${OCF_RESKEY_conffile} ]; then
        ocf_log err "Kamailio configuration file provided in the parameter conffile [${OCF_RESKEY_conffile}] does not exist!"
        return $OCF_ERR_CONFIGURED
    fi

    case $OCF_RESKEY_proto in
    "")   ocf_log err "Parameter $OCF_RESKEY_proto is empty!"
          return $OCF_ERR_CONFIGURED
          ;;
    udp|tcp|udptcp)    : OK
        ;;
    *)   ocf_log err "Parameter value $OCF_RESKEY_proto for parameter [proto] not yet supported!"
         return $OCF_ERR_CONFIGURED
         ;;
    esac

    return $OCF_SUCCESS
}

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

case $__OCF_ACTION in
    meta-data) meta_data
        exit $OCF_SUCCESS
    ;;
    start|stop|status|monitor)
        kamailio_${__OCF_ACTION}
    ;;
    validate-all) kamailio_validate_all
    ;;
    notify) exit $OCF_SUCCESS
    ;;
    usage) usage
        exit $OCF_SUCCESS
    ;;
#   reload) #Not supported by Kamailio, but not needed by pacemaker
#   ;;
#   recover #Not needed by pacemaker
#   ;;
    *) usage
        exit $OCF_ERR_UNIMPLEMENTED
        ;;
esac

exit $?
