#!/bin/sh # # # Asterisk # # Description: Manages an Asterisk PBX as an HA resource # # Authors: Martin Gerhard Loschwitz # Florian Haas # # Support: users@clusterlabs.org # License: GNU General Public License (GPL) # # (c) 2011 hastexo Professional Services GmbH # # This resource agent is losely derived from the MySQL resource # agent, which itself is made available to the public under the # following copyright: # # (c) 2002-2005 International Business Machines, Inc. # 2005-2010 Linux-HA contributors # # See usage() function below for more details ... # # OCF instance parameters: # OCF_RESKEY_binary # OCF_RESKEY_canary_binary # OCF_RESKEY_config # OCF_RESKEY_user # OCF_RESKEY_group # OCF_RESKEY_additional_parameters # OCF_RESKEY_realtime@ # OCF_RESKEY_maxfiles ####################################################################### # Initialization: : ${OCF_FUNCTIONS_DIR=/usr/lib/ocf/lib/heartbeat} . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs ####################################################################### # Fill in some defaults if no values are specified HOSTOS=`uname` OCF_RESKEY_user_default="root" OCF_RESKEY_group_default="root" OCF_RESKEY_binary_default="asterisk" OCF_RESKEY_canary_binary_default="astcanary" OCF_RESKEY_config_default="/etc/asterisk/asterisk.conf" OCF_RESKEY_additional_parameters_default="-g -vvv" OCF_RESKEY_realtime_default="false" OCF_RESKEY_maxfiles_default="8192" : ${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}} : ${OCF_RESKEY_canary_binary=${OCF_RESKEY_canary_binary_default}} : ${OCF_RESKEY_config=${OCF_RESKEY_config_default}} : ${OCF_RESKEY_user=${OCF_RESKEY_user_default}} : ${OCF_RESKEY_group=${OCF_RESKEY_group_default}} : ${OCF_RESKEY_additional_parameters=${OCF_RESKEY_additional_parameters_default}} : ${OCF_RESKEY_realtime=${OCF_RESKEY_realtime_default}} : ${OCF_RESKEY_maxfiles=${OCF_RESKEY_maxfiles_default}} ####################################################################### usage() { cat < 1.0 Resource agent for the Asterisk May manage an Asterisk in MOR High Availability Cluster setup Manages an Asterisk END } # Functions invoked by resource manager actions asterisk_validate() { true } asterisk_status() { if systemctl is-active --quiet asterisk; then return $OCF_SUCCESS else ocf_log info "Asterisk is not running" return $OCF_NOT_RUNNING fi } asterisk_monitor() { local rc #Check if Asterisk is running asterisk_status rc=$? # If status returned an error, return that immediately if [ $rc -ne $OCF_SUCCESS ]; then return $rc fi # Put any other monitoring logging check here: # Cheeck if not freezed, if respond to sipsak, whatever ocf_log debug "Asterisk PBX monitor succeeded" return $OCF_SUCCESS } asterisk_start() { local rc if systemctl is-active --quiet asterisk; then ocf_log info "Asterisk already running" return $OCF_SUCCESS fi systemctl start asterisk 2>/dev/null # 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 started" return $OCF_SUCCESS } asterisk_stop() { local rc asterisk_status rc=$? if [ $rc -eq $OCF_NOT_RUNNING ]; then ocf_log info "Asterisk PBX already stopped" return $OCF_SUCCESS fi systemctl stop asterisk # 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 still hasn't stopped yet. Waiting ..." done asterisk_status rc=$? if [ $rc -ne $OCF_NOT_RUNNING ]; then ocf_log info "Asterisk PBX failed to stop after ${shutdown_timeout}s. Trying SIGKILL ..." ocf_run pkill -9 asterisk 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 $? # 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