May 30 2009

Daily Accounting in Freeradius

Published by dave at 5:50 pm under Cisco,Freeradius


We have seen a few posts on Freeradius user list and other forums asking how to collect accounting periodically. The Radius protocol provides accounting but not in the way that many would like. Here’s a short list of things we would like to modify or improve:

  • Traffic is not collected at regular intervals
    As a result, it is impossible to generate daily graphs with download/upload. Traffic will be corrupted because it is again, impossible to calculate traffic over a given period of time with accuracy

  • The active session doesn’t appear because the database only contains null values after a start record has been inserted

  • Some records may be lost on the way (between routers and the Radius server). The Radius protocol runs on UDP, which doesn’t support acknowledgements.

  • Any usage over 4GB is reset to 0 if the session is not interrupted before then. The format of the field is a 32 bit unsigned value as defined in the RFC.

A number of issues arises for those who want to extract this data and do something out of it. No restrictions can be applied for example. Nor collect values and export them to a billing software. Graphs display nul values if the user hasn’t disconnected for a while. Even though there are some ways to send updates from the Network Access Server (NAS), we weren’t happy with this as the stop record time remains 00-00-00 and a query might become complicated and time-consuming if previous stop-records were lost on the way. In the best scenario, traffic can be collected (including the active session), but it remains impossible to know your daily traffic accurately for instance.

 

Method

Some suggested to reset all connections at regular intervals but why should customers be disconnected to collect a fair amount of traffic? How could we justify this for an always-on connection? Some simple modifications can fix these small issues. Let’s check briefly what can be done:
Traffic is not collected at regular intervals. We could send updates from the router every so often which is a good step. But that’s not sufficient as the new data overwrites the previous amount of traffic. A fair way to proceed is to create a new stop and start record instead from this update. This would minimize the missing last record impact if the time period is small enough. Lost records wouldn’t be a problem either if we compare the new value against the last received.
Finally, we can work around the 4GB limit with Acct-Input-Gigawords and Acct-Input-Gigawords Radius extension attributes if your router supports it (Method we are going to use here). If your hardware doesn’t support it, a few modifications in the SQL code below would do. It’s been tested in a live environment with 3 Radius servers and over 5000 customers. Data is then transferred in to a billing software.

 

Before You Start

We have applied these changes on Fedora Core and Solaris with Freeradius 1.1.3. The operating system shouldn’t really matter as most changes are made on SQL queries. You will need a running setup on Freeradius and Mysql. Scott Barlett has written some precious notes on this, available at http://www.frontios.com/freeradius.html.
Mysql 5.0 or higher is absolutely needed in this implementation. Previous versions don’t support stored procedures. Make also sure your router (NAS) supports Radius accounting updates and Acct-Input-Gigawords attributes (usually the case for Cisco).

 

Adding Support for Radius Extension Attributes

As mentionned in RFC2869, additional attributes were created to answer certain needs through various useful functions. Acct-Input-Gigawords and Acct-Output-Gigawords attributes indicate how many times Acct-Input-Octets and Acct-Output-Octets counters were reset to 0 after reaching 2^32. They are present in Stop and Interim-Update records, which is just what we want.
You can activate this on a Cisco router by entering

aaa accounting gigawords

We don’t need to create extra columns in the Radius database; We’ll calculate the new amount of traffic on the fly. The benefits are it keeps the original structure of the database and accounting scripts you may have written don’t need to be modified. On the other hand, it saves a lot of extra space.
The second modification is done in the SQL query.

Note You will be asked to reload the router to apply the new settings. Do this at a convenient time.

 

Configuring the NAS to send accounting update

There are 2 ways to achieve this. You either configure your router (NAS) to send regular accounting updates to the Radius server, either add an extra parameter in the customer’s details. We’d rather do it on the router as it takes precedence over the Radius parameter. We are using a Cisco router for which the command is:

aaa accounting update periodic 180

This sends an update every 3 hours. I should mention that this applies when the customer’s connection is established, meaning you need to reset appropriate interfaces.

Caution Using the aaa accounting update periodic command can cause heavy congestion when many users are logged in to the network.

 

