5 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
6 use FS::UID qw( adminsuidsetup );
7 use FS::Record qw( qsearch qsearchs );
12 my $user = shift or die &usage;
14 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
15 daemonize1('freeside-cdrrewrited');
19 adminsuidsetup($user);
21 logfile( "%%%FREESIDE_LOG%%%/cdrrewrited-log.". $FS::UID::datasrc );
27 die "not running; cdr-asterisk_forward_rewrite, cdr-charged_party_rewrite ".
28 " and cdr-taqua-accountcode_rewrite conf options are all off\n"
33 my %sessionnum_unmatch = ();
34 my $sessionnum_retry = 4 * 60 * 60; # 4 hours
35 my $sessionnum_giveup = 4 * 24 * 60 * 60; # 4 days
39 #hmm... don't want to do an expensive search with an ever-growing bunch
40 # of unprocessed CDRs during the month... better to mark them all as
41 # rewritten "skipped", i.e. why we're a daemon in the first place
42 # instead of just doing this search like normal CDRs
45 my @recent = grep { ($sessionnum_unmatch{$_} + $sessionnum_retry) > time }
46 keys %sessionnum_unmatch;
47 my $extra_sql = scalar(@recent)
48 ? ' AND acctid NOT IN ('. join(',', @recent). ') '
56 'extra_sql' => 'FOR UPDATE',
58 'extra_sql' => 'WHERE freesidestatus IS NULL '.
59 ' AND freesiderewritestatus IS NULL '.
61 ' LIMIT 1024', #arbitrary, but don't eat too much memory
65 next if $skip{$cdr->acctid};
70 if ( $conf->exists('cdr-asterisk_forward_rewrite')
71 && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
77 warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
78 "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
80 $cdr->charged_party($cdr->dst);
84 push @status, 'asterisk_forward';
88 if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
90 $cdr->set_charged_party;
91 push @status, 'charged_party';
95 if ( $cdr->cdrtypenum == 1
98 $conf->exists('cdr-taqua-accountcode_rewrite') or
99 $conf->exists('cdr-taqua-callerid_rewrite') )
103 #find the matching CDR
104 my %search = ( 'sessionnum' => $cdr->sessionnum );
105 if ( $cdr->lastapp eq 'acctcode' ) {
106 $search{'src'} = $cdr->subscriber;
107 } elsif ( $cdr->lastapp eq 'CallerId' ) {
108 $search{'dst'} = $cdr->subscriber;
110 my $primary = qsearchs('cdr', \%search);
112 unless ( $primary ) {
114 my $cantfind = "can't find primary CDR with session ". $cdr->sessionnum.
115 ", src ". $cdr->subscriber;
116 if ( $cdr->calldate_unix + $sessionnum_giveup < time ) {
117 warn "ERROR: $cantfind; giving up\n";
118 push @status, 'taqua-sessionnum-NOTFOUND';
119 $cdr->status('done'); #so it doesn't try to rate
120 delete $sessionnum_unmatch{$cdr->acctid}; #so it doesn't suck mem
122 warn "WARNING: $cantfind; will keep trying\n";
123 $sessionnum_unmatch{$cdr->acctid} = time;
129 if ( $cdr->lastapp eq 'acctcode' ) {
130 # lastdata contains the dialed account code
131 $primary->accountcode( $cdr->lastdata );
132 push @status, 'taqua-accountcode';
133 } elsif ( $cdr->lastapp eq 'CallerId' ) {
134 # lastdata contains "allowed" or "restricted"
135 # or case variants thereof
136 if ( lc($cdr->lastdata) eq 'restricted' ) {
137 $primary->clid( 'PRIVATE' );
139 push @status, 'taqua-callerid';
141 warn "unknown Taqua service name: ".$cdr->lastapp."\n";
143 #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
144 my $error = $primary->replace if $primary->modified;
146 warn "WARNING: error rewriting primary CDR (will retry): $error\n";
149 $skip{$primary->acctid} = 1;
151 $cdr->status('done'); #so it doesn't try to rate
157 $cdr->freesiderewritestatus(
158 scalar(@status) ? join('/', @status) : 'skipped'
161 my $error = $cdr->replace;
164 warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
166 sleep 30; #i dunno, wait and see if the database comes back?
169 last if sigterm() || sigint();
173 myexit() if sigterm() || sigint();
174 #sleep 1 unless $found;
175 sleep 5 unless $found;
182 $conf->exists('cdr-asterisk_forward_rewrite')
183 || $conf->exists('cdr-charged_party_rewrite')
184 || $conf->exists('cdr-taqua-accountcode_rewrite')
185 || $conf->exists('cdr-taqua-callerid_rewrite')
191 die "Usage:\n\n freeside-cdrrewrited user\n";
196 freeside-cdrrewrited - Real-time daemon for CDR rewriting
204 Runs continuously, searches for CDRs and does forwarded-call rewriting if any
205 of the "cdr-asterisk_forward_rewrite", "cdr-charged_party_rewrite" or
206 "cdr-taqua-accountcode_rewrite" config options are enabled.