Browse Source

fastping → fastpinger

Élie Bouttier 7 years ago
parent
commit
4ab7bd6778

+ 1 - 1
services/admin.py

@@ -28,7 +28,7 @@ from banking.models import PaymentUpdate
 from .models import Service, ServiceType, IPPrefix, IPResource, Route, Tunnel, \
                     ServiceAllocation, Antenna, AntennaAllocation, Allocation, \
                     Switch, Port
-from .utils import notify_allocation
+from .utils.notifications import notify_allocation
 from .forms import AntennaForm
 
 

+ 4 - 4
services/management/commands/fastping.py

@@ -1,15 +1,15 @@
 from django.core.management.base import BaseCommand, CommandError
 
-from services.utils import fastping_update
+from services.utils.fastpinger import fastpinger_update
 
 
 class Command(BaseCommand):
-    help = 'Analyse d’un fichier fastping'
+    help = 'Analyse d’un fichier fastpinger'
 
     def add_arguments(self, parser):
-        parser.add_argument('file', help='Fichier fastping')
+        parser.add_argument('file', help='Fichier fastpinger')
 
     def handle(self, *args, **options):
         with open(options['file'], 'rb') as f:
-            stats = fastping_update(f)
+            stats = fastpinger_update(f)
         self.stdout.write(stats + '\n')

+ 6 - 5
services/management/commands/fetchport.py

@@ -12,13 +12,13 @@ class Command(BaseCommand):
     def handle(self, *args, **options):
         dell_iface_regex = re.compile('^Interface TenGigabitEthernet 0/(?P<id>[0-9]+)$')
         ubnt_iface_regex = re.compile('^Interface Slot: 0 Port: (?P<id>[0-9]+) (Gigabit|10G) - Level$')
-        status_regex = re.compile('^OK .* \((?P<status>up|down)\)')
+        status_regex = re.compile('\w+ - \[[\w-]+\] \((?P<status>up|down|admin down)\)')
         for sw in Switch.objects.all():
             up_count, down_count, unknown_count = sw.ports.filter(up=True).count(), sw.ports.filter(up=False).count(), sw.ports.filter(up__isnull=True).count()
             up, down = [], []
             hosts = from_livestatus('hosts', query=['Filter: host_name = %s' % sw.name], columns=['services_with_info'])
             if len(hosts) != 1:
-                return
+                continue
             host = hosts[0]
             for service in host.services_with_info:
                 description, _, _, info = service
@@ -32,12 +32,13 @@ class Command(BaseCommand):
                     continue
                 g = status_regex.match(info)
                 if not g:
+                    self.stdout.write(self.style.WARNING("Switch %s port %d status unknown: %s" % (sw.name, port, info)))
                     continue
                 status = g.group('status')
                 if status == 'up':
                     up.append(port)
                 else:
-                    assert(status == 'down')
+                    assert(status == 'down' or status == 'admin down')
                     down.append(port)
             unknown = set(range(sw.first_port, sw.last_port + 1)) - set(up) - set(down)
             sw.ports.filter(port__in=up).exclude(up=True).update(up=True)
@@ -45,5 +46,5 @@ class Command(BaseCommand):
             sw.ports.filter(port__in=unknown).exclude(up__isnull=True).update(up=None)
             upped, downed, unknowned = len(up) - up_count, len(down) - down_count, len(unknown) - unknown_count
             if upped or downed or unknowned:
-                print("Switch %s: UP: %d (%+d), DOWN: %d (%+d), UNKNOWN: %d (%+d)" \
-                        % (sw.name, len(up), upped, len(down), downed, len(unknown), unknowned))
+                self.stdout.write("Switch %s: UP: %d (%+d), DOWN: %d (%+d), UNKNOWN: %d (%+d)" \
+                                  % (sw.name, len(up), upped, len(down), downed, len(unknown), unknowned))

+ 1 - 1
services/urls.py

@@ -5,5 +5,5 @@ from . import views
 
 urlpatterns = [
     url(r'^services/(?P<pk>[0-9]+)/$', views.ServiceDetail.as_view(), name='service-detail'),
-    url(r'^api/fastping/$', views.fastping, name='fastping'),
+    url(r'^api/fastpinger/$', views.fastpinger, name='fastpinger'),
 ]

+ 1 - 43
services/utils.py

@@ -1,55 +1,13 @@
-from django.urls import reverse
-from django.conf import settings
 from django.db.models import Q, F
 from django.utils import timezone
 
-from djadhere.utils import send_notification
 from services.models import IPResource
 
 import re
 from ipaddress import IPv4Address, AddressValueError
 
 
