login | register
Sat 17 of May, 2008 [05:30 UTC]

voip-info.org

Search with Google
Search this site with Google. Results may not include recent changes.
 
Google Ads
Shoutbox
  • Juan Ortega, Thu 15 of May, 2008 [10:33 UTC]: Hi everybody, I'm Juan, an ITCom student, and I need to know what basic elements I need to create a VoIP network. Can anybody helpme, please?,Thank you very much
  • gineta, Wed 14 of May, 2008 [03:58 UTC]: any here not fine the configuration of firewall juniper -screem for VOIP asterisk????
  • Anoop Prabhakaran, Tue 13 of May, 2008 [12:16 UTC]: I am developing Asterisk IVR, Whenever i make a internation call to the IVR system, the DTMF is not getting detected properly, this happens only for the first time, second call onwards system works fine. why this is happening
  • joe, Mon 12 of May, 2008 [04:27 UTC]: Is there an opensource browser based softphone, or a system like Busta where everything is not manages through their website?
  • Nick Barnes, Fri 09 of May, 2008 [11:36 UTC]: Christopher - yesterday I tried an Asterisk install on a CentOS 5.1 box with stock GUI and it all worked fine. Sorry I can't help.
  • aero, Fri 09 of May, 2008 [08:20 UTC]: can someone help me out on this, i tried to play some sound files on my asterisk box and this is the error message i got. WARNING[4429]: format_wav.c:169 check_header: Unexpected freqency 22050 May 8 11:17:39 WARNING[4433]: codec_gsm.c:194 gsmtolin_fra
  • Christopher Faust, Thu 08 of May, 2008 [14:15 UTC]: I beleive that I may have to change something in the xserver configuration. Please advise
  • Christopher Faust, Thu 08 of May, 2008 [14:14 UTC]: Everything was perfect. In the bios I have increased the memory allocated Still receive input not supported on my display.
  • Christopher Faust, Thu 08 of May, 2008 [14:13 UTC]: This would not be my main box. I am doing some testing to see if I can install zaptel and asterisk 1.4 on a full centos 5.1 box with development software Its bizzare, because before I went through the asterisk and zaptel installation everything was perfe
  • Nick Barnes, Thu 08 of May, 2008 [13:44 UTC]: Christopher - I can't see any way in which an Asterisk installation would muck your GUI, but remember that it is advised not to use a GUI on an Asterisk box anyway.
Server Stats
  • Execution time: 0.36s
  • Memory usage: 2.19MB
  • Database queries: 33
  • GZIP: Disabled
  • Server load: 0.43

Asterisk CDR csv mysql import

Here is another take on a PHP script to import cdr.csv files into a mysql
cdr database. Useful for transitioning from text based cdr files to
database or even for nightly imports.

This script is safe to run multiple times on a log file that keeps growing
because it first checks the database table to find the oldest record and
then only imports CDRs that are NEWER than the oldest database record.
Note: Make sure you run this script before you activate mysql logging in Asterisk otherwise this script will import nothing since the newest record in the DB will be newer than the records in the Master.csv file.
Copy and paste the script into a text file, for example "importcdr.php" and then use php at the command line to execute it as follows:

  1. php importcdr.php Master.csv

UPDATE August 23, 2007: Still Working fine, tested with Asterisk 1.2.16, Mysql 4.1.20 and PHP 5.0.4

UPDATE July 21, 2005: I fixed this code so it should copy and paste properly now. The wiki doesn't like square brakets!

NOTE: The fields in the Master.csv file can vary depending on your settings. There is a comment in the source about it.

UPDATE December 4, 2006: This script still works fine with Asterisk 1.2.13 and PHP5.


<?
/*** process asterisk cdr file (Master.csv) insert usage
 * values into a mysql database which is created for use
 * with the Asterisk_addons cdr_addon_mysql.so
 * The script will only insert NEW records so it is safe
 * to run on the same log over-and-over such as in the
 * case where logs have not been rotated.
 *
 * Author: John Lange (john.lange@open-it.ca)
 * Date: May 4, 2005. Updated July 21, 2005
 *
 * Here is what the script does:
 *
 * 1) Find the last log entry in the database cdr table.
 * 2) scan the asterisk logs until the dates are larger than the last log entry (so we don't duplicate entries)
 * 3) parse each row from the text log and insert it into the database.
 * 
 */

$locale_db_host  = 'localhost';
$locale_db_name  = 'asteriskcdrdb';
$locale_db_login = 'asteriskcdruser';
$locale_db_pass  = 'password';

if($argc == 2) {
    $logfile = $argv[1];
} else {
    print("Usage ".$argv[0]." <filename>\n");
    print("Where filename is the path to the Asterisk csv file to import (Master.csv)\n");
    print("This script is safe to run multiple times on a growing log file as it only imports records that are newer than the database\n");
    exit(0);
}

// connect to db
$linkmb = mysql_connect($locale_db_host, $locale_db_login, $locale_db_pass)
       or die("Could not connect : " . mysql_error());
//echo "Connected successfully\n";
mysql_select_db($locale_db_name, $linkmb) or die("Could not select database $locale_db_name");

/** 1) Find the last log entry **/
// look in cdr table to see when the last entry was made.
// this establishes the starting point for the asterisk data.
$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 = date("Y-m-d H:i:s", $result_array['voip_stamp']);
$lasttimestamp = $result_array['calldate'];

//** 2) Find new records in the asterisk log file. **

$rows = 0;
$handle = fopen($logfile, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // NOTE: the fields in Master.csv can vary. This should work by default on all installations but you may have to edit the next line to match your configuration
    list($accountcode,$src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp, $lastdata, $start, $answer, $end, $duration,
     $billsec, $disposition, $amaflags ) = $data;
    
    // 3) parse each row and add to the database
    if(strtotime($end) > $lasttimestamp) { // we found a new record so add it to the DB
        $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");
?>



see also * Asterisk cdr csv

Created by John Lange, Last modification by Jera on Thu 17 of Jan, 2008 [15:56 UTC]

Comments Filter

by cbermudez on Wednesday 25 of April, 2007 [18:47:08 UTC]
i'm testing this but the records imported to the DB is whitout date, any hints?
In the cvs the records are fine.
EDITED
nevermind, it's solved now.

text has errors

by monachus on Friday 03 of June, 2005 [22:12:29 UTC]
log in and select 'edit' to get the php script without wiki-ized modifications that render the code non-functional.

Please update this page with new information, just login and click on the "Edit" or "Add Comment" button above. Get a free login here: Register Thanks! - support@voip-info.org

Page Changes | Comments

Sponsored by:

Terms of Service Privacy Policy
© 2003-2008 VOIP-Info.org LLC

Powered by bitweaver