HOWTO :: VoIP: Whosesale :: Calling Card :: OpenSer :: Radius :: Asterisk :: FreeSwitch :: A2Billing :: IVR :: Colo :: Colocations :: GADGETS

HOWTO: Asterisk CDR + MYSQL

July 8, 2008

HOWTO: Asterisk CDR + MYSQL

Filed under: Asterisk CDR + MYSQL — Tags: , , — nelson @ 3:31 am








This tutorial will teach you how to configure Asterisk to store its CDR in MYSQL Database. Im using Debian Etch as my Operating System.

Installing Mysql Server 5 and Client

apt-get install mysql-server mysql-common php5-mysql mysql-client-5.0 mysql-client libmysqlclient15-dev

Creating a Database, Tables and Permissions

asterisk88:~# mysql -u root -p <enter>
Enter password: <enter>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.0.32-Debian_7etch5 Debian etch distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> create database asterisk;
mysql> use asterisk;
mysql> 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 ”,
userfield varchar(255) NOT NULL default ”,
uniqueid varchar(32) NOT NULL default ”
);

ALTER TABLE cdr ADD INDEX ( calldate );
ALTER TABLE cdr ADD INDEX ( dst );
ALTER TABLE cdr ADD INDEX ( uniqueid );

You can add more indices if you want.

mysql> \q

asterisk88:~# mysql_setpermission <enter>
Password for user to connect to MySQL: <enter>
######################################################################
## Welcome to the permission setter 1.3 for MySQL.
## made by Luuk de Boer
######################################################################
What would you like to do:
1. Set password for an existing user.
2. Create a database + user privilege for that database
and host combination (user can only do SELECT)
3. Create/append user privilege for an existing database
and host combination (user can only do SELECT)
4. Create/append broader user privileges for an existing
database and host combination
(user can do SELECT,INSERT,UPDATE,DELETE)
5. Create/append quite extended user privileges for an
existing database and host combination (user can do
SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,
LOCK TABLES,CREATE TEMPORARY TABLES)
6. Create/append database administrative privileges for an
existing database and host combination (user can do
SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,LOCK TABLES,
CREATE TEMPORARY TABLES,SHOW DATABASES,PROCESS)
7. Create/append full privileges for an existing database
and host combination (user has FULL privilege)
8. Remove all privileges for for an existing database and
host combination.
(user will have all permission fields set to N)
0. exit this program

Make your choice [1,2,3,4,5,6,7,0]: 5 <enter>

Which database from existing databases would you like to select:
You can choose from:
- information_schema
- asterisk
- mysql
Which database will it be (case sensitive). Type * for any:
asterisk <type ‘asterisk’ then <enter> >
The database asterisk will be used.

What username is to be created: administrator
Username = administrator <username to be created> <enter>
Would you like to set a password for administrator [y/n]: y <enter>
What password do you want to specify for administrator: <enter password then enter>
Type the password again: <retype password then enter>
We now need to know from what host(s) the user will connect.
Keep in mind that % means ‘from any host’ …
The host please: % <type ‘%’ for anyhost>
Would you like to add another host [yes/no]: no <enter>
Okay we keep it with this …
The following host(s) will be used: %.
######################################################################

That was it … here is an overview of what you gave to me:
The database name : asterisk
The username : administrator
The host(s) : %
######################################################################

Are you pretty sure you would like to implement this [yes/no]: yes <enter>
Okay … let’s go then …

Everything is inserted and mysql privileges have been reloaded.

Make your choice [1,2,3,4,5,6,7,0]: 0 <enter>

Now lets test the connection:

asterisk88:~# mysql -h localhost -u administrator -p asterisk

Enter password: <enter newly created password for ‘administrator’>

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.0.32-Debian_7etch5 Debian etch distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> show tables;
+——————–+
| Tables_in_asterisk |
+——————–+
| cdr |
+——————–+
1 row in set (0.00 sec)

