Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
1808 richard 1
#!/usr/bin/perl
2
#
3
# Delete sessions from the radacct table which are older than $back_days
4
# Works with mysql and postgresql
5
#
6
use POSIX;
7
use File::Temp qw(tempfile tempdir);
8
 
9
$conf=shift||'/etc/freeradius-web/admin.conf';
10
$back_days = 365;
11
 
12
 
13
open CONF, "<$conf"
14
	or die "Could not open configuration file\n";
15
while(<CONF>){
16
	chomp;
17
	($key,$val)=(split /:\s*/,$_);
18
	$sql_type = $val if ($key eq 'sql_type');
19
	$sql_server = $val if ($key eq 'sql_server');
20
	$sql_username = $val if ($key eq 'sql_username');
21
	$sql_password = $val if ($key eq 'sql_password');
22
	$sql_database = $val if ($key eq 'sql_database');
23
	$sql_accounting_table = $val if ($key eq 'sql_accounting_table');
24
	$sqlcmd = $val if ($key eq 'sql_command');
25
}
26
close CONF;
27
 
28
die "sql_command directive is not set in admin.conf\n" if ($sqlcmd eq '');
29
die "sql command '$sqlcmd' not found or does not seem to be executable\n" if (! -x $sqlcmd);
30
 
31
if ($sql_type eq 'mysql'){
32
	$sql_password = (!$sql_password) ? '' : "-p$sql_password";
33
}
34
$sql_password =~ s/(\W)/\\$1/g;
35
 
36
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
37
$date = POSIX::strftime("%Y-%m-%d %T",$sec,$min,$hour,($mday - $back_days),$mon,$year,$wday,$yday,$isdst);
38
print "$date\n";
39
if (POSIX::strftime("%Y-%m-%d %T",localtime) eq $date){
40
	die "Could not set correct back date.\n";
41
}
42
$query = "";
43
$query = "LOCK TABLES $sql_accounting_table WRITE;" if ($sql_type eq 'mysql');
44
$query .= "DELETE FROM $sql_accounting_table WHERE AcctStopTime < '$date' AND AcctStopTime IS NOT NULL ;";
45
$query .= "UNLOCK TABLES;" if ($sql_type eq 'mysql');
46
print "$query\n";
47
my ($fh, $tmp_filename) = tempfile() or die "Could not open tmp file\n";
48
print $fh "ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS.FF TZH:TZM';\n" if ($sql_type eq 'oracle');
49
print $fh $query;
50
close $fh;
51
$command = "$sqlcmd -h$sql_server -u$sql_username $sql_password $sql_database < $tmp_filename" if ($sql_type eq 'mysql');
52
$command = "$sqlcmd  -U $sql_username -f  $tmp_filename $sql_database" if ($sql_type eq 'pg');
53
$command = "$sqlcmd  $sql_username/$pass" . "@" . "$sql_database <$tmpfile.$server" if ($sql_type eq 'oracle');
54
$command = "$sqlcmd '$sql_server' '$sql_port' '' '$sql_username' '$sql_password' < $tmp_filename" if ($sql_type eq 'sqlrelay');
55
`$command`;