1 package FS::part_export::phone_sqlradius;
3 use vars qw(@ISA $DEBUG %info );
5 use FS::Record qw( dbh str2time_sql ); #qsearch qsearchs );
7 use FS::part_export::sqlradius qw(sqlradius_connect);
10 #use Carp qw( cluck );
12 @ISA = qw(FS::part_export::sqlradius);
16 tie %options, 'Tie::IxHash',
17 'datasrc' => { label=>'DBI data source ' },
18 'username' => { label=>'Database username' },
19 'password' => { label=>'Database password' },
20 'ignore_accounting' => {
22 label => 'Ignore accounting records from this database'
26 label => 'Hide IP address information on session reports',
30 label => 'Hide download/upload information on session reports',
33 #should be default for this one, right?
34 #'show_called_station' => {
36 # label => 'Show the Called-Station-ID on session reports',
43 'desc' => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS) for phone provisioning and rating',
44 'options' => \%options,
46 Real-time export of <b>radcheck</b> table
47 to any SQL database for <a href="http://www.freeradius.org/">FreeRADIUS</a>
48 or <a href="http://radius.innercite.com/">ICRADIUS</a>.
51 This export is for phone/VoIP provisioning and rating. For a regular RADIUS
52 export, see sqlradius.
55 <!--An existing RADIUS database will be updated in realtime, but you can use
56 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Developer/bin/freeside-phone_sqlradius-reset">freeside-phone_sqlradius-reset</a>
57 to delete the entire RADIUS database and repopulate the tables from the
63 <a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>
65 <a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
66 for the exact syntax of a DBI data source.
71 sub rebless { shift; }
74 my($self, $svc_phone) = (shift, shift);
75 $svc_phone->countrycode. $svc_phone->phonenum;
78 sub _export_suspend {}
79 sub _export_unsuspend {}
81 #probably harmless that we ->can('usage_sessions').... ?
83 #we want to feed these into CDRs, not update svc_acct records
88 my $dbh = sqlradius_connect( map $self->option($_),
89 qw( datasrc username password ) );
91 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
93 my @fields = qw( radacctid username realm acctsessiontime );
98 my $sth = $dbh->prepare("
99 SELECT RadAcctId, UserName, AcctSessionTime,
100 $str2time AcctStartTime), $str2time AcctStopTime),
101 CallingStationID, CalledStationID
103 WHERE FreesideStatus IS NULL
104 AND AcctStopTime != 0
105 ") or die $dbh->errstr;
106 $sth->execute() or die $sth->errstr;
108 while ( my $row = $sth->fetchrow_arrayref ) {
109 my( $RadAcctId, $UserName, $AcctSessionTime,
110 $AcctStartTime, $AcctStopTime,
111 $CallingStationID, $CalledStationID,
113 warn "processing record: ".
114 "$RadAcctId ($UserName for ${AcctSessionTime}s"
117 my $oldAutoCommit = $FS::UID::AutoCommit; # can't undo side effects, but at
118 local $FS::UID::AutoCommit = 0; # least we can avoid over counting
120 my $cdr = new FS::cdr {
121 'src' => $CallingStationID,
122 'charged_party' => $UserName,
123 'dst' => $CalledStationID,
124 'startdate' => $AcctStartTime,
125 'enddate' => $AcctStopTime,
126 'duration' => $AcctStopTime - $AcctStartTime,
127 'billsec' => $AcctSessionTime,
130 my $errinfo = "for RADIUS detail RadAcctID $RadAcctId ".
131 "(UserName $UserName)";
133 my $error = $cdr->insert;
134 my $status = $error ? 'skipped' : 'done';
136 warn "setting FreesideStatus to $status $errinfo\n" if $DEBUG;
137 my $psth = $dbh->prepare("UPDATE radacct
138 SET FreesideStatus = ?
140 ) or die $dbh->errstr;
141 $psth->execute($status, $RadAcctId) or die $psth->errstr;
143 $fdbh->commit or die $fdbh->errstr if $oldAutoCommit;