Defining New Queries for Update and Stop Records

The SQL code for update and stop queries needs to be replaced. We call stored procedures because there’s a bit of calculation to be done and Mysql client wouldn’t accept 2 statements in one query. There might be a workaround but this is more flexible. A single change doesn’t have to be replicated on each server. Insert into Mysql Radius database the 2 procedures below

DROP PROCEDURE IF EXISTS radius.acct_update;
delimiter //
CREATE PROCEDURE radius.acct_update(
  IN S DATETIME,
  IN Acct_Session_Time INT(12),
  IN Acct_Input_Octets BIGINT(20),
  IN Acct_Output_Octets BIGINT(20),
  IN Acct_Terminate_Cause VARCHAR(32),
  IN Acct_Session_Id varchar(64),
  IN SQL_User_Name VARCHAR(64),
  IN NAS_IP_Address VARCHAR(15),
  IN Acct_Unique_Session_Id VARCHAR(32),
  IN Realm VARCHAR(64),
  IN NAS_Port VARCHAR(15),
  IN NAS_Port_Type VARCHAR(32),
  IN Acct_Authentic VARCHAR(32),
  IN Called_Station_Id VARCHAR(50),
  IN Calling_Station_Id VARCHAR(50),
  IN Service_Type VARCHAR(32),
  IN Framed_Protocol VARCHAR(32),
  IN Framed_IP_Address VARCHAR(15)
)
BEGIN
  DECLARE Prev_Acct_Input_Octets BIGINT(20);
  DECLARE Prev_Acct_Output_Octets BIGINT(20);
  DECLARE Prev_Acct_Session_Time INT(12);

  # Collect traffic previous values
  SELECT SUM(AcctInputOctets), SUM(AcctOutputOctets), SUM(AcctSessionTime)
    INTO Prev_Acct_Input_Octets, Prev_Acct_Output_Octets, Prev_Acct_Session_Time
    FROM radacct
    WHERE AcctSessionId = Acct_Session_Id
    AND UserName = SQL_User_Name
    AND NASIPAddress = NAS_IP_Address
    AND ( AcctStopTime > 0);

  # Set values to 0 when no previous records
  IF (Prev_Acct_Session_Time IS NULL) THEN
    SET Prev_Acct_Session_Time = 0;
    SET Prev_Acct_Input_Octets = 0;
    SET Prev_Acct_Output_Octets = 0;
  END IF;

  # Update record with new traffic
  UPDATE radacct SET AcctStopTime = S,
    AcctSessionTime = (Acct_Session_Time - Prev_Acct_Session_Time),
    AcctInputOctets = (Acct_Input_Octets - Prev_Acct_Input_Octets),
    AcctOutputOctets = (Acct_Output_Octets - Prev_Acct_Output_Octets),
    AcctTerminateCause = Acct_Terminate_Cause
    WHERE AcctSessionId = Acct_Session_Id
    AND UserName = SQL_User_Name
    AND NASIPAddress = NAS_IP_Address
    AND (AcctStopTime IS NULL OR AcctStopTime = 0);

  # Create new record
  INSERT INTO radacct
   (AcctSessionId, AcctUniqueId, UserName,
    Realm, NASIPAddress, NASPortId, NASPortType,
    AcctStartTime, AcctStopTime, AcctSessionTime,
    AcctAuthentic, AcctInputOctets, AcctOutputOctets,
    CalledStationId, CallingStationId, AcctTerminateCause,
    ServiceType, FramedProtocol, FramedIPAddress,
    AcctStartDelay, AcctStopDelay)
  VALUES
   (Acct_Session_Id, Acct_Unique_Session_Id, SQL_User_Name,
    Realm, NAS_IP_Address, NAS_Port, NAS_Port_Type,
    S, '0', '0',
    Acct_Authentic, '0', '0',
    Called_Station_Id, Calling_Station_Id, '',
    Service_Type, Framed_Protocol, Framed_IP_Address,
    '0', '0');
END;
//
delimiter ;

 

