Asterisk + FreePBX – Removing queue members with logout *12

Some organizations probably need this, so we thought of sharing.
The requirements form one of client is to “auto” logout someone from an Asterisk/FreePBX queue module when he/she logout from the user/device mode.
This script uses AGI. I have tested this on;
1) Debian 5
2) FreePBX 2.8 and below
3) Asterisk 1.6 (may work for 1.4- need to probably tweak the script)
NOTE: If the codes appear truncated, simply copy the whole table and paste into a text editor
Firstly, we use the override function in FreePBX to override the macro-user-logoff directive

nano /etc/asterisk/extensions_override_freepbx.conf

Enter the following code;

[macro-user-logoff]
; check device type
;
exten => s,1,Set(DEVICETYPE=${DB(DEVICE/${CALLERID(number)}/type)})
exten => s,n,GotoIf($[“${DEVICETYPE}” = “fixed”]?s-FIXED,1)
exten => s,n,Set(dev=${DB(DEVICE/${CALLERID(num)}/user})
exten => s,n,AGI(kick.sh,${dev})
exten => s,n,AGI(user_login_out.agi,logout,${CALLERID(number)})
exten => s,n(done),Playback(vm-goodbye)
exten => s-FIXED,1,NoOp(Device is FIXED and cannot be logged out of)
exten => s-FIXED,n,Playback(an-error-has-occured&vm-goodbye)
exten => s-FIXED,n,Hangup

The two items highlighted in bold are basically the ones we add to an existing macro-user-logoff function in FreePBX
Now, create the AGI / bash script that will do the trick

nano /var/lib/asterisk/agi-bin/kick.sh

Paste the following code in the editor, MAKE SURE YOU MODIFY YOUR QUEUE NUMBERS show in “queuenumbers”

#!/bin/bash
# script by Sanjay Willie
# v0.1-beta
# tested on Asterisk 1.6/FreePBX 2.8/Debian 5.06
# maintainer: [email protected]
# the directory /tmp should exist and can be accessible by user asterisk and group asterisk
# NOTE: FILES IN /tmp normally gets flushed on restarts
# On grabbing agent number, should look exactly like “Local/1000@from-queue” eventually and should not contain any preceding or trailing characters
# the extraction of agents in the script in the line “for agents…” may or not yield output, so test it out.
# The sed part is to remove brackets from the output in the front and back (seen in FBX 2.8)
# if the sed isn’t required (like in FreePBX 2.7 and below) you can remove it, also lookout for the print $3, in FreePBX 2.7 and below its print $1, so test it out first
stdin=”0″
datenano=`date +%N-%F`
regulardate=`date +%F-%T`
gennum=$RANDOM-$datenano.int-tmp
queuenumbers=”4001 4002 4003 5001 5002 5003 4021 4022 4023 5021 5022 5023″
###########
while [ “$stdin” != “” ]
        do
                read stdin
                if [ “$stdin” != EOF ]
                then
                        echo $stdin | grep agi_arg_1 | awk {‘print $2’} >> $gennum
                fi
        done
############
include=`cat $gennum`
#include=2006
############
cleanup=`rm -v /tmp/$gennum`
#echo $cleanup >> /tmp/cleanup
############
if [[ “$include” == “” ]] ; then
        echo Nothing to do, quitting
        exit
else
for queue in $queuenumbers; do
        #echo $queue >> /tmp/logfile.txt
        # see NOTES above
        #for agents in `asterisk -rx “queue show $queue” | grep from-queue | awk ‘{print $3}’ | grep -w “$include” | sed ‘s/.(.*)/1/’ | sed ‘s/(.*)./1/’`; do # freePBX 2.8 or higher
        for agents in `asterisk -rx “queue show $queue” | grep from-queue | awk ‘{print $1}’ | grep -w “$include”`; do
                #echo $agents >> /tmp/logfile.txt
                asterisk -rx “queue remove member $agents from $queue”
                echo “we remove $agents in $queue”
                echo $regulardate – Attempt removing agent $agents from queue $queue >> /tmp/agentremover.log
        done
done
fi
############
exit 0

This code does this;
1) Once the Asterisk dial plan calls the script, it will parse the user into bash and we will remove that user with a script that walks through all queues you have specified looking for this user and does a removal using asterisk –rx
2) Stores a simple log in /tmp
Make it executable, chmod +x /var/lib/asterisk/agi-bin/kick.sh and make sure the asterisk user can read/run the file, chown asterisk:asterisk /var/lib/asterisk/agi-bin/kick.sh
Reload the dialplan

asterisk -rx “dialplan reload”

Go ahead and test it…
Let us know if this worked out. For help, write to [email protected]