Upgrade 3CX to v18 and get it hosted free!

cdrsqltocsv

Author image

SQL CDR to CDR CSV

userpagelesnet

I had a situation where Asterisk stopped logging to the text file cdr-csv/Master.csv (It happened twice in 48 hours)

Luckily, I was also logging to Postgresql CDR.

I wrote this PHP script that query’s the SQL CDR and generates a nearly perfect Master.csv output file.

So what I did (to be safe) was copy my inconsistent Master.csv to Master-fubar.csv

Then I ran ./dump.php >Master.csv to re-generate my Master.csv from the SQL CDR.

#!/usr/bin/php -q
<?
$pgdb = pg_connect("dbname=asterisk_cdr");
$qry = pg_query($pgdb,"select accountcode,src,dst,dcontext,clid,channel,dstchannel,lastapp,lastdata,calldate,".
"timestamp with time zone ‘epoch’ + (date_part(‘epoch’,calldate)+duration-billsec) * interval ‘1 second’,".
"timestamp with time zone ‘epoch’ + (date_part(‘epoch’,calldate)+duration) * interval ‘1 second’,".
"duration,billsec,disposition,amaflags from cdr order by calldate");
for($x=0;$x<pg_num_rows($qry);$x++) {
$data = pg_fetch_row($qry);
$z = sizeof($data);
$data[$z-1] = "DOCUMENTATION";
if ($data[14] == "NO ANSWER") {
$data[10] = "";
}
for($y=0;$y<sizeof($data);$y++) {
$data[$y] = ereg_replace("-05$","",$data[$y]);
$data[$y] = str_replace(‘"’,’""’,$data[$y]);
if ($y) { echo ","; }
if (!(($y >= ($z-4)) && ($y <= ($z-3)))) {
$data[$y] = ‘"’.$data[$y].’"’;
}
if (($y==10) && ($data[$y] == ‘""’)) {
$data[$y] = ”;
}
echo $data[$y];
}
echo "\n";
}
?>


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.
Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.