How to send an email when a call is abandoned or times out

Howto


Since Asternic Stats PRO version 2.1.0, there is a new option in the asterniclog service to run custom scripts whenever the queue_log file is updated. This can be useful if you want to perform some task whenever a certain event is recorded in queue_log.

The command line option for specifying that script is -t and should be followed by the full path of your custom script.

That script will be executed any time something is written in the queue_log file, you can add logic into that script to perform any kind of action like sending an email.

As an example, in /etc/sysconfig/asterniclog you can add the parameter like this:

OPTIONS="-u qstatsUser -p qstatsPassw0rd -d qstats -h localhost -l /var/log/asterisk/queue_log -c -t /usr/local/parselog/myscript.php --daemon"

Then write a script /usr/local/parselog/myscript.php that sends an email:

#!/usr/bin/php
<?php
$mailto   = "your@mail.com";
$subject  = "Call Center Alert";
$headers  = 'From: webmaster@callcenter.com' . "\r\n" . 'Reply-To: webmaster@callcenter.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

$date  = $argv[1];
$queue = $argv[2];
$agent = $argv[3];
$event = $argv[4];
$uniq  = $argv[5];
$text  = "";

$formatted_date = gmdate("Y-m-d\TH:i:s\Z",$date);

if($event == 'ABANDON') {
    exec("/usr/sbin/asterisk -rx 'queue show $queue'",$return);
    exec("grep $uniq /var/log/asterisk/queue_log | grep ENTERQUEUE | cut -d \| -f 7",$clid);
    exec("grep $uniq /var/log/asterisk/queue_log | grep RINGNOANSWER | cut -d \| -f 4",$ringnoanswer);
    exec("grep $uniq /var/log/asterisk/queue_log | grep ABANDON | tail -n 1 | cut -d \| -f 8",$waittime);

    $waittime = implode('',$waittime);

    if($waittime<6) {
        // Ignoring short abandon
        exit;
    }

    $agents = extract_members($return);

    $agent_string = '';
    foreach($agents as $state=>$agentsinstate) {
        $agent_string .= "Agents $state: ". implode(",",$agentsinstate)."\n";
    }

    $ringnoanswer = array_unique($ringnoanswer);
    $ringno_string = implode(",",$ringnoanswer);
    $text = "Call from $clid[0] on queue $queue was abandoned at $formatted_date after $waittime seconds wait.\n";
    $text.= "Available agents at time of event:\n$agent_string\n\n";
    if($ringno_string<>'') {
       $text.= "Agents that failed to answer were:\n$ringno_string\n\n";
    }
}

if($event == 'EXITWITHTIMEOUT') {
    exec("/usr/sbin/asterisk -rx 'queue show $queue'",$return);
    exec("grep $uniq /var/log/asterisk/queue_log | grep ENTERQUEUE | cut -d \| -f 7",$clid);
    exec("grep $uniq /var/log/asterisk/queue_log | grep RINGNOANSWER | cut -d \| -f 4",$ringnoanswer);
    exec("grep $uniq /var/log/asterisk/queue_log | grep EXITWITHTIMEOUT | tail -n 1 | cut -d \| -f 8",$waittime);

    $waittime = implode('',$waittime);

    if($waittime<6) {
        // Ignoring short timeout
        exit;
    }

    $agents = extract_members($return);

    $agent_string = '';
    foreach($agents as $state=>$agentsinstate) {
        $agent_string .= "Agents $state: ". implode(",",$agentsinstate)."\n";
    }

    $ringnoanswer = array_unique($ringnoanswer);
    $ringno_string = implode(",",$ringnoanswer);

    $text = "Call from $clid[0] on queue $queue timed out at $formatted_date after $waittime seconds.\n";
    $text.= "Available agents at time of event:\n$agent_string\n\n";
    if($ringno_string<>'') {
       $text.= "Agents that failed to answer were:\n$ringno_string\n\n";
    }
}

if($text <> '') {
    mail($mailto, $subject, $text, $headers);
}

function extract_members($data) {
    $ret = array();
    foreach($data as $linea) {
        if(substr($linea,0,5)=="     ") {

            $partes = preg_split("/\(/",$linea);
            $agente = trim($partes[0]);

            if(preg_match("/Not in use/i",$linea)) {
                $ret['available'][] = $agente;
            } else if(preg_match("/unavailable/i",$linea)) {
                $ret['unavailable'][] = $agente;
            } else if(preg_match("/busy/i",$linea)) {
                $ret['busy'][] = $agente;
            } else if(preg_match("/in use/i",$linea)) {
                $ret['in use'][] = $agente;
            } else if(preg_match("/invalid/i",$linea)) {
                $ret['invalid'][] = $agente;
            } else {
                $ret['unknown'][] = $agente;
            }

        }
    }
    return $ret;
}

Finally restart the asterniclog service with:

service asterniclog restart

The script can be used to perform any kind of tasks, not only sending emails. It is up to your imagination.