File vicidial of Package vicibox-install

#!/bin/bash
# 
# ViciDial Init Script for systemd (openSUSE 15.3+ and 16.0)
#
# Author: James Pearson, feedback at www.vicidial.org in vicibox forum
#
### BEGIN INIT INFO
# Provides:          vicidial
# Required-Start:    $syslog $local_fs $network
# Should-Start:      $time mysql
# Required-Stop:     $syslog $local_fs $network
# Should-Stop:       mysql
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: ViciDial Telephony Server
# Description:
#       Starts ViciDial services and drivers that are needed to operate
#       a telephony server including DAHDI and Asterisk. Wanpipe and
#       VoiceSync are loaded if hardware is detected otherwise just
#       dahdi_dummy is loaded for a timing source.
### END INIT INFO

# Some useful global variables that determine how we start vicidial
DAHDI_MOD=dahdi_dummy
DAHDI_MOD2=dahdi # This is the fallback module to load just in case
DAHDI_EXTRA="" # Extra DAHDI modules to load seperated by a space
SLEEPSECS=5 # How long to sleep to make sure the network is up, this is ran twice
PRIORITY=-1 # Priority to run the Asterisk process with, basically slightly higher then normal
NETWKCHK=1 # 1 = run DB/Network check, anything else means don't run the check

# All our binaries and default paths, you should NOT need to modify these, but here they are
AST_BIN=/usr/sbin/asterisk
VICI_CONF=/etc/astguiclient.conf
DAHDI_CFG_BIN=/usr/sbin/dahdi_cfg
WAN_BIN=/usr/sbin/wanrouter
ASTPIDFILE=/var/run/asterisk.pid
RENICE_BIN=/usr/bin/renice
LSPCI_BIN=/sbin/lspci
SCREEN_BIN=/usr/bin/screen
IPRELAY_BIN=/usr/bin/ip_relay

# Load sysconfig stuff if found
if [ -f "/etc/sysconfig/vicidial" ]; then
	. /etc/sysconfig/vicidial
fi

# Function to provide systemd-compatible status handling
rc_status() {
    case "$1" in
        -v) echo "$2" ;;
        *) return 0 ;;
    esac
}

rc_reset() {
    return 0
}

rc_failed() {
    exit $1
}

rc_exit() {
    exit $?
}

# Make sure Asterisk is installed
if [ ! -x $AST_BIN ]; then
	echo "No $AST_BIN found! Asterisk installed?";
	if [ "$1" = "stop" ]; then
		rc_status -v
		rc_exit
	else
		rc_failed 5
		rc_exit
	fi
fi

# Make sure ViciDial has a config file
if [ ! -f $VICI_CONF ]; then
	echo "No $VICI_CONF file found! ViciDial installed?";
	if [ "$1" = "stop" ]; then
		rc_status -v
		rc_exit
	else
		rc_failed 6
		rc_exit
	fi
fi

# Make sure ViciDial is installed
VICI_DIR=`cat $VICI_CONF | grep PATHhome | awk '{print $3}'`
if [ ! -x $VICI_DIR"/start_asterisk_boot.pl" ]; then
	echo "No $VICI_DIR/start_asterisk_boot.pl found! ViciDial installed?";
	if [ "$1" = "stop" ]; then
		rc_status -v
		rc_exit
	else
		rc_failed 5
		rc_exit
	fi
fi

# Make sure dahdi is installed
if [ ! -x $DAHDI_CFG_BIN ]; then
	echo "No $DAHDI_CFG_BIN found! DAHDI installed?";
	if [ "$1" = "stop" ]; then
		rc_status -v
		rc_exit
	else
		rc_failed 5
		rc_exit
	fi
fi

# Make sure Screen is installed
if [ ! -x $SCREEN_BIN ]; then
	echo "No $SCREEN_BIN found! Screen installed?";
	if [ "$1" = "stop" ]; then
		rc_status -v
		rc_exit
	else
		rc_failed 5
		rc_exit
	fi
fi

# Search for an Amfletec timer on the PCI bus, and load if we find it
#if [ -x $LSPCI_BIN ]; then
#	if [[ "$($LSPCI_BIN | /usr/bin/grep Exar)" ]]; then
#		DAHDI_EXTRA=voicesync
#	fi
#fi

# Determine asterisk version from installed binary
# so we know the commands to use for starting and stopping it
RAWASTVER=`$AST_BIN -V`
RAWASTARY=($RAWASTVER)
ASTVERSION=${RAWASTARY[1]}
if [[ $ASTVERSION =~ ^18 ]]; then
        ASTBRANCH="18"
        ASTUPTIMECMD="core show uptime"
        ASTSTOPCMD="core stop now"
fi
if [[ $ASTVERSION =~ ^16 ]]; then
	ASTBRANCH="16"
	ASTUPTIMECMD="core show uptime"
	ASTSTOPCMD="core stop now"
