Browse Source

correction script ports des switchs

Élie Bouttier 7 years ago
parent
commit
5d141d84dc
1 changed files with 8 additions and 5 deletions
  1. 8 5
      services/management/commands/fetchport.py

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

@@ -14,6 +14,7 @@ class Command(BaseCommand):
         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)\)')
         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:
@@ -39,8 +40,10 @@ class Command(BaseCommand):
                     assert(status == 'down')
                     down.append(port)
             unknown = set(range(sw.first_port, sw.last_port + 1)) - set(up) - set(down)
-            upped = sw.ports.filter(port__in=up).exclude(up=True).update(up=True)
-            downed = sw.ports.filter(port__in=down).exclude(up=False).update(up=False)
-            unknowned = sw.ports.filter(port__in=unknown).exclude(up__isnull=True).update(up=None)
-            #print("Switch %s: UP: %d (%+d), DOWN: %d (%+d), UNKNOWN: %d (%+d)" \
-            #        % (sw.name, len(up), upped, len(down), downed, len(unknown), unknowned))
+            sw.ports.filter(port__in=up).exclude(up=True).update(up=True)
+            sw.ports.filter(port__in=down).exclude(up=False).update(up=False)
+            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))