Clever Call Recording With Asterisk PBX

In addition to being an author, I’m also a telecom/ linux geek.  Someone recently posted on “StackOverflow” that they couldn’t find a good “how-to” on call recording for Asterisk PBX systems.

I’m not convinced they looked too hard, but I figured it would not take me too long to post one up.  This works with both Asterisk 1.4.x and Asterisk 1.8.x.  YMMV on other revs.

Please note that this post is written for someone that has a basic understanding of Asterisk call routing and dial plans.  If you don’t have that, I strongly recommend you take a read of:

So, let’s get started:

Add a line like this to your outbound dial route and your inbound dial route:

same => n, Macro(do_call_recording|${MACRO_CONTEXT}|${CRS_ID}|Outbound)

… and then add the following macro to your dialplan:

[macro-do_call_recording]
; ARG1: Company Name or Account ID for Recording
; ARG2: Extension # or DID # being recorded
; ARG3: Call direction (inbound/ outbound)
;
exten =>s,1, NoOp(** Starting CRS for ${ARG2} call for ${ARG1}… )
same => n, Set(RECDISK=/tmp)
same => n, Set(WEBDIR=/var/www/yourPBX/recordings)
same => n, Set(FILENAME=${ARG1}=${ARG2}=${ARG3}=${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}.wav)
same => n, MixMonitor(${RECDISK}/${FILENAME},,/var/www/yourPBX/scripts/after_rec_call.sh ^{RECDISK} ^{WEBDIR} ^{FILENAME})
same => n, NoOp(** Call now being recorded!)
; — — EOF — —

… and then add a shell script that looks like this:

#!/bin/bash
# $1 = recording path
# $2 = web directory path
# $3 = recorded file name
LOGFILE=”/var/log/pbx_call_recording_app.log”
DATE=`date +%G%m%d::%H:%M:%S`

echo ” — — — — — ” >> $LOGFILE
echo “[$DATE][init] ** after_rec_call triggered ” >> $LOGFILE
echo “[$DATE][info] … moving $3 from $1 to $2 …” >> $LOGFILE
mkdir -p $2
mv $1/$3 $2 >> $LOGFILE
echo “[$DATE][end] ** done file move” >> $LOGFILE
# — — EOF — —

… make sure that “/var/www/yourPBX/recordings” and “/var/www/yourPBX/scripts/” are valid/ meaningful.

Also, note that you can do something clever like add SoX to the shell script and have it convert the .WAV to MP3, or email it to someone, or update a database, etc, etc.

A simple web interface would allow you the option to download the recordings, etc.

I use this on a couple of customer systems and it handles hundreds of calls per day with very little impact on the PBX.  The biggest issue is disk space;  I recommend that you consider putting a 1TB disk at the disposal of your recordings archive.  A moderate sized customer (20 phones, financial company) generates nearly 8gb of call recordings a month in .WAV format.

Please leave your comments below.  I’d love to hear from anyone who finds this useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.