RT# 81961 Repair broken links in POD documentation
[freeside.git] / FS / bin / freeside-sqlradius-radacctd
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw( @part_export );
5 use subs qw(myshutdown);
6 use POSIX qw(:sys_wait_h);
7 #use IO::File;
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);
11 use FS::part_export;
12 use FS::part_export::sqlradius;
13 #use FS::svc_acct;
14 #use FS::cust_svc;
15
16 my $user = shift or die &usage;
17
18 #daemonize1('freeside-sqlradius-radacctd', $user); #keep unique pid files w/multi installs
19 daemonize1('freeside-sqlradius-radacctd');
20
21 drop_root();
22
23 #$ENV{HOME} = (getpwuid($>))[7]; #for ssh
24
25 adminsuidsetup $user;
26
27 logfile( "%%%FREESIDE_LOG%%%/sqlradius-radacctd-log.". $FS::UID::datasrc );
28
29 daemonize2();
30
31 #--
32
33 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
34
35 die "no sqlradius, sqlradius_withdomain, radiator or phone_sqlradius exports".
36     " without ignore_accounting"
37   unless @part_export;
38
39 while (1) {
40
41   #fork off one kid per export (machine)
42   # _>{'_radacct_kid'} is an evil kludge
43   foreach my $part_export ( grep ! $_->{'_radacct_kid'}, @part_export ) {
44  
45     defined( my $pid = fork ) or do {
46       warn "WARNING: can't fork to spawn child for ". $part_export->machine;
47       next;
48     };
49
50     if ( $pid ) {
51       $part_export->{'_radacct_kid'} = $pid;
52       warn "child $pid spawned for ". $part_export->machine;
53     } else { #kid time
54
55       adminsuidsetup($user); #get our own db handle
56
57       until ( sigint || sigterm ) {
58         $part_export->update_svc();
59         sleep 1;
60       }
61
62       warn "child for ". $part_export->machine. " done";
63       exit;
64
65     } #eo kid
66
67   }
68
69   #reap up any kids that died...
70   &reap_kids;
71
72   myshutdown() if sigterm() || sigint();
73
74   sleep 5;
75 }
76
77 #-- 
78
79 sub myshutdown {
80   &reap_kids;
81
82   #kill all the kids
83   kill 'TERM', $_ foreach grep $_, map $_->{'_radacct_kid'}, @part_export;
84
85   my $wait = 12; #wait up to 1 minute
86   while ( ( grep $_->{'_radacct_kid'}, @part_export ) && $wait-- ) {
87     warn "waiting for children to terminate";
88     sleep 5;
89     &reap_kids;
90   }
91   warn "abandoning children" if grep $_->{'_radacct_kid'}, @part_export;
92   die "exiting";
93 }
94
95 sub reap_kids {
96   #warn "reaping kids\n";
97   foreach my $part_export ( grep $_->{'_radacct_kid'}, @part_export ) {
98     my $pid = $part_export->{'_radacct_kid'};
99     my $kid = waitpid($pid, WNOHANG);
100     if ( $kid > 0 ) {
101       $part_export->{'_radacct_kid'} = '';
102     }
103   }
104   #warn "done reaping\n";
105 }
106
107 sub usage {
108   die "Usage:\n\n  freeside-sqlradius-radacctd user\n";
109 }
110
111 =head1 NAME
112
113 freeside-sqlradius-radacctd - Real-time radacct import daemon
114
115 =head1 SYNOPSIS
116
117   freeside-sqlradius-radacctd username
118
119 =head1 DESCRIPTION
120
121 Imports records from an the SQL radacct tables of all sqlradius, 
122 sqlradius_withdomain and radiator exports (except those with the
123 ignore_accounting flag) and updates the following fields in svc_acct (see
124 L<FS::svc_acct>) for each account: last_login, last_logout, seconds,
125 upbytes, downbytes, totalbytes.  Runs as a daemon and updates the database
126 in real-time.
127
128 B<username> is a username added by freeside-adduser.
129
130 =head1 RADIUS DATABASE CHANGES
131
132 In 1.7.4+, freeside-upgrade should have taken care of these changes already.
133
134 ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL;
135
136 If you want to ignore the existing accountg records, also do:
137
138 UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL;
139
140 =head1 SEE ALSO
141
142 =cut
143
144 1;
145