Skip to content
Snippets Groups Projects
Commit fd1632ff authored by Nigel Kukard's avatar Nigel Kukard
Browse files

BUGFIX: Fixed the name of attributes used and template components

parent b6fc5fc4
No related branches found
No related tags found
1 merge request!412Bugfix branch merge
# SQL accounting database # SQL accounting database
# Copyright (C) 2007-2015, AllWorldIT # Copyright (C) 2007-2016, AllWorldIT
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
...@@ -127,26 +127,26 @@ sub init ...@@ -127,26 +127,26 @@ sub init
%{request.NAS-Identifier}, %{request.NAS-Identifier},
%{request.NAS-IP-Address}, %{request.NAS-IP-Address},
%{request.Acct-Delay-Time}, %{request.Acct-Delay-Time},
%{request.SessionTime}, %{request.Acct-Session-Time},
%{request.InputOctets}, %{request.Acct-Input-Octets},
%{request.InputGigawords}, %{request.Acct-Input-Gigawords},
%{request.InputPackets}, %{request.Acct-Input-Packets},
%{request.OutputOctets}, %{request.Acct-Output-Octets},
%{request.OutputGigawords}, %{request.Acct-Output-Gigawords},
%{request.OutputPackets}, %{request.Acct-Output-Packets},
%{query.PeriodKey} %{query.PeriodKey}
) )
'; ';
$config->{'accounting_update_get_records_query'} = ' $config->{'accounting_update_get_records_query'} = '
SELECT SELECT
SUM(AcctInputOctets) AS InputOctets, SUM(AcctInputOctets) AS AcctInputOctets,
SUM(AcctInputPackets) AS InputPackets, SUM(AcctInputPackets) AS AcctInputPackets,
SUM(AcctOutputOctets) AS OutputOctets, SUM(AcctOutputOctets) AS AcctOutputOctets,
SUM(AcctOutputPackets) AS OutputPackets, SUM(AcctOutputPackets) AS AcctOutputPackets,
SUM(AcctInputGigawords) AS InputGigawords, SUM(AcctInputGigawords) AS AcctInputGigawords,
SUM(AcctOutputGigawords) AS OutputGigawords, SUM(AcctOutputGigawords) AS AcctOutputGigawords,
SUM(AcctSessionTime) AS SessionTime, SUM(AcctSessionTime) AS AcctSessionTime,
PeriodKey PeriodKey
FROM FROM
@TP@accounting @TP@accounting
...@@ -165,13 +165,13 @@ sub init ...@@ -165,13 +165,13 @@ sub init
UPDATE UPDATE
@TP@accounting @TP@accounting
SET SET
AcctSessionTime = %{query.SessionTime}, AcctSessionTime = %{query.Acct-Session-Time},
AcctInputOctets = %{query.InputOctets}, AcctInputOctets = %{query.Acct-Input-Octets},
AcctInputGigawords = %{query.InputGigawords}, AcctInputGigawords = %{query.Acct-Input-Gigawords},
AcctInputPackets = %{query.InputPackets}, AcctInputPackets = %{query.Acct-Input-Packets},
AcctOutputOctets = %{query.OutputOctets}, AcctOutputOctets = %{query.Acct-Output-Octets},
AcctOutputGigawords = %{query.OutputGigawords}, AcctOutputGigawords = %{query.Acct-Output-Gigawords},
AcctOutputPackets = %{query.OutputPackets}, AcctOutputPackets = %{query.Acct-Output-Packets},
AcctStatusType = %{request.Acct-Status-Type} AcctStatusType = %{request.Acct-Status-Type}
WHERE WHERE
Username = %{user.Username} Username = %{user.Username}
...@@ -408,7 +408,7 @@ sub getUsage ...@@ -408,7 +408,7 @@ sub getUsage
# If we using caching and got here, it means that we must cache the result # If we using caching and got here, it means that we must cache the result
if (defined($config->{'accounting_usage_cache_time'})) { if (defined($config->{'accounting_usage_cache_time'})) {
$res{'CachedUntil'} = $user->{'_Internal'}->{'Timestamp-Unix'} + $config->{'accounting_usage_cache_time'}; $res{'CachedUntil'} = $user->{'_Internal'}->{'Timestamp-Unix'} + $config->{'accounting_usage_cache_time'};
# Cache the result # Cache the result
cacheStoreComplexKeyPair('mod_accounting_sql(getUsage)',$user->{'Username'}."/".$template->{'query'}->{'PeriodKey'},\%res); cacheStoreComplexKeyPair('mod_accounting_sql(getUsage)',$user->{'Username'}."/".$template->{'query'}->{'PeriodKey'},\%res);
} }
...@@ -429,6 +429,7 @@ sub acct_log ...@@ -429,6 +429,7 @@ sub acct_log
{ {
my ($server,$user,$packet) = @_; my ($server,$user,$packet) = @_;
# Build template # Build template
my $template; my $template;
foreach my $attr ($packet->attributes) { foreach my $attr ($packet->attributes) {
...@@ -447,11 +448,12 @@ sub acct_log ...@@ -447,11 +448,12 @@ sub acct_log
# For our queries # For our queries
$template->{'query'}->{'PeriodKey'} = $periodKey; $template->{'query'}->{'PeriodKey'} = $periodKey;
# Default to being a new period, only if we update on INTERIM or STOP do we set this to 0
my $newPeriod = 1;
# #
# U P D A T E & S T O P P A C K E T # U P D A T E & S T O P P A C K E T
# #
# If its a new period we're going to trigger START
my $newPeriod;
if ($packet->rawattr('Acct-Status-Type') eq "2" || $packet->rawattr('Acct-Status-Type') eq "3") { if ($packet->rawattr('Acct-Status-Type') eq "2" || $packet->rawattr('Acct-Status-Type') eq "3") {
# Replace template entries # Replace template entries
my @dbDoParams = templateReplace($config->{'accounting_update_get_records_query'},$template); my @dbDoParams = templateReplace($config->{'accounting_update_get_records_query'},$template);
...@@ -478,21 +480,22 @@ sub acct_log ...@@ -478,21 +480,22 @@ sub acct_log
# Loop through previous records and subtract them from our session totals # Loop through previous records and subtract them from our session totals
while (my $sessionPart = hashifyLCtoMC($sth->fetchrow_hashref(), while (my $sessionPart = hashifyLCtoMC($sth->fetchrow_hashref(),
qw(InputOctets InputPackets OutputOctets OutputPackets InputGigawords OutputGigawords SessionTime PeriodKey) qw(AcctInputOctets AcctInputPackets AcctOutputOctets AcctOutputPackets AcctInputGigawords AcctOutputGigawords
SessionTime PeriodKey)
)) { )) {
# Convert this session usage to bytes # Convert this session usage to bytes
my $sessionInputBytes = Math::BigInt->new(); my $sessionInputBytes = Math::BigInt->new();
$sessionInputBytes->badd($sessionPart->{'InputGigawods'})->bmul(UINT_MAX); $sessionInputBytes->badd($sessionPart->{'AcctInputGigawods'})->bmul(UINT_MAX);
$sessionInputBytes->badd($sessionPart->{'InputOctets'}); $sessionInputBytes->badd($sessionPart->{'AcctInputOctets'});
my $sessionOutputBytes = Math::BigInt->new(); my $sessionOutputBytes = Math::BigInt->new();
$sessionOutputBytes->badd($sessionPart->{'OutputGigawods'})->bmul(UINT_MAX); $sessionOutputBytes->badd($sessionPart->{'AcctOutputGigawods'})->bmul(UINT_MAX);
$sessionOutputBytes->badd($sessionPart->{'OutputOctets'}); $sessionOutputBytes->badd($sessionPart->{'AcctOutputOctets'});
# And packets # And packets
my $sessionInputPackets = Math::BigInt->new($sessionPart->{'InputPackets'}); my $sessionInputPackets = Math::BigInt->new($sessionPart->{'AcctInputPackets'});
my $sessionOutputPackets = Math::BigInt->new($sessionPart->{'OutputPackets'}); my $sessionOutputPackets = Math::BigInt->new($sessionPart->{'AcctOutputPackets'});
# Finally session time # Finally session time
my $sessionSessionTime = Math::BigInt->new($sessionPart->{'SessionTime'}); my $sessionSessionTime = Math::BigInt->new($sessionPart->{'AcctSessionTime'});
# Check if this record is from an earlier period # Check if this record is from an earlier period
if (defined($sessionPart->{'PeriodKey'}) && $sessionPart->{'PeriodKey'} ne $periodKey) { if (defined($sessionPart->{'PeriodKey'}) && $sessionPart->{'PeriodKey'} ne $periodKey) {
...@@ -511,19 +514,19 @@ sub acct_log ...@@ -511,19 +514,19 @@ sub acct_log
DBFreeRes($sth); DBFreeRes($sth);
# Sanitize # Sanitize
if ($totalInputBytes->is_neg()) { if ($totalInputBytes->is_neg()) {
$totalInputBytes->bzero(); $totalInputBytes->bzero();
} }
if ($totalOutputBytes->is_neg()) { if ($totalOutputBytes->is_neg()) {
$totalOutputBytes->bzero(); $totalOutputBytes->bzero();
} }
if ($totalInputPackets->is_neg()) { if ($totalInputPackets->is_neg()) {
$totalInputPackets->bzero(); $totalInputPackets->bzero();
} }
if ($totalOutputPackets->is_neg()) { if ($totalOutputPackets->is_neg()) {
$totalOutputPackets->bzero(); $totalOutputPackets->bzero();
} }
if ($totalSessionTime->is_neg()) { if ($totalSessionTime->is_neg()) {
$totalSessionTime->bzero(); $totalSessionTime->bzero();
} }
...@@ -532,15 +535,15 @@ sub acct_log ...@@ -532,15 +535,15 @@ sub acct_log
my ($outputGigawordsStr,$outputOctetsStr) = $totalOutputBytes->bdiv(UINT_MAX); my ($outputGigawordsStr,$outputOctetsStr) = $totalOutputBytes->bdiv(UINT_MAX);
# Conversion to strings # Conversion to strings
$template->{'query'}->{'InputGigawords'} = $inputGigawordsStr->bstr(); $template->{'query'}->{'Acct-Input-Gigawords'} = $inputGigawordsStr->bstr();
$template->{'query'}->{'InputOctets'} = $inputOctetsStr->bstr(); $template->{'query'}->{'Acct-Input-Octets'} = $inputOctetsStr->bstr();
$template->{'query'}->{'OutputGigawords'} = $outputGigawordsStr->bstr(); $template->{'query'}->{'Acct-Output-Gigawords'} = $outputGigawordsStr->bstr();
$template->{'query'}->{'OutputOctets'} = $outputOctetsStr->bstr(); $template->{'query'}->{'Acct-Output-Octets'} = $outputOctetsStr->bstr();
$template->{'query'}->{'InputPackets'} = $totalInputPackets->bstr(); $template->{'query'}->{'Acct-Input-Packets'} = $totalInputPackets->bstr();
$template->{'query'}->{'OutputPackets'} = $totalOutputPackets->bstr(); $template->{'query'}->{'Acct-Output-Packets'} = $totalOutputPackets->bstr();
$template->{'query'}->{'SessionTime'} = $totalSessionTime->bstr(); $template->{'query'}->{'Acct-Session-Time'} = $totalSessionTime->bstr();
# Replace template entries # Replace template entries
...@@ -559,7 +562,7 @@ sub acct_log ...@@ -559,7 +562,7 @@ sub acct_log
# Be very sneaky .... if we updated something, this is obviously NOT a new period # Be very sneaky .... if we updated something, this is obviously NOT a new period
$newPeriod = 0; $newPeriod = 0;
# If we updated a few things ... possibly duplicates? # If we updated a few things ... possibly duplicates?
if ($sth > 1) { if ($sth > 1) {
fixDuplicates($server, $template); fixDuplicates($server, $template);
} }
} }
...@@ -575,7 +578,6 @@ sub acct_log ...@@ -575,7 +578,6 @@ sub acct_log
if ($packet->rawattr('Acct-Status-Type') eq "1" || $newPeriod) { if ($packet->rawattr('Acct-Status-Type') eq "1" || $newPeriod) {
# Replace template entries # Replace template entries
my @dbDoParams = templateReplace($config->{'accounting_start_query'},$template); my @dbDoParams = templateReplace($config->{'accounting_start_query'},$template);
# Insert into database # Insert into database
my $sth = DBDo(@dbDoParams); my $sth = DBDo(@dbDoParams);
if (!$sth) { if (!$sth) {
...@@ -592,7 +594,6 @@ sub acct_log ...@@ -592,7 +594,6 @@ sub acct_log
$user->{'_UserDB'}->{'Users_data_set'}($server,$user,'global','FirstLogin',$user->{'_Internal'}->{'Timestamp-Unix'}); $user->{'_UserDB'}->{'Users_data_set'}($server,$user,'global','FirstLogin',$user->{'_Internal'}->{'Timestamp-Unix'});
} }
} }
} }
...@@ -613,6 +614,7 @@ sub acct_log ...@@ -613,6 +614,7 @@ sub acct_log
} }
} }
return MOD_RES_ACK; return MOD_RES_ACK;
} }
...@@ -682,7 +684,7 @@ sub cleanup ...@@ -682,7 +684,7 @@ sub cleanup
# Last month.. # Last month..
my $lastMonth = $thisMonth->clone()->subtract( months => 1 ); my $lastMonth = $thisMonth->clone()->subtract( months => 1 );
my $prevPeriodKey = $lastMonth->strftime("%Y-%m"); my $prevPeriodKey = $lastMonth->strftime("%Y-%m");
# Begin transaction # Begin transaction
DBBegin(); DBBegin();
......
...@@ -306,26 +306,26 @@ accounting_start_query=<<EOT ...@@ -306,26 +306,26 @@ accounting_start_query=<<EOT
%{request.NAS-Identifier}, %{request.NAS-Identifier},
%{request.NAS-IP-Address}, %{request.NAS-IP-Address},
%{request.Acct-Delay-Time}, %{request.Acct-Delay-Time},
%{request.SessionTime}, %{request.Acct-Session-Time},
%{request.InputOctets}, %{request.Acct-Input-Octets},
%{request.InputGigawords}, %{request.Acct-Input-Gigawords},
%{request.InputPackets}, %{request.Acct-Input-Packets},
%{request.OutputOctets}, %{request.Acct-Output-Octets},
%{request.OutputGigawords}, %{request.Acct-Output-Gigawords},
%{request.OutputPackets}, %{request.Acct-Output-Packets},
%{query.PeriodKey} %{query.PeriodKey}
) )
EOT EOT
accounting_update_get_records_query=<<EOT accounting_update_get_records_query=<<EOT
SELECT SELECT
SUM(AcctInputOctets) AS InputOctets, SUM(AcctInputOctets) AS AcctInputOctets,
SUM(AcctInputPackets) AS InputPackets, SUM(AcctInputPackets) AS AcctInputPackets,
SUM(AcctOutputOctets) AS OutputOctets, SUM(AcctOutputOctets) AS AcctOutputOctets,
SUM(AcctOutputPackets) AS OutputPackets, SUM(AcctOutputPackets) AS AcctOutputPackets,
SUM(AcctInputGigawords) AS InputGigawords, SUM(AcctInputGigawords) AS AcctInputGigawords,
SUM(AcctOutputGigawords) AS OutputGigawords, SUM(AcctOutputGigawords) AS AcctOutputGigawords,
SUM(AcctSessionTime) AS SessionTime, SUM(AcctSessionTime) AS AcctSessionTime,
PeriodKey PeriodKey
FROM FROM
@TP@accounting @TP@accounting
...@@ -344,13 +344,13 @@ accounting_update_query=<<EOT ...@@ -344,13 +344,13 @@ accounting_update_query=<<EOT
UPDATE UPDATE
@TP@accounting @TP@accounting
SET SET
AcctSessionTime = %{query.SessionTime}, AcctSessionTime = %{query.Acct-Session-Time},
AcctInputOctets = %{query.InputOctets}, AcctInputOctets = %{query.Acct-Input-Octets},
AcctInputGigawords = %{query.InputGigawords}, AcctInputGigawords = %{query.Acct-Input-Gigawords},
AcctInputPackets = %{query.InputPackets}, AcctInputPackets = %{query.Acct-Input-Packets},
AcctOutputOctets = %{query.OutputOctets}, AcctOutputOctets = %{query.Output-Octets},
AcctOutputGigawords = %{query.OutputGigawords}, AcctOutputGigawords = %{query.Output-Gigawords},
AcctOutputPackets = %{query.OutputPackets}, AcctOutputPackets = %{query.Output-Packets},
AcctStatusType = %{request.Acct-Status-Type} AcctStatusType = %{request.Acct-Status-Type}
WHERE WHERE
Username = %{user.Username} Username = %{user.Username}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment