forked from openwisp/django-freeradius
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[accounting] Added command to deal with unterminated radacct entries o…
…penwisp#47 Implements openwisp#3 of openwisp#47
- Loading branch information
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
django_freeradius/management/commands/cleanup_stale_radacct.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters