Using ODBC with Asterisk – cdr_odbc

So in my previous post, I wrote about how useful ODBC is to Asterisk and I showed how to install it. Now we will explore how we can use it within Asterisk.

First we’ll quickly talk about cdr_odbc. This allows us to store our CDR data directly to a database. Most people are probably going to be using MySQL. And you probably thought that you had to install the Asterisk-Addons to be able to use MySQL (Due to licensing concerns.) Well, not anymore. Not with ODBC. Okay, so I’m going to assume that you have already installed ODBC and recompiled Asterisk as per my previous post.  In our /etc/odbc.ini file we created database connections. Hopefully you have already created the databases. If not, go ahead and do that now.   Here’s the table structure for CDR:

CREATE TABLE IF NOT EXISTS `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 ”,
KEY `calldate` (`calldate`),
KEY `dst` (`dst`),
KEY `accountcode` (`accountcode`),
KEY `dst_2` (`dst`),
KEY `uniqueid` (`uniqueid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

There’s been so much talk about how the CDR system was going to be changed but I never saw any developments from that. ODBC doesn’t support ‘start’,'answer’,'end’ for the CDR. ‘calldate’ is going to be the time that the call hit the PBX.   Anyway, back to it…

You can edit your /etc/asterisk/cdr.conf to include the desired settings you want. I like to enable the following:

[general]
unanswered=yes
loguniqueid=yes
loguserfield=yes

Okay, now edit your cdr_odbc.conf to match the settings from your previous created database and cdr table.

[global]
dsn=asterisk     ;<= This matches your ‘context’ from /etc/odbc.ini
username=asterisk    ;<= Don’t really need these due to the dsn already containing login details.
password=yourpassword
loguniqueid=yes
dispositionstring=yes
table=cdr               ;”cdr” is default table name
usegmtime=no             ; set to “yes” to log in GMT

Restart Asterisk and you should be logging CDR data to your database now. Pretty easy. And you didn’t even have to install Asterisk-Addons. :-)

Leave a Reply