8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-25 12:53:11 +00:00
re2o/logs/views.py

480 lines
17 KiB
Python
Raw Normal View History

2017-01-15 23:01:18 +00:00
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017 Gabriel Détraz
# Copyright © 2017 Goulven Kermarec
# Copyright © 2017 Augustin Lemesle
#
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2016-10-31 16:27:27 +00:00
# App de gestion des statistiques pour re2o
# Gabriel Détraz
# Gplv2
2017-10-14 02:17:42 +00:00
"""
Vues des logs et statistiques générales.
La vue index générale affiche une selection des dernières actions,
classées selon l'importance, avec date, et user formatés.
Stats_logs renvoie l'ensemble des logs.
Les autres vues sont thématiques, ensemble des statistiques et du
nombre d'objets par models, nombre d'actions par user, etc
"""
from __future__ import unicode_literals
2017-10-26 15:58:41 +00:00
from django.urls import reverse
2016-10-31 16:27:27 +00:00
from django.shortcuts import render, redirect
from django.contrib import messages
2017-12-28 15:32:48 +00:00
from django.contrib.auth.decorators import login_required
from django.db.models import Count, Max, F
2016-10-31 16:27:27 +00:00
from reversion.models import Revision
from reversion.models import Version, ContentType
2016-10-31 16:27:27 +00:00
2017-10-27 00:44:19 +00:00
from users.models import (
User,
ServiceUser,
School,
ListRight,
ListShell,
Ban,
Whitelist,
Adherent,
Club
)
from cotisations.models import (
Facture,
Vente,
Article,
Banque,
Paiement,
Cotisation
)
from machines.models import (
Machine,
MachineType,
IpType,
Extension,
Interface,
Domain,
IpList,
OuverturePortList,
Service,
Vlan,
Nas,
SOA,
Mx,
Ns
)
from topologie.models import (
Switch,
Port,
Room,
Stack,
ModelSwitch,
ConstructorSwitch,
AccessPoint
2017-10-27 00:44:19 +00:00
)
from preferences.models import GeneralOption
2017-10-14 02:17:42 +00:00
from re2o.views import form
2017-12-28 15:32:48 +00:00
from re2o.utils import (
all_whitelisted,
all_baned,
all_has_access,
all_adherent,
re2o_paginator,
)
from re2o.acl import (
2017-12-28 15:32:48 +00:00
can_view_all,
can_view_app,
can_edit_history,
)
2017-10-15 01:47:17 +00:00
from re2o.utils import all_active_assigned_interfaces_count
from re2o.utils import all_active_interfaces_count, SortTable
2016-11-15 14:25:27 +00:00
2016-10-31 16:27:27 +00:00
@login_required
2017-12-28 15:32:48 +00:00
@can_view_app('logs')
2016-10-31 16:27:27 +00:00
def index(request):
2017-10-14 02:17:42 +00:00
"""Affiche les logs affinés, date reformatées, selectionne
les event importants (ajout de droits, ajout de ban/whitelist)"""
pagination_number = GeneralOption.get_cached_value('pagination_number')
# The types of content kept for display
2017-10-14 02:17:42 +00:00
content_type_filter = ['ban', 'whitelist', 'vente', 'interface', 'user']
# Select only wanted versions
2017-10-14 02:17:42 +00:00
versions = Version.objects.filter(
content_type__in=ContentType.objects.filter(
model__in=content_type_filter
)
).select_related('revision')
versions = SortTable.sort(
versions,
request.GET.get('col'),
request.GET.get('order'),
SortTable.LOGS_INDEX
)
versions = re2o_paginator(request, versions, pagination_number)
# Force to have a list instead of QuerySet
versions.count(0)
# Items to remove later because invalid
to_remove = []
# Parse every item (max = pagination_number)
2017-10-14 02:17:42 +00:00
for i in range(len(versions.object_list)):
if versions.object_list[i].object:
version = versions.object_list[i]
versions.object_list[i] = {
2017-10-14 02:17:42 +00:00
'rev_id': version.revision.id,
'comment': version.revision.comment,
'datetime': version.revision.date_created.strftime(
'%d/%m/%y %H:%M:%S'
),
'username':
version.revision.user.get_username()
if version.revision.user else '?',
'user_id': version.revision.user_id,
'version': version}
else:
to_remove.insert(0, i)
# Remove all tagged invalid items
2017-10-14 02:17:42 +00:00
for i in to_remove:
versions.object_list.pop(i)
return render(request, 'logs/index.html', {'versions_list': versions})
2017-10-14 02:17:42 +00:00
@login_required
2017-12-28 15:32:48 +00:00
@can_view_all(GeneralOption)
def stats_logs(request):
2017-10-14 02:17:42 +00:00
"""Affiche l'ensemble des logs et des modifications sur les objets,
classés par date croissante, en vrac"""
pagination_number = GeneralOption.get_cached_value('pagination_number')
revisions = Revision.objects.all().select_related('user')\
2017-10-14 02:17:42 +00:00
.prefetch_related('version_set__object')
revisions = SortTable.sort(
revisions,
request.GET.get('col'),
request.GET.get('order'),
SortTable.LOGS_STATS_LOGS
)
revisions = re2o_paginator(request, revisions, pagination_number)
2017-10-14 02:17:42 +00:00
return render(request, 'logs/stats_logs.html', {
'revisions_list': revisions
})
@login_required
2017-12-28 15:32:48 +00:00
@can_edit_history
def revert_action(request, revision_id):
""" Annule l'action en question """
try:
revision = Revision.objects.get(id=revision_id)
except Revision.DoesNotExist:
2017-10-14 02:17:42 +00:00
messages.error(request, u"Revision inexistante")
if request.method == "POST":
revision.revert()
messages.success(request, "L'action a été supprimée")
2017-10-26 15:58:41 +00:00
return redirect(reverse('logs:index'))
2017-10-14 02:17:42 +00:00
return form({
'objet': revision,
'objet_name': revision.__class__.__name__
}, 'logs/delete.html', request)
2017-05-26 02:32:45 +00:00
@login_required
@can_view_all(IpList, Interface, User)
2017-05-26 02:32:45 +00:00
def stats_general(request):
2017-10-14 02:17:42 +00:00
"""Statistiques générales affinées sur les ip, activées, utilisées par
range, et les statistiques générales sur les users : users actifs,
cotisants, activés, archivés, etc"""
ip_dict = dict()
2017-10-27 00:44:19 +00:00
for ip_range in IpType.objects.select_related('vlan').all():
2017-05-26 02:32:45 +00:00
all_ip = IpList.objects.filter(ip_type=ip_range)
used_ip = Interface.objects.filter(ipv4__in=all_ip).count()
2017-10-14 02:17:42 +00:00
active_ip = all_active_assigned_interfaces_count().filter(
ipv4__in=IpList.objects.filter(ip_type=ip_range)
).count()
2017-10-27 00:44:19 +00:00
ip_dict[ip_range] = [ip_range, ip_range.vlan, all_ip.count(),
2017-10-14 02:17:42 +00:00
used_ip, active_ip, all_ip.count()-used_ip]
2017-10-27 00:44:19 +00:00
_all_adherent = all_adherent()
_all_has_access = all_has_access()
_all_baned = all_baned()
_all_whitelisted = all_whitelisted()
_all_active_interfaces_count = all_active_interfaces_count()
2018-04-13 20:37:04 +00:00
_all_active_assigned_interfaces_count = \
all_active_assigned_interfaces_count()
2017-05-26 02:32:45 +00:00
stats = [
2018-04-13 20:37:04 +00:00
[ # First set of data (about users)
[ # Headers
"Categorie",
"Nombre d'utilisateurs (total club et adhérents)",
"Nombre d'adhérents",
"Nombre de clubs"
],
{ # Data
'active_users': [
"Users actifs",
User.objects.filter(state=User.STATE_ACTIVE).count(),
(Adherent.objects
.filter(state=Adherent.STATE_ACTIVE)
.count()),
Club.objects.filter(state=Club.STATE_ACTIVE).count()
],
'inactive_users': [
"Users désactivés",
User.objects.filter(state=User.STATE_DISABLED).count(),
(Adherent.objects
.filter(state=Adherent.STATE_DISABLED)
.count()),
Club.objects.filter(state=Club.STATE_DISABLED).count()
],
'archive_users': [
"Users archivés",
User.objects.filter(state=User.STATE_ARCHIVE).count(),
(Adherent.objects
.filter(state=Adherent.STATE_ARCHIVE)
.count()),
Club.objects.filter(state=Club.STATE_ARCHIVE).count()
],
'adherent_users': [
"Cotisant à l'association",
_all_adherent.count(),
_all_adherent.exclude(adherent__isnull=True).count(),
_all_adherent.exclude(club__isnull=True).count()
],
'connexion_users': [
"Utilisateurs bénéficiant d'une connexion",
_all_has_access.count(),
_all_has_access.exclude(adherent__isnull=True).count(),
_all_has_access.exclude(club__isnull=True).count()
],
'ban_users': [
"Utilisateurs bannis",
_all_baned.count(),
_all_baned.exclude(adherent__isnull=True).count(),
_all_baned.exclude(club__isnull=True).count()
],
'whitelisted_user': [
"Utilisateurs bénéficiant d'une connexion gracieuse",
_all_whitelisted.count(),
_all_whitelisted.exclude(adherent__isnull=True).count(),
_all_whitelisted.exclude(club__isnull=True).count()
],
'actives_interfaces': [
"Interfaces actives (ayant accès au reseau)",
_all_active_interfaces_count.count(),
(_all_active_interfaces_count
.exclude(machine__user__adherent__isnull=True)
.count()),
(_all_active_interfaces_count
.exclude(machine__user__club__isnull=True)
.count())
],
'actives_assigned_interfaces': [
"Interfaces actives et assignées ipv4",
_all_active_assigned_interfaces_count.count(),
(_all_active_assigned_interfaces_count
.exclude(machine__user__adherent__isnull=True)
.count()),
(_all_active_assigned_interfaces_count
.exclude(machine__user__club__isnull=True)
.count())
]
}
],
[ # Second set of data (about ip adresses)
[ # Headers
"Range d'ip",
"Vlan",
"Nombre d'ip totales",
"Ip assignées",
"Ip assignées à une machine active",
"Ip non assignées"
],
ip_dict # Data already prepared
2017-10-14 02:17:42 +00:00
]
2018-04-13 20:37:04 +00:00
]
2017-05-26 02:32:45 +00:00
return render(request, 'logs/stats_general.html', {'stats_list': stats})
@login_required
@can_view_app('users', 'cotisations', 'machines', 'topologie')
def stats_models(request):
2017-10-14 02:17:42 +00:00
"""Statistiques générales, affiche les comptages par models:
nombre d'users, d'écoles, de droits, de bannissements,
de factures, de ventes, de banque, de machines, etc"""
stats = {
2017-10-14 02:17:42 +00:00
'Users': {
'users': [User.PRETTY_NAME, User.objects.count()],
2017-10-27 00:44:19 +00:00
'adherents': [Adherent.PRETTY_NAME, Adherent.objects.count()],
'clubs': [Club.PRETTY_NAME, Club.objects.count()],
2017-10-14 02:17:42 +00:00
'serviceuser': [ServiceUser.PRETTY_NAME,
ServiceUser.objects.count()],
'school': [School.PRETTY_NAME, School.objects.count()],
'listright': [ListRight.PRETTY_NAME, ListRight.objects.count()],
'listshell': [ListShell.PRETTY_NAME, ListShell.objects.count()],
'ban': [Ban.PRETTY_NAME, Ban.objects.count()],
'whitelist': [Whitelist.PRETTY_NAME, Whitelist.objects.count()]
},
'Cotisations': {
2018-04-13 20:37:04 +00:00
'factures': [
Facture._meta.verbose_name.title(),
Facture.objects.count()
],
'vente': [
Vente._meta.verbose_name.title(),
Vente.objects.count()
],
'cotisation': [
Cotisation._meta.verbose_name.title(),
Cotisation.objects.count()
],
'article': [
Article._meta.verbose_name.title(),
Article.objects.count()
],
'banque': [
Banque._meta.verbose_name.title(),
Banque.objects.count()
],
2017-10-14 02:17:42 +00:00
},
'Machines': {
'machine': [Machine.PRETTY_NAME, Machine.objects.count()],
'typemachine': [MachineType.PRETTY_NAME,
MachineType.objects.count()],
'typeip': [IpType.PRETTY_NAME, IpType.objects.count()],
'extension': [Extension.PRETTY_NAME, Extension.objects.count()],
'interface': [Interface.PRETTY_NAME, Interface.objects.count()],
'alias': [Domain.PRETTY_NAME,
Domain.objects.exclude(cname=None).count()],
'iplist': [IpList.PRETTY_NAME, IpList.objects.count()],
2017-10-27 00:44:19 +00:00
'service': [Service.PRETTY_NAME, Service.objects.count()],
'ouvertureportlist': [
OuverturePortList.PRETTY_NAME,
OuverturePortList.objects.count()
],
'vlan': [Vlan.PRETTY_NAME, Vlan.objects.count()],
2017-12-28 15:32:48 +00:00
'SOA': [SOA.PRETTY_NAME, SOA.objects.count()],
2017-10-27 00:44:19 +00:00
'Mx': [Mx.PRETTY_NAME, Mx.objects.count()],
'Ns': [Ns.PRETTY_NAME, Ns.objects.count()],
'nas': [Nas.PRETTY_NAME, Nas.objects.count()],
2017-10-14 02:17:42 +00:00
},
'Topologie': {
'switch': [Switch.PRETTY_NAME, Switch.objects.count()],
'bornes': [AccessPoint.PRETTY_NAME, AccessPoint.objects.count()],
2017-10-14 02:17:42 +00:00
'port': [Port.PRETTY_NAME, Port.objects.count()],
'chambre': [Room.PRETTY_NAME, Room.objects.count()],
2017-10-27 00:44:19 +00:00
'stack': [Stack.PRETTY_NAME, Stack.objects.count()],
'modelswitch': [
ModelSwitch.PRETTY_NAME,
ModelSwitch.objects.count()
],
'constructorswitch': [
ConstructorSwitch.PRETTY_NAME,
ConstructorSwitch.objects.count()
],
2017-10-14 02:17:42 +00:00
},
'Actions effectuées sur la base':
{
'revision': ["Nombre d'actions", Revision.objects.count()],
},
}
2017-10-14 02:17:42 +00:00
return render(request, 'logs/stats_models.html', {'stats_list': stats})
2016-11-01 02:11:32 +00:00
@login_required
2017-12-28 15:32:48 +00:00
@can_view_app('users')
2016-11-01 02:11:32 +00:00
def stats_users(request):
2017-10-14 02:17:42 +00:00
"""Affiche les statistiques base de données aggrégées par user :
nombre de machines par user, d'etablissements par user,
de moyens de paiements par user, de banque par user,
de bannissement par user, etc"""
2016-11-01 02:11:32 +00:00
stats = {
2017-10-14 02:17:42 +00:00
'Utilisateur': {
'Machines': User.objects.annotate(
num=Count('machine')
).order_by('-num')[:10],
'Facture': User.objects.annotate(
num=Count('facture')
).order_by('-num')[:10],
'Bannissement': User.objects.annotate(
num=Count('ban')
).order_by('-num')[:10],
'Accès gracieux': User.objects.annotate(
num=Count('whitelist')
).order_by('-num')[:10],
'Droits': User.objects.annotate(
num=Count('groups')
2017-10-14 02:17:42 +00:00
).order_by('-num')[:10],
},
'Etablissement': {
'Utilisateur': School.objects.annotate(
num=Count('user')
).order_by('-num')[:10],
},
'Moyen de paiement': {
'Utilisateur': Paiement.objects.annotate(
num=Count('facture')
).order_by('-num')[:10],
},
'Banque': {
'Utilisateur': Banque.objects.annotate(
num=Count('facture')
).order_by('-num')[:10],
},
2016-11-01 02:11:32 +00:00
}
2018-04-14 15:30:14 +00:00
return render(request, 'logs/stats_users.html', {'stats_list': stats})
2017-10-14 02:17:42 +00:00
2016-11-01 02:11:32 +00:00
@login_required
2017-12-28 15:32:48 +00:00
@can_view_app('users')
2016-11-01 02:11:32 +00:00
def stats_actions(request):
2017-10-14 02:17:42 +00:00
"""Vue qui affiche les statistiques de modifications d'objets par
utilisateurs.
Affiche le nombre de modifications aggrégées par utilisateurs"""
2016-11-01 02:11:32 +00:00
stats = {
2017-10-14 02:17:42 +00:00
'Utilisateur': {
'Action': User.objects.annotate(
num=Count('revision')
).order_by('-num')[:40],
},
2016-11-01 02:11:32 +00:00
}
return render(request, 'logs/stats_users.html', {'stats_list': stats})
2018-04-13 20:37:04 +00:00
@login_required
@can_view_app('users')
def stats_droits(request):
"""Affiche la liste des droits et les users ayant chaque droit"""
2018-04-13 20:37:04 +00:00
stats_list = {}
2018-03-25 21:01:06 +00:00
for droit in ListRight.objects.all().select_related('group_ptr'):
2018-04-13 20:37:04 +00:00
stats_list[droit] = droit.user_set.all().annotate(
num=Count('revision'),
last=Max('revision__date_created'),
2018-04-13 20:37:04 +00:00
)
stats_list['Superuser'] = User.objects.filter(is_superuser=True).annotate(
num=Count('revision'),
last=Max('revision__date_created'),
)
2018-04-13 20:37:04 +00:00
return render(
request,
'logs/stats_droits.html',
{'stats_list': stats_list}
)