Incorporate the ability to log output of safe_asterisk to syslog (closes issue #9882)
authorTilghman Lesher <tilghman@meg.abyt.es>
Thu, 6 Sep 2007 16:57:20 +0000 (16:57 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Thu, 6 Sep 2007 16:57:20 +0000 (16:57 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@81744 65c4cc65-6c06-0410-ace0-fbb531ad65f3

contrib/scripts/safe_asterisk

index ed1bc57..67bd8b4 100644 (file)
@@ -6,6 +6,8 @@ TTY=9                                           # TTY (if you want one) for Asterisk to run on
 CONSOLE=yes                                    # Whether or not you want a console
 #NOTIFY=ben@alkaloid.net       # Who to notify about crashes
 #EXEC=/path/to/somescript      # Run this command if Asterisk crashes
+#LOGFILE=/path/to/logfile      # Where to place the normal logfile (disabled if blank)
+#SYSLOG=local0                         # Which syslog facility to use (disabled if blank)
 MACHINE=`hostname`                     # To specify which machine has crashed when getting the mail
 DUMPDROP=/tmp
 SLEEPSECS=4
@@ -26,15 +28,26 @@ PRIORITY=0
 # set to the system's maximum files open devided by two, if not set here.
 # MAXFILES=32768
 
+function message() {
+       echo "$1" >&2
+       if [ "$SYSLOG" != "" ]; then
+           logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
+       fi
+       if [ "$LOGFILE" != "" ]; then
+           echo "safe_asterisk[$$]: $1" >> "$LOGFILE"
+       fi
+}
+
 # since we're going to change priority and open files limits, we need to be
 # root. if running asterisk as other users, pass that to asterisk on the command
 # line.
 # if we're not root, fall back to standard everything.
 if [ `id -u` != 0 ]
 then
-       echo "Ops. I'm not root. Falling back to standard prio and file max." >&2
+       echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
        echo "This is NOT suitable for large systems." >&2
        PRIORITY=0
+       message "safe_asterisk was started by `id -n` (uid `id -u`)."
 else
        if `echo $OSTYPE | grep linux 2>&1 > /dev/null `
        then
@@ -83,7 +96,7 @@ if [ "$TTY" != "" ]; then
        elif [ -c /dev/vc/${TTY} ]; then
                TTY=vc/${TTY}
        else
-               echo "Cannot find your TTY (${TTY})" >&2
+               message "Cannot find specified TTY (${TTY})"
                exit 1
        fi
        ASTARGS="${ASTARGS} -vvvg"
@@ -92,7 +105,7 @@ if [ "$TTY" != "" ]; then
        fi
 fi
 if [ ! -w ${DUMPDROP} ]; then  
-       echo "Cannot write to ${DUMPDROP}" >&2
+       message "Cannot write to ${DUMPDROP}"
        exit 1
 fi
 
@@ -126,10 +139,10 @@ run_asterisk()
                        nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS}
                fi
                EXITSTATUS=$?
-               echo "Asterisk ended with exit status $EXITSTATUS"
+               message "Asterisk ended with exit status $EXITSTATUS"
                if [ "$EXITSTATUS" = "0" ]; then
                        # Properly shutdown....
-                       echo "Asterisk shutdown normally."
+                       message "Asterisk shutdown normally."
                        exit 0
                elif [ $EXITSTATUS -gt 128 ]; then
                        let EXITSIGNAL=EXITSTATUS-128
@@ -137,6 +150,7 @@ run_asterisk()
                        if [ "$NOTIFY" != "" ]; then
                                echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL.  Might want to take a peek." | \
                                mail -s "Asterisk Died" $NOTIFY
+                               message "Exited on signal $EXITSIGNAL"
                        fi
                        if [ "$EXEC" != "" ]; then
                                $EXEC
@@ -149,21 +163,16 @@ run_asterisk()
                                mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
                        fi
                else
-                       if [ "${EXITSTATUS}" = "0" ]; then
-                               echo "Asterisk ended normally.  Aborting."
-                               exit 0
-                       else
-                               echo "Asterisk died with code $EXITSTATUS."
-
-                               PID=`cat ${ASTPIDFILE}`
-                               if [ -f /tmp/core.${PID} ]; then
-                                       mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
-                               elif [ -f /tmp/core ]; then
-                                       mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
-                               fi
+                       message "Asterisk died with code $EXITSTATUS."
+
+                       PID=`cat ${ASTPIDFILE}`
+                       if [ -f /tmp/core.${PID} ]; then
+                               mv /tmp/core.${PID} ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
+                       elif [ -f /tmp/core ]; then
+                               mv /tmp/core ${DUMPDROP}/core.`hostname`-`date -Iseconds` &
                        fi
                fi
-               echo "Automatically restarting Asterisk."
+               message "Automatically restarting Asterisk."
                sleep $SLEEPSECS
                if [ $KILLALLMPG123 ]
                then