| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# Copyright (c) 2010 by Precise Networks, Inc. All rights reserved. http://precisenetworksinc.com |
| 4 |
|
| 5 |
# See http://www.asterisk.org for more information about |
| 6 |
# the Asterisk project. Please do not directly contact |
| 7 |
# any of the maintainers of this project for assistance; |
| 8 |
# the project provides a web site, mailing lists and IRC |
| 9 |
# channels for your use. |
| 10 |
# |
| 11 |
# This program is free software, distributed under the terms of |
| 12 |
# the GNU General Public License Version 2. See the LICENSE file |
| 13 |
# at the top of the source tree. |
| 14 |
|
| 15 |
# 2010-01-30 by Patrick Bennett Hagen: original version. |
| 16 |
|
| 17 |
use strict; |
| 18 |
use DBI(); |
| 19 |
|
| 20 |
# main |
| 21 |
!$ARGV[6] && die "Required arguments: <cdr_log_file> <mysql_hostname> <database> <table> <username> <password> <preview|import>\n"; |
| 22 |
|
| 23 |
open(cdr_csv, "<$ARGV[0]" || die "Unable to open $ARGV[0]\n"); |
| 24 |
my @cdr = <cdr_csv>; |
| 25 |
close(cdr_csv); |
| 26 |
|
| 27 |
my $dbh = DBI->connect("DBI:mysql:database=$ARGV[2];host=$ARGV[1]","$ARGV[4]","$ARGV[5]"); |
| 28 |
my $sth = undef; |
| 29 |
my ($accountcode, $src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp, $lastdata, $start, $answer, $end, $duration, $billsec, $disposition, $amastr, $amaflags, $uniqueid, $userfield) = undef; |
| 30 |
|
| 31 |
foreach (@cdr) { |
| 32 |
($accountcode, $_) = getNextField($_); |
| 33 |
($src , $_) = getNextField($_); |
| 34 |
($dst , $_) = getNextField($_); |
| 35 |
($dcontext , $_) = getNextField($_); |
| 36 |
($clid , $_) = getNextField($_); |
| 37 |
$clid =~ s/\\\"\\\"/\\\"/g; |
| 38 |
($channel , $_) = getNextField($_); |
| 39 |
($dstchannel , $_) = getNextField($_); |
| 40 |
($lastapp , $_) = getNextField($_); |
| 41 |
($lastdata , $_) = getNextField($_); |
| 42 |
($start , $_) = getNextField($_); |
| 43 |
($answer , $_) = getNextField($_); |
| 44 |
($end , $_) = getNextField($_, ","); |
| 45 |
($duration , $_) = getNextField($_, ","); |
| 46 |
($billsec , $_) = getNextField($_, ","); |
| 47 |
($disposition, $_) = getNextField($_); |
| 48 |
($amastr , $_) = getNextField($_); |
| 49 |
($amastr eq "'DEFAULT'") && ($amaflags="'0'"); |
| 50 |
($amastr eq "'OMIT'") && ($amaflags="'1'"); |
| 51 |
($amastr eq "'BILLING'") && ($amaflags="'2'"); |
| 52 |
($amastr eq "'DOCUMENTATION'") && ($amaflags="'3'"); |
| 53 |
($uniqueid , $_) = getNextField($_); |
| 54 |
($userfield , $_) = getNextField($_, "\n"); |
| 55 |
|
| 56 |
my $s = "insert into $ARGV[3] (accountcode, src, dst, dcontext, clid, channel, dstchannel, lastapp, lastdata, calldate, duration, billsec, disposition, amaflags, uniqueid, userfield) values ($accountcode, $src, $dst, $dcontext, $clid, $channel, $dstchannel, $lastapp, $lastdata, $start, $duration, $billsec, $disposition, $amaflags, $uniqueid, $userfield)"; |
| 57 |
$sth = $dbh->prepare($s); |
| 58 |
if ($ARGV[6] eq "import") { |
| 59 |
$sth->execute(); |
| 60 |
print "."; |
| 61 |
} |
| 62 |
($ARGV[6] eq "preview") && (print "$s\n\n"); |
| 63 |
} |
| 64 |
$dbh->disconnect(); print "done.\n"; |
| 65 |
# main |
| 66 |
|
| 67 |
sub getNextField { |
| 68 |
my $s = shift; |
| 69 |
my $delimiter = shift; |
| 70 |
(!$delimiter) && ($delimiter = "\",\""); |
| 71 |
my $endPos = index $s, "$delimiter"; |
| 72 |
($delimiter eq ",") && ($endPos++); |
| 73 |
($delimiter eq "\n") && ($endPos++); |
| 74 |
($delimiter eq "\",\"") && ($endPos+=2); |
| 75 |
my $field = substr $s, 0, $endPos, ""; |
| 76 |
$field = substr $field, 0, (length $field) - 1; |
| 77 |
((substr $field, -1) eq "\"") && ($field = substr $field, 0, (length $field) - 1); |
| 78 |
((substr $field, 0, 1) eq "\"") && ($field = substr $field, 1, length $field); |
| 79 |
$field = $dbh->quote($field); |
| 80 |
return $field, $s; |
| 81 |
} # getNextField |