How to Install a web-based Asterisk CDR analyzer

By default Asterisk generates CDR records in a CSV files located in /var/log/asterisk/cdr-csv directory. Asterisk can also store CDR records in a database such as MySQL or PostgreSQL database as an alternative to CSV files. If your CDR records are stored in a database, you can install a web-based CDR analyzer to easily manage and analyze your CDR records like Asterisk-stat by http://areski.net.

Here are some features of Asterisk-stat

  • CDR report (monthly or daily)
  • Monthly traffic
  • Daily load
  • Compare call load with previous days
  • Many criterias to define the report
  • Export CDR report to PDF and CSV
  • Support MySQL and PostgreSQL database
  • Many others

To install asterisk-stat, you need to install HTTP server, PHP and MySQL database. I assume that you are running CentOS 4.4 or higher and have Asterisk 1.2 installed in you server. To install Asterisk 1.2, checkout this tutorial http://www.cahilig.org/install-asterisk-12-centos-4-and-centos-5 or download the auto install script form http://www.cahilig.org/install-asterisk-12-auto-install-script-centos-4-and-centos-5.

To get started, install the Apache web server, PHP and MySQL database. Run this command in the console.

apt-get install httpd php php-mysql php-gd mysql mysql-devel mysql-server

Start the Apache and MySQL and make sure that it will run during boot time by typing this command.

service httpd start
chkconfig httpd on
service mysqld start
chkconfig mysqld on

Go to /usr/src/directory, download and install asterisk-addon source from Asterisk website.

wget http://downloads.digium.com/pub/asterisk/releases/asterisk-addons-1.2.8.tar.gz
tar -zxvf asterisk-addons-1.2.8.tar.gz
make clean
make
make install

Copy the file cdr_mysql.conf.sample from configs directory to /etc/asterisk, rename it to cdr_mysql.conf. Edit the database configuration.

[global]
hostname=localhost
dbname=asteriskcdrdb
table=cdr
password=password
user=asterisk
port=3306

Then edit your /etc/asterisk/modules.conf and add the cdr_addon_mysql.so under the [global] section.

[global]
load => cdr_addon_mysql.so

Login to your MySQL server and create a root password to secure your server then create a database to store your CDR records.

mysql -u root
SET PASSWORD FOR root@localhost = PASSWORD ('password');

Create Asterisk CDR databases and user to the databases.

create database asteriskcdrdb;
GRANT SELECT,INSERT ON asteriskcdrdb.* TO asterisk@localhost IDENTIFIED BY 'password';

Create a table called cdr under the database name you will be using the following schema.

USE asteriskcdrdb;
CREATE TABLE cdr (
calldate datetime NOT NULL default '0000-00-00 00:00:00',
clid varchar(80) NOT NULL default '',
src varchar(80) NOT NULL default '',
dst varchar(80) NOT NULL default '',
dcontext varchar(80) NOT NULL default '',
channel varchar(80) NOT NULL default '',
dstchannel varchar(80) NOT NULL default '',
lastapp varchar(80) NOT NULL default '',
lastdata varchar(80) NOT NULL default '',
duration int(11) NOT NULL default '0',
billsec int(11) NOT NULL default '0',
disposition varchar(45) NOT NULL default '',
amaflags int(11) NOT NULL default '0',
accountcode varchar(20) NOT NULL default '',
uniqueid varchar(32) NOT NULL default '',
userfield varchar(255) NOT NULL default ''
);

quit

Restart your Asterisk PBX, start making a call and check if the CDR is written in your database.

use asteriskcdrdb;
select uniqueid, src, calldate from cdr;

You can also check in Asterisk console by typing this Asterisk command:

asterisk -ncrvvvvvvvvvv
cdr mysql status

It will display the message that look exactly like this:

Connected to asteriskcdrdb@localhost using table cdr for 0 hours, 15 minutes, 15 seconds.
Wrote 1 records since last restart.

If you see the message similar from above, you have successfully installed asterisk-addon.

Go to /var/www/html/ directory and grab asterisk-stats package from http://areski.net.

wget http://areski.net/asterisk-stat-v2/asterisk-stat-v2_0_1.tar.gz
tar -zxvf asterisk-stat-v2_0_1.tar.gz

Rename asterisk-stat-v2_0_1 to asterisk-stats and edit the defines.php in /lib directory.

define ("WEBROOT", "http://hostname/asterisk-stats/");
define ("FSROOT", "/var/www/html/asterisk-stats/");



define ("LIBDIR", FSROOT."lib/");


define ("HOST", "localhost");
define ("PORT", "3306");
define ("USER", "asterisk");
define ("PASS", "password");
define ("DBNAME", "asteriskcdrdb");
define ("DB_TYPE", "mysql");


define ("DB_TABLENAME", "cdr");

Create a symlink from cdr.php to index.php

ln -sf cdr.php index.php

And the URL would be:
http://hostname/asterisk-stats

The asterisk-stats v2.0.1 has a bug when you are exporting CDR in pdf format.
To fix the bug, you need to change line 5 of export_pdf.php from:

include_once(dirname(FILE) . "/lib/fpdf.php');

to:

include_once(dirname(FILE) . "/lib/fpdf.php");

It should be fine now, you can export your CDR to PDF files.

Your Asterisk-stat is done, however it is not yet secured, everybody can login without any authentication. If you want to put an authentication to your Asterisk-stat, just follow this howto. It will require web site visitors to login with a user id and password.

You can delete the CDR in your MySQL database using phpMyAdmin.