mysql> desc cdr;
+————-+————–+——+—–+———————+——-+
| Field | Type | Null | Key | Default | Extra |
+————-+————–+——+—–+———————+——-+
| calldate | datetime | NO | MUL | 0000-00-00 00:00:00 | |
| clid | varchar(80) | NO | | | |
| src | varchar(80) | NO | | | |
| dst | varchar(80) | NO | MUL | | |
| dcontext | varchar(80) | NO | | | |
| channel | varchar(80) | NO | | | |
| dstchannel | varchar(80) | NO | | | |
| lastapp | varchar(80) | NO | | | |
| lastdata | varchar(80) | NO | | | |
| duration | int(11) | NO | | 0 | |
| billsec | int(11) | NO | | 0 | |
| disposition | varchar(45) | NO | | | |
| amaflags | int(11) | NO | | 0 | |
| accountcode | varchar(20) | NO | | | |
| userfield | varchar(255) | NO | | | |
| uniqueid | varchar(32) | NO | MUL | | |
+————-+————–+——+—–+———————+——-+
16 rows in set (0.00 sec)

mysql>

Our database is now ready for use with Asterisk. Now, Let’s configure Asterisk to save its CDR to this database:

Download Asterisk-addons from Digium:


cd /usr/src
wget http://downloads.digium.com/pub/asterisk/releases/asterisk-addons-1.4.7.tar.gz
tar xvfz asterisk-addons-1.4.7.tar.gz
cd asterisk-addons-1.4.7/cdr/
vi cdr_addon_mysql.c
add #define MYSQL_LOGUNIQUEID after #define DATE_FORMAT “%Y-%m-%d %T”

cd ..
./configure
make menuselect

Check the following if selected:

—> 1. Applications
[*] 1. app_addon_sql_mysql
2. Call Detail Recording
[*] 1. cdr_addon_mysql
3. Channel Drivers
4. Format Interpreters
5. Resource Mod

then q (quit) and S (save)

make && make install

Edit /etc/asterisk/cdr.conf

vi /etc/asterisk/cdr.conf

[general]
enable=yes

Edit /etc/asterisk/cdr_mysql.conf

[global]
hostname=localhost
dbname=asterisk
table=cdr
password=need2fix
user=admin
port=3306
;sock=/tmp/mysql.sock
;userfield=1

Edit /etc/asterisk/modules.conf

vi /etc/asterisk/modules.conf

add the entry below:

load => cdr_addon_mysql.so

We’re finished! Restart asterisk and check if mysql module is loaded

asterisk88# asterisk -r
Asterisk 1.4.20, Copyright (C) 1999 - 2008 Digium, Inc. and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type ‘core show warranty’ for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type ‘core show license’ for details.
=========================================================================
Connected to Asterisk 1.4.20 currently running on asterisk88 (pid = 26648)
Verbosity is at least 3
asterisk88*CLI> stop now
asterisk88*CLI>
Disconnected from Asterisk server
asterisk88# safe_asterisk
asterisk88# asterisk -r
Asterisk 1.4.20, Copyright (C) 1999 - 2008 Digium, Inc. and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type ‘core show warranty’ for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type ‘core show license’ for details.
=========================================================================
Connected to Asterisk 1.4.20 currently running on asterisk88 (pid = 26880)
Verbosity is at least 3
asterisk88*CLI> cdr mysql status
Connected to asterisk@localhost, port 3306 using table cdr for 15 seconds.
Wrote 0 records since last restart.
asterisk88*CLI> quit

Make a successful test call and see if it CDR is placed in DB:

To check:

asterisk88:/usr/src/asterisk-addons-1.4.7# mysql -u administrator -p asterisk
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.0.32-Debian_7etch5 Debian etch distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> select * from cdr;
+———————+—————————+————+————-+———-+———————-+————-+———+————————–+———-+———+————-+———-+————-+———–+————–+
| calldate | clid | src | dst | dcontext | channel | dstchannel | lastapp | lastdata | duration | billsec | disposition | amaflags | accountcode | userfield | uniqueid |
+———————+—————————+————+————-+———-+———————-+————-+———+————————–+———-+———+————-+———-+————-+———–+————–+
| 2008-07-08 05:03:25 | “XXXXXXXXXX” <XXXXXXXXXX> | 0117913501 | 12127773456 | default | IAX2/0117913501-8484 | H323/CARRIER-1 | Dial | H323/XXXXXXXXXX@Carrier||R | 19 | 0 | NO ANSWER | 3 | | | 1215518605.1 |
+———————+—————————+————+————-+———-+———————-+————-+———+————————–+———-+———+————-+———-+————-+———–+————–+
1 row in set (0.00 sec)

mysql>

Share/Save/Bookmark

1 Comment »

  1. [...] On Researchâ

    Pingback by Database Management » Blog Archive » HOWTO: Asterisk CDR + MYSQL — July 8, 2008 @ 3:33 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress