by brett05 » Sat May 22, 2010 10:17 am
i use cdr before
and you need to have a script that convert csv to cdr mysql database .
i will show you my tips:
asterisk store in :/var/log/asterisk/cdr-csv/Master.csv
so what we need here we need to convert this csv file to a mysql table.
first 1=
we create a cdr table into asterisk database >
USE asterisk;
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 ''
);
ALTER TABLE `cdr` ADD `uniqueid` VARCHAR(32) NOT NULL default '';
ALTER TABLE `cdr` ADD INDEX ( `calldate` );
ALTER TABLE `cdr` ADD INDEX ( `dst` );
ALTER TABLE `cdr` ADD INDEX ( `accountcode` );
then we need a script thant convert csv to mysql:
so do this :
cd /usr/src
then pico or nano or vim a file named csv2cdr.php and put in it this :
<?PHP
$locale_db_host = 'localhost';
$locale_db_name = 'asterisk';
$locale_db_login = 'XXXXX';
$locale_db_pass = 'XXXXX';
$logfile = "/var/log/asterisk/cdr-csv/Master.csv";
$linkmb = mysql_connect($locale_db_host, $locale_db_login, $locale_db_pass)
or die("Could not connect : " . mysql_error());
mysql_select_db($locale_db_name, $linkmb)
or die("Could not select database $locale_db_name");
$sql="SELECT UNIX_TIMESTAMP(calldate) as calldate FROM cdr ORDER BY calldate DESC LIMIT 1";
if(!($result = mysql_query($sql, $linkmb))) {
print("Invalid query: " . mysql_error()."\n");
print("SQL: $sql\n");
die();
}
$result_array = mysql_fetch_array($result);
$lasttimestamp = $result_array['calldate'];
$rows = 0;
$handle = fopen($logfile, "r");
while ( ($data = fgetcsv($handle, 1000, ",") ) !== FALSE) {
list($accountcode, $src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp,
$lastdata, $start, $answer, $end, $duration, $billsec, $disposition,
$amaflags ) = $data;
if (strtotime($end) > $lasttimestamp) {
$sql = "INSERT INTO cdr (calldate, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata, duration, billsec, disposition, amaflags, accountcode) VALUES('$end', '".mysql_real_escape_string($clid)."', '$src', '$dst', '$dcontext', '$channel', '$dstchannel', '$lastapp', '$lastdata', '$duration', '$billsec','$disposition', '$amaflags', '$accountcode')";
if(!($result2 = mysql_query($sql, $linkmb))) {
print("Invalid query: " . mysql_error()."\n");
print("SQL: $sql\n");
die();
}
$rows++;
}
}
fclose($handle);
print("$rows imported\n");
?>
ok here we have all this we just miss to run the php file to have csv2cdr database.
so do this : php /usr/src/csv2cdr.php and you will see after that the table cdr in the database asterisk have all the information about the csv file .
you can also do a crontab if you want :
@hourly php /usr/src/csv2cdr.php
enjoy
Jasperreports & Queuemetrics & SugarCRM integration - Customization and Add-ons
Freepbx||Billing||Centos||Opensuse||Debian||Centos||Fedora||Sangoma||Diguim