Thursday, June 12, 2014

Asterisk ACD pause status BLF

Recently I installed for a customer an Asterisk PBX with ACD service.
This customer has the necessity to have two queues with specialized agents.
If situation requires, not specialized agent should answer the call.
In the situation in which only one specialized agent is logged in, and he/she is not at the phone, because whatever reason, it would happen that the call is not routed to other agent with lower priority.
To fix this scenario, you can use the autopause feature, which put in the pause status an agent if he/she does not answer the call.
But when the agent returns back at him/her position, he/she can not know he/she has been auto paused, and then he/she could sit and wait a call that will never arrive.
It would be nice to have a signal on the telephone stating the ACD pause status.

I do not find any built-in feature in the Asterisk I were using that let me do this, so I wrote a script to have such service.
The idea is to watch the "queue_log" where asterisk puts all events about ACD and agents.
When agent pause event appears in such log, you can set the respective devicestate using "devstate change" Asterisk cli command.

Here my script.

#    Agentwatch 0.1 2014 Alessandro Carminati

        rm /tmp/aw
        exit 0

trap 'on_die' TERM INT KILL

[ -f /tmp/aw ] && echo "aw exists, exit" && exit;
touch /tmp/aw

for i in $(rasterisk -x "queue show queue1" | grep "SIP"); do
    echo $i |grep "paused" >/dev/null && status="INUSE";
    ext=$(echo $i | cut -d "(" -f1| sed -e 's/ //g'| sed -e 's/.*sip//');
    rasterisk -x "devstate change custom:queue1_${ext} ${status}" >/dev/null
    [ "${1}" = "-d" ] && echo "devstate change custom:queue1_${ext} ${status}"
for i in $(rasterisk -x "queue show queue2" | grep "SIP"); do
    echo $i |grep "paused" >/dev/null && status="INUSE";
    ext=$(echo $i | cut -d "(" -f1| sed -e 's/ //g'| sed -e 's/.*sip//');
    rasterisk -x "devstate change custom:queue2_${ext} ${status}" >/dev/null
    [ "${1}" = "-d" ] && echo "devstate change custom:queue2_${ext} ${status}"

tail -n0 -f /var/log/asterisk/queue_log | while read LOGLINE
   if [[ "${LOGLINE}" == *"|PAUSE|"* ]]; then
                                             coda=$(echo "${LOGLINE}" | cut -d"|" -f3);
                                             agente=$(echo "${LOGLINE}" | cut -d"|" -f4|cut -d"p" -f2);
                                             [ "${1}" = "-d" ] && echo "l'agente $agente entra in pausa per la coda $coda";
                                             rasterisk -x "devstate change custom:${coda}_${agente} INUSE" >/dev/null
   if [[ "${LOGLINE}" == *"|UNPAUSE|"* ]]; then
                                             coda=$(echo "${LOGLINE}" | cut -d"|" -f3);
                                             agente=$(echo "${LOGLINE}" | cut -d"|" -f4|cut -d"p" -f2);
                                             [ "${1}" = "-d" ] && echo "l'agente $agente esce dalla pausa per la coda $coda";
                                             rasterisk -x "devstate change custom:${coda}_${agente} NOT_INUSE" >/dev/null


Because you want to see this on the phone, you have to define some hints so them can be configured as blf on the phone. (Agent extensions goes from 511 to 519).

exten => *9*511,             hint,   Custom:queue1_511&Custom:queue2_511
exten => *9*512,             hint,   Custom:queue1_512&Custom:queue2_512
exten => *9*513,             hint,   Custom:queue1_513&Custom:queue2_513
exten => *9*514,             hint,   Custom:queue1_514&Custom:queue2_514
exten => *9*515,             hint,   Custom:queue1_515&Custom:queue2_515
exten => *9*516,             hint,   Custom:queue1_516&Custom:queue2_516
exten => *9*517,             hint,   Custom:queue1_517&Custom:queue2_517
exten => *9*518,             hint,   Custom:queue1_518&Custom:queue2_518
exten => *9*519,             hint,   Custom:queue1_519&Custom:queue2_519

And at last, because you want this service starts automaticaly when server goes up, here is my redhat fashon rc script.

# $Id: rc.redhat.agentwatch 268535 2010-06-06 05:31:11Z AC$
# AgentWatch    Starts, Stops and Reloads
# chkconfig: 2345 90 60
# description: Track using blf pause status
# processname: agentwatch
# pidfile: /var/run/
. /etc/init.d/functions

if test -e /etc/sysconfig/mouse ; then
    . /etc/sysconfig/mouse


start() {
        echo  "Starting AgentWatch service: "
        /usr/aw/ >/dev/null &
        echo $! > /var/run/

stop() {
        echo  "Shutting down AgentWatch service: "
        kill -9 $(cat /var/run/;

case "$1" in
        echo $"Usage: $0 {start|stop|restart}"
        exit 1

exit $RETVAL

Any comment about this argument is very welcome.