Note You need to update the table names if they have been changed in sql.conf. We use the default value from the file and database structure “Radacct”.
Note Don’t forget to redefine the delimiter before and after the procedure or you’ll get an error!
We need to retrieve the traffic from previous updates because the router sends a counter and not the amount of traffic sent during that period of time. A stop record is then created with the traffic difference. The stop query needs to be modified as well:

DROP PROCEDURE IF EXISTS radius.acct_stop;
delimiter //
CREATE PROCEDURE radius.acct_stop(
  IN S DATETIME,
  IN Acct_Session_Time INT(12),
  IN Acct_Input_Octets BIGINT(20),
  IN Acct_Output_Octets BIGINT(20),
  IN Acct_Terminate_Cause VARCHAR(32),
  IN Acct_Delay_Time INT(12),
  IN Connect_Info VARCHAR(32),
  IN Acct_Session_Id varchar(64),
  IN SQL_User_Name VARCHAR(64),
  IN NAS_IP_Address VARCHAR(15)
)
BEGIN
  DECLARE Prev_Acct_Input_Octets BIGINT(20);
  DECLARE Prev_Acct_Output_Octets BIGINT(20);
  DECLARE Prev_Acct_Session_Time INT(12);

  # Collect traffic previous values
  SELECT SUM(AcctInputOctets), SUM(AcctOutputOctets), SUM(AcctSessionTime)
    INTO Prev_Acct_Input_Octets, Prev_Acct_Output_Octets, Prev_Acct_Session_Time
    FROM radacct
    WHERE AcctSessionId = Acct_Session_Id
    AND UserName = SQL_User_Name
    AND NASIPAddress = NAS_IP_Address
    AND ( AcctStopTime > 0);

  # Set values to 0 when no previous records
  IF (Prev_Acct_Session_Time IS NULL) THEN
    SET Prev_Acct_Session_Time = 0;
    SET Prev_Acct_Input_Octets = 0;
    SET Prev_Acct_Output_Octets = 0;
  END IF;

  # Update record with new traffic
  UPDATE radacct SET AcctStopTime = S,
    AcctSessionTime = (Acct_Session_Time - Prev_Acct_Session_Time),
    AcctInputOctets = (Acct_Input_Octets - Prev_Acct_Input_Octets),
    AcctOutputOctets = (Acct_Output_Octets - Prev_Acct_Output_Octets),
    AcctTerminateCause = Acct_Terminate_Cause,
    AcctStopDelay = Acct_Delay_Time,
    ConnectInfo_stop = Connect_Info
    WHERE AcctSessionId = Acct_Session_Id
    AND UserName = SQL_User_Name
    AND NASIPAddress = NAS_IP_Address
    AND (AcctStopTime IS NULL OR AcctStopTime=0);
END;
//
delimiter ;

 

This is the same as the original query except that it retrieves the previous traffic again. If you don’t use accounting updates, this will do the same as before.

 

Updating sql.conf

The last bit is to replace the SQL code in sql.conf, to call the 2 procedures above.
Replace accounting_update_query with

accounting_update_query = " \
 CALL acct_update( \
          '%S', \
          '%{Acct-Session-Time}', \
          '%{%{Acct-Input-Gigawords}:-0}'  << 32 | '%{%{Acct-Input-Octets}:-0}', \
          '%{%{Acct-Output-Gigawords}:-0}' << 32 | '%{%{Acct-Output-Octets}:-0}', \
          'Acct-Update', \
          '%{Acct-Session-Id}', \
          '%{SQL-User-Name}', \
          '%{NAS-IP-Address}', \
          '%{Acct-Unique-Session-Id}', \
          '%{Realm}', \
          '%{NAS-Port}', \
          '%{NAS-Port-Type}', \
          '%{Acct-Authentic}', \
          '%{Called-Station-Id}', \
          '%{Calling-Station-Id}', \
          '%{Service-Type}', \
          '%{Framed-Protocol}', \
          '%{Framed-IP-Address}')"

 
and accounting_stop_query with