fi
if [[ $ASTVERSION =~ ^13 ]]; then
	ASTBRANCH="13"
	ASTUPTIMECMD="core show uptime"
	ASTSTOPCMD="core stop now"
fi

if [[ $ASTVERSION =~ ^11 ]]; then
	ASTBRANCH="11"
	ASTUPTIMECMD="core show uptime"
	ASTSTOPCMD="core stop now"
fi
if [[ $ASTVERSION =~ ^1.8 ]]; then
	ASTBRANCH="1.8"
	ASTUPTIMECMD="core show uptime"
	ASTSTOPCMD="core stop now"
fi
if [[ $ASTVERSION =~ ^1.4 ]]; then
	ASTBRANCH="1.4"
	ASTUPTIMECMD="show uptime"
	ASTSTOPCMD="stop now"
fi

case "$1" in
start)
	echo "Starting ViciDial Telephony Server... "
	echo "" > /var/log/vicidial.log
	# Make sure Asterisk isn't already running
	# Use the process list incase it crashed and was restarted
	ASTERISK_PS=`/usr/bin/ps ax | /usr/bin/grep asterisk | /usr/bin/grep -v grep | /usr/bin/grep -v SCREEN`
	if [[ $ASTERISK_PS ]]; then
		ASTERISK_PID=($ASTERISK_PS)
		echo "   Asterisk already running at PID $ASTERISK_PID! Aborting startup."
		rc_status -v
		rc_exit
	fi
	
	# Load DAHDI, our drivers, and other madness
	if [[ "$(/sbin/lsmod | /usr/bin/grep dahdi)" ]]; then
		echo "   DAHDI already loaded, skipping."
	else
		if [ -x $WAN_BIN -a "$(find /etc/wanpipe -maxdepth 1 -name 'wanpipea*.conf' -print -quit)" ]; then
			echo -n "   Sangoma wanrouter config found, loading... "
			$WAN_BIN start 2>>/var/log/vicidial.log 1>&2
			if [ ! /sbin/lsmod | /usr/bin/grep wanpipe ]; then
				echo "failed."
				echo -n "   Fallback to $DAHDI_MOD module... "
				/sbin/modprobe $DAHDI_MOD 2>>/var/log/vicidial.log 1>&2
				if [ ! "$(/sbin/lsmod | /usr/bin/grep $DAHDI_MOD)" ]; then
					echo "failed."
					echo -n "   Fallback to $DAHDI_MOD2 module... "
					/sbin/modprobe $DAHDI_MOD2 2>>/var/log/vicidial.log 1>&2
					if [ ! "$(/sbin/lsmod | /usr/bin/grep $DAHDI_MOD2)" ]; then
						echo "final failure! Aborting startup."
						rc_failed 5
						rc_exit
					else
						echo "done."
					fi
				fi
			fi
			echo "done."
		else
			echo -n "   Loading $DAHDI_MOD module... "
			/sbin/modprobe $DAHDI_MOD 2>>/var/log/vicidial.log 1>&2
			if [ ! "$(/sbin/lsmod | /usr/bin/grep $DAHDI_MOD)" ]; then
				echo "failed."
				echo -n "   Fallback to $DAHDI_MOD2 module... "
				/sbin/modprobe $DAHDI_MOD2 2>>/var/log/vicidial.log 1>&2
				if [ ! "$(/sbin/lsmod | /usr/bin/grep $DAHDI_MOD2)" ]; then
					echo "final failure! Aborting startup."
					rc_failed 5
					rc_exit
				else
					echo "done."
				fi
			else
				echo "done."
			fi
		fi
	fi
	
	# Load our extra DAHDI modules since everything else loaded
	if [ -n "$DAHDI_EXTRA" ]; then
		echo -n "   Loading Extra DAHDI modules: "
		for EXTRA_MOD in $DAHDI_EXTRA
		do
			/sbin/modprobe $EXTRA_MOD 2>>/var/log/vicidial.log 1>&2
			echo -n "$EXTRA_MOD "
		done
		echo ", done."
	fi
	
	# Here we sleep cause hardware sometimes takes time to initialize
	# Especially on hardware with onboard echo cancellation
	echo -n "   Initializing DAHDI Hardware... "
	sleep $SLEEPSECS
	$DAHDI_CFG_BIN -vvv 2>>/var/log/vicidial.log 1>&2
	echo "done."
	
	# This is a network/database connectivity check. Sometimes when the network
	# initializes it will appear up to the OS but the switch has it in an STP/LACP
	# learning state for a bit. This checks to see if we can ping the database
	# server before attempting to actually start asterisk. Also a good sanity check
	I="0"
	DBSERVERIP=`cat $VICI_CONF | grep VARDB_server | awk '{print $3}'`
	echo -n "   Pinging database... "
	while [ $I -lt 4 ]; do
		I=$[$I+1]
		if [ $I -eq 4 ]; then
			echo "   Database server unreachable! Aborting startup."
			rc_failed 5
			rc_exit
		fi
		ping -c1 $DBSERVERIP 2>>/var/log/vicidial.log 1>&2
		if [ $? -ne 0 ]; then
			echo "   DB unreachable at $DBSERVERIP, waiting $SLEEPSECS seconds (attempt $I)"
			sleep $SLEEPSECS
		else
			I="4"
			echo "is reachable, done."
		fi
	done
	
	# Do some house keeping before starting, mainly flush old running stats
	echo -n "   Resetting vars and rolling logs... "
	$VICI_DIR/ADMIN_restart_roll_logs.pl 2>>/var/log/vicidial.log 1>&2
	if [ $? -ne 0 ]; then
		echo "Failed! Check /var/log/vicidial.log"
		rc_failed 5
		rc_exit
	fi
	$VICI_DIR/AST_reset_mysql_vars.pl 2>>/var/log/vicidial.log 1>&2
	if [ $? -ne 0 ]; then
		echo "Failed! Check /var/log/vicidial.log"
		rc_failed 5
		rc_exit
	fi
	echo "done."
	
	# And now we start asterisk
	echo -n "   Starting Asterisk... "
	$VICI_DIR/start_asterisk_boot.pl 2>>/var/log/vicidial.log 1>&2
	if [ $? -ne 0 ]; then
		echo "Failed! Check /var/log/vicidial.log"
		rc_failed 5
		rc_exit
	fi
	ASTERISK_PS=`ps ax | grep asterisk | grep -v grep | grep -v SCREEN`
	if [[ ! $ASTERISK_PS ]]; then
		echo "Failed! Check /var/log/vicidial.log"
		rc_failed 5
		rc_exit
	else
		ASTERISK_PID=($ASTERISK_PS)
		echo "PID $ASTERISK_PID, done."
		echo $ASTERISK_PID > $ASTPIDFILE
	fi
	
	# Change priority of the asterisk process to -3 so that things like
	# the recording process doesn't rob Asterisk of CPU resources
	if [ -x $RENICE_BIN -a "$PRIORITY" -ne "0" ]; then
		echo -n "   Giving asterisk $PRIORITY process priority... "
		$RENICE_BIN -n $PRIORITY -p $ASTERISK_PID 2>>/var/log/vicidial.log 1>&2
		echo "done."
	fi
	
	# If we have a distribution specific ip_relay, then use that instead
	if [ -x $IPRELAY_BIN ]; then
		echo -n "   Starting ip_relay with OS specific version... "
		if [[ `ps -C ip_relay -o pid --no-heading` ]]; then
			killall ip_relay 2>>/var/log/vicidial.log 1>&2
		fi
		exec -a ip_relay $IPRELAY_BIN 40569 127.0.0.1 4569 9999999 &>/dev/null &
		exec -a ip_relay $IPRELAY_BIN 41569 127.0.0.1 4569 9999999 &>/dev/null &
		exec -a ip_relay $IPRELAY_BIN 42569 127.0.0.1 4569 9999999 &>/dev/null &
		echo "done."
	fi
	
	# Remember status and be verbose
	echo "ViciDial Telephony Server Started. Log at /var/log/vicidial.log"
	rc_status -v
	;;
