4 use vars qw( @part_export );
5 use subs qw(myshutdown);
6 use POSIX qw(:sys_wait_h);
8 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
9 use FS::UID qw(adminsuidsetup); #forksuidsetup driver_name dbh myconnect);
10 use FS::Record qw(qsearch); # qsearchs);
15 my $user = shift or die &usage;
17 #daemonize1('freeside-sqlradius-radacctd', $user); #keep unique pid files w/multi installs
18 daemonize1('freeside-sqlradius-radacctd');
22 #$ENV{HOME} = (getpwuid($>))[7]; #for ssh
26 logfile( "/usr/local/etc/freeside/sqlradius-radacctd-log.". $FS::UID::datasrc );
33 qsearch('part_export', { 'exporttype' => 'sqlradius' } );
35 qsearch('part_export', { 'exporttype' => 'sqlradius_withdomain' } );
37 @part_export = grep { ! $_->option('ignore_accounting') } @part_export;
39 die "no sqlradius or sqlradius_withdomain exports without ignore_accounting"
44 #fork off one kid per export (machine)
45 # _>{'_radacct_kid'} is an evil kludge
46 foreach my $part_export ( grep ! $_->{'_radacct_kid'}, @part_export ) {
48 defined( my $pid = fork ) or do {
49 warn "WARNING: can't fork to spawn child for ". $part_export->machine;
54 $part_export->{'_radacct_kid'} = $pid;
55 warn "child $pid spawned for ". $part_export->machine;
58 adminsuidsetup($user); #get our own db handle
60 until ( sigint || sigterm ) {
61 $part_export->update_svc_acct();
65 warn "child for ". $part_export->machine. " done";
72 #reap up any kids that died...
75 myshutdown() if sigterm() || sigint();
86 kill 'TERM', $_ foreach grep $_, map $_->{'_radacct_kid'}, @part_export;
88 my $wait = 12; #wait up to 1 minute
89 while ( ( grep $_->{'_radacct_kid'}, @part_export ) && $wait-- ) {
90 warn "waiting for children to terminate";
94 warn "abandoning children" if grep $_->{'_radacct_kid'}, @part_export;
99 #warn "reaping kids\n";
100 foreach my $part_export ( grep $_->{'_radacct_kid'}, @part_export ) {
101 my $pid = $part_export->{'_radacct_kid'};
102 my $kid = waitpid($pid, WNOHANG);
104 $part_export->{'_radacct_kid'} = '';
107 #warn "done reaping\n";
111 die "Usage:\n\n freeside-sqlradius-radacctd user\n";
116 freeside-sqlradius-radacctd - Real-time radacct import daemon
120 freeside-sqlradius-radacctd username
124 Imports records from an the SQL radacct tables of all sqlradius and
125 sqlradius_withdomain exports (except those with the ignore_accounting flag) and
126 updates the svc_acct.seconds for each account. Runs as a daemon and updates
127 the database in real-time.
129 B<username> is a username added by freeside-adduser.
131 =head1 RADIUS DATABASE CHANGES
133 ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL;
135 If you want to ignore the existing accountg records, also do:
137 UPDATE TABLE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL;