From: ivan Date: Sun, 18 May 2008 22:42:56 +0000 (+0000) Subject: on upgrade, automatically seed from sqlradius databases, and start freeside-sqlradius... X-Git-Tag: root_of_webpay_support~646 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=12587c5314c2a2ee40ab046af1552a40db5e0eaf on upgrade, automatically seed from sqlradius databases, and start freeside-sqlradius-radacctd by default --- diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index fcf28e333..dec4d483d 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -5,13 +5,14 @@ use vars qw( @ISA @EXPORT_OK ); use Exporter; use Tie::IxHash; use FS::UID qw( dbh driver_name ); +use FS::Conf; use FS::Record; use FS::svc_domain; $FS::svc_domain::whois_hack = 1; @ISA = qw( Exporter ); -@EXPORT_OK = qw( upgrade ); +@EXPORT_OK = qw( upgrade upgrade_sqlradius ); =head1 NAME @@ -105,6 +106,70 @@ sub upgrade_data { } +sub upgrade_sqlradius { + #my %opt = @_; + + my $conf = new FS::Conf; + + my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting(); + + foreach my $part_export ( @part_export ) { + my $dbh = DBI->connect( map $part_export->option($_), + qw ( datasrc username password ) ); + + my $str2time = str2time_sql( $dbh->{Driver}->{Name} ); + my $group = "UserName"; + $group .= ",Realm" + if ( ref($part_export) =~ /withdomain/ ); + + my $sth_alter = $dbh->prepare( + "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL" + ); + if ( $sth_alter && $sth_alter->execute ) { + my $sth_update = $dbh->prepare( + "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL" + ) or die $dbh->errstr; + $sth_update->execute or die $sth_update->errstr; + } + + my $sth = $dbh->prepare("SELECT UserName, + Realm, + $str2time max(AcctStartTime)), + $str2time max(AcctStopTime)) + FROM radacct + WHERE FreesideStatus = 'done' + AND AcctStartTime != 0 + AND AcctStopTime != 0 + GROUP BY $group + ") + or die $dbh->errstr; + $sth->execute() or die $sth->errstr; + + while (my $row = $sth->fetchrow_arrayref ) { + my ($username, $realm, $start, $stop) = @$row; + + $username = lc($username) unless $conf->exists('username-uppercase'); + my $extra_sql = ''; + if ( ref($part_export) =~ /withdomain/ ) { + $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain + WHERE svc_domain.svcnum = svc_acct.domsvc ) "; + } + + my $svc_acct = qsearchs( 'svc_acct', + { 'username' => $username }, + '', + $extra_sql, + ); + if ($svc_acct) { + $svc_acct->last_login($start) + if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login); + $svc_acct->last_logout($stop) + if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout); + } + } + } + +} =back diff --git a/FS/FS/part_export/sqlradius.pm b/FS/FS/part_export/sqlradius.pm index 5e63e1004..474f27756 100644 --- a/FS/FS/part_export/sqlradius.pm +++ b/FS/FS/part_export/sqlradius.pm @@ -718,5 +718,27 @@ sub _try_decrement { return 'skipped'; } +### +#class methods +### + +sub all_sqlradius { + #my $class = shift; + + #don't just look for ->can('usage_sessions'), we're sqlradius-specific + # (radiator is supposed to be setup with a radacct table) + #i suppose it would be more slick to look for things that inherit from us.. + + my @part_export = (); + push @part_export, qsearch('part_export', { 'exporttype' => $_ } ) + foreach qw(sqlradius sqlradius_withdomain radiator); + @part_export; +} + +sub all_sqlradius_withaccounting { + my $class = shift; + grep { ! $_->option('ignore_accounting') } $class->_part_export_sqlradius; +} + 1; diff --git a/FS/bin/freeside-sqlradius-radacctd b/FS/bin/freeside-sqlradius-radacctd index 83fd4bfd1..4c5c11a2d 100644 --- a/FS/bin/freeside-sqlradius-radacctd +++ b/FS/bin/freeside-sqlradius-radacctd @@ -9,6 +9,7 @@ use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm); use FS::UID qw(adminsuidsetup); #forksuidsetup driver_name dbh myconnect); use FS::Record qw(qsearch); # qsearchs); use FS::part_export; +use FS::part_export::sqlradius; #use FS::svc_acct; #use FS::cust_svc; @@ -29,17 +30,7 @@ daemonize2(); #-- -#don't just look for ->can('usage_sessions'), we're sqlradius-specific -# (radiator is supposed to be setup with a radacct table) - -@part_export = - qsearch('part_export', { 'exporttype' => 'sqlradius' } ); -push @part_export, - qsearch('part_export', { 'exporttype' => 'sqlradius_withdomain' } ); -push @part_export, - qsearch('part_export', { 'exporttype' => 'radiator' } ); - -@part_export = grep { ! $_->option('ignore_accounting') } @part_export; +my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting(); die "no sqlradius, sqlradius_withdomain or radiator exports without". " ignore_accounting" @@ -129,13 +120,17 @@ freeside-sqlradius-radacctd - Real-time radacct import daemon Imports records from an the SQL radacct tables of all sqlradius, sqlradius_withdomain and radiator exports (except those with the -ignore_accounting flag) and updates the svc_acct.seconds for each account. -Runs as a daemon and updates the database in real-time. +ignore_accounting flag) and updates the following fields in svc_acct (see +L) for each account: last_login, last_logout, seconds, +upbytes, downbytes, totalbytes. Runs as a daemon and updates the database +in real-time. B is a username added by freeside-adduser. =head1 RADIUS DATABASE CHANGES +In 1.7.4+, freeside-upgrade should have taken care of these changes already. + ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL; If you want to ignore the existing accountg records, also do: diff --git a/FS/bin/freeside-upgrade b/FS/bin/freeside-upgrade index e68f5e3fb..c539c2748 100755 --- a/FS/bin/freeside-upgrade +++ b/FS/bin/freeside-upgrade @@ -123,6 +123,9 @@ $dbh = adminsuidsetup($user); upgrade() unless $DRY_RUN || $opt_s; +upgrade_sqlradius() + unless $DRY_RUN || $opt_s; + $dbh->commit or die $dbh->errstr; $dbh->disconnect or die $dbh->errstr; @@ -135,7 +138,7 @@ sub dbdef_create { # reverse engineer the schema from the DB and save to file } sub usage { - die "Usage:\n freeside-upgrade [ -d ] [ -q | -v ] user\n"; + die "Usage:\n freeside-upgrade [ -d ] [ -s ] [ -q | -v ] user\n"; } =head1 NAME diff --git a/init.d/freeside-init b/init.d/freeside-init index edca3c964..ddee5d2d7 100644 --- a/init.d/freeside-init +++ b/init.d/freeside-init @@ -21,9 +21,9 @@ case "$1" in freeside-queued $QUEUED_USER echo "done." - #echo -n "Starting freeside-sqlradius-radacctd: " - #freeside-sqlradius-radacctd $QUEUED_USER - #echo "done." + echo -n "Starting freeside-sqlradius-radacctd: " + freeside-sqlradius-radacctd $QUEUED_USER + echo "done." echo -n "Starting freeside-prepaidd: " freeside-prepaidd $QUEUED_USER