Setup CDR log to Mysql on Asterisk with ODBC module


If you install asterisk server already you need to compile asterisk again to get module odbc from select menu but you need to back up all file first with asteris gui and folder /etc/asterisk because it will reset to default.

Now follow this step:

1-Install Dependancies support with odbc

   +for debian:
    install:
        unixodbc-dev – UnixODBC devel libraries
        libmyodbc – ODBC MySQL Connector
        libmysqlclient-dev – MySQL Client

=>#sudo apt-get install unixodbc-dev libmyodbc libmysqlclient-dev

   +for centos 6.x:
      install package:

=>#yum install unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel mysql-connector-odbc


-Then go to asterisk download folder:

# cd asterisk-11.*
# ./configure   if using 64bit => #./configure --libdir=/var/lib64

# make menuselect
=>mysql-connector-odbc=> select cdr adaptive odbc (default is elected)
                                      => select on cdr_odbc  (default is selected)
then click x on keyboard to save.


#make
#make install
#make sample
#make config

* Configure ODBC for Centos:

#vim /etc/odbc.ini


[asterisk-cdr]
Description     = MySQL ODBC CDR
Driver          = MySQL
Database        = asterisk_db  #database name
Server          = localhost   
User            = asterisk_user  #database user
Password        = 123456   #database password
Port            = 3306
Option          = 3
Socket          = /var/lib/mysql/mysql.sock 
#vim /etc/odbcinst.ini
[MySQL]  # for 64bit
Description     = ODBC for MySQL
#Driver         = /usr/lib64/libodbc5.so
#Setup          = /usr/lib64/libodbcmyS.so
Driver64        = /usr/lib64/libmyodbc5.so
Setup64         = /usr/lib64/libodbcmyS.so
FileUsage       = 1
UsageCount      = 3

* Configure ODBC for Debian:


#vim /etc/odbc.ini
[asterisk-cdr]

Description = MySQL Asterisk database
Driver = MySQL
Server = localhost
User = asterisk_user
Password = 123456
Port = 3306
Option    = 3
Database = asterisk_db
Socket                = /var/run/mysqld/mysqld.sock

#vim /etc/odbcinst.ini


[MySQL]

Description  = MySQL driver
Driver          = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so
Setup           = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so
UsageCount = 1

2-Configure Asterisk


#vim /etc/asterisk/res_odbc.conf  
add:


[asterisk-cdr]
enabled => yes
dsn => asterisk-cdr   #name in odbc.ini
username => asterisk_user
password => 123456
pooling => no
limit => 1
pre-connect => yes

#vim /etc/asterisk/cdr_odbc.conf
add:

[global]
dsn=asterisk-cdr
loguniqueid=yes
dispositionstring=yes
table=cdr ;"cdr" is default table name
usegmtime=no ; set to "no" to log in GMT
hrtime=yes ;Enables microsecond accuracy with the billsec and duration fields

3-Setup MySQL


Make sure you have the user entered in the ODBC configs setup and with access from which ever location. Now create the cdr table, this exact create was suggest

CREATE TABLE `cdr` (
 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
 `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 '',
 `lastapp` VARCHAR(200) NOT NULL DEFAULT '',
 `lastdata` VARCHAR(200) NOT NULL DEFAULT '',
 `duration` FLOAT UNSIGNED NULL DEFAULT NULL,
 `billsec` FLOAT UNSIGNED NULL DEFAULT NULL,
 `disposition` ENUM('ANSWERED','BUSY','FAILED','NO ANSWER','CONGESTION') NULL DEFAULT NULL,
 `channel` VARCHAR(50) NULL DEFAULT NULL,
 `dstchannel` VARCHAR(50) NULL DEFAULT NULL,
 `amaflags` VARCHAR(50) NULL DEFAULT NULL,
 `accountcode` VARCHAR(20) NULL DEFAULT NULL,
 `uniqueid` VARCHAR(32) NOT NULL DEFAULT '',
 `userfield` FLOAT UNSIGNED NULL DEFAULT NULL,
 `answer` DATETIME NOT NULL,
 `end` DATETIME NOT NULL,
 PRIMARY KEY (`id`),
 INDEX `calldate` (`calldate`),
 INDEX `dst` (`dst`),
 INDEX `src` (`src`),
 INDEX `dcontext` (`dcontext`),
 INDEX `clid` (`clid`)
)
COLLATE='utf8_bin'
ENGINE=InnoDB;
 

4-Testing it out


At this point if everything is setup correctly, starting asterisk and taking a call should generate a CDR. Start asterisk and give it a shot! Don’t want to be flying blind?  Try out a couple steps to try.

Check isql for ODBC MySQL connectionWith isql you can connect with the ODBC DSN you just created.

# isql asterisk-cdr -v

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>quit


-Check Asterisk for ODBC modules and connection

#asterisk -r
asterisk*CLI>reload

asterisk*CLI> odbc show
 
ODBC DSN Settings
-----------------
Name: asterisk-cdr
DSN: asterisk-cdr
Last connection attempt: 1970-01-01 00:00:00
Pooled: No
Connected: Yes

========================
reference:
-http://chrismendes.com/2014/06/27/mysql-cdr-for-asterisk-w-odbc/
-http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/installing_configuring_odbc.html