accounting_stop_query = " \
CALL acct_stop( \
          '%S', \
          '%{Acct-Session-Time}', \
          '%{%{Acct-Input-Gigawords}:-0}' << 32 | '%{%{Acct-Input-Octets}:-0}', \
          '%{%{Acct-Output-Gigawords}:-0}' << 32 | '%{%{Acct-Output-Octets}:-0}', \
          '%{Acct-Terminate-Cause}', \
          '%{%{Acct-Delay-Time}:-0}', \
          '%{Connect-Info}', \
          '%{Acct-Session-Id}', \
          '%{SQL-User-Name}', \
          '%{NAS-IP-Address}')"

 

Reboot the radius server to apply the new settings, you're done!


12 responses so far

12 Responses to “Daily Accounting in Freeradius”

  1. Ebay hot itemson 07 Sep 2008 at 11:02 am

    Very interesting site, nice design, greetings

  2. Cason 03 Dec 2008 at 12:33 pm

    Please start the mysql procedures with line:
    DELIMITER //

    and replace last lines with:
    END;
    //

    DELIMITER ;

    to overcome an 1064, syntax error near ‘BIGINT(12)’ in line 26 error.

  3. Tonyon 29 Jan 2009 at 12:28 am

    I’m trying to implement the above with MySQL 5 and Freeradius 2.0.2.
    The MySQL part went well but I’ve encountered and error when trying to start freeradius with the new entries.
    I’m getting:

    including configuration file /usr/local/etc/raddb/sql.conf
    including configuration file /usr/local/etc/raddb/sql/mysql/dialup.conf
    /usr/local/etc/raddb/sql/mysql/dialup.conf[168]: Expecting section start brace ‘{‘ after “CALL acct_update”

    Anyone know why?
    Thanks
    Tony

  4. Ivan Kalikon 05 May 2009 at 3:41 pm

    Because this article is now outdated. It works for Freeradius 1.1.x versions. In 2.x gigaword accounting is enabled by default.

  5. Daveon 30 May 2009 at 5:43 pm

    I’ve updated the script for Freeradius 2.1.
    Minor changes were needed since 1.1.7, not gigawords related though.

    I think above “start brace” error was due to missing backslashes in the end of the lines.

    Dave

  6. Terry Aon 14 Aug 2009 at 9:00 am

    Hi Dave
    Thanks for this if nothing else I can now do procedures in mysql.
    I still get this when I run radiusd -X
    Expecting section start brace ?{? after ?CALL acct_update?
    checked the backslashes all seem to be there

    ALSO
    still had to do this to get the procedure in
    Please start the mysql procedures with line:
    DELIMITER //

    and replace last lines with:
    END;
    //

    DELIMITER ;

    Any ideas re the brace

  7. daveon 25 Aug 2009 at 11:18 pm

    Think I know what you did:
    Replace accounting_update_query meaning replace it with:

    accounting_update_query = ” \
    CALL acct_update( \
    and so on

    You probably skipped the first line
    I get the brace issue to if I remove it

  8. daveon 25 Aug 2009 at 11:25 pm

    I updated the post by the way

  9. Marceloon 30 Aug 2009 at 2:26 am

    Hi,

    Wouldn’t be easier to use triggers instead ?

    I have a WISP and I needed a solution like that a couple months ago and I created another table like user_acct and for every update I triggered a trigger to insert a new row in that table, so I can still have one row per session in the radacct table.

    Just another idea,

    Bye

  10. daveon 30 Aug 2009 at 7:49 pm

    Hi Marcelo,

    You’re right and I did think about the trigger solution but was still in beta version at the time I wrote this

    I wouldn’t say it is easier but a clear advantage is it needs no change in Freeradius. That’s no bother anyway since SQL code in Freeradius isn’t modified much.
    On the other hand, it’s annoying to store twice the same data and it gets confusing to have 2 accounting tables.

    In the end, the 2 solutions let everyone come closer to the right amount of traffic limiting packet loss impact on the network and get a better picture of customer’s usage

    Thanks for this great suggestion
    Dave

  11. Jo Kingon 13 Sep 2009 at 5:37 pm

    Can you please write update version of this article based on FreeRadius 2

  12. daveon 13 Sep 2009 at 6:32 pm

    Hi, what makes you tell this isn’t for FR2?

Comments RSS

Leave a Reply

Security Code: