6.6. Log file

Events that happen in queues are written in a detailed format to the queue log (usually /var/log/asterisk/queue_log) in addition to the regular call detail recording. Events are recorded in queue_log on a line-by-line basis in the following format:
The Unix time of the event.
The unique ID of the call (alphanumeric). Can be NULL or NONE (e.g. in the case of QUEUESTART events).
The name of the queue, e.g. support. Can be NULL or NONE.
The name of the bridged channel, e.g. Agent/1001. Can be NULL or NONE.
The name (type) of the event (see below). The parameters which follow depend on the event type.
Possible events are, among others (see also doc/queuelog.txt):
The caller abandoned the queue by hanging up. Parameters: position, entry position, wait time.
The agent dumped the caller while listening to the queue announcement.
An agent logged in. Parameters: channel (e.g. SIP/
A callback agent logged in. Parameters: login_extension[@context].
An agent logged out. Parameters: channel, login time.
An agent was logged out. Parameters: login_extension[@context], cogin time, cause (e.g. autologoff).
Call between caller and agent was ended by agent. Parameters: wait time, call time, queue entry position.
Call between caller and agent was ended by caller. Parameters: wait time, call time, queue entry position.
Configuration was reloaded (e.g. with asterisk -rx "reload").
Caller was connected to an agent. Parameters: Wait time.
Caller was placed in the queue. Parameters: URL (if available), caller ID.
Caller was exited from the queue because no members were available[35]. Parameters: queue position, entry position, wait time.
Caller left the key by pressing a key. Parameters: key, queue position.
Caller was in the queue too long and the timeout expired. Parameters: queue position.
The queue was started. The call_id, queue and channel fields are set NULL.
An available agent was called, but did not answer within the timeout. Parameters: ring time (in milliseconds).
The agent accepted a call, but the channels were not compatible and the call was ended.
Caller was transferred to another extension. Parameters: extension, context.
Some commercial queue log analysis tools include QueueMetrics[36] or Easy PABX[37]. See also Section B.89, “QueueLog().

Importing the queue-Log into MySQL

As yet, Asterisk cannot write the queue log directly into an SQL database. Unfortunately, all of the widely used statistical tools assume and expect data to be in an SQL database. A variety of scripts seek to mitigate this problem[38] (most of them in Perl), or are provided with the analysis tools. They all follow the same principle: the queue_log is replaced with a named pipe (FIFO); as soon as Asterisk attempts to write to the log, the log entry is converted and entered into the database.


This method works in 99 % of cases. There remains the risk, however, that the script fails to start before Asterisk or terminates unexpectedly and stops reading from the named pipe, such that Asterisk receives a SIGPIPE when trying to write to the logs and crashes!
Since there is no alternative without native Asterisk SQL support, we provide a sample Perl script from William Lloyd despite the associated risk[39]:
#!/usr/bin/perl -w
# wlloyd at slap.net

# The asterisk version indpendant way to get queue stats into Mysql,  
# or whatever is supported by Perl DBI

# It's all about named pipes

# to setup this software
# stop asterisk
# rm /var/log/asterisk/queue_log
# mkfifo /var/log/asterisk/queue_log

# make sure permissions are setup
# chmod 777 /var/log/asterisk/queue_log

# run this program as root or under another user as you see fit.
# should start BEFORE asterisk.  Add to /etc/rc.d/rc.local or whatever

# restart asterisk

# requires a DB table like the following..
# CREATE TABLE csr_queue (
#  qname varchar(30) default NULL,
#  agent varchar(30) default NULL,
#  action text,
#  info1 text,
#  info2 text,
#  info3 text,
#  timestamp int(11) NOT NULL default '0',
#  id tinytext NOT NULL

use DBI;
use IO::File;

my $opt_debug = 0;

# if you want postgres change this to "Pg"
my $db_type = "mysql";
my $db_host = "";
my $db_user_name = 'username';
my $db_password = 'password';
my $db_database = 'asteriskstat';

my $dbh = DBI->connect("DBI:$db_type:dbname=$db_database;host= 
$db_host;", $db_user_name, $db_password);

open(FIFO, "< /var/log/asterisk/queue_log")        or die "Can't open  
queue_log : $!\n";

while (1) {

     $message = <FIFO>;
     next unless defined $message;   # interrupted or nothing logged
     chomp $message;

     # remove chars that will cause DB problems
     $message =~ s/\"\'//g;

     @data = split(/\|/,$message);

     # these messages are almost useless for my purposes
     next if ($data[4] eq "QUEUESTART" );
     next if ($data[4] eq "CONFIGRELOAD" );

     if (!defined($data[5])) {
       $data[5] = '';
     if (!defined($data[6])) {
       $data[6] = '';
     if (!defined($data[7])) {
       $data[7] = '';

     my $sql = "INSERT INTO csr_queue (timestamp, id, qname, agent,  
action, info1, info2, info3) VALUES ('$data[0]', '$data[1]', '$data 
[2]', '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]')";

     print "$sql \n\n" if ($opt_debug);


# if you want an actual logfile you might want to uncomment this
#        if ( open(LOG, ">> /var/log/asterisk/queue_log_real") ) {
#            print LOG "$message\n";
#            close(LOG);
#        } else {
#            warn "Couldn't log to /var/log/asterisk_queue_log: $!\n";
#        }


exit 0;
QueueMetrics (the free demo version) comes packaged with a Perl script called queueLoader.pl (again - heed the warning above!).