stop)
	echo "Stopping ViciDial Telephony Server... "
	ASTERISK_PS=`/usr/bin/ps ax | /usr/bin/grep asterisk | /usr/bin/grep -v grep | /usr/bin/grep -v SCREEN`
	if [[ ! $ASTERISK_PS ]]; then
		echo "   Asterisk not running, skipping."
		# clean up screen just in case
	else
		echo -n "   Stopping Asterisk process... "
		$AST_BIN -rx "$ASTSTOPCMD" 2>>/var/log/vicidial.log 1>&2
		ASTERISK_PS=`/usr/bin/ps ax | /usr/bin/grep asterisk | /usr/bin/grep -v grep | /usr/bin/grep -v SCREEN`
		if [[ $ASTERISK_PS ]]; then
			# Since asterisk didn't kill itself, we kill it from the kernel instead
			ASTERISK_PID=($ASTERISK_PS)
			/usr/bin/kill -9 $ASTERISK_PID
		fi
		rm $ASTPIDFILE 2>>/var/log/vicidial.log 1>&2
		echo "done."
	fi
	
	# Kill off the screen processes
	SCREENPID1=`ps -ef | grep asterisk | grep -v grep | grep SCREEN | awk '{print $2}'`
	SCREENPID2=`ps -ef | grep astshell | grep -v grep | grep SCREEN | awk '{print $2}'`
	if [ -n "$SCREENPID1" -o -n "$SCREENPID2" ]; then
		echo -n "   Stopping asterisk screens... "
		/usr/bin/kill -9 $SCREENPID1  2>>/var/log/vicidial.log 1>&2  # First try it gracefully
		/usr/bin/kill -9 $SCREENPID2  2>>/var/log/vicidial.log 1>&2  # Same here
		/usr/bin/killall $SCREEN_BIN 2>>/var/log/vicidial.log 1>&2  # Gave up on being graceful, just kill 'em all!
		$SCREEN_BIN -wipe  2>>/var/log/vicidial.log 1>&2
		echo "done."
	else
		echo "   No asterisk screens found, skipping."
	fi
	
	# Kill of ip_relay
	IPRELAY_PS=`ps -C ip_relay -o pid --no-heading`
	if [[ $IPRELAY_PS ]]; then
		echo -n "   Stopping ip_relay processes... "
		kill $IPRELAY_PS 2>>/var/log/vicidial.log 1>&2
		echo "done."
	else
		echo "   No ip_relay processes found, skipping."
	fi
	
	# Unload wanrouter, DAHDI, etc
	if [ "$(/sbin/lsmod | /usr/bin/grep wanrouter)" ]; then
		echo -n "   Unloading wanrouter... "
		$WAN_BIN stop 2>>/var/log/vicidial.log 1>&2
		echo "done."
	fi
	if [ "$(/sbin/lsmod | /usr/bin/grep dahdi)" ]; then
		echo -n "   Unloading DAHDI and modules... "
		$DAHDI_CFG_BIN -s -vvv 2>>/var/log/vicidial.log 1>&2
		if [ "$(/sbin/lsmod | /usr/bin/grep $DAHDI_MOD)" ]; then
			/sbin/rmmod $DAHDI_MOD 2>>/var/log/vicidial.log 1>&2
		fi
		if [ "$(/sbin/lsmod | /usr/bin/grep $DAHDI_MOD2)" ]; then
			/sbin/rmmod $DAHDI_MOD2 2>>/var/log/vicidial.log 1>&2
		fi
		if [ -n "$EXTRA_MOD" ]; then
			for EXTRA_MOD in $DAHDI_EXTRA
			do
				/sbin/rmmod $EXTRA_MOD 2>>/var/log/vicidial.log 1>&2
			done
		fi
		if [ "$(/sbin/lsmod | /usr/bin/grep dahdi)" ]; then
			/sbin/rmmod dahdi  2>>/var/log/vicidial.log 1>&2
		fi
		echo "done."
	else
		echo "   DAHDI and modules not loaded, skipping."
	fi
	
	# Remember status and be verbose
	echo "ViciDial Telephony server stopped. Log at /var/log/vicidial.log"
	rc_status -v
	;;
	try-restart|condrestart)
	## Do a restart only if the service was active before.
	## Note: try-restart is now part of LSB (as of 1.9).
	## RH has a similar command named condrestart.
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	# Remember status and be quiet
	rc_status
	;;
restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
status)
    echo -n "Checking status for ViciDial Telephony Server... "
	# Return value is slightly different for the status command:
	# 0 - service up and running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running (unused)
	# 4 - service status unknown :-(
	# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
	
	# clean up just in case
	$SCREEN_BIN -wipe  2>>/var/log/vicidial.log 1>&2
	
	# Check for our stuff
	ASTERISK_PS=`/usr/bin/ps ax | /usr/bin/grep asterisk | /usr/bin/grep -v grep | /usr/bin/grep -v SCREEN`
	if [[ ! $ASTERISK_PS ]]; then
		echo "   Asterisk not running..."
		rc_failed 3
		rc_exit
	else
		ASTERISK_PID=($ASTERISK_PS)
		ASTERISK_UP=$AST_BIN" -rx "$ASTUPTIMECMD" | /usr/bin/grep System"
		SCREENPID1=`/usr/bin/ps -ef | /usr/bin/grep asterisk | /usr/bin/grep -v grep | /usr/bin/grep SCREEN | /usr/bin/awk '{print $2}'`
		SCREENPID2=`/usr/bin/ps -ef | /usr/bin/grep astshell | /usr/bin/grep -v grep | /usr/bin/grep SCREEN | /usr/bin/awk '{print $2}'`
		echo "   Asterisk has been up for $ASTERISK_UP"
		echo "   Asterisk running on PID $ASTERISK_PID"
		echo "   Asterisk Screen running on PID $SCREENPID1"
		echo "   Astshell Screen running on PID $SCREENPID2"
	fi
	
	rc_status -v[5]
	;;
probe)
	## Optional: Probe for the necessity of a reload, print out the
	## argument to this init script which is required for a reload.
	## Note: probe is not (yet) part of LSB (as of 1.9)

	test /etc/FOO/FOO.conf -nt /var/run/FOO.pid && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
openSUSE Build Service is sponsored by