import-cdr-csv-mysql.pl 3.5 KB

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