From 81794be272ff12605479bc6b51bdeba8be42bcf3 Mon Sep 17 00:00:00 2001 From: Wymanrag Date: Mon, 7 Sep 2020 16:07:30 +0100 Subject: [PATCH 1/3] Update kamailio.in --- heartbeat/kamailio.in | 245 ++++++++++++++++++++++++++++++------------ 1 file changed, 178 insertions(+), 67 deletions(-) diff --git a/heartbeat/kamailio.in b/heartbeat/kamailio.in index 8e4ffc9d5a..f4c2e6b5e1 100644 --- a/heartbeat/kamailio.in +++ b/heartbeat/kamailio.in @@ -1,4 +1,4 @@ -#!@BASH_SHELL@ +#@BASH_SHELL@ # # OCF resource agent for Kamailio for pacemaker # @@ -41,6 +41,11 @@ # OCF_RESKEY_kamuser # OCF_RESKEY_kamgroup # OCF_RESKEY_extra_options +# OCF_RESKEY_systemd +# OCF_RESKEY_register +# OCF_RESKEY_register_extension +# OCF_RESKEY_register_ext_pwd +# OCF_RESKEY_register_hostname # Initialization: @@ -62,6 +67,11 @@ RESKEY_kamctlrc_default="/etc/kamailio/kamctlrc" RESKEY_kamuser_default="" RESKEY_kamgroup_default="" RESKEY_extra_options_default="" +RESKEY_systemd_default=false +RESKEY_register_default=false +RESKEY_register_extension_default="100" +RESKEY_register_ext_pwd_default="" +RESKEY_register_hostname_default="127.0.0.1" ####################################################################### : ${OCF_RESKEY_binary=${RESKEY_binary_default}} @@ -76,6 +86,12 @@ RESKEY_extra_options_default="" : ${OCF_RESKEY_kamuser=${RESKEY_kamuser_default}} : ${OCF_RESKEY_kamgroup=${RESKEY_kamgroup_default}} : ${OCF_RESKEY_extra_options=${RESKEY_extra_options_default}} +: ${OCF_RESKEY_systemd=${RESKEY_systemd_default}} +: ${OCF_RESKEY_register=${RESKEY_register_default}} +: ${OCF_RESKEY_register_extension=${RESKEY_register_extension_default}} +: ${OCF_RESKEY_register_ext_pwd=${RESKEY_register_ext_pwd_default}} +: ${OCF_RESKEY_register_hostname=${RESKEY_register_hostname_default}} + ####################################################################### usage() { @@ -161,9 +177,9 @@ Parameters for a third Kamailio instance: 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, + In order to respond with a 200 OK response to the SIP OPTION requests, the kamailio.cfg file needs to contain following section: - Note: The following "kamailio.cfg" code snippet is part of an XML section. + Note: The following "kamailio.cfg" code snippet is part of an XML section. Therefore it contains two & characters, which need to be replaced with two ampersand characters within "kamailio.cfg": @@ -181,7 +197,7 @@ Parameters for a third Kamailio instance: - + SIP IP address the kamailio will listen on. @@ -234,6 +250,19 @@ Parameters for a third Kamailio instance: + + + If systemd is used for management of the Kamailio process, its possible to keep it, + setting systemd=true. + Consider that in this case, all other parameters are overridden and should be defined + inside Kamailio systemd definition, with exception of pidfile and monitoring_ip + are used for monitoring and may be defined. + + systemd management + + + + The location of the "kamctlrc" file for the Kamailio instance. @@ -283,6 +312,41 @@ Parameters for a third Kamailio instance: kamailio group + + + + Monitor if kamailio can register an extension. In some cases, kamailio responds to options + but is not possible to register an extension. Uses UDP to send the register message. + If enabled, use register_extension, register_hostname and register_ext_pwd (optional) to set + the register parameters. + + enable extension register + + + + + + The extension to use in the registration monitoring. + + register extension + + + + + + The extension's password to use in the registration monitoring. + + register extension password + + + + + + Sip-uri hostname. Example: domain.example.com. + + register hostname + + @@ -401,20 +465,27 @@ kamailio_cmd() 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!" + + if [ -z "$PID" ]; then + #PID file does not contain a valid PID + rm -f ${OCF_RESKEY_pidfile} return 2 fi - #PID file does not contain a valid PID - rm -f ${OCF_RESKEY_pidfile} + # if systemd is defined, one can not validate our own kamailio instance + if [ $OCF_RESKEY_systemd == true ]; then + #ocf_log debug "Found kamailio process PID with value: $PID." + return 0 + fi + + #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 @@ -431,6 +502,27 @@ kamailio_get_pid() { return 1 } +kamailio_monitor_register() { + # Checks if it is possible to register an extension. + + # bulid sipsak usrloc-mode string. + reg_forced_timeout="timeout 5" + reg_sipsak_opts="-U -d -x 1200" + reg_sip_uri="-s sip:${OCF_RESKEY_register_extension}@${OCF_RESKEY_register_hostname}" + [[ -z ${OCF_RESKEY_register_ext_pwd} ]] && reg_ext_pwd="" || reg_ext_pwd="-a ${OCF_RESKEY_register_ext_pwd}" + + output=`${reg_forced_timeout} $OCF_RESKEY_sipsak ${reg_sipsak_opts} ${reg_sip_uri} ${reg_ext_pwd}>/dev/null 2>>$errorfile` + result=$? + + 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 REGISTER ${OCF_RESKEY_proto} failed with $(kamailio_format_result $result "$output" "$error")" + return $OCF_ERR_GENERIC + fi +} + kamailio_status() { local not_running_log_level="warn" local errorfile error output @@ -459,7 +551,7 @@ kamailio_status() { fi rc=0 - # In case that OCF_RESKEY_kamctlrc we perfom a health check via "kamctl monitor 1" + # In case that OCF_RESKEY_kamctlrc we perform 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 @@ -499,10 +591,14 @@ kamailio_status() { 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")" + ocf_log $not_running_log_level "Kamailio is running, but not functional as sipsak OPTIONS ${OCF_RESKEY_proto} failed with $(kamailio_format_result $result "$output" "$error")" return $OCF_ERR_GENERIC fi + if [ ${OCF_RESKEY_register} == true ]; then + kamailio_monitor_register + fi + return $OCF_SUCCESS } @@ -529,17 +625,25 @@ kamailio_start() { fi fi - kamailio_cmd - if [ "$OCF_RESKEY_kamuser" != "" ]; then - kam_cmd="su -s @BASH_SHELL@ $OCF_RESKEY_kamuser -c \"$kam_cmd\"" - fi + if + $OCF_RESKEY_systemd + then + ocf_log info "start kamailio with systemd." + systemctl start kamailio + result=$? + else + kamailio_cmd + if [ "$OCF_RESKEY_kamuser" != "" ]; then + kam_cmd="su -s @BASH_SHELL@ $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 + ocf_log info "start kamailio with $kam_cmd." + errorfile=`mktemp` + output=$(eval ${kam_cmd} 2>>$errorfile) + result=$? + error=`cat $errorfile` + rm -f $errorfile + fi if [ $result -eq 0 ]; then result=1 @@ -573,59 +677,66 @@ kamailio_stop() { local TRIES=0 result=$OCF_SUCCESS - kamailio_cmd + if + $OCF_RESKEY_systemd + then + ocf_log info "stop kamailio with systemd." + systemctl stop kamailio + else + 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 + # in case of not specified pidfile, delete the created directory + # otherwise only the pidfile itself + if [ "${OCF_RESKEY_pidfile}" == "${RESKEY_pidfile_default}" ]; then + piddir=`dirname "${OCF_RESKEY_pidfile}"` + rm -rf "$piddir" + else + rm -f "${OCF_RESKEY_pidfile}" + fi + return $result + fi - 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 - # in case of not specified pidfile, delete the created directory - # otherwise only the pidfile itself - if [ "${OCF_RESKEY_pidfile}" == "${RESKEY_pidfile_default}" ]; then - piddir=`dirname "${OCF_RESKEY_pidfile}"` - rm -rf "$piddir" + if [ "$OCF_RESKEY_CRM_meta_timeout" != "" ]; then + KAMAILIO_STOP_TIMEOUT=$(( ($OCF_RESKEY_CRM_meta_timeout/1000) - 7 )) else - rm -f "${OCF_RESKEY_pidfile}" + KAMAILIO_STOP_TIMEOUT=20 fi - 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 ] + while isRunning_cmd "${kam_cmd}" && [ "$TRIES" -lt "${KAMAILIO_STOP_TIMEOUT}" ] do sleep 1 - ocf_log info "kamailio ${kam_cmd} is still running after SIGKILL" + 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 fatal "kamailio is still running even after SIGKILL" - result=$OCF_ERR_GENERIC + 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 - else - ocf_log info "${kam_cmd} has stopped." fi # in case of not specified pidfile, delete the created directory From d2d606207988976497d7a4756563fe357079d8fe Mon Sep 17 00:00:00 2001 From: Wymanrag Date: Mon, 7 Sep 2020 16:10:55 +0100 Subject: [PATCH 2/3] Fix typo --- heartbeat/kamailio.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/heartbeat/kamailio.in b/heartbeat/kamailio.in index f4c2e6b5e1..089e4af318 100644 --- a/heartbeat/kamailio.in +++ b/heartbeat/kamailio.in @@ -1,4 +1,4 @@ -#@BASH_SHELL@ +#!@BASH_SHELL@ # # OCF resource agent for Kamailio for pacemaker # From a4a0565fe52a0679933cdacb184074e737fe236d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira=20Rodrigues?= Date: Tue, 22 Sep 2020 15:46:07 +0100 Subject: [PATCH 3/3] Add multiple register extension and/or interfaces --- heartbeat/kamailio.in | 99 +++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 55 deletions(-) diff --git a/heartbeat/kamailio.in b/heartbeat/kamailio.in index 089e4af318..ca4b47d8ac 100644 --- a/heartbeat/kamailio.in +++ b/heartbeat/kamailio.in @@ -43,9 +43,7 @@ # OCF_RESKEY_extra_options # OCF_RESKEY_systemd # OCF_RESKEY_register -# OCF_RESKEY_register_extension -# OCF_RESKEY_register_ext_pwd -# OCF_RESKEY_register_hostname +# OCF_RESKEY_register_array # Initialization: @@ -69,9 +67,7 @@ RESKEY_kamgroup_default="" RESKEY_extra_options_default="" RESKEY_systemd_default=false RESKEY_register_default=false -RESKEY_register_extension_default="100" -RESKEY_register_ext_pwd_default="" -RESKEY_register_hostname_default="127.0.0.1" +RESKEY_register_array_default="127.0.0.1;100;test.domain.com;sip1234" ####################################################################### : ${OCF_RESKEY_binary=${RESKEY_binary_default}} @@ -88,10 +84,7 @@ RESKEY_register_hostname_default="127.0.0.1" : ${OCF_RESKEY_extra_options=${RESKEY_extra_options_default}} : ${OCF_RESKEY_systemd=${RESKEY_systemd_default}} : ${OCF_RESKEY_register=${RESKEY_register_default}} -: ${OCF_RESKEY_register_extension=${RESKEY_register_extension_default}} -: ${OCF_RESKEY_register_ext_pwd=${RESKEY_register_ext_pwd_default}} -: ${OCF_RESKEY_register_hostname=${RESKEY_register_hostname_default}} - +: ${OCF_RESKEY_register_array=${RESKEY_register_array_default}} ####################################################################### usage() { @@ -317,35 +310,26 @@ Parameters for a third Kamailio instance: Monitor if kamailio can register an extension. In some cases, kamailio responds to options but is not possible to register an extension. Uses UDP to send the register message. - If enabled, use register_extension, register_hostname and register_ext_pwd (optional) to set - the register parameters. + If enabled, use register_array to set the register parameters. enable extension register - + - The extension to use in the registration monitoring. + The extension parameters to use in the registration monitoring. It is possible to set multiple + extensions to test. + The extensions must be separated by a space (" "), each extension + containing the following parameters, separated by a semicolon: + Socket IP: The kamailio interface to send the register to (Ex:127.0.0.1); + Extension: The extension; + Domain: The domain to register the extension; + Extension password: The password of the extension; + Ex: "127.0.0.1;1000;test.domain.com;sip1234 192.168.0.1;2000;test.domain.com;sip5678" register extension - - - - - - The extension's password to use in the registration monitoring. - - register extension password - - - - - - Sip-uri hostname. Example: domain.example.com. - - register hostname - + @@ -502,27 +486,6 @@ kamailio_get_pid() { return 1 } -kamailio_monitor_register() { - # Checks if it is possible to register an extension. - - # bulid sipsak usrloc-mode string. - reg_forced_timeout="timeout 5" - reg_sipsak_opts="-U -d -x 1200" - reg_sip_uri="-s sip:${OCF_RESKEY_register_extension}@${OCF_RESKEY_register_hostname}" - [[ -z ${OCF_RESKEY_register_ext_pwd} ]] && reg_ext_pwd="" || reg_ext_pwd="-a ${OCF_RESKEY_register_ext_pwd}" - - output=`${reg_forced_timeout} $OCF_RESKEY_sipsak ${reg_sipsak_opts} ${reg_sip_uri} ${reg_ext_pwd}>/dev/null 2>>$errorfile` - result=$? - - 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 REGISTER ${OCF_RESKEY_proto} failed with $(kamailio_format_result $result "$output" "$error")" - return $OCF_ERR_GENERIC - fi -} - kamailio_status() { local not_running_log_level="warn" local errorfile error output @@ -594,10 +557,36 @@ kamailio_status() { ocf_log $not_running_log_level "Kamailio is running, but not functional as sipsak OPTIONS ${OCF_RESKEY_proto} failed with $(kamailio_format_result $result "$output" "$error")" return $OCF_ERR_GENERIC fi + + # Checks if it is possible to register an extension. + if [ ${OCF_RESKEY_register} == true ]; then + # Iterate on the extensions array + for extension in ${OCF_RESKEY_register_array[@]}; do + # Split the extension parameters into an array: + # Ex: "127.0.0.1;100;test.domain.com;sip1234" -> ("127.0.0.1" "100" "test.domain.com" "sip1234") + IFS=';' read -ra EXTADDR <<< "$extension" + reg_hostname=${EXTADDR[0]} + reg_extension=${EXTADDR[1]} + reg_domain=${EXTADDR[2]} + [[ -z ${EXTADDR[3]} ]] && reg_password="" || reg_password="-a ${EXTADDR[3]}" + + # Builds sipsak usrloc-mode string. + reg_forced_timeout="timeout 3" + reg_sipsak_opts="-U -d -x 5 -p $reg_hostname -H $reg_hostname" + reg_sip_uri="-s sip:$reg_extension@$reg_domain" + output=`${reg_forced_timeout} $OCF_RESKEY_sipsak ${reg_sipsak_opts} ${reg_sip_uri} ${reg_password}>/dev/null 2>>$errorfile` + result=$? - if [ ${OCF_RESKEY_register} == true ]; then - kamailio_monitor_register - fi + error=`cat $errorfile` + rm -f $errorfile + # If cannot register the extension, raise an OCF_ERR + if [ $result -ne 0 ]; then + ocf_log $not_running_log_level "Kamailio is running, but not functional as sipsak REGISTER ${OCF_RESKEY_proto} failed with $(kamailio_format_result $result "$output" "$error")" + return $OCF_ERR_GENERIC + fi + echo "RESULT: $result" + done + fi return $OCF_SUCCESS }