Since some people on the mailing lists are asking for this, I decided to release it into the public domain.
Asterisk MWI is an interesting animal. Most people don’t realize that the MWI light is turned on simply by the presence or absence of the msg.txt files in the voicemail user’s directory. Using the method I’m outlining here will allow you to keep the load light on any call servers and have the voicemail server(s) handle notification and voicemail storage. This method is also immediate, since every time a call is made, Asterisk does the processing and sending of the MWI instead of a cron job that’s run every X minutes.
The first thing you’ll need is the notify_vmail_extern.sh script:
#!/bin/bash
CONTEXT=$1
EXTEN=$2
NUMVMS=$3
SCMCMD="/usr/local/bin/notify_extern.sh $CONTEXT $EXTEN $NUMVMS"
ssh ${SERVER} "$SCMCMD" > /dev/null 2>&1 &
This script goes on the voicemail box(es). Asterisk automatically sends the three arguments to the externnotify script, so don’t worry about those, just make sure your script has them. The SCMCMD is actually the script and controls that will be run on the other servers as well. The ssh command is what actually does the notification. You can have as many of these in here as necessary to send notifications out. If your phone is registered to only one server, you can probably do some creative lookups based on the arguments (either a db lookup for location information based on extension). If the phone can be registered anywhere, you can just put a series of ssh commands in to send the command to all the servers at once.
In order for that script to work, you must have ssh keys from the user Asterisk is running under on the voicemail box to the user that Asterisk is running under on the other machines. That is outside the scope of this article.
Next, you’ll need the script that runs on the call servers that will be doing the final notification:
#!/bin/bash
CONTEXT=$1
EXTEN=$2
NUMVMS=$3
NOCMD="rm -f /var/spool/asterisk/voicemail/$CONTEXT/$EXTEN/INBOX/*"
CREATECMD="mkdir -p /var/spool/asterisk/voicemail/$CONTEXT/$EXTEN/INBOX"
if [[ $NUMVMS = "0" ]]; then
$NOCMD
else
$NOCMD
$CREATECMD
for ((count = 1; count <= $NUMVMS; count++)); do
if [ $count -lt 10 ] ; then msgcount=000$count
elif [ $count -lt 100 ] ; then msgcount=00$count
elif [ $count -lt 1000 ] ; then msgcount=0$count
fi
TOUCHCMD="touch /var/spool/asterisk/voicemail/$CONTEXT/$EXTEN/INBOX/msg$msgcount.txt"
$TOUCHCMD
done
fi
This script is pretty straightforward. All it does is take the arguments sent, parse through the number of voicemails, and dependent on how many there are, either create that number of message files, or delete them outright. This process is about as immediate as the ssh comand can get.
Finally, on the voicemail server, edit the voicemail.conf file and configure the externnotify option like this:
externnotify=/usr/local/bin/notify_vmail_extern.sh
In Asterisk, do a reload app_voicemail.so and you’re done
Watch and make sure that it’s working.
UPDATE: In response to comments below, Asterisk 1.4 switched to checking for specific lengths of the msgXXXX.txt file name, so the old script didn’t work anymore. The update in blue above makes it work with later versions of 1.4.
#1 by Jeremy - December 6th, 2006 at 13:21
Thanks for the script man. This saves us a bunch of time. If you go to Astricon or Educause let me know, lunch is on me.
#2 by Wei - March 3rd, 2010 at 21:21
In Asterisk 1.4, msg$count.txt needs to include the leading 000 (i.e. msg0000.txt), so I changed it to msg000$count.txt. The message count will be wrong if there are more than 10, though.
#3 by Aaron - March 4th, 2010 at 11:51
That’s true, somewhere in mid-1.4 the developers switched to checking for specific filename formats. I’ve updated the script and post detailing the changes required to make it work for all messages. Thanks for the catch