From 62f57daeafaf84ec1df57b209c86346793d609a8 Mon Sep 17 00:00:00 2001 From: Nigel Kukard Date: Wed, 15 May 2019 12:51:45 +0000 Subject: [PATCH] Added variable period usage accounting --- .../modules/accounting/mod_accounting_sql.pm | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/smradius/modules/accounting/mod_accounting_sql.pm b/lib/smradius/modules/accounting/mod_accounting_sql.pm index 98976aa..b9aa890 100644 --- a/lib/smradius/modules/accounting/mod_accounting_sql.pm +++ b/lib/smradius/modules/accounting/mod_accounting_sql.pm @@ -1,5 +1,5 @@ # SQL accounting database -# Copyright (C) 2007-2016, AllWorldIT +# Copyright (C) 2007-2019, AllWorldIT # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -206,6 +206,20 @@ sub init AND PeriodKey = %{query.PeriodKey} '; + $config->{'accounting_usage_query_period'} = ' + SELECT + SUM(AcctInputOctets) AS AcctInputOctets, + SUM(AcctOutputOctets) AS AcctOutputOctets, + SUM(AcctInputGigawords) AS AcctInputGigawords, + SUM(AcctOutputGigawords) AS AcctOutputGigawords, + SUM(AcctSessionTime) AS AcctSessionTime + FROM + @TP@accounting + WHERE + Username = %{user.Username} + AND EventTimestamp > %{query.PeriodKey} + '; + $config->{'accounting_select_duplicates_query'} = ' SELECT ID @@ -280,6 +294,15 @@ sub init $config->{'accounting_usage_query'} = $scfg->{'mod_accounting_sql'}->{'accounting_usage_query'}; } } + if (defined($scfg->{'mod_accounting_sql'}->{'accounting_usage_query_period'}) && + $scfg->{'mod_accounting_sql'}->{'accounting_usage_query_period'} ne "") { + if (ref($scfg->{'mod_accounting_sql'}->{'accounting_usage_query_period'}) eq "ARRAY") { + $config->{'accounting_usage_query_period'} = join(' ', + @{$scfg->{'mod_accounting_sql'}->{'accounting_usage_query_period'}}); + } else { + $config->{'accounting_usage_query_period'} = $scfg->{'mod_accounting_sql'}->{'accounting_usage_query_period'}; + } + } if (defined($scfg->{'mod_accounting_sql'}->{'accounting_select_duplicates_query'}) && $scfg->{'mod_accounting_sql'}->{'accounting_select_duplicates_query'} ne "") { if (ref($scfg->{'mod_accounting_sql'}->{'accounting_select_duplicates_query'}) eq "ARRAY") { @@ -327,9 +350,10 @@ sub init # Function to get radius user data usage +# The 'period' parameter is optional and is the number of days to return usage for sub getUsage { - my ($server,$user,$packet) = @_; + my ($server,$user,$packet,$period) = @_; # Build template my $template; @@ -341,9 +365,27 @@ sub getUsage $template->{'user'}->{'ID'} = $user->{'ID'}; $template->{'user'}->{'Username'} = $user->{'Username'}; - # Current PeriodKey + # Current PeriodKey, this is used for non-$period queries my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'}); - $template->{'query'}->{'PeriodKey'} = $now->strftime("%Y-%m"); + + # Query template to use below + my $queryTemplate; + # If we're doing a query for a specific period + if (defined($period)) { + # We need to switch out the query to the period query + $queryTemplate = "accounting_usage_query_period"; + # Grab a clone of now, and create the start date DateTime object + my $startDate = $now->clone->subtract( 'days' => $period ); + # And we add the start date + $template->{'query'}->{'PeriodKey'} = $startDate->ymd(); + + # If not, we just use PeriodKey as normal... + } else { + # Set the normal PeriodKey query template to use + $queryTemplate = "accounting_usage_query"; + # And set the period key to this month + $template->{'query'}->{'PeriodKey'} = $now->strftime("%Y-%m"); + } # If we using caching, check how old the result is if (defined($config->{'accounting_usage_cache_time'})) { @@ -355,7 +397,7 @@ sub getUsage } # Replace template entries - my (@dbDoParams) = templateReplace($config->{'accounting_usage_query'},$template); + my (@dbDoParams) = templateReplace($config->{$queryTemplate},$template); # Fetch data my $sth = DBSelect(@dbDoParams); -- GitLab