X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=bin%2Fcdr-opensips.import;h=b8169d5236f44f708e666c15a48c947455eb1114;hp=2df6effc6bda7969de310ca243fc0cd380e46785;hb=88e9a56677d343392416c262f976f069157b06cb;hpb=82a358b848e8755e3f710a3beb4232b9e59cf18c diff --git a/bin/cdr-opensips.import b/bin/cdr-opensips.import index 2df6effc6..b8169d523 100755 --- a/bin/cdr-opensips.import +++ b/bin/cdr-opensips.import @@ -10,7 +10,7 @@ use DBI; use Getopt::Std; my %opt; -getopts('H:U:P:D:T:', \%opt); +getopts('H:U:P:D:T:s:e:c:', \%opt); my $user = shift or die &usage; my $dsn = 'dbi:mysql'; @@ -20,6 +20,16 @@ $dsn .= ":host=$opt{H}" if $opt{H}; my $mysql = DBI->connect($dsn, $opt{U}, $opt{P}) or die $DBI::errstr; +my ($start, $end) = ('', ''); +if ( $opt{s} ) { + $start = str2time($opt{s}) or die "can't parse start date $opt{s}\n"; + $start = time2str('%Y-%m-%d', $start); +} +if ( $opt{e} ) { + $end = str2time($opt{e}) or die "can't parse end date $opt{e}\n"; + $end = time2str('%Y-%m-%d', $end); +} + adminsuidsetup $user; my $fsdbh = FS::UID::dbh; @@ -41,7 +51,12 @@ my @cols = ( qw( time ) ); -my $sql = 'SELECT '.join(',', @cols). " FROM $table WHERE freesidestatus IS NULL AND sip_code = 200"; # only want successful calls +my $sql = 'SELECT '.join(',', @cols). " FROM $table". + ' WHERE freesidestatus IS NULL' . + ' AND sip_code = 200 ' . # only want successful calls + ($start && " AND time >= '$start'") . + ($end && " AND time < '$end'") . + ' ORDER BY time'; # should ensure INVITE/ACK/BYE order my $sth = $mysql->prepare($sql); $sth->execute; print "Importing ".$sth->rows." records...\n"; @@ -58,9 +73,38 @@ my $updates = 0; my %cdrs; my $row; while ( $row = $sth->fetchrow_hashref ) { - my ($callid) = $row->{'callid'} =~ /(.*)@/; - my ($src) = $row->{'caller_id'} =~ /^sip:(\d+)@/; - my ($dst) = $row->{'callee_id'} =~ /^sip:(\d+)@/; + my ($callid) = $row->{'callid'}; + $callid =~ s/@.*//; + if ( !$callid ) { + warn $row->{'time'} . ": no callid, skipped.\n"; + next; + } + + #i guess now we're NANPA-centric, but at least we warn on non-numeric numbers + my $src = ''; + my $src_ip = ''; + if ( $row->{'caller_id'} =~ /^sip:(\+1?)?(\w+)@(.*)/ ) { + $src = $2; + my $rest = $3; + if ($rest =~ /^([\d\.]{7,15})/) { + # canonicalize it so that ascii sort order works + $src_ip = sprintf('%03d.%03d.%03d.%03d', split('\.', $1)); + } + } else { + warn "unparseable caller_id ". $row->{'caller_id'}. "\n"; + } + + my $dst = ''; + my $dst_ip = ''; + if ( $row->{'callee_id'} =~ /^sip:(\+1?)?(\w+)@(.*)/ ) { + $dst = $2; + my $rest = $3; + if ($rest =~ /^([\d\.]{7,15})/) { + $dst_ip = sprintf('%03d.%03d.%03d.%03d', split('\.', $1)); + } + } else { + warn "unparseable callee_id ". $row->{'callee_id'}. "\n"; + } my $cdr = $cdrs{$callid}; if ( !$cdr ) { @@ -68,18 +112,23 @@ while ( $row = $sth->fetchrow_hashref ) { uniqueid => $callid, cdrbatchnum => $cdrbatchnum, }); + $cdr->cdrtypenum($opt{c}) if $opt{c}; } my $date = str2time($row->{'time'}); if ( $row->{'method'} eq 'INVITE' ) { $cdr->startdate($date); $cdr->src($src); $cdr->dst($dst); + $cdr->src_ip_addr($src_ip); + $cdr->dst_ip_addr($dst_ip); } elsif ( $row->{'method'} eq 'ACK' ) { $cdr->answerdate($date); + next if !check_cdr($cdr, $src, $dst); } elsif ( $row->{'method'} eq 'BYE' ) { $cdr->enddate($date); + next if !check_cdr($cdr, $src, $dst); } if ( $cdr->startdate and $cdr->answerdate and $cdr->enddate ) { $cdr->duration($cdr->enddate - $cdr->startdate); @@ -110,6 +159,19 @@ if ( keys(%cdrs) ) { $mysql->disconnect; sub usage { - "Usage: \n cdr-opensips.import\n\t[ -H host ]\n\t-D database\n\t-U user\n\t-P password\n\tfreesideuser\n"; + "Usage: \n cdr-opensips.import\n\t[ -H host ]\n\t-D database\n\t-U user\n\t-P password\n\t[ -s start ] [ -e end ] [ -c cdrtypenum ] \n\tfreesideuser\n"; } +sub check_cdr { + # Verify that these records belong to the same call. + # BYE records sometimes have the caller/callee fields swapped. + # We allow empty src/dst so as not to make noise about incomplete calls. If + # this check fails, something is wrong with the source data. + my ($cdr, $a, $b) = @_; + if ( ( $cdr->src and $cdr->src ne $a and $cdr->src ne $b ) + or ( $cdr->dst and $cdr->dst ne $a and $cdr->dst ne $b ) ) { + warn $cdr->uniqueid . ": src/dst mismatch, skipped.\n"; + return 0; + } + return 1; +}