import from HEAD:
[freeradius.git] / dialup_admin / bin / clean_radacct
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
9 $conf=shift||'/usr/local/dialup_admin/conf/admin.conf';
10 $back_days = 35;
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 "Could not find sql binary. Please make sure that the \$sqlcmd variable points to the right location\n" if (! -x $sqlcmd);
30
31 $sql_password = ($sql_password eq '') ? '' : "-p$sql_password";
32
33 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
34 $date = POSIX::strftime("%Y-%m-%d %T",$sec,$min,$hour,($mday - $back_days),$mon,$year,$wday,$yday,$isdst);
35 print "$date\n";
36
37 $query = "DELETE FROM $sql_accounting_table WHERE AcctStopTime IS NULL AND AcctStartTime < '$date';";
38 print "$query\n";
39 open TMP, ">/tmp/clean_radacct.query"
40         or die "Could not open tmp file\n";
41 print TMP $query;
42 close TMP;
43 $command = "$sqlcmd -h$sql_server -u$sql_username $sql_password $sql_database </tmp/clean_radacct.query" if ($sql_type eq 'mysql');
44 $command = "$sqlcmd  -U $sql_username -f /tmp/clean_radacct.query $sql_database" if ($sql_type eq 'pg');
45 `$command`;