login | register
Sat 17 of May, 2008 [06:27 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.20s
  • Memory usage: 2.20MB
  • Database queries: 29
  • GZIP: Disabled
  • Server load: 0.55

Asterisk CDR csv conversion mysql

I hope this can help others, so this is it.
Use it at your own risk. I have test it on 3 separate systems without any problem.

Take care to edit the following files taking into consideration your own settings.
If you have all the CDR info in the Master.csv too, then delete all the data from the 'cdr' table in MySQL before running the script below in order to prevent duplicate records.

In my example, I have the following config:
CDR database: asteriskcdrdb
CDR table: cdr
CVS file: /var/log/asterisk/cdr-csv/Master.csv

1. Create a file named 'impcdr2sql' with the following content:

#!/bin/bash
# make a copy of the original Master.csv file to Master.csv.mod
cp -vf /var/log/asterisk/cdr-csv/Master.csv /var/log/asterisk/cdr-csv/Master.csv.mod
#  format the file to comply with the MySQL data (delete '"' chars when needed)
#  use a VIM script (nofielddelims.vim) for this purpose
ex /var/log/asterisk/cdr-csv/Master.csv.mod -c ":sourcenofielddelims.vim" -c ":exit"
# run the MySQL commands from the cmd.sql file
mysql < cmd.sql

2. Enter the command to make the script executable:

chmod 755 impcdr2sql

3. Create a file named 'nofielddelims.vim' with the following content:

"
" Delete '"' chars at the beginning of the line
"
:%s/^"//
"
" Delete '"' chars at the end of the line
"
:%s/"$//
"
" Delete '"' chars near the ',' char
"
:%s/",/,/g
:%s/,"/,/g
"
" Replace '""' by '"'
"
:%s/""/"/g

4. Create a file named 'cmd.sql' with the following content:

use asteriskcdrdb;
ALTER TABLE `cdr` ADD `tmp1` VARCHAR(30)  DEFAULT "x" NOT NULL;
ALTER TABLE `cdr` ADD `tmp2` VARCHAR(30)  DEFAULT "y" NOT NULL;
LOAD DATA INFILE '/var/log/asterisk/cdr-csv/Master.csv.mod'
replace INTO TABLE cdr
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(accountcode,src,dst,dcontext,clid,channel,dstchannel,lastapp,lastdata,calldate,tmp1,tmp2,duration,billsec,disposition,amaflags,uniqueid,userfield);
ALTER TABLE `cdr` DROP `tmp1`;
ALTER TABLE `cdr` DROP `tmp2`;

5. Keep all the files in the same directory. All you need to do is to run the script:

./impcdr2sql

as root or as an user with full rights on the asteriskcdrdb database and cdrtable
E... voila!
All your old data from Master.csv is now in the MySQL database in thecorrect format (I hope).

Please feel free to make any improvements you want.
I'm not a Linux expert.

Best regards to you all,
Dan

Another solution that's much simpler and is currently used to update the database each 5 minutes is this:
ard@patty:~$ cat cdr.sql 
create database asterisk;
use asterisk;
create table cdr (
        accountcode varchar (30),
        src varchar(64),
        dst varchar(64),
        dcontext varchar(32),
        clid varchar(32),
        channel varchar(32),
        dstchannel varchar(32),
        lastapp varchar(32),
        lastdata varchar(64),
        calldate timestamp NOT NULL,
        callpickup timestamp ,
        callhangup timestamp ,
        duration int(8) unsigned default NULL,
        billsec int(8) unsigned default NULL,
        status varchar(32),
        userfield varchar(128),
        PRIMARY KEY (clid,channel,calldate)
);
ard@patty:~$ cat read-master-csv 
#!/bin/sh
ssh 192.168.1.10 cat /var/log/asterisk/cdr-csv/Master.csv|
sed 's/,,/,"",/;s/\(^.*$\)/REPLACE INTO cdr VALUES\(\1\);/'|
mysql -u root asterisk

ard@patty:~$ crontab -l
*/5 * * * * /home/ard/read-master-csv
ard@patty:~$ 

Well, you get the idea.
(Of course this is only temporarily until asterisk does it straight away. On the other hand, having text files as originals is never a bad idea.)



http://kaneda.bohater.net/files/asterisk_csv2mysql_converter.sh

#!/bin/bash
# simple asterisk csv2mysql converter
# Before insert - CHECK 2 times everything :]
# Works for my configuration on 1.2.18/1.4.2/1.4.13 asterisk
# ver 20071115
# http://kaneda.bohater.net


if [ -z "$1" ] ; then
 echo "Error: Choose csv file"
 echo "Example: $0 ./Master.csv"
 exit
fi

export IFS='
'

for linia in `cat $1|sed s/'",,"'/'","","'/g` ; do

 clid=`echo $linia|awk -F '","' '{print $2}'| sed s/'"'//g`
 src=`echo $linia|awk -F '","' '{print $5}'| sed s/'"'//g`
 dst=`echo $linia|awk -F '","' '{print $3}'| sed s/'"'//g|awk '{print $1}'`
 dcontext=`echo $linia|awk -F '","' '{print $4}'| sed s/'"'//g`
 channel=`echo $linia|awk -F '","' '{print $6}'| sed s/'"'//g`
 dstchannel=`echo $linia|awk -F '","' '{print $7}'| sed s/'"'//g`
 lastapp=`echo $linia|awk -F '","' '{print $8}'| sed s/'"'//g`
 lastdata=`echo $linia|awk -F '","' '{print $9}'| sed s/'"'//g`
 start=`echo $linia|awk -F '","' '{print $10}'| sed s/'"'//g`
 answer=`echo $linia|awk -F '","' '{print $11}'| sed s/'"'//g`
 end=`echo $linia|awk -F '","' '{print $12}'| sed s/'"'//g`

 duration=`echo $linia|awk -F ',"DOCUMENTATION' '{print $1}'|awk -F "," '{print $(NF-2)}'`
 billsec=`echo $linia|awk -F ',"DOCUMENTATION' '{print $1}'|awk -F "," '{print $(NF-1)}'`
 disposition=`echo $linia|awk -F ',"DOCUMENTATION' '{print $1}'|awk -F "," '{print $NF}'`

 #DOCUMENTATION == 3
 amaflags="3"
 accountcode=""
 userfield=""

 echo "insert into cdr values ('$start','$clid','$src','$dst','$dcontext','$channel','$dstchannel','$lastapp','$lastdata','$duration','$billsec','$disposition','$amaflags','$accountcode','$userfield');"

done


My contribution: (simplest yet, pure sql):

USE asterisk
LOAD DATA LOCAL INFILE '/var/log/asterisk/cdr-csv/Master.csv'
INTO TABLE cdr
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(accountcode, src, dst, dcontext, clid, channel, dstchannel, lastapp, lastdata, calldate, duration, billsec, disposition, amaflags);

in a file named cdr-import.sql, then

cat cdr-import.sql | mysql -u asterisk -p
            
This assumes the table is already setup, and that you have a password on the mySQL account (you have, haven't you?)

Back to Asterisk billing
Created by flavour, Last modification by ichneumon on Thu 07 of Feb, 2008 [02:21 UTC]

Comments Filter

Help me!

by sharon on Tuesday 23 of May, 2006 [08:34:01 UTC]
running ex /var/log/asterisk/cdr-csv/Master.csv.mod -c ":sourcenofielddelims.vim" -c ":exit"
or ex /var/log/asterisk/cdr-csv/Master.csv.mod -c ":nofielddelims.vim" -c ":exit"
alway appear the following wrong info:"Not an editor command: :sourcenofielddelims.vim"

Why ,Thanks a lot.
Edit

Re: mysql problems

by Anonymous on Monday 07 of February, 2005 [23:01:33 UTC]
It's the command to query a MySQL database. (:idea:)
Edit

mysql problems

by Anonymous on Wednesday 25 of August, 2004 [17:21:50 UTC]
i had some problems with the sentence:

mysql

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