epicfail.py 1.2 KB

1234567891011121314151617181920212223242526272829
  1. from django.core.management.base import BaseCommand
  2. from django.utils import timezone
  3. from datetime import timedelta
  4. from itertools import groupby
  5. from services.models import ServiceAllocation, IPResource, IPResourceState
  6. class Command(BaseCommand):
  7. help = 'Détection des pannes'
  8. def handle(self, *args, **options):
  9. down = IPResource.objects.filter(
  10. category=IPResource.CATEGORY_PUBLIC,
  11. last_state__state=IPResourceState.STATE_DOWN,
  12. last_time_up__gt=timezone.now()-timedelta(minutes=5))
  13. allocations = ServiceAllocation.objects.filter(resource__pk__in=down)
  14. #allocations = allocations.values('resource', 'route')
  15. allocations = sorted(allocations, key=lambda a: a.route.pk)
  16. for route, allocs in groupby(allocations, key=lambda a: a.route.pk):
  17. allocs = list(allocs)
  18. route = allocs[0].route
  19. print("%s: %d" % (route, len(allocs)))
  20. for alloc in allocs:
  21. print("\t%s" % alloc.resource)
  22. if len(allocs) >= 4:
  23. logger.info('%s %s' % (route, allocs))
  24. self.stdout.write(self.style.SUCCESS('Aucune panne détectée.'))