Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
1808 richard 1
#!/usr/bin/perl
2
#
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
6
#
7
use POSIX;
8
use File::Temp qw(tempfile tempdir);
9
 
10
$conf=shift||'/etc/freeradius-web/admin.conf';
11
$back_days = 30;
12
 
13
 
14
open CONF, "<$conf"
15
	or die "Could not open configuration file\n";
16
while(<CONF>){
17
	chomp;
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');
26
}
27
close CONF;
28
 
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);
31
 
32
if ($sql_type eq 'mysql'){
33
	$sql_password = (!$sql_password) ? '' : "-p$sql_password";
34
}
35
$sql_password =~ s/(\W)/\\$1/g;
36
 
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);
39
print "$date\n";
40
if (POSIX::strftime("%Y-%m-%d %T",localtime) eq $date){
41
	die "Could not set correct back date.\n";
42
}
43
 
44
$query = "DELETE FROM $sql_accounting_table WHERE AcctStopTime IS NULL AND AcctStartTime < '$date';";
45
print "$query\n";
46
my ($fh, $tmp_filename) = tempfile() or die "Could not open tmp file\n";
47
print $fh $query;
48
close $fh;
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');
52
`$command`;