As posted to the list by Alexander Kubatkin
[freeradius.git] / dialup_admin / bin / monthly_tot_stats
1 #!/usr/bin/perl
2 use POSIX;
3 use File::Temp;
4
5 # Log in the mtotacct table aggregated accounting information for
6 # each user spaning in one month period.
7 # If the current month has not ended it will log information up to
8 # the current month day
9 # Works only with mysql and postgresql
10 #
11
12 $conf=shift||'/usr/local/dialup_admin/conf/admin.conf';
13
14
15 open CONF, "<$conf"
16         or die "Could not open configuration file\n";
17 while(<CONF>){
18         chomp;
19         ($key,$val)=(split /:\s*/,$_);
20         $sql_type = $val if ($key eq 'sql_type');
21         $sql_server = $val if ($key eq 'sql_server');
22         $sql_username = $val if ($key eq 'sql_username');
23         $sql_password = $val if ($key eq 'sql_password');
24         $sql_database = $val if ($key eq 'sql_database');
25         $sql_accounting_table = $val if ($key eq 'sql_accounting_table');
26         $sqlcmd = $val if ($key eq 'sql_command');
27 }
28 close CONF;
29
30 die "sql_command directive is not set in admin.conf\n" if ($sqlcmd eq '');
31 die "sql command '$sqlcmd' not found or does not seem to be executable\n" if (! -x $sqlcmd);
32
33 if ($sql_type eq 'mysql'){
34         $sql_password = (!$sql_password) ? '' : "-p$sql_password";
35 }
36 $sql_password =~ s/(\W)/\\$1/g;
37
38 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
39 if ($mday == 1){
40         $mon--;
41 }
42 $date_start = POSIX::strftime("%Y-%m-%d",0,0,0,1,$mon,$year,$wday,$yday,$isdst);
43 $date_end = POSIX::strftime("%Y-%m-%d",0,0,0,$mday,$mon,$year,$wday,$yday,$isdst);
44
45 $query1 = "DELETE FROM mtotacct WHERE AcctDate = '$date_start';";
46 $query2 = "INSERT INTO mtotacct (UserName,AcctDate,ConnNum,ConnTotDuration,
47         ConnMaxDuration,ConnMinDuration,InputOctets,OutputOctets,NASIPAddress)
48         SELECT UserName,'$date_start',SUM(ConnNum),SUM(ConnTotDuration),
49         MAX(ConnMaxDuration),MIN(ConnMinDuration),SUM(InputOctets),
50         SUM(OutputOctets),NASIPAddress FROM totacct
51         WHERE AcctDate >= '$date_start' AND
52         AcctDate <= '$date_end' GROUP BY UserName,NASIPAddress;";
53 print "$query1\n";
54 print "$query2\n";
55 my ($fh, $tmp_filename) = tempfile() or die "Could not open tmp file\n";
56 print $fh "ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS.FF TZH:TZM';\n" if ($sql_type eq 'oracle');
57 print $fh $query1;
58 print $fh $query2;
59 close $fh;
60 $command = "$sqlcmd -h $sql_server -u $sql_username $sql_password $sql_database < $tmp_filename" if ($sql_type eq 'mysql');
61 $command = "$sqlcmd  -U $sql_username -f  $tmp_filename $sql_database" if ($sql_type eq 'pg');
62 $command = "$sqlcmd  $sql_username/$pass" . "@" . "$sql_database <$tmpfile.$server" if ($sql_type eq 'oracle');
63 $command = "$sqlcmd '$sql_server' '$sql_port' '' '$sql_username' '$sql_password' < $tmp_filename" if ($sql_type eq 'sqlrelay');
64 `$command`;