-def notify_allocation(request, new_alloc, old_alloc=None):
-    fields = ['resource', 'service', 'route', 'start', 'end', 'notes']
-
-    benevole = '%s <%s>' % (request.user.username, request.user.email)
-    message = 'Bénévole : ' + benevole
-    message += '\n\nAllocation :'
-
-    diff = False
-    for field in fields:
-        new_attr = getattr(new_alloc, field)
-        if new_attr == '' or new_attr is None:
-            new_attr = '-'
-        if old_alloc:
-            old_attr = getattr(old_alloc, field)
-            if old_attr == '' or old_attr is None:
-                old_attr = '-'
-        if old_alloc and old_attr != new_attr:
-            message += '\n-%12s: %s\n+%12s: %s' % (field, old_attr, field, new_attr)
-            diff = True
-        else:
-            message += '\n %12s: %s' % (field, new_attr)
-
-    url = 'https' if request.is_secure() else 'http'
-    url += '://' + request.get_host()
-    url += reverse('admin:services_ipresource_change', args=(new_alloc.resource.pk,))
-    message += '\n\nVoir : ' + url
-
-    if old_alloc and diff:
-        sujet = 'Modification d’une allocation'
-    elif not old_alloc:
-        sujet = 'Nouvelle allocation'
-    else:
-        sujet = None
-
-    if sujet:
-        sujet += ' ADT%d' % new_alloc.service.adhesion.pk
-        send_notification(sujet, message, settings.ALLOCATIONS_EMAILS, cc=[benevole])
-
-
-def fastping_update(f):
+def fastpinger_update(f):
     regex = re.compile('^(?P<ip>[0-9.]+)[ ]+: (?P<p1>([0-9]+.[0-9]+|-)) (?P<p2>([0-9]+.[0-9]+|-))'
                        ' (?P<p3>([0-9]+.[0-9]+|-)) (?P<p4>([0-9]+.[0-9]+|-)) (?P<p5>([0-9]+.[0-9]+|-))$')
     up_count = IPResource.objects.filter(last_check__isnull=False).filter(last_time_up=F('last_check')).count()

+ 43 - 0
services/utils/notifications.py

@@ -0,0 +1,43 @@
+from django.urls import reverse
+from django.conf import settings
+
+from djadhere.utils import send_notification
+
+
+def notify_allocation(request, new_alloc, old_alloc=None):
+    fields = ['resource', 'service', 'route', 'start', 'end', 'notes']
+
+    benevole = '%s <%s>' % (request.user.username, request.user.email)
+    message = 'Bénévole : ' + benevole
+    message += '\n\nAllocation :'
+
+    diff = False
+    for field in fields:
+        new_attr = getattr(new_alloc, field)
+        if new_attr == '' or new_attr is None:
+            new_attr = '-'
+        if old_alloc:
+            old_attr = getattr(old_alloc, field)
+            if old_attr == '' or old_attr is None:
+                old_attr = '-'
+        if old_alloc and old_attr != new_attr:
+            message += '\n-%12s: %s\n+%12s: %s' % (field, old_attr, field, new_attr)
+            diff = True
+        else:
+            message += '\n %12s: %s' % (field, new_attr)
+
+    url = 'https' if request.is_secure() else 'http'
+    url += '://' + request.get_host()
+    url += reverse('admin:services_ipresource_change', args=(new_alloc.resource.pk,))
+    message += '\n\nVoir : ' + url
+
+    if old_alloc and diff:
+        sujet = 'Modification d’une allocation'
+    elif not old_alloc:
+        sujet = 'Nouvelle allocation'
+    else:
+        sujet = None
+
+    if sujet:
+        sujet += ' ADT%d' % new_alloc.service.adhesion.pk
+        send_notification(sujet, message, settings.ALLOCATIONS_EMAILS, cc=[benevole])

+ 5 - 5
services/views.py

@@ -6,7 +6,7 @@ from django.conf import settings
 from django.views.decorators.csrf import csrf_exempt
 
 from .models import Service
-from .utils import fastping_update
+from .utils.fastpinger import fastpinger_update
 
 
 class ServiceDetail(LoginRequiredMixin, DetailView):
@@ -17,10 +17,10 @@ class ServiceDetail(LoginRequiredMixin, DetailView):
 
 @csrf_exempt
 @require_POST
-def fastping(request):
-    if request.POST.get('key', None) != settings.FASTPING_KEY:
+def fastpinger(request):
+    if request.POST.get('key', None) != settings.FASTPINGER_KEY:
         return HttpResponseForbidden('Invalid key.')
-    if 'fastping' not in request.FILES:
+    if 'fastpinger' not in request.FILES:
         return HttpResponse(status=400)  # Bad Request
-    stats = fastping_update(request.FILES['fastping'])
+    stats = fastpinger_update(request.FILES['fastping'])
     return HttpResponse(stats + '\n')