Skip to content

Commit

Permalink
[accounting] Added command to deal with unterminated radacct entries o…
Browse files Browse the repository at this point in the history
  • Loading branch information
R9295 committed Dec 29, 2017
1 parent bdd8f92 commit b205e3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
25 changes: 25 additions & 0 deletions django_freeradius/management/commands/cleanup_stale_radacct.py
@@ -0,0 +1,25 @@
from datetime import timedelta

from django.core.management import BaseCommand
from django.utils.timezone import now

from django_freeradius.models import RadiusAccounting


class Command(BaseCommand):
help = "Closes active accounting sessions older than <days>"

def add_arguments(self, parser):
parser.add_argument('number_of_days', type=int)

def handle(self, *args, **options):
if options['number_of_days']:
days = now() - timedelta(days=options['number_of_days'])
sessions = RadiusAccounting.objects.filter(start_time__lt=days, stop_time=None)
for session in sessions:
# calculate seconds in between two dates
session.session_time = (now() - session.start_time).total_seconds()
session.stop_time = now()
session.update_time = session.stop_time
session.save()
self.stdout.write('Closed active sessions older than {} days'.format(options['number_of_days']))
12 changes: 12 additions & 0 deletions django_freeradius/tests/test_admin.py
Expand Up @@ -213,3 +213,15 @@ def test_nas_admin_save_model(self):
self.assertNotContains(response, 'error')
nas.refresh_from_db()
self.assertEqual(nas.type, 'my-custom-type')

def test_cleanup_stale_radacct_command(self):
RadiusAccounting.objects.create(
unique_id='117', username='bob', nas_ip_address='127.0.0.1', start_time='2017-06-10 10:50:00',
authentication='RADIUS', connection_info_start='f', connection_info_stop='hgh',
input_octets='1', output_octets='4', update_time='2017-03-10 11:50:00'
)
call_command('cleanup_stale_radacct', 30)
session = RadiusAccounting.objects.get(unique_id='117')
self.assertNotEqual(session.stop_time, None)
self.assertNotEqual(session.session_time, None)
self.assertEqual(session.update_time, session.stop_time)
15 changes: 15 additions & 0 deletions docs/source/general/management_commands.rst
Expand Up @@ -42,3 +42,18 @@ For example:
.. code-block:: shell
./manage.py delete_old_postauth 365
``cleanup_stale_radacct``
-------------------------

This command closes active RADIUS sessions that are open since <days>.

.. code-block:: shell
./manage.py cleanup_stale_radacct <days>
For example:

.. code-block:: shell
./manage.py cleanup_stale_radacct 30

0 comments on commit b205e3d

Please sign in to comment.