3 # Clean stale open sessions from the radacct table.
4 # we only clean up sessions which are older than $back_days
5 # Works with mysql and postgresql
10 $conf=shift||'/usr/local/dialup_admin/conf/admin.conf';
15 or die "Could not open configuration file\n";
18 ($key,$val)=(split /:\s*/,$_);
19 $sql_type = $val if ($key eq 'sql_type');
20 $sql_server = $val if ($key eq 'sql_server');
21 $sql_username = $val if ($key eq 'sql_username');
22 $sql_password = $val if ($key eq 'sql_password');
23 $sql_database = $val if ($key eq 'sql_database');
24 $sql_accounting_table = $val if ($key eq 'sql_accounting_table');
25 $sqlcmd = $val if ($key eq 'sql_command');
29 die "sql_command directive is not set in admin.conf\n" if ($sqlcmd eq '');
30 die "sql command '$sqlcmd' not found or does not seem to be executable\n" if (! -x $sqlcmd);
32 if ($sql_type eq 'mysql'){
33 $sql_password = (!$sql_password) ? '' : "-p$sql_password";
35 $sql_password =~ s/(\W)/\\$1/g;
37 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
38 $date = POSIX::strftime("%Y-%m-%d %T",$sec,$min,$hour,($mday - $back_days),$mon,$year,$wday,$yday,$isdst);
40 if (POSIX::strftime("%Y-%m-%d %T",localtime) eq $date){
41 die "Could not set correct back date.\n";
44 $query = "DELETE FROM $sql_accounting_table WHERE AcctStopTime IS NULL AND AcctStartTime < '$date';";
46 my ($fh, $tmp_filename) = tempfile() or die "Could not open tmp file\n";
49 $command = "$sqlcmd -h$sql_server -u$sql_username $sql_password $sql_database < $tmp_filename" if ($sql_type eq 'mysql');
50 $command = "$sqlcmd -U $sql_username -f $tmp_filename $sql_database" if ($sql_type eq 'pg');
51 $command = "$sqlcmd '$sql_server' '$sql_port' '' '$sql_username' '$sql_password' < $tmp_filename" if ($sql_type eq 'sqlrelay');