\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"); ?>