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',
40 #'overlimit_groups' => { label => 'Radius groups to assign to svc_acct which has exceeded its bandwidth or time limit', } ,
41 #'groups_susp_reason' => { label =>
42 # 'Radius group mapping to reason (via template user) (svcnum|username|username@domain reasonnum|reason)',
50 'desc' => 'Real-time export to SQL-backed RADIUS (FreeRADIUS, ICRADIUS) for phone provisioning and rating',
51 'options' => \%options,
53 Real-time export of <b>radcheck</b> table
54 <!--, <b>radreply</b> and <b>usergroup</b>-- tables>
55 to any SQL database for <a href="http://www.freeradius.org/">FreeRADIUS</a>
56 or <a href="http://radius.innercite.com/">ICRADIUS</a>.
59 This export is for phone/VoIP provisioning and rating. For a regular RADIUS
60 export, see sqlradius.
63 <!--An existing RADIUS database will be updated in realtime, but you can use
64 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Developer/bin/freeside-phone_sqlradius-reset">freeside-phone_sqlradius-reset</a>
65 to delete the entire RADIUS database and repopulate the tables from the
71 <a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>
73 <a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
74 for the exact syntax of a DBI data source.
79 sub rebless { shift; }
82 my($self, $svc_phone) = (shift, shift);
83 $svc_phone->countrycode. $svc_phone->phonenum;
86 sub _export_suspend {}
87 sub _export_unsuspend {}
89 #probably harmless that we ->can('usage_sessions').... ?
91 #we want to feed these into CDRs, not update svc_acct records
96 my $dbh = sqlradius_connect( map $self->option($_),
97 qw( datasrc username password ) );
99 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
101 my @fields = qw( radacctid username realm acctsessiontime );
106 my $sth = $dbh->prepare("
107 SELECT RadAcctId, UserName, AcctSessionTime,
108 $str2time AcctStartTime), $str2time AcctStopTime),
109 CallingStationID, CalledStationID
111 WHERE FreesideStatus IS NULL
112 AND AcctStopTime != 0
113 ") or die $dbh->errstr;
114 $sth->execute() or die $sth->errstr;
116 while ( my $row = $sth->fetchrow_arrayref ) {
117 my( $RadAcctId, $UserName, $AcctSessionTime,
118 $AcctStartTime, $AcctStopTime,
119 $CallingStationID, $CalledStationID,
121 warn "processing record: ".
122 "$RadAcctId ($UserName for ${AcctSessionTime}s"
125 my $oldAutoCommit = $FS::UID::AutoCommit; # can't undo side effects, but at
126 local $FS::UID::AutoCommit = 0; # least we can avoid over counting
128 my $cdr = new FS::cdr {
129 'src' => $CallingStationID,
130 'charged_party' => $UserName,
131 'dst' => $CalledStationID,
132 'startdate' => $AcctStartTime,
133 'enddate' => $AcctStopTime,
134 'duration' => $AcctStopTime - $AcctStartTime,
135 'billsec' => $AcctSessionTime,
138 my $errinfo = "for RADIUS detail RadAcctID $RadAcctId ".
139 "(UserName $UserName)";
141 my $error = $cdr->insert;
142 my $status = $error ? 'skipped' : 'done';
144 warn "setting FreesideStatus to $status $errinfo\n" if $DEBUG;
145 my $psth = $dbh->prepare("UPDATE radacct
146 SET FreesideStatus = ?
148 ) or die $dbh->errstr;
149 $psth->execute($status, $RadAcctId) or die $psth->errstr;
151 $fdbh->commit or die $fdbh->errstr if $oldAutoCommit;