#!/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_kamctl
#  OCF_RESKEY_kamctlrc
#  OCF_RESKEY_kamuser
#  OCF_RESKEY_kamgroup
#  OCF_RESKEY_extra_options

# 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_${OCF_RESOURCE_INSTANCE}/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_kamctl_default="/usr/bin/kamctl"
RESKEY_kamctlrc_default="/etc/kamailio/kamctlrc"
RESKEY_kamuser_default=""
RESKEY_kamgroup_default=""
RESKEY_extra_options_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_kamctl=${RESKEY_kamctl_default}}
: ${OCF_RESKEY_kamctlrc=${RESKEY_kamctlrc_default}}
: ${OCF_RESKEY_kamuser=${RESKEY_kamuser_default}}
: ${OCF_RESKEY_kamgroup=${RESKEY_kamgroup_default}}
: ${OCF_RESKEY_extra_options=${RESKEY_extra_options_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=""

 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=""
</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. The directory used must be writable by kamailio
     process user. Be sure to use unique name for running more than one
     instance. Try to use absolute path names.
     If empty, resource agent create a unique directory from the resource
     instance name for the PID file and assign it to the process user.
   </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 snippet is part of an XML section.
         Therefore it contains two &amp; characters, which need to be replaced 
         with two ampersand characters within "kamailio.c                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           