Just thought I'd post up a sample script I've been using to automatically notify my technicians of who's on-call. We use a dedicated ring-group with their external number and "confirm call" enabled to connect our customers to our on call technicians. We've had problems in the past where the techs weren't told they were on-call (which obviously can be a problem). So here's the script... it could use some refinement (and I'd love some feedback on causing an "apply changes" via command line to really make it complete.
The Script:
#!/bin/bash
#Author: Chris Vanderbles
#Filename: on-call-notify.sh
#Date: 09-30-2009
#Description: Script that will extract a phone number from a Ring-Group
# and compare to previous lookup to determine if who is
# on call has changed. After a successful lookup, the script
# will attempt to lookup the phone number in the asterisk
# phonebook database. This makes managing the name/number
# association much easier for end users. Run this script as
# a cron-job (I use every 5 minutes) so my crontab entry
# looks like this:
# */5 * * * * /root/on-call-notify.sh
# Make sure you make this script executable (chmod +x)
#Define Your Variables here
ringgroup="601" #Ring Group to query for single on-call number
mysqluser="root" #Username to connect to the mysql database
mysqlpass="passw0rd" #Passwork to connecte to the mysql database
defaultname="Unknown Name" #Name to assign to phone number if phonebook lookup fails
defaultnum="Unknown Number" #Number to show in e-mail alert if mysql lookup fails
filename="/root/on-call-notify.txt" #File to check for previous on-call number
subject="On-Call Notification" #Subject line of e-mail to send
to="email@domain.com" #E-mail receipient's address
#YOU SHOULDN'T NEED TO EDIT BELOW HERE UNLESS YOU KNOW WHAT YOU'RE DOING!
# Connect to mysql and pull value from ringgroup
oncallnum=$(mysql -u $mysqluser -p$mysqlpass asterisk -sN -e "select grplist from ringgroups where grpnum='$ringgroup'")
# Trim the trailing # from the entry
oncallnum=${oncallnum%#}
# If mysql lookup fails, assign oncallnum to defaultnumber
oncallnum=${oncallnum:=$defaultnum}
# Get CID database record (if available) of new on-call number
oncallname=$(/usr/sbin/asterisk -Rx "database show cidname $oncallnum")
# Clean-up on-call name retrieved from asterisk phonebook
oncallname=${oncallname#*:}
oncallname=$(echo "$oncallname" | sed 's/^ *//' | sed 's/ *$//')
#If oncallname is null, then replace with defaultname
oncallname=${oncallname:=$defaultname}
#Check if file previous on-call file exists, if not, create and populate w/ empty data
if [ ! -f "$filename" ] ; then
echo empty > $filename
fi
#Compare new and old on-call values
#Read the file into the prevoncall value
prevoncall="`cat $filename`"
#This section was intended to detect if changes were made in freepbx but
#not reloaded. The test is successful, but I don't know how to automatically
#cause freepbx apply the changes
#if grep -q "$oncallnum#,$ringgroup" "/etc/asterisk/extensions_additional.conf" ; then
# echo "Changes have been applied"
#else
# echo "Apply your changes!"
#fi
#Check if the on-call number has changed
if [ "$oncallnum" != "$prevoncall" ]; then
#Write the new on-call number to the file
echo $oncallnum > $filename
#Send an email notification of on-call change
echo "$oncallname at $oncallnum is now on-call." | /bin/mail -s "$subject" "$to"
fi
What it does:
1. Runs as a cron job (I use every 5 minutes)
2. Queries the ringgroup of your choosing and extracts the number
3. Queries the Asterisk phonebook database to put a name to the number
4. Compares it to the previously obtained number (if it exists)
5. E-mails out a notification message if the on-call technician has changed
That's it... I know it's pretty straight-forward, but I couldn't find anything like it on-line, so here you go.
Regards,
Chris